dom/devicestorage/nsDeviceStorage.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:5654c9a0da50
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/. */
4
5 #ifndef nsDeviceStorage_h
6 #define nsDeviceStorage_h
7
8 class nsPIDOMWindow;
9 #include "mozilla/Attributes.h"
10 #include "PCOMContentPermissionRequestChild.h"
11
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/dom/devicestorage/DeviceStorageRequestChild.h"
32 #include "mozilla/StaticPtr.h"
33
34 namespace mozilla {
35 class ErrorResult;
36 } // namespace mozilla
37
38 #define POST_ERROR_EVENT_FILE_EXISTS "NoModificationAllowedError"
39 #define POST_ERROR_EVENT_FILE_DOES_NOT_EXIST "NotFoundError"
40 #define POST_ERROR_EVENT_FILE_NOT_ENUMERABLE "TypeMismatchError"
41 #define POST_ERROR_EVENT_PERMISSION_DENIED "SecurityError"
42 #define POST_ERROR_EVENT_ILLEGAL_TYPE "TypeMismatchError"
43 #define POST_ERROR_EVENT_UNKNOWN "Unknown"
44
45 enum DeviceStorageRequestType {
46 DEVICE_STORAGE_REQUEST_READ,
47 DEVICE_STORAGE_REQUEST_WRITE,
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
59 };
60
61 class DeviceStorageUsedSpaceCache MOZ_FINAL
62 {
63 public:
64 static DeviceStorageUsedSpaceCache* CreateOrGet();
65
66 DeviceStorageUsedSpaceCache();
67 ~DeviceStorageUsedSpaceCache();
68
69
70 class InvalidateRunnable MOZ_FINAL : public nsRunnable
71 {
72 public:
73 InvalidateRunnable(DeviceStorageUsedSpaceCache* aCache,
74 const nsAString& aStorageName)
75 : mCache(aCache)
76 , mStorageName(aStorageName) {}
77
78 ~InvalidateRunnable() {}
79
80 NS_IMETHOD Run() MOZ_OVERRIDE
81 {
82 nsRefPtr<DeviceStorageUsedSpaceCache::CacheEntry> cacheEntry;
83 cacheEntry = mCache->GetCacheEntry(mStorageName);
84 if (cacheEntry) {
85 cacheEntry->mDirty = true;
86 }
87 return NS_OK;
88 }
89 private:
90 DeviceStorageUsedSpaceCache* mCache;
91 nsString mStorageName;
92 };
93
94 void Invalidate(const nsAString& aStorageName)
95 {
96 MOZ_ASSERT(NS_IsMainThread());
97 MOZ_ASSERT(mIOThread);
98
99 nsRefPtr<InvalidateRunnable> r = new InvalidateRunnable(this, aStorageName);
100 mIOThread->Dispatch(r, NS_DISPATCH_NORMAL);
101 }
102
103 void Dispatch(nsIRunnable* aRunnable)
104 {
105 MOZ_ASSERT(NS_IsMainThread());
106 MOZ_ASSERT(mIOThread);
107
108 mIOThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
109 }
110
111 nsresult AccumUsedSizes(const nsAString& aStorageName,
112 uint64_t* aPictureSize, uint64_t* aVideosSize,
113 uint64_t* aMusicSize, uint64_t* aTotalSize);
114
115 void SetUsedSizes(const nsAString& aStorageName,
116 uint64_t aPictureSize, uint64_t aVideosSize,
117 uint64_t aMusicSize, uint64_t aTotalSize);
118
119 private:
120 friend class InvalidateRunnable;
121
122 struct CacheEntry
123 {
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(DeviceStorageUsedSpaceCache::CacheEntry)
128
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;
136 };
137 already_AddRefed<CacheEntry> GetCacheEntry(const nsAString& aStorageName);
138
139 nsTArray<nsRefPtr<CacheEntry>> mCacheEntries;
140
141 nsCOMPtr<nsIThread> mIOThread;
142
143 static mozilla::StaticAutoPtr<DeviceStorageUsedSpaceCache> sDeviceStorageUsedSpaceCache;
144 };
145
146 class DeviceStorageTypeChecker MOZ_FINAL
147 {
148 public:
149 static DeviceStorageTypeChecker* CreateOrGet();
150
151 DeviceStorageTypeChecker();
152 ~DeviceStorageTypeChecker();
153
154 void InitFromBundle(nsIStringBundle* aBundle);
155
156 bool Check(const nsAString& aType, nsIDOMBlob* aBlob);
157 bool Check(const nsAString& aType, nsIFile* aFile);
158 void GetTypeFromFile(nsIFile* aFile, nsAString& aType);
159 void GetTypeFromFileName(const nsAString& aFileName, nsAString& aType);
160
161 static nsresult GetPermissionForType(const nsAString& aType, nsACString& aPermissionResult);
162 static nsresult GetAccessForRequest(const DeviceStorageRequestType aRequestType, nsACString& aAccessResult);
163 static bool IsVolumeBased(const nsAString& aType);
164
165 private:
166 nsString mPicturesExtensions;
167 nsString mVideosExtensions;
168 nsString mMusicExtensions;
169
170 static mozilla::StaticAutoPtr<DeviceStorageTypeChecker> sDeviceStorageTypeChecker;
171 };
172
173 class ContinueCursorEvent MOZ_FINAL : public nsRunnable
174 {
175 public:
176 ContinueCursorEvent(already_AddRefed<mozilla::dom::DOMRequest> aRequest);
177 ContinueCursorEvent(mozilla::dom::DOMRequest* aRequest);
178 ~ContinueCursorEvent();
179 void Continue();
180
181 NS_IMETHOD Run() MOZ_OVERRIDE;
182 private:
183 already_AddRefed<DeviceStorageFile> GetNextFile();
184 nsRefPtr<mozilla::dom::DOMRequest> mRequest;
185 };
186
187 class nsDOMDeviceStorageCursor MOZ_FINAL
188 : public mozilla::dom::DOMCursor
189 , public nsIContentPermissionRequest
190 , public PCOMContentPermissionRequestChild
191 , public mozilla::dom::devicestorage::DeviceStorageRequestChildCallback
192 {
193 public:
194 NS_DECL_ISUPPORTS_INHERITED
195 NS_DECL_NSICONTENTPERMISSIONREQUEST
196 NS_FORWARD_NSIDOMDOMCURSOR(mozilla::dom::DOMCursor::)
197
198 // DOMCursor
199 virtual void Continue(mozilla::ErrorResult& aRv) MOZ_OVERRIDE;
200
201 nsDOMDeviceStorageCursor(nsPIDOMWindow* aWindow,
202 nsIPrincipal* aPrincipal,
203 DeviceStorageFile* aFile,
204 PRTime aSince);
205
206
207 nsTArray<nsRefPtr<DeviceStorageFile> > mFiles;
208 bool mOkToCallContinue;
209 PRTime mSince;
210
211 virtual bool Recv__delete__(const bool& allow,
212 const InfallibleTArray<PermissionChoice>& choices) MOZ_OVERRIDE;
213 virtual void IPDLRelease() MOZ_OVERRIDE;
214
215 void GetStorageType(nsAString & aType);
216
217 void RequestComplete() MOZ_OVERRIDE;
218
219 private:
220 ~nsDOMDeviceStorageCursor();
221
222 nsRefPtr<DeviceStorageFile> mFile;
223 nsCOMPtr<nsIPrincipal> mPrincipal;
224 };
225
226 //helpers
227 JS::Value
228 StringToJsval(nsPIDOMWindow* aWindow, nsAString& aString);
229
230 JS::Value
231 nsIFileToJsval(nsPIDOMWindow* aWindow, DeviceStorageFile* aFile);
232
233 JS::Value
234 InterfaceToJsval(nsPIDOMWindow* aWindow, nsISupports* aObject, const nsIID* aIID);
235
236 #endif

mercurial