bug 812562 - click-to-play: reshow notification for blocklisted plugins r=jaws
[gecko.git] / xpcom / build / FileLocation.h
blobb05bd1813c694e9812587ed985390e2d39724638
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_FileLocation_h
6 #define mozilla_FileLocation_h
8 #include "nsString.h"
9 #include "nsCOMPtr.h"
10 #include "nsAutoPtr.h"
11 #include "nsIFile.h"
12 #include "nsIURI.h"
13 #include "FileUtils.h"
15 class nsZipArchive;
16 class nsZipItem;
18 namespace mozilla {
20 class FileLocation
22 public:
23 /**
24 * FileLocation is an helper to handle different kind of file locations
25 * within Gecko:
26 * - on filesystems
27 * - in archives
28 * - in archives within archives
29 * As such, it stores a path within an archive, as well as the archive
30 * path itself, or the complete file path alone when on a filesystem.
31 * When the archive is in an archive, an nsZipArchive is stored instead
32 * of a file path.
34 FileLocation() { }
36 /**
37 * Constructor for plain files
39 FileLocation(nsIFile *file)
41 Init(file);
44 /**
45 * Constructors for path within an archive. The archive can be given either
46 * as nsIFile or nsZipArchive.
48 FileLocation(nsIFile *zip, const char *path)
50 Init(zip, path);
53 FileLocation(nsZipArchive *zip, const char *path)
55 Init(zip, path);
58 /**
59 * Creates a new file location relative to another one.
61 FileLocation(const FileLocation &file, const char *path = NULL);
63 /**
64 * Initialization functions corresponding to constructors
66 void Init(nsIFile *file)
68 mBaseZip = NULL;
69 mBaseFile = file;
70 mPath.Truncate();
73 void Init(nsIFile *zip, const char *path)
75 mBaseZip = NULL;
76 mBaseFile = zip;
77 mPath = path;
80 void Init(nsZipArchive *zip, const char *path)
82 mBaseZip = zip;
83 mBaseFile = NULL;
84 mPath = path;
87 /**
88 * Returns an URI string corresponding to the file location
90 void GetURIString(nsACString &result) const;
92 /**
93 * Returns the base file of the location, where base file is defined as:
94 * - The file itself when the location is on a filesystem
95 * - The archive file when the location is in an archive
96 * - The outer archive file when the location is in an archive in an archive
98 already_AddRefed<nsIFile> GetBaseFile();
101 * Returns whether the "base file" (see GetBaseFile) is an archive
103 bool IsZip() const
105 return !mPath.IsEmpty();
109 * Returns the path within the archive, when within an archive
111 void GetPath(nsACString &result) const
113 result = mPath;
117 * Boolean value corresponding to whether the file location is initialized
118 * or not.
120 operator bool() const
122 return mBaseFile || mBaseZip;
126 * Returns whether another FileLocation points to the same resource
128 bool Equals(const FileLocation &file) const;
131 * Data associated with a FileLocation.
133 class Data
135 public:
137 * Returns the data size
139 nsresult GetSize(uint32_t *result);
142 * Copies the data in the given buffer
144 nsresult Copy(char *buf, uint32_t len);
145 protected:
146 friend class FileLocation;
147 nsZipItem *mItem;
148 nsRefPtr<nsZipArchive> mZip;
149 mozilla::AutoFDClose mFd;
153 * Returns the data associated with the resource pointed at by the file
154 * location.
156 nsresult GetData(Data &data);
157 private:
158 nsCOMPtr<nsIFile> mBaseFile;
159 nsRefPtr<nsZipArchive> mBaseZip;
160 nsCString mPath;
161 }; /* class FileLocation */
163 } /* namespace mozilla */
165 #endif /* mozilla_FileLocation_h */