1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozila.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Mozilla Corporation
19 : * Portions created by the Initial Developer are Copyright (C) 2007
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Dave Camp <dcamp@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsDOMFile_h__
40 : #define nsDOMFile_h__
41 :
42 : #include "nsICharsetDetectionObserver.h"
43 : #include "nsIFile.h"
44 : #include "nsIDOMFile.h"
45 : #include "nsIDOMFileList.h"
46 : #include "nsIInputStream.h"
47 : #include "nsIJSNativeInitializer.h"
48 : #include "nsIMutable.h"
49 : #include "nsCOMArray.h"
50 : #include "nsCOMPtr.h"
51 : #include "nsString.h"
52 : #include "nsIXMLHttpRequest.h"
53 : #include "prmem.h"
54 : #include "nsAutoPtr.h"
55 :
56 : #include "mozilla/GuardObjects.h"
57 : #include "mozilla/StandardInteger.h"
58 : #include "mozilla/dom/DOMError.h"
59 : #include "mozilla/dom/indexedDB/FileInfo.h"
60 : #include "mozilla/dom/indexedDB/FileManager.h"
61 : #include "mozilla/dom/indexedDB/IndexedDatabaseManager.h"
62 : #include "nsWrapperCache.h"
63 : #include "nsCycleCollectionParticipant.h"
64 :
65 : class nsIFile;
66 : class nsIInputStream;
67 : class nsIClassInfo;
68 : class nsIBlobBuilder;
69 :
70 : nsresult NS_NewBlobBuilder(nsISupports* *aSupports);
71 :
72 : class nsDOMFileBase : public nsIDOMFile,
73 : public nsIXHRSendable,
74 : public nsIMutable
75 : {
76 : public:
77 : typedef mozilla::dom::indexedDB::FileInfo FileInfo;
78 :
79 10 : nsDOMFileBase(const nsAString& aName, const nsAString& aContentType,
80 : PRUint64 aLength)
81 : : mIsFile(true), mImmutable(false), mContentType(aContentType),
82 10 : mName(aName), mStart(0), mLength(aLength)
83 : {
84 : // Ensure non-null mContentType by default
85 10 : mContentType.SetIsVoid(false);
86 10 : }
87 :
88 0 : nsDOMFileBase(const nsAString& aContentType, PRUint64 aLength)
89 : : mIsFile(false), mImmutable(false), mContentType(aContentType),
90 0 : mStart(0), mLength(aLength)
91 : {
92 : // Ensure non-null mContentType by default
93 0 : mContentType.SetIsVoid(false);
94 0 : }
95 :
96 0 : nsDOMFileBase(const nsAString& aContentType,
97 : PRUint64 aStart, PRUint64 aLength)
98 : : mIsFile(false), mImmutable(false), mContentType(aContentType),
99 0 : mStart(aStart), mLength(aLength)
100 : {
101 0 : NS_ASSERTION(aLength != UINT64_MAX,
102 : "Must know length when creating slice");
103 : // Ensure non-null mContentType by default
104 0 : mContentType.SetIsVoid(false);
105 0 : }
106 :
107 20 : virtual ~nsDOMFileBase() {}
108 :
109 : virtual already_AddRefed<nsIDOMBlob>
110 : CreateSlice(PRUint64 aStart, PRUint64 aLength,
111 : const nsAString& aContentType) = 0;
112 :
113 : virtual const nsTArray<nsCOMPtr<nsIDOMBlob> >*
114 0 : GetSubBlobs() const { return nsnull; }
115 :
116 : NS_DECL_ISUPPORTS
117 : NS_DECL_NSIDOMBLOB
118 : NS_DECL_NSIDOMFILE
119 : NS_DECL_NSIXHRSENDABLE
120 : NS_DECL_NSIMUTABLE
121 :
122 : protected:
123 2 : bool IsSizeUnknown()
124 : {
125 2 : return mLength == UINT64_MAX;
126 : }
127 :
128 0 : virtual bool IsStoredFile()
129 : {
130 0 : return false;
131 : }
132 :
133 0 : virtual bool IsWholeFile()
134 : {
135 0 : NS_NOTREACHED("Should only be called on dom blobs backed by files!");
136 0 : return false;
137 : }
138 :
139 : bool mIsFile;
140 : bool mImmutable;
141 : nsString mContentType;
142 : nsString mName;
143 :
144 : PRUint64 mStart;
145 : PRUint64 mLength;
146 :
147 : // Protected by IndexedDatabaseManager::FileMutex()
148 : nsTArray<nsRefPtr<FileInfo> > mFileInfos;
149 : };
150 :
151 : class nsDOMFileFile : public nsDOMFileBase,
152 : public nsIJSNativeInitializer
153 40 : {
154 : public:
155 : // Create as a file
156 0 : nsDOMFileFile(nsIFile *aFile)
157 0 : : nsDOMFileBase(EmptyString(), EmptyString(), UINT64_MAX),
158 0 : mFile(aFile), mWholeFile(true), mStoredFile(false)
159 : {
160 0 : NS_ASSERTION(mFile, "must have file");
161 : // Lazily get the content type and size
162 0 : mContentType.SetIsVoid(true);
163 0 : mFile->GetLeafName(mName);
164 0 : }
165 :
166 : // Create as a blob
167 0 : nsDOMFileFile(nsIFile *aFile, const nsAString& aContentType,
168 : nsISupports *aCacheToken = nsnull)
169 : : nsDOMFileBase(aContentType, UINT64_MAX),
170 : mFile(aFile), mWholeFile(true), mStoredFile(false),
171 0 : mCacheToken(aCacheToken)
172 : {
173 0 : NS_ASSERTION(mFile, "must have file");
174 0 : }
175 :
176 : // Create as a stored file
177 0 : nsDOMFileFile(const nsAString& aName, const nsAString& aContentType,
178 : PRUint64 aLength, nsIFile* aFile,
179 : FileInfo* aFileInfo)
180 : : nsDOMFileBase(aName, aContentType, aLength),
181 0 : mFile(aFile), mWholeFile(true), mStoredFile(true)
182 : {
183 0 : NS_ASSERTION(mFile, "must have file");
184 0 : mFileInfos.AppendElement(aFileInfo);
185 0 : }
186 :
187 : // Create as a stored blob
188 0 : nsDOMFileFile(const nsAString& aContentType, PRUint64 aLength,
189 : nsIFile* aFile, FileInfo* aFileInfo)
190 : : nsDOMFileBase(aContentType, aLength),
191 0 : mFile(aFile), mWholeFile(true), mStoredFile(true)
192 : {
193 0 : NS_ASSERTION(mFile, "must have file");
194 0 : mFileInfos.AppendElement(aFileInfo);
195 0 : }
196 :
197 : // Create as a file to be later initialized
198 10 : nsDOMFileFile()
199 20 : : nsDOMFileBase(EmptyString(), EmptyString(), UINT64_MAX),
200 20 : mWholeFile(true), mStoredFile(false)
201 : {
202 : // Lazily get the content type and size
203 10 : mContentType.SetIsVoid(true);
204 10 : mName.SetIsVoid(true);
205 10 : }
206 :
207 : NS_DECL_ISUPPORTS_INHERITED
208 :
209 : // nsIJSNativeInitializer
210 : NS_IMETHOD Initialize(nsISupports* aOwner,
211 : JSContext* aCx,
212 : JSObject* aObj,
213 : PRUint32 aArgc,
214 : jsval* aArgv);
215 :
216 : // Overrides
217 : NS_IMETHOD GetSize(PRUint64* aSize);
218 : NS_IMETHOD GetType(nsAString& aType);
219 : NS_IMETHOD GetMozFullPathInternal(nsAString& aFullPath);
220 : NS_IMETHOD GetInternalStream(nsIInputStream**);
221 :
222 : // DOMClassInfo constructor (for File("foo"))
223 : static nsresult
224 : NewFile(nsISupports* *aNewObject);
225 :
226 : protected:
227 : // Create slice
228 0 : nsDOMFileFile(const nsDOMFileFile* aOther, PRUint64 aStart, PRUint64 aLength,
229 : const nsAString& aContentType)
230 : : nsDOMFileBase(aContentType, aOther->mStart + aStart, aLength),
231 : mFile(aOther->mFile), mWholeFile(false),
232 0 : mStoredFile(aOther->mStoredFile), mCacheToken(aOther->mCacheToken)
233 : {
234 0 : NS_ASSERTION(mFile, "must have file");
235 0 : mImmutable = aOther->mImmutable;
236 :
237 0 : if (mStoredFile) {
238 : FileInfo* fileInfo;
239 :
240 0 : if (!mozilla::dom::indexedDB::IndexedDatabaseManager::IsClosed()) {
241 0 : mozilla::dom::indexedDB::IndexedDatabaseManager::FileMutex().Lock();
242 : }
243 :
244 0 : NS_ASSERTION(!aOther->mFileInfos.IsEmpty(),
245 : "A stored file must have at least one file info!");
246 :
247 0 : fileInfo = aOther->mFileInfos.ElementAt(0);
248 :
249 0 : if (!mozilla::dom::indexedDB::IndexedDatabaseManager::IsClosed()) {
250 0 : mozilla::dom::indexedDB::IndexedDatabaseManager::FileMutex().Unlock();
251 : }
252 :
253 0 : mFileInfos.AppendElement(fileInfo);
254 : }
255 0 : }
256 : virtual already_AddRefed<nsIDOMBlob>
257 : CreateSlice(PRUint64 aStart, PRUint64 aLength,
258 : const nsAString& aContentType);
259 :
260 0 : virtual bool IsStoredFile()
261 : {
262 0 : return mStoredFile;
263 : }
264 :
265 0 : virtual bool IsWholeFile()
266 : {
267 0 : return mWholeFile;
268 : }
269 :
270 : nsCOMPtr<nsIFile> mFile;
271 : bool mWholeFile;
272 : bool mStoredFile;
273 : nsCOMPtr<nsISupports> mCacheToken;
274 : };
275 :
276 : class nsDOMMemoryFile : public nsDOMFileBase
277 0 : {
278 : public:
279 : // Create as file
280 0 : nsDOMMemoryFile(void *aMemoryBuffer,
281 : PRUint64 aLength,
282 : const nsAString& aName,
283 : const nsAString& aContentType)
284 : : nsDOMFileBase(aName, aContentType, aLength),
285 0 : mDataOwner(new DataOwner(aMemoryBuffer))
286 : {
287 0 : NS_ASSERTION(mDataOwner && mDataOwner->mData, "must have data");
288 0 : }
289 :
290 : // Create as blob
291 0 : nsDOMMemoryFile(void *aMemoryBuffer,
292 : PRUint64 aLength,
293 : const nsAString& aContentType)
294 : : nsDOMFileBase(aContentType, aLength),
295 0 : mDataOwner(new DataOwner(aMemoryBuffer))
296 : {
297 0 : NS_ASSERTION(mDataOwner && mDataOwner->mData, "must have data");
298 0 : }
299 :
300 : NS_IMETHOD GetInternalStream(nsIInputStream**);
301 :
302 : protected:
303 : // Create slice
304 0 : nsDOMMemoryFile(const nsDOMMemoryFile* aOther, PRUint64 aStart,
305 : PRUint64 aLength, const nsAString& aContentType)
306 : : nsDOMFileBase(aContentType, aOther->mStart + aStart, aLength),
307 0 : mDataOwner(aOther->mDataOwner)
308 : {
309 0 : NS_ASSERTION(mDataOwner && mDataOwner->mData, "must have data");
310 0 : mImmutable = aOther->mImmutable;
311 0 : }
312 : virtual already_AddRefed<nsIDOMBlob>
313 : CreateSlice(PRUint64 aStart, PRUint64 aLength,
314 : const nsAString& aContentType);
315 :
316 : friend class DataOwnerAdapter; // Needs to see DataOwner
317 : class DataOwner {
318 : public:
319 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataOwner)
320 0 : DataOwner(void* aMemoryBuffer)
321 0 : : mData(aMemoryBuffer)
322 : {
323 0 : }
324 0 : ~DataOwner() {
325 0 : PR_Free(mData);
326 0 : }
327 : void* mData;
328 : };
329 :
330 : // Used when backed by a memory store
331 : nsRefPtr<DataOwner> mDataOwner;
332 : };
333 :
334 : class nsDOMFileList MOZ_FINAL : public nsIDOMFileList,
335 : public nsWrapperCache
336 0 : {
337 : public:
338 0 : nsDOMFileList(nsISupports *aParent) : mParent(aParent)
339 : {
340 0 : SetIsProxy();
341 0 : }
342 :
343 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
344 1396 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsDOMFileList)
345 :
346 : NS_DECL_NSIDOMFILELIST
347 :
348 : virtual JSObject* WrapObject(JSContext *cx, XPCWrappedNativeScope *scope,
349 : bool *triedToWrap);
350 :
351 0 : nsISupports* GetParentObject()
352 : {
353 0 : return mParent;
354 : }
355 :
356 0 : void Disconnect()
357 : {
358 0 : mParent = nsnull;
359 0 : }
360 :
361 0 : bool Append(nsIDOMFile *aFile) { return mFiles.AppendObject(aFile); }
362 :
363 : bool Remove(PRUint32 aIndex) { return mFiles.RemoveObjectAt(aIndex); }
364 0 : void Clear() { return mFiles.Clear(); }
365 :
366 0 : static nsDOMFileList* FromSupports(nsISupports* aSupports)
367 : {
368 : #ifdef DEBUG
369 : {
370 0 : nsCOMPtr<nsIDOMFileList> list_qi = do_QueryInterface(aSupports);
371 :
372 : // If this assertion fires the QI implementation for the object in
373 : // question doesn't use the nsIDOMFileList pointer as the nsISupports
374 : // pointer. That must be fixed, or we'll crash...
375 0 : NS_ASSERTION(list_qi == static_cast<nsIDOMFileList*>(aSupports),
376 : "Uh, fix QI!");
377 : }
378 : #endif
379 :
380 0 : return static_cast<nsDOMFileList*>(aSupports);
381 : }
382 :
383 : private:
384 : nsCOMArray<nsIDOMFile> mFiles;
385 : nsISupports *mParent;
386 : };
387 :
388 : class NS_STACK_CLASS nsDOMFileInternalUrlHolder {
389 : public:
390 : nsDOMFileInternalUrlHolder(nsIDOMBlob* aFile, nsIPrincipal* aPrincipal
391 : MOZ_GUARD_OBJECT_NOTIFIER_PARAM);
392 : ~nsDOMFileInternalUrlHolder();
393 : nsAutoString mUrl;
394 : private:
395 : MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
396 : };
397 :
398 : #endif
|