dom/devicestorage/DeviceStorage.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:5b5cbb4dcd9c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef DeviceStorage_h
8 #define DeviceStorage_h
9
10 #include "nsIDOMDeviceStorage.h"
11 #include "nsIFile.h"
12 #include "nsIPrincipal.h"
13 #include "nsIObserver.h"
14 #include "mozilla/DOMEventTargetHelper.h"
15 #include "mozilla/RefPtr.h"
16 #include "mozilla/StaticPtr.h"
17 #include "mozilla/dom/DOMRequest.h"
18
19 #define DEVICESTORAGE_PICTURES "pictures"
20 #define DEVICESTORAGE_VIDEOS "videos"
21 #define DEVICESTORAGE_MUSIC "music"
22 #define DEVICESTORAGE_APPS "apps"
23 #define DEVICESTORAGE_SDCARD "sdcard"
24 #define DEVICESTORAGE_CRASHES "crashes"
25
26 class DeviceStorageFile;
27 class nsIInputStream;
28
29 namespace mozilla {
30 class EventListenerManager;
31 namespace dom {
32 class DeviceStorageEnumerationParameters;
33 class DOMCursor;
34 class DOMRequest;
35 class Promise;
36 class DeviceStorageFileSystem;
37 } // namespace dom
38 namespace ipc {
39 class FileDescriptor;
40 }
41 } // namespace mozilla
42
43 class DeviceStorageFile MOZ_FINAL
44 : public nsISupports {
45 public:
46 nsCOMPtr<nsIFile> mFile;
47 nsString mStorageType;
48 nsString mStorageName;
49 nsString mRootDir;
50 nsString mPath;
51 bool mEditable;
52 nsString mMimeType;
53 uint64_t mLength;
54 uint64_t mLastModifiedDate;
55
56 // Used when the path will be set later via SetPath.
57 DeviceStorageFile(const nsAString& aStorageType,
58 const nsAString& aStorageName);
59 // Used for non-enumeration purposes.
60 DeviceStorageFile(const nsAString& aStorageType,
61 const nsAString& aStorageName,
62 const nsAString& aPath);
63 // Used for enumerations. When you call Enumerate, you can pass in a
64 // directory to enumerate and the results that are returned are relative to
65 // that directory, files related to an enumeration need to know the "root of
66 // the enumeration" directory.
67 DeviceStorageFile(const nsAString& aStorageType,
68 const nsAString& aStorageName,
69 const nsAString& aRootDir,
70 const nsAString& aPath);
71
72 void SetPath(const nsAString& aPath);
73 void SetEditable(bool aEditable);
74
75 static already_AddRefed<DeviceStorageFile>
76 CreateUnique(nsAString& aFileName,
77 uint32_t aFileType,
78 uint32_t aFileAttributes);
79
80 NS_DECL_THREADSAFE_ISUPPORTS
81
82 bool IsAvailable();
83 void GetFullPath(nsAString& aFullPath);
84
85 // we want to make sure that the names of file can't reach
86 // outside of the type of storage the user asked for.
87 bool IsSafePath();
88 bool IsSafePath(const nsAString& aPath);
89
90 void Dump(const char* label);
91
92 nsresult Remove();
93 nsresult Write(nsIInputStream* aInputStream);
94 nsresult Write(InfallibleTArray<uint8_t>& bits);
95 void CollectFiles(nsTArray<nsRefPtr<DeviceStorageFile> >& aFiles,
96 PRTime aSince = 0);
97 void collectFilesInternal(nsTArray<nsRefPtr<DeviceStorageFile> >& aFiles,
98 PRTime aSince, nsAString& aRootPath);
99
100 void AccumDiskUsage(uint64_t* aPicturesSoFar, uint64_t* aVideosSoFar,
101 uint64_t* aMusicSoFar, uint64_t* aTotalSoFar);
102
103 void GetDiskFreeSpace(int64_t* aSoFar);
104 void GetStatus(nsAString& aStatus);
105 void GetStorageStatus(nsAString& aStatus);
106 void DoFormat(nsAString& aStatus);
107 void DoMount(nsAString& aStatus);
108 void DoUnmount(nsAString& aStatus);
109 static void GetRootDirectoryForType(const nsAString& aStorageType,
110 const nsAString& aStorageName,
111 nsIFile** aFile);
112
113 nsresult CalculateSizeAndModifiedDate();
114 nsresult CalculateMimeType();
115 nsresult CreateFileDescriptor(mozilla::ipc::FileDescriptor& aFileDescriptor);
116
117 private:
118 void Init();
119 void NormalizeFilePath();
120 void AppendRelativePath(const nsAString& aPath);
121 void AccumDirectoryUsage(nsIFile* aFile,
122 uint64_t* aPicturesSoFar,
123 uint64_t* aVideosSoFar,
124 uint64_t* aMusicSoFar,
125 uint64_t* aTotalSoFar);
126 };
127
128 /*
129 The FileUpdateDispatcher converts file-watcher-notify
130 observer events to file-watcher-update events. This is
131 used to be able to broadcast events from one child to
132 another child in B2G. (f.e., if one child decides to add
133 a file, we want to be able to able to send a onchange
134 notifications to every other child watching that device
135 storage object).
136
137 We create this object (via GetSingleton) in two places:
138 * ContentParent::Init (for IPC)
139 * nsDOMDeviceStorage::Init (for non-ipc)
140 */
141 class FileUpdateDispatcher MOZ_FINAL
142 : public nsIObserver
143 {
144 public:
145 NS_DECL_ISUPPORTS
146 NS_DECL_NSIOBSERVER
147
148 static FileUpdateDispatcher* GetSingleton();
149 private:
150 static mozilla::StaticRefPtr<FileUpdateDispatcher> sSingleton;
151 };
152
153 class nsDOMDeviceStorage MOZ_FINAL
154 : public mozilla::DOMEventTargetHelper
155 , public nsIDOMDeviceStorage
156 , public nsIObserver
157 {
158 typedef mozilla::ErrorResult ErrorResult;
159 typedef mozilla::dom::DeviceStorageEnumerationParameters
160 EnumerationParameters;
161 typedef mozilla::dom::DOMCursor DOMCursor;
162 typedef mozilla::dom::DOMRequest DOMRequest;
163 typedef mozilla::dom::Promise Promise;
164 typedef mozilla::dom::DeviceStorageFileSystem DeviceStorageFileSystem;
165 public:
166 typedef nsTArray<nsString> VolumeNameArray;
167
168 NS_DECL_ISUPPORTS_INHERITED
169 NS_DECL_NSIDOMDEVICESTORAGE
170
171 NS_DECL_NSIOBSERVER
172 NS_DECL_NSIDOMEVENTTARGET
173
174 virtual mozilla::EventListenerManager*
175 GetExistingListenerManager() const MOZ_OVERRIDE;
176 virtual mozilla::EventListenerManager*
177 GetOrCreateListenerManager() MOZ_OVERRIDE;
178
179 virtual void
180 AddEventListener(const nsAString& aType,
181 mozilla::dom::EventListener* aListener,
182 bool aUseCapture,
183 const mozilla::dom::Nullable<bool>& aWantsUntrusted,
184 ErrorResult& aRv) MOZ_OVERRIDE;
185
186 virtual void RemoveEventListener(const nsAString& aType,
187 mozilla::dom::EventListener* aListener,
188 bool aUseCapture,
189 ErrorResult& aRv) MOZ_OVERRIDE;
190
191 nsDOMDeviceStorage(nsPIDOMWindow* aWindow);
192
193 nsresult Init(nsPIDOMWindow* aWindow, const nsAString& aType,
194 const nsAString& aVolName);
195
196 bool IsAvailable();
197 bool IsFullPath(const nsAString& aPath)
198 {
199 return aPath.Length() > 0 && aPath.CharAt(0) == '/';
200 }
201
202 void SetRootDirectoryForType(const nsAString& aType,
203 const nsAString& aVolName);
204
205 // WebIDL
206 nsPIDOMWindow*
207 GetParentObject() const
208 {
209 return GetOwner();
210 }
211 virtual JSObject*
212 WrapObject(JSContext* aCx) MOZ_OVERRIDE;
213
214 IMPL_EVENT_HANDLER(change)
215
216 already_AddRefed<DOMRequest>
217 Add(nsIDOMBlob* aBlob, ErrorResult& aRv);
218 already_AddRefed<DOMRequest>
219 AddNamed(nsIDOMBlob* aBlob, const nsAString& aPath, ErrorResult& aRv);
220
221 already_AddRefed<DOMRequest>
222 Get(const nsAString& aPath, ErrorResult& aRv)
223 {
224 return GetInternal(aPath, false, aRv);
225 }
226 already_AddRefed<DOMRequest>
227 GetEditable(const nsAString& aPath, ErrorResult& aRv)
228 {
229 return GetInternal(aPath, true, aRv);
230 }
231 already_AddRefed<DOMRequest>
232 Delete(const nsAString& aPath, ErrorResult& aRv);
233
234 already_AddRefed<DOMCursor>
235 Enumerate(const EnumerationParameters& aOptions, ErrorResult& aRv)
236 {
237 return Enumerate(NullString(), aOptions, aRv);
238 }
239 already_AddRefed<DOMCursor>
240 Enumerate(const nsAString& aPath, const EnumerationParameters& aOptions,
241 ErrorResult& aRv);
242 already_AddRefed<DOMCursor>
243 EnumerateEditable(const EnumerationParameters& aOptions, ErrorResult& aRv)
244 {
245 return EnumerateEditable(NullString(), aOptions, aRv);
246 }
247 already_AddRefed<DOMCursor>
248 EnumerateEditable(const nsAString& aPath,
249 const EnumerationParameters& aOptions, ErrorResult& aRv);
250
251 already_AddRefed<DOMRequest> FreeSpace(ErrorResult& aRv);
252 already_AddRefed<DOMRequest> UsedSpace(ErrorResult& aRv);
253 already_AddRefed<DOMRequest> Available(ErrorResult& aRv);
254 already_AddRefed<DOMRequest> Format(ErrorResult& aRv);
255 already_AddRefed<DOMRequest> StorageStatus(ErrorResult& aRv);
256 already_AddRefed<DOMRequest> Mount(ErrorResult& aRv);
257 already_AddRefed<DOMRequest> Unmount(ErrorResult& aRv);
258
259 bool Default();
260
261 // Uses XPCOM GetStorageName
262
263 already_AddRefed<Promise>
264 GetRoot();
265
266 static void
267 CreateDeviceStorageFor(nsPIDOMWindow* aWin,
268 const nsAString& aType,
269 nsDOMDeviceStorage** aStore);
270
271 static void
272 CreateDeviceStoragesFor(nsPIDOMWindow* aWin,
273 const nsAString& aType,
274 nsTArray<nsRefPtr<nsDOMDeviceStorage> >& aStores);
275
276 void Shutdown();
277
278 static void GetOrderedVolumeNames(nsTArray<nsString>& aVolumeNames);
279
280 static void GetDefaultStorageName(const nsAString& aStorageType,
281 nsAString &aStorageName);
282
283 static bool ParseFullPath(const nsAString& aFullPath,
284 nsAString& aOutStorageName,
285 nsAString& aOutStoragePath);
286 private:
287 ~nsDOMDeviceStorage();
288
289 already_AddRefed<DOMRequest>
290 GetInternal(const nsAString& aPath, bool aEditable, ErrorResult& aRv);
291
292 void
293 GetInternal(nsPIDOMWindow* aWin, const nsAString& aPath, DOMRequest* aRequest,
294 bool aEditable);
295
296 void
297 DeleteInternal(nsPIDOMWindow* aWin, const nsAString& aPath,
298 DOMRequest* aRequest);
299
300 already_AddRefed<DOMCursor>
301 EnumerateInternal(const nsAString& aName,
302 const EnumerationParameters& aOptions, bool aEditable,
303 ErrorResult& aRv);
304
305 nsString mStorageType;
306 nsCOMPtr<nsIFile> mRootDirectory;
307 nsString mStorageName;
308
309 already_AddRefed<nsDOMDeviceStorage> GetStorage(const nsAString& aFullPath,
310 nsAString& aOutStoragePath);
311 already_AddRefed<nsDOMDeviceStorage>
312 GetStorageByName(const nsAString &aStorageName);
313
314 nsCOMPtr<nsIPrincipal> mPrincipal;
315
316 bool mIsWatchingFile;
317 bool mAllowedToWatchFile;
318
319 nsresult Notify(const char* aReason, class DeviceStorageFile* aFile);
320
321 friend class WatchFileEvent;
322 friend class DeviceStorageRequest;
323
324 static mozilla::StaticAutoPtr<nsTArray<nsString>> sVolumeNameCache;
325
326 #ifdef MOZ_WIDGET_GONK
327 nsString mLastStatus;
328 void DispatchMountChangeEvent(nsAString& aVolumeStatus);
329 #endif
330
331 // nsIDOMDeviceStorage.type
332 enum {
333 DEVICE_STORAGE_TYPE_DEFAULT = 0,
334 DEVICE_STORAGE_TYPE_SHARED,
335 DEVICE_STORAGE_TYPE_EXTERNAL
336 };
337
338 nsRefPtr<DeviceStorageFileSystem> mFileSystem;
339 };
340
341 #endif

mercurial