Bumping manifests a=b2g-bump
[gecko.git] / dom / devicestorage / nsDeviceStorage.h
blob3a81512cdc97c37a47f0f2929da959fc1d5f8670
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 "nsInterfaceHashtable.h"
22 #include "nsIPrincipal.h"
23 #include "nsString.h"
24 #include "nsWeakPtr.h"
25 #include "nsIDOMEventListener.h"
26 #include "nsIObserver.h"
27 #include "nsIStringBundle.h"
28 #include "mozilla/Mutex.h"
29 #include "prtime.h"
30 #include "DeviceStorage.h"
31 #include "mozilla/StaticPtr.h"
33 namespace mozilla {
34 class ErrorResult;
35 } // namespace mozilla
37 #define POST_ERROR_EVENT_FILE_EXISTS "NoModificationAllowedError"
38 #define POST_ERROR_EVENT_FILE_DOES_NOT_EXIST "NotFoundError"
39 #define POST_ERROR_EVENT_FILE_NOT_ENUMERABLE "TypeMismatchError"
40 #define POST_ERROR_EVENT_PERMISSION_DENIED "SecurityError"
41 #define POST_ERROR_EVENT_ILLEGAL_TYPE "TypeMismatchError"
42 #define POST_ERROR_EVENT_UNKNOWN "Unknown"
44 enum DeviceStorageRequestType {
45 DEVICE_STORAGE_REQUEST_READ,
46 DEVICE_STORAGE_REQUEST_WRITE,
47 DEVICE_STORAGE_REQUEST_APPEND,
48 DEVICE_STORAGE_REQUEST_CREATE,
49 DEVICE_STORAGE_REQUEST_DELETE,
50 DEVICE_STORAGE_REQUEST_WATCH,
51 DEVICE_STORAGE_REQUEST_FREE_SPACE,
52 DEVICE_STORAGE_REQUEST_USED_SPACE,
53 DEVICE_STORAGE_REQUEST_AVAILABLE,
54 DEVICE_STORAGE_REQUEST_STATUS,
55 DEVICE_STORAGE_REQUEST_FORMAT,
56 DEVICE_STORAGE_REQUEST_MOUNT,
57 DEVICE_STORAGE_REQUEST_UNMOUNT,
58 DEVICE_STORAGE_REQUEST_CREATEFD
61 class DeviceStorageUsedSpaceCache MOZ_FINAL
63 public:
64 static DeviceStorageUsedSpaceCache* CreateOrGet();
66 DeviceStorageUsedSpaceCache();
67 ~DeviceStorageUsedSpaceCache();
70 class InvalidateRunnable MOZ_FINAL : public nsRunnable
72 public:
73 InvalidateRunnable(DeviceStorageUsedSpaceCache* aCache,
74 const nsAString& aStorageName)
75 : mCache(aCache)
76 , mStorageName(aStorageName) {}
78 ~InvalidateRunnable() {}
80 NS_IMETHOD Run() MOZ_OVERRIDE
82 nsRefPtr<DeviceStorageUsedSpaceCache::CacheEntry> cacheEntry;
83 cacheEntry = mCache->GetCacheEntry(mStorageName);
84 if (cacheEntry) {
85 cacheEntry->mDirty = true;
87 return NS_OK;
89 private:
90 DeviceStorageUsedSpaceCache* mCache;
91 nsString mStorageName;
94 void Invalidate(const nsAString& aStorageName)
96 MOZ_ASSERT(NS_IsMainThread());
97 MOZ_ASSERT(mIOThread);
99 nsRefPtr<InvalidateRunnable> r = new InvalidateRunnable(this, aStorageName);
100 mIOThread->Dispatch(r, NS_DISPATCH_NORMAL);
103 void Dispatch(nsIRunnable* aRunnable)
105 MOZ_ASSERT(NS_IsMainThread());
106 MOZ_ASSERT(mIOThread);
108 mIOThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
111 nsresult AccumUsedSizes(const nsAString& aStorageName,
112 uint64_t* aPictureSize, uint64_t* aVideosSize,
113 uint64_t* aMusicSize, uint64_t* aTotalSize);
115 void SetUsedSizes(const nsAString& aStorageName,
116 uint64_t aPictureSize, uint64_t aVideosSize,
117 uint64_t aMusicSize, uint64_t aTotalSize);
119 private:
120 friend class InvalidateRunnable;
122 struct CacheEntry
124 // Technically, this doesn't need to be threadsafe, but the implementation
125 // of the non-thread safe one causes ASSERTS due to the underlying thread
126 // associated with a LazyIdleThread changing from time to time.
127 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheEntry)
129 bool mDirty;
130 nsString mStorageName;
131 int64_t mFreeBytes;
132 uint64_t mPicturesUsedSize;
133 uint64_t mVideosUsedSize;
134 uint64_t mMusicUsedSize;
135 uint64_t mTotalUsedSize;
137 private:
138 ~CacheEntry() {}
140 already_AddRefed<CacheEntry> GetCacheEntry(const nsAString& aStorageName);
142 nsTArray<nsRefPtr<CacheEntry>> mCacheEntries;
144 nsCOMPtr<nsIThread> mIOThread;
146 static mozilla::StaticAutoPtr<DeviceStorageUsedSpaceCache> sDeviceStorageUsedSpaceCache;
149 class DeviceStorageTypeChecker MOZ_FINAL
151 public:
152 static DeviceStorageTypeChecker* CreateOrGet();
154 DeviceStorageTypeChecker();
155 ~DeviceStorageTypeChecker();
157 void InitFromBundle(nsIStringBundle* aBundle);
159 bool Check(const nsAString& aType, nsIDOMBlob* aBlob);
160 bool Check(const nsAString& aType, nsIFile* aFile);
161 bool Check(const nsAString& aType, const nsString& aPath);
162 void GetTypeFromFile(nsIFile* aFile, nsAString& aType);
163 void GetTypeFromFileName(const nsAString& aFileName, nsAString& aType);
165 static nsresult GetPermissionForType(const nsAString& aType, nsACString& aPermissionResult);
166 static nsresult GetAccessForRequest(const DeviceStorageRequestType aRequestType, nsACString& aAccessResult);
167 static bool IsVolumeBased(const nsAString& aType);
168 static bool IsSharedMediaRoot(const nsAString& aType);
170 private:
171 nsString mPicturesExtensions;
172 nsString mVideosExtensions;
173 nsString mMusicExtensions;
175 static mozilla::StaticAutoPtr<DeviceStorageTypeChecker> sDeviceStorageTypeChecker;
178 class ContinueCursorEvent MOZ_FINAL : public nsRunnable
180 public:
181 explicit ContinueCursorEvent(already_AddRefed<mozilla::dom::DOMRequest> aRequest);
182 explicit ContinueCursorEvent(mozilla::dom::DOMRequest* aRequest);
183 ~ContinueCursorEvent();
184 void Continue();
186 NS_IMETHOD Run() MOZ_OVERRIDE;
187 private:
188 already_AddRefed<DeviceStorageFile> GetNextFile();
189 nsRefPtr<mozilla::dom::DOMRequest> mRequest;
192 class nsDOMDeviceStorageCursor MOZ_FINAL
193 : public mozilla::dom::DOMCursor
194 , public nsIContentPermissionRequest
195 , public mozilla::dom::devicestorage::DeviceStorageRequestChildCallback
197 public:
198 NS_DECL_ISUPPORTS_INHERITED
199 NS_DECL_NSICONTENTPERMISSIONREQUEST
200 NS_FORWARD_NSIDOMDOMCURSOR(mozilla::dom::DOMCursor::)
202 // DOMCursor
203 virtual void Continue(mozilla::ErrorResult& aRv) MOZ_OVERRIDE;
205 nsDOMDeviceStorageCursor(nsPIDOMWindow* aWindow,
206 nsIPrincipal* aPrincipal,
207 DeviceStorageFile* aFile,
208 PRTime aSince);
211 nsTArray<nsRefPtr<DeviceStorageFile> > mFiles;
212 bool mOkToCallContinue;
213 PRTime mSince;
215 void GetStorageType(nsAString & aType);
217 void RequestComplete() MOZ_OVERRIDE;
219 private:
220 ~nsDOMDeviceStorageCursor();
222 nsRefPtr<DeviceStorageFile> mFile;
223 nsCOMPtr<nsIPrincipal> mPrincipal;
226 //helpers
227 bool
228 StringToJsval(nsPIDOMWindow* aWindow, nsAString& aString,
229 JS::MutableHandle<JS::Value> result);
231 JS::Value
232 nsIFileToJsval(nsPIDOMWindow* aWindow, DeviceStorageFile* aFile);
234 JS::Value
235 InterfaceToJsval(nsPIDOMWindow* aWindow, nsISupports* aObject, const nsIID* aIID);
237 #endif