1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Mozilla code
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mike Hommey <mh@glandium.org>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef mozilla_FileLocation_h
39 : #define mozilla_FileLocation_h
40 :
41 : #include "nsString.h"
42 : #include "nsCOMPtr.h"
43 : #include "nsAutoPtr.h"
44 : #include "nsILocalFile.h"
45 : #include "nsIURI.h"
46 : #include "FileUtils.h"
47 :
48 : class nsZipArchive;
49 : class nsZipItem;
50 :
51 : namespace mozilla {
52 :
53 : class FileLocation
54 : {
55 : public:
56 : /**
57 : * FileLocation is an helper to handle different kind of file locations
58 : * within Gecko:
59 : * - on filesystems
60 : * - in archives
61 : * - in archives within archives
62 : * As such, it stores a path within an archive, as well as the archive
63 : * path itself, or the complete file path alone when on a filesystem.
64 : * When the archive is in an archive, an nsZipArchive is stored instead
65 : * of a file path.
66 : */
67 : FileLocation() { }
68 :
69 : /**
70 : * Constructor for plain files
71 : */
72 : FileLocation(nsILocalFile *file)
73 : {
74 : Init(file);
75 : }
76 :
77 : /**
78 : * Constructors for path within an archive. The archive can be given either
79 : * as nsILocalFile or nsZipArchive.
80 : */
81 : FileLocation(nsILocalFile *zip, const char *path)
82 : {
83 : Init(zip, path);
84 : }
85 :
86 : FileLocation(nsZipArchive *zip, const char *path)
87 : {
88 : Init(zip, path);
89 : }
90 :
91 : /**
92 : * Creates a new file location relative to another one.
93 : */
94 : FileLocation(const FileLocation &file, const char *path = NULL);
95 :
96 : /**
97 : * Initialization functions corresponding to constructors
98 : */
99 0 : void Init(nsILocalFile *file)
100 : {
101 0 : mBaseZip = NULL;
102 0 : mBaseFile = file;
103 0 : mPath.Truncate();
104 0 : }
105 :
106 211 : void Init(nsILocalFile *zip, const char *path)
107 : {
108 211 : mBaseZip = NULL;
109 211 : mBaseFile = zip;
110 211 : mPath = path;
111 211 : }
112 :
113 0 : void Init(nsZipArchive *zip, const char *path)
114 : {
115 0 : mBaseZip = zip;
116 0 : mBaseFile = NULL;
117 0 : mPath = path;
118 0 : }
119 :
120 : /**
121 : * Returns an URI string corresponding to the file location
122 : */
123 : void GetURIString(nsACString &result) const;
124 :
125 : /**
126 : * Returns the base file of the location, where base file is defined as:
127 : * - The file itself when the location is on a filesystem
128 : * - The archive file when the location is in an archive
129 : * - The outer archive file when the location is in an archive in an archive
130 : */
131 : already_AddRefed<nsILocalFile> GetBaseFile();
132 :
133 : /**
134 : * Returns whether the "base file" (see GetBaseFile) is an archive
135 : */
136 1262471 : bool IsZip() const
137 : {
138 1262471 : return !mPath.IsEmpty();
139 : }
140 :
141 : /**
142 : * Returns the path within the archive, when within an archive
143 : */
144 : void GetPath(nsACString &result) const
145 : {
146 : result = mPath;
147 : }
148 :
149 : /**
150 : * Boolean value corresponding to whether the file location is initialized
151 : * or not.
152 : */
153 : operator bool() const
154 : {
155 : return mBaseFile || mBaseZip;
156 : }
157 :
158 : /**
159 : * Returns whether another FileLocation points to the same resource
160 : */
161 : bool Equals(const FileLocation &file) const;
162 :
163 : /**
164 : * Data associated with a FileLocation.
165 : */
166 : class Data
167 : {
168 : public:
169 : /**
170 : * Returns the data size
171 : */
172 : nsresult GetSize(PRUint32 *result);
173 :
174 : /**
175 : * Copies the data in the given buffer
176 : */
177 : nsresult Copy(char *buf, PRUint32 len);
178 : protected:
179 : friend class FileLocation;
180 : nsZipItem *mItem;
181 : nsRefPtr<nsZipArchive> mZip;
182 : mozilla::AutoFDClose mFd;
183 : };
184 :
185 : /**
186 : * Returns the data associated with the resource pointed at by the file
187 : * location.
188 : */
189 : nsresult GetData(Data &data);
190 : private:
191 : nsCOMPtr<nsILocalFile> mBaseFile;
192 : nsRefPtr<nsZipArchive> mBaseZip;
193 : nsCString mPath;
194 : }; /* class FileLocation */
195 :
196 : } /* namespace mozilla */
197 :
198 : #endif /* mozilla_FileLocation_h */
|