Bumping manifests a=b2g-bump
[gecko.git] / dom / devicestorage / nsDeviceStorage.h
blob68b5de0020ff12745954e41bd750560a92475049
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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsDeviceStorage_h
6 #define nsDeviceStorage_h
8 class nsPIDOMWindow;
9 #include "mozilla/Attributes.h"
10 #include "mozilla/dom/devicestorage/DeviceStorageRequestChild.h"
12 #include "DOMRequest.h"
13 #include "DOMCursor.h"
14 #include "nsAutoPtr.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsDOMClassInfoID.h"
17 #include "nsIClassInfo.h"
18 #include "nsIContentPermissionPrompt.h"
19 #include "nsIDOMWindow.h"
20 #include "nsIURI.h"
21 #include "nsIPrincipal.h"
22 #include "nsString.h"
23 #include "nsWeakPtr.h"
24 #include "nsIDOMEventListener.h"
25 #include "nsIObserver.h"
26 #include "nsIStringBundle.h"
27 #include "mozilla/Mutex.h"
28 #include "prtime.h"
29 #include "DeviceStorage.h"
30 #include "mozilla/StaticPtr.h"
32 namespace mozilla {
33 class ErrorResult;
34 } // namespace mozilla
36 #define POST_ERROR_EVENT_FILE_EXISTS "NoModificationAllowedError"
37 #define POST_ERROR_EVENT_FILE_DOES_NOT_EXIST "NotFoundError"
38 #define POST_ERROR_EVENT_FILE_NOT_ENUMERABLE "TypeMismatchError"
39 #define POST_ERROR_EVENT_PERMISSION_DENIED "SecurityError"
40 #define POST_ERROR_EVENT_ILLEGAL_TYPE "TypeMismatchError"
41 #define POST_ERROR_EVENT_UNKNOWN "Unknown"
43 enum DeviceStorageRequestType {
44 DEVICE_STORAGE_REQUEST_READ,
45 DEVICE_STORAGE_REQUEST_WRITE,
46 DEVICE_STORAGE_REQUEST_APPEND,
47 DEVICE_STORAGE_REQUEST_CREATE,
48 DEVICE_STORAGE_REQUEST_DELETE,
49 DEVICE_STORAGE_REQUEST_WATCH,
50 DEVICE_STORAGE_REQUEST_FREE_SPACE,
51 DEVICE_STORAGE_REQUEST_USED_SPACE,
52 DEVICE_STORAGE_REQUEST_AVAILABLE,
53 DEVICE_STORAGE_REQUEST_STATUS,
54 DEVICE_STORAGE_REQUEST_FORMAT,
55 DEVICE_STORAGE_REQUEST_MOUNT,
56 DEVICE_STORAGE_REQUEST_UNMOUNT,
57 DEVICE_STORAGE_REQUEST_CREATEFD
60 class DeviceStorageUsedSpaceCache MOZ_FINAL
62 public:
63 static DeviceStorageUsedSpaceCache* CreateOrGet();
65 DeviceStorageUsedSpaceCache();
66 ~DeviceStorageUsedSpaceCache();
69 class InvalidateRunnable MOZ_FINAL : public nsRunnable
71 public:
72 InvalidateRunnable(DeviceStorageUsedSpaceCache* aCache,
73 const nsAString& aStorageName)
74 : mCache(aCache)
75 , mStorageName(aStorageName) {}
77 ~InvalidateRunnable() {}
79 NS_IMETHOD Run() MOZ_OVERRIDE
81 nsRefPtr<DeviceStorageUsedSpaceCache::CacheEntry> cacheEntry;
82 cacheEntry = mCache->GetCacheEntry(mStorageName);
83 if (cacheEntry) {
84 cacheEntry->mDirty = true;
86 return NS_OK;
88 private:
89 DeviceStorageUsedSpaceCache* mCache;
90 nsString mStorageName;
93 void Invalidate(const nsAString& aStorageName)
95 MOZ_ASSERT(NS_IsMainThread());
96 MOZ_ASSERT(mIOThread);
98 nsRefPtr<InvalidateRunnable> r = new InvalidateRunnable(this, aStorageName);
99 mIOThread->Dispatch(r, NS_DISPATCH_NORMAL);
102 void Dispatch(nsIRunnable* aRunnable)
104 MOZ_ASSERT(NS_IsMainThread());
105 MOZ_ASSERT(mIOThread);
107 mIOThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
110 nsresult AccumUsedSizes(const nsAString& aStorageName,
111 uint64_t* aPictureSize, uint64_t* aVideosSize,
112 uint64_t* aMusicSize, uint64_t* aTotalSize);
114 void SetUsedSizes(const nsAString& aStorageName,
115 uint64_t aPictureSize, uint64_t aVideosSize,
116 uint64_t aMusicSize, uint64_t aTotalSize);
118 private:
119 friend class InvalidateRunnable;
121 struct CacheEntry
123 // Technically, this doesn't need to be threadsafe, but the implementation
124 // of the non-thread safe one causes ASSERTS due to the underlying thread
125 // associated with a LazyIdleThread changing from time to time.
126 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheEntry)
128 bool mDirty;
129 nsString mStorageName;
130 int64_t mFreeBytes;
131 uint64_t mPicturesUsedSize;
132 uint64_t mVideosUsedSize;
133 uint64_t mMusicUsedSize;
134 uint64_t mTotalUsedSize;
136 private:
137 ~CacheEntry() {}
139 already_AddRefed<CacheEntry> GetCacheEntry(const nsAString& aStorageName);
141 nsTArray<nsRefPtr<CacheEntry>> mCacheEntries;
143 nsCOMPtr<nsIThread> mIOThread;
145 static mozilla::StaticAutoPtr<DeviceStorageUsedSpaceCache> sDeviceStorageUsedSpaceCache;
148 class DeviceStorageTypeChecker MOZ_FINAL
150 public:
151 static DeviceStorageTypeChecker* CreateOrGet();
153 DeviceStorageTypeChecker();
154 ~DeviceStorageTypeChecker();
156 void InitFromBundle(nsIStringBundle* aBundle);
158 bool Check(const nsAString& aType, nsIDOMBlob* aBlob);
159 bool Check(const nsAString& aType, nsIFile* aFile);
160 bool Check(const nsAString& aType, const nsString& aPath);
161 void GetTypeFromFile(nsIFile* aFile, nsAString& aType);
162 void GetTypeFromFileName(const nsAString& aFileName, nsAString& aType);
164 static nsresult GetPermissionForType(const nsAString& aType, nsACString& aPermissionResult);
165 static nsresult GetAccessForRequest(const DeviceStorageRequestType aRequestType, nsACString& aAccessResult);
166 static bool IsVolumeBased(const nsAString& aType);
167 static bool IsSharedMediaRoot(const nsAString& aType);
169 private:
170 nsString mPicturesExtensions;
171 nsString mVideosExtensions;
172 nsString mMusicExtensions;
174 static mozilla::StaticAutoPtr<DeviceStorageTypeChecker> sDeviceStorageTypeChecker;
177 class ContinueCursorEvent MOZ_FINAL : public nsRunnable
179 public:
180 explicit ContinueCursorEvent(already_AddRefed<mozilla::dom::DOMRequest> aRequest);
181 explicit ContinueCursorEvent(mozilla::dom::DOMRequest* aRequest);
182 ~ContinueCursorEvent();
183 void Continue();
185 NS_IMETHOD Run() MOZ_OVERRIDE;
186 private:
187 already_AddRefed<DeviceStorageFile> GetNextFile();
188 nsRefPtr<mozilla::dom::DOMRequest> mRequest;
191 class nsDOMDeviceStorageCursor MOZ_FINAL
192 : public mozilla::dom::DOMCursor
193 , public nsIContentPermissionRequest
194 , public mozilla::dom::devicestorage::DeviceStorageRequestChildCallback
196 public:
197 NS_DECL_ISUPPORTS_INHERITED
198 NS_DECL_NSICONTENTPERMISSIONREQUEST
199 NS_FORWARD_NSIDOMDOMCURSOR(mozilla::dom::DOMCursor::)
201 // DOMCursor
202 virtual void Continue(mozilla::ErrorResult& aRv) MOZ_OVERRIDE;
204 nsDOMDeviceStorageCursor(nsPIDOMWindow* aWindow,
205 nsIPrincipal* aPrincipal,
206 DeviceStorageFile* aFile,
207 PRTime aSince);
210 nsTArray<nsRefPtr<DeviceStorageFile> > mFiles;
211 bool mOkToCallContinue;
212 PRTime mSince;
214 void GetStorageType(nsAString & aType);
216 void RequestComplete() MOZ_OVERRIDE;
218 private:
219 ~nsDOMDeviceStorageCursor();
221 nsRefPtr<DeviceStorageFile> mFile;
222 nsCOMPtr<nsIPrincipal> mPrincipal;
225 //helpers
226 bool
227 StringToJsval(nsPIDOMWindow* aWindow, nsAString& aString,
228 JS::MutableHandle<JS::Value> result);
230 JS::Value
231 nsIFileToJsval(nsPIDOMWindow* aWindow, DeviceStorageFile* aFile);
233 JS::Value
234 InterfaceToJsval(nsPIDOMWindow* aWindow, nsISupports* aObject, const nsIID* aIID);
236 #endif