|
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/. */ |
|
4 |
|
5 #ifndef mozilla_FileLocation_h |
|
6 #define mozilla_FileLocation_h |
|
7 |
|
8 #include "nsString.h" |
|
9 #include "nsCOMPtr.h" |
|
10 #include "nsAutoPtr.h" |
|
11 #include "nsIFile.h" |
|
12 #include "FileUtils.h" |
|
13 |
|
14 class nsZipArchive; |
|
15 class nsZipItem; |
|
16 |
|
17 namespace mozilla { |
|
18 |
|
19 class FileLocation |
|
20 { |
|
21 public: |
|
22 /** |
|
23 * FileLocation is an helper to handle different kind of file locations |
|
24 * within Gecko: |
|
25 * - on filesystems |
|
26 * - in archives |
|
27 * - in archives within archives |
|
28 * As such, it stores a path within an archive, as well as the archive |
|
29 * path itself, or the complete file path alone when on a filesystem. |
|
30 * When the archive is in an archive, an nsZipArchive is stored instead |
|
31 * of a file path. |
|
32 */ |
|
33 FileLocation(); |
|
34 ~FileLocation(); |
|
35 |
|
36 /** |
|
37 * Constructor for plain files |
|
38 */ |
|
39 FileLocation(nsIFile *file); |
|
40 |
|
41 /** |
|
42 * Constructors for path within an archive. The archive can be given either |
|
43 * as nsIFile or nsZipArchive. |
|
44 */ |
|
45 FileLocation(nsIFile *zip, const char *path); |
|
46 |
|
47 FileLocation(nsZipArchive *zip, const char *path); |
|
48 |
|
49 /** |
|
50 * Creates a new file location relative to another one. |
|
51 */ |
|
52 FileLocation(const FileLocation &file, const char *path = nullptr); |
|
53 |
|
54 /** |
|
55 * Initialization functions corresponding to constructors |
|
56 */ |
|
57 void Init(nsIFile *file); |
|
58 |
|
59 void Init(nsIFile *zip, const char *path); |
|
60 |
|
61 void Init(nsZipArchive *zip, const char *path); |
|
62 |
|
63 /** |
|
64 * Returns an URI string corresponding to the file location |
|
65 */ |
|
66 void GetURIString(nsACString &result) const; |
|
67 |
|
68 /** |
|
69 * Returns the base file of the location, where base file is defined as: |
|
70 * - The file itself when the location is on a filesystem |
|
71 * - The archive file when the location is in an archive |
|
72 * - The outer archive file when the location is in an archive in an archive |
|
73 */ |
|
74 already_AddRefed<nsIFile> GetBaseFile(); |
|
75 |
|
76 /** |
|
77 * Returns whether the "base file" (see GetBaseFile) is an archive |
|
78 */ |
|
79 bool IsZip() const |
|
80 { |
|
81 return !mPath.IsEmpty(); |
|
82 } |
|
83 |
|
84 /** |
|
85 * Returns the path within the archive, when within an archive |
|
86 */ |
|
87 void GetPath(nsACString &result) const |
|
88 { |
|
89 result = mPath; |
|
90 } |
|
91 |
|
92 /** |
|
93 * Boolean value corresponding to whether the file location is initialized |
|
94 * or not. |
|
95 */ |
|
96 operator bool() const |
|
97 { |
|
98 return mBaseFile || mBaseZip; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Returns whether another FileLocation points to the same resource |
|
103 */ |
|
104 bool Equals(const FileLocation &file) const; |
|
105 |
|
106 /** |
|
107 * Data associated with a FileLocation. |
|
108 */ |
|
109 class Data |
|
110 { |
|
111 public: |
|
112 /** |
|
113 * Returns the data size |
|
114 */ |
|
115 nsresult GetSize(uint32_t *result); |
|
116 |
|
117 /** |
|
118 * Copies the data in the given buffer |
|
119 */ |
|
120 nsresult Copy(char *buf, uint32_t len); |
|
121 protected: |
|
122 friend class FileLocation; |
|
123 nsZipItem *mItem; |
|
124 nsRefPtr<nsZipArchive> mZip; |
|
125 mozilla::AutoFDClose mFd; |
|
126 }; |
|
127 |
|
128 /** |
|
129 * Returns the data associated with the resource pointed at by the file |
|
130 * location. |
|
131 */ |
|
132 nsresult GetData(Data &data); |
|
133 private: |
|
134 nsCOMPtr<nsIFile> mBaseFile; |
|
135 nsRefPtr<nsZipArchive> mBaseZip; |
|
136 nsCString mPath; |
|
137 }; /* class FileLocation */ |
|
138 |
|
139 } /* namespace mozilla */ |
|
140 |
|
141 #endif /* mozilla_FileLocation_h */ |