dom/devicestorage/nsDeviceStorage.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/devicestorage/nsDeviceStorage.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,236 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef nsDeviceStorage_h
     1.9 +#define nsDeviceStorage_h
    1.10 +
    1.11 +class nsPIDOMWindow;
    1.12 +#include "mozilla/Attributes.h"
    1.13 +#include "PCOMContentPermissionRequestChild.h"
    1.14 +
    1.15 +#include "DOMRequest.h"
    1.16 +#include "DOMCursor.h"
    1.17 +#include "nsAutoPtr.h"
    1.18 +#include "nsCycleCollectionParticipant.h"
    1.19 +#include "nsDOMClassInfoID.h"
    1.20 +#include "nsIClassInfo.h"
    1.21 +#include "nsIContentPermissionPrompt.h"
    1.22 +#include "nsIDOMWindow.h"
    1.23 +#include "nsIURI.h"
    1.24 +#include "nsInterfaceHashtable.h"
    1.25 +#include "nsIPrincipal.h"
    1.26 +#include "nsString.h"
    1.27 +#include "nsWeakPtr.h"
    1.28 +#include "nsIDOMEventListener.h"
    1.29 +#include "nsIObserver.h"
    1.30 +#include "nsIStringBundle.h"
    1.31 +#include "mozilla/Mutex.h"
    1.32 +#include "prtime.h"
    1.33 +#include "DeviceStorage.h"
    1.34 +#include "mozilla/dom/devicestorage/DeviceStorageRequestChild.h"
    1.35 +#include "mozilla/StaticPtr.h"
    1.36 +
    1.37 +namespace mozilla {
    1.38 +class ErrorResult;
    1.39 +} // namespace mozilla
    1.40 +
    1.41 +#define POST_ERROR_EVENT_FILE_EXISTS                 "NoModificationAllowedError"
    1.42 +#define POST_ERROR_EVENT_FILE_DOES_NOT_EXIST         "NotFoundError"
    1.43 +#define POST_ERROR_EVENT_FILE_NOT_ENUMERABLE         "TypeMismatchError"
    1.44 +#define POST_ERROR_EVENT_PERMISSION_DENIED           "SecurityError"
    1.45 +#define POST_ERROR_EVENT_ILLEGAL_TYPE                "TypeMismatchError"
    1.46 +#define POST_ERROR_EVENT_UNKNOWN                     "Unknown"
    1.47 +
    1.48 +enum DeviceStorageRequestType {
    1.49 +    DEVICE_STORAGE_REQUEST_READ,
    1.50 +    DEVICE_STORAGE_REQUEST_WRITE,
    1.51 +    DEVICE_STORAGE_REQUEST_CREATE,
    1.52 +    DEVICE_STORAGE_REQUEST_DELETE,
    1.53 +    DEVICE_STORAGE_REQUEST_WATCH,
    1.54 +    DEVICE_STORAGE_REQUEST_FREE_SPACE,
    1.55 +    DEVICE_STORAGE_REQUEST_USED_SPACE,
    1.56 +    DEVICE_STORAGE_REQUEST_AVAILABLE,
    1.57 +    DEVICE_STORAGE_REQUEST_STATUS,
    1.58 +    DEVICE_STORAGE_REQUEST_FORMAT,
    1.59 +    DEVICE_STORAGE_REQUEST_MOUNT,
    1.60 +    DEVICE_STORAGE_REQUEST_UNMOUNT,
    1.61 +    DEVICE_STORAGE_REQUEST_CREATEFD
    1.62 +};
    1.63 +
    1.64 +class DeviceStorageUsedSpaceCache MOZ_FINAL
    1.65 +{
    1.66 +public:
    1.67 +  static DeviceStorageUsedSpaceCache* CreateOrGet();
    1.68 +
    1.69 +  DeviceStorageUsedSpaceCache();
    1.70 +  ~DeviceStorageUsedSpaceCache();
    1.71 +
    1.72 +
    1.73 +  class InvalidateRunnable MOZ_FINAL : public nsRunnable
    1.74 +  {
    1.75 +    public:
    1.76 +      InvalidateRunnable(DeviceStorageUsedSpaceCache* aCache, 
    1.77 +                         const nsAString& aStorageName)
    1.78 +        : mCache(aCache)
    1.79 +        , mStorageName(aStorageName) {}
    1.80 +
    1.81 +      ~InvalidateRunnable() {}
    1.82 +
    1.83 +      NS_IMETHOD Run() MOZ_OVERRIDE
    1.84 +      {
    1.85 +        nsRefPtr<DeviceStorageUsedSpaceCache::CacheEntry> cacheEntry;
    1.86 +        cacheEntry = mCache->GetCacheEntry(mStorageName);
    1.87 +        if (cacheEntry) {
    1.88 +          cacheEntry->mDirty = true;
    1.89 +        }
    1.90 +        return NS_OK;
    1.91 +      }
    1.92 +    private:
    1.93 +      DeviceStorageUsedSpaceCache* mCache;
    1.94 +      nsString mStorageName;
    1.95 +  };
    1.96 +
    1.97 +  void Invalidate(const nsAString& aStorageName)
    1.98 +  {
    1.99 +    MOZ_ASSERT(NS_IsMainThread());
   1.100 +    MOZ_ASSERT(mIOThread);
   1.101 +
   1.102 +    nsRefPtr<InvalidateRunnable> r = new InvalidateRunnable(this, aStorageName);
   1.103 +    mIOThread->Dispatch(r, NS_DISPATCH_NORMAL);
   1.104 +  }
   1.105 +
   1.106 +  void Dispatch(nsIRunnable* aRunnable)
   1.107 +  {
   1.108 +    MOZ_ASSERT(NS_IsMainThread());
   1.109 +    MOZ_ASSERT(mIOThread);
   1.110 +
   1.111 +    mIOThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
   1.112 +  }
   1.113 +
   1.114 +  nsresult AccumUsedSizes(const nsAString& aStorageName,
   1.115 +                          uint64_t* aPictureSize, uint64_t* aVideosSize,
   1.116 +                          uint64_t* aMusicSize, uint64_t* aTotalSize);
   1.117 +
   1.118 +  void SetUsedSizes(const nsAString& aStorageName,
   1.119 +                    uint64_t aPictureSize, uint64_t aVideosSize,
   1.120 +                    uint64_t aMusicSize, uint64_t aTotalSize);
   1.121 +
   1.122 +private:
   1.123 +  friend class InvalidateRunnable;
   1.124 +
   1.125 +  struct CacheEntry
   1.126 +  {
   1.127 +    // Technically, this doesn't need to be threadsafe, but the implementation
   1.128 +    // of the non-thread safe one causes ASSERTS due to the underlying thread
   1.129 +    // associated with a LazyIdleThread changing from time to time.
   1.130 +    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeviceStorageUsedSpaceCache::CacheEntry)
   1.131 +
   1.132 +    bool mDirty;
   1.133 +    nsString mStorageName;
   1.134 +    int64_t  mFreeBytes;
   1.135 +    uint64_t mPicturesUsedSize;
   1.136 +    uint64_t mVideosUsedSize;
   1.137 +    uint64_t mMusicUsedSize;
   1.138 +    uint64_t mTotalUsedSize;
   1.139 +  };
   1.140 +  already_AddRefed<CacheEntry> GetCacheEntry(const nsAString& aStorageName);
   1.141 +
   1.142 +  nsTArray<nsRefPtr<CacheEntry>> mCacheEntries;
   1.143 +
   1.144 +  nsCOMPtr<nsIThread> mIOThread;
   1.145 +
   1.146 +  static mozilla::StaticAutoPtr<DeviceStorageUsedSpaceCache> sDeviceStorageUsedSpaceCache;
   1.147 +};
   1.148 +
   1.149 +class DeviceStorageTypeChecker MOZ_FINAL
   1.150 +{
   1.151 +public:
   1.152 +  static DeviceStorageTypeChecker* CreateOrGet();
   1.153 +
   1.154 +  DeviceStorageTypeChecker();
   1.155 +  ~DeviceStorageTypeChecker();
   1.156 +
   1.157 +  void InitFromBundle(nsIStringBundle* aBundle);
   1.158 +
   1.159 +  bool Check(const nsAString& aType, nsIDOMBlob* aBlob);
   1.160 +  bool Check(const nsAString& aType, nsIFile* aFile);
   1.161 +  void GetTypeFromFile(nsIFile* aFile, nsAString& aType);
   1.162 +  void GetTypeFromFileName(const nsAString& aFileName, nsAString& aType);
   1.163 +
   1.164 +  static nsresult GetPermissionForType(const nsAString& aType, nsACString& aPermissionResult);
   1.165 +  static nsresult GetAccessForRequest(const DeviceStorageRequestType aRequestType, nsACString& aAccessResult);
   1.166 +  static bool IsVolumeBased(const nsAString& aType);
   1.167 +
   1.168 +private:
   1.169 +  nsString mPicturesExtensions;
   1.170 +  nsString mVideosExtensions;
   1.171 +  nsString mMusicExtensions;
   1.172 +
   1.173 +  static mozilla::StaticAutoPtr<DeviceStorageTypeChecker> sDeviceStorageTypeChecker;
   1.174 +};
   1.175 +
   1.176 +class ContinueCursorEvent MOZ_FINAL : public nsRunnable
   1.177 +{
   1.178 +public:
   1.179 +  ContinueCursorEvent(already_AddRefed<mozilla::dom::DOMRequest> aRequest);
   1.180 +  ContinueCursorEvent(mozilla::dom::DOMRequest* aRequest);
   1.181 +  ~ContinueCursorEvent();
   1.182 +  void Continue();
   1.183 +
   1.184 +  NS_IMETHOD Run() MOZ_OVERRIDE;
   1.185 +private:
   1.186 +  already_AddRefed<DeviceStorageFile> GetNextFile();
   1.187 +  nsRefPtr<mozilla::dom::DOMRequest> mRequest;
   1.188 +};
   1.189 +
   1.190 +class nsDOMDeviceStorageCursor MOZ_FINAL
   1.191 +  : public mozilla::dom::DOMCursor
   1.192 +  , public nsIContentPermissionRequest
   1.193 +  , public PCOMContentPermissionRequestChild
   1.194 +  , public mozilla::dom::devicestorage::DeviceStorageRequestChildCallback
   1.195 +{
   1.196 +public:
   1.197 +  NS_DECL_ISUPPORTS_INHERITED
   1.198 +  NS_DECL_NSICONTENTPERMISSIONREQUEST
   1.199 +  NS_FORWARD_NSIDOMDOMCURSOR(mozilla::dom::DOMCursor::)
   1.200 +
   1.201 +  // DOMCursor
   1.202 +  virtual void Continue(mozilla::ErrorResult& aRv) MOZ_OVERRIDE;
   1.203 +
   1.204 +  nsDOMDeviceStorageCursor(nsPIDOMWindow* aWindow,
   1.205 +                           nsIPrincipal* aPrincipal,
   1.206 +                           DeviceStorageFile* aFile,
   1.207 +                           PRTime aSince);
   1.208 +
   1.209 +
   1.210 +  nsTArray<nsRefPtr<DeviceStorageFile> > mFiles;
   1.211 +  bool mOkToCallContinue;
   1.212 +  PRTime mSince;
   1.213 +
   1.214 +  virtual bool Recv__delete__(const bool& allow,
   1.215 +                              const InfallibleTArray<PermissionChoice>& choices) MOZ_OVERRIDE;
   1.216 +  virtual void IPDLRelease() MOZ_OVERRIDE;
   1.217 +
   1.218 +  void GetStorageType(nsAString & aType);
   1.219 +
   1.220 +  void RequestComplete() MOZ_OVERRIDE;
   1.221 +
   1.222 +private:
   1.223 +  ~nsDOMDeviceStorageCursor();
   1.224 +
   1.225 +  nsRefPtr<DeviceStorageFile> mFile;
   1.226 +  nsCOMPtr<nsIPrincipal> mPrincipal;
   1.227 +};
   1.228 +
   1.229 +//helpers
   1.230 +JS::Value
   1.231 +StringToJsval(nsPIDOMWindow* aWindow, nsAString& aString);
   1.232 +
   1.233 +JS::Value
   1.234 +nsIFileToJsval(nsPIDOMWindow* aWindow, DeviceStorageFile* aFile);
   1.235 +
   1.236 +JS::Value
   1.237 +InterfaceToJsval(nsPIDOMWindow* aWindow, nsISupports* aObject, const nsIID* aIID);
   1.238 +
   1.239 +#endif

mercurial