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
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"
21 #include "nsIPrincipal.h"
23 #include "nsWeakPtr.h"
24 #include "nsIDOMEventListener.h"
25 #include "nsIObserver.h"
26 #include "nsIStringBundle.h"
27 #include "mozilla/Mutex.h"
29 #include "DeviceStorage.h"
30 #include "mozilla/StaticPtr.h"
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
63 static DeviceStorageUsedSpaceCache
* CreateOrGet();
65 DeviceStorageUsedSpaceCache();
66 ~DeviceStorageUsedSpaceCache();
69 class InvalidateRunnable MOZ_FINAL
: public nsRunnable
72 InvalidateRunnable(DeviceStorageUsedSpaceCache
* aCache
,
73 const nsAString
& aStorageName
)
75 , mStorageName(aStorageName
) {}
77 ~InvalidateRunnable() {}
79 NS_IMETHOD
Run() MOZ_OVERRIDE
81 nsRefPtr
<DeviceStorageUsedSpaceCache::CacheEntry
> cacheEntry
;
82 cacheEntry
= mCache
->GetCacheEntry(mStorageName
);
84 cacheEntry
->mDirty
= true;
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
);
119 friend class InvalidateRunnable
;
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
)
129 nsString mStorageName
;
131 uint64_t mPicturesUsedSize
;
132 uint64_t mVideosUsedSize
;
133 uint64_t mMusicUsedSize
;
134 uint64_t mTotalUsedSize
;
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
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
);
170 nsString mPicturesExtensions
;
171 nsString mVideosExtensions
;
172 nsString mMusicExtensions
;
174 static mozilla::StaticAutoPtr
<DeviceStorageTypeChecker
> sDeviceStorageTypeChecker
;
177 class ContinueCursorEvent MOZ_FINAL
: public nsRunnable
180 explicit ContinueCursorEvent(already_AddRefed
<mozilla::dom::DOMRequest
> aRequest
);
181 explicit ContinueCursorEvent(mozilla::dom::DOMRequest
* aRequest
);
182 ~ContinueCursorEvent();
185 NS_IMETHOD
Run() MOZ_OVERRIDE
;
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
197 NS_DECL_ISUPPORTS_INHERITED
198 NS_DECL_NSICONTENTPERMISSIONREQUEST
199 NS_FORWARD_NSIDOMDOMCURSOR(mozilla::dom::DOMCursor::)
202 virtual void Continue(mozilla::ErrorResult
& aRv
) MOZ_OVERRIDE
;
204 nsDOMDeviceStorageCursor(nsPIDOMWindow
* aWindow
,
205 nsIPrincipal
* aPrincipal
,
206 DeviceStorageFile
* aFile
,
210 nsTArray
<nsRefPtr
<DeviceStorageFile
> > mFiles
;
211 bool mOkToCallContinue
;
214 void GetStorageType(nsAString
& aType
);
216 void RequestComplete() MOZ_OVERRIDE
;
219 ~nsDOMDeviceStorageCursor();
221 nsRefPtr
<DeviceStorageFile
> mFile
;
222 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
227 StringToJsval(nsPIDOMWindow
* aWindow
, nsAString
& aString
,
228 JS::MutableHandle
<JS::Value
> result
);
231 nsIFileToJsval(nsPIDOMWindow
* aWindow
, DeviceStorageFile
* aFile
);
234 InterfaceToJsval(nsPIDOMWindow
* aWindow
, nsISupports
* aObject
, const nsIID
* aIID
);