dom/indexedDB/OpenDatabaseHelper.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef mozilla_dom_indexeddb_opendatabasehelper_h__
     6 #define mozilla_dom_indexeddb_opendatabasehelper_h__
     8 #include "AsyncConnectionHelper.h"
    10 #include "nsIRunnable.h"
    12 #include "mozilla/dom/quota/StoragePrivilege.h"
    14 #include "DatabaseInfo.h"
    15 #include "IDBDatabase.h"
    16 #include "IDBRequest.h"
    18 class mozIStorageConnection;
    20 namespace mozilla {
    21 namespace dom {
    22 class ContentParent;
    23 }
    24 }
    26 BEGIN_INDEXEDDB_NAMESPACE
    28 class CheckPermissionsHelper;
    30 class OpenDatabaseHelper : public HelperBase
    31 {
    32   friend class CheckPermissionsHelper;
    34   typedef mozilla::dom::quota::PersistenceType PersistenceType;
    35   typedef mozilla::dom::quota::StoragePrivilege StoragePrivilege;
    37 public:
    38   OpenDatabaseHelper(IDBOpenDBRequest* aRequest,
    39                      const nsAString& aName,
    40                      const nsACString& aGroup,
    41                      const nsACString& aASCIIOrigin,
    42                      uint64_t aRequestedVersion,
    43                      PersistenceType aPersistenceType,
    44                      bool aForDeletion,
    45                      mozilla::dom::ContentParent* aContentParent,
    46                      StoragePrivilege aPrivilege)
    47     : HelperBase(aRequest), mOpenDBRequest(aRequest), mName(aName),
    48       mGroup(aGroup), mASCIIOrigin(aASCIIOrigin),
    49       mRequestedVersion(aRequestedVersion), mPersistenceType(aPersistenceType),
    50       mForDeletion(aForDeletion), mPrivilege(aPrivilege),
    51       mContentParent(aContentParent), mCurrentVersion(0), mLastObjectStoreId(0),
    52       mLastIndexId(0), mState(eCreated), mResultCode(NS_OK),
    53       mLoadDBMetadata(false),
    54       mTrackingQuota(aPrivilege != mozilla::dom::quota::Chrome)
    55   {
    56     NS_ASSERTION(!aForDeletion || !aRequestedVersion,
    57                  "Can't be for deletion and request a version!");
    58   }
    60   NS_DECL_THREADSAFE_ISUPPORTS
    61   NS_DECL_NSIRUNNABLE
    63   nsresult Init();
    65   nsresult WaitForOpenAllowed();
    66   nsresult Dispatch(nsIEventTarget* aDatabaseThread);
    67   nsresult DispatchToIOThread();
    68   nsresult RunImmediately();
    70   void SetError(nsresult rv)
    71   {
    72     NS_ASSERTION(NS_FAILED(rv), "Why are you telling me?");
    73     mResultCode = rv;
    74   }
    76   virtual nsresult GetResultCode() MOZ_OVERRIDE
    77   {
    78     return mResultCode;
    79   }
    81   nsresult NotifySetVersionFinished();
    82   nsresult NotifyDeleteFinished();
    83   void BlockDatabase();
    85   const nsACString& Id() const
    86   {
    87     return mDatabaseId;
    88   }
    90   IDBDatabase* Database() const
    91   {
    92     NS_ASSERTION(mDatabase, "Calling at the wrong time!");
    93     return mDatabase;
    94   }
    96   const StoragePrivilege& Privilege() const
    97   {
    98     return mPrivilege;
    99   }
   101   static
   102   nsresult CreateDatabaseConnection(nsIFile* aDBFile,
   103                                     nsIFile* aFMDirectory,
   104                                     const nsAString& aName,
   105                                     PersistenceType aPersistenceType,
   106                                     const nsACString& aGroup,
   107                                     const nsACString& aOrigin,
   108                                     mozIStorageConnection** aConnection);
   110 protected:
   111   // Methods only called on the main thread
   112   nsresult EnsureSuccessResult();
   113   nsresult StartSetVersion();
   114   nsresult StartDelete();
   115   virtual nsresult GetSuccessResult(JSContext* aCx,
   116                                     JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
   117   void DispatchSuccessEvent();
   118   void DispatchErrorEvent();
   119   virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
   121   // Called by CheckPermissionsHelper on the main thread before dispatch.
   122   void SetUnlimitedQuotaAllowed()
   123   {
   124     mTrackingQuota = false;
   125   }
   127   // Methods only called on the DB thread
   128   nsresult DoDatabaseWork();
   130   // In-params.
   131   nsRefPtr<IDBOpenDBRequest> mOpenDBRequest;
   132   nsString mName;
   133   nsCString mGroup;
   134   nsCString mASCIIOrigin;
   135   uint64_t mRequestedVersion;
   136   PersistenceType mPersistenceType;
   137   bool mForDeletion;
   138   StoragePrivilege mPrivilege;
   139   nsCString mDatabaseId;
   140   mozilla::dom::ContentParent* mContentParent;
   142   // Out-params.
   143   nsTArray<nsRefPtr<ObjectStoreInfo> > mObjectStores;
   144   uint64_t mCurrentVersion;
   145   nsString mDatabaseFilePath;
   146   int64_t mLastObjectStoreId;
   147   int64_t mLastIndexId;
   148   nsRefPtr<IDBDatabase> mDatabase;
   150   // State variables
   151   enum OpenDatabaseState {
   152     eCreated = 0, // Not yet dispatched to the DB thread
   153     eOpenPending, // Waiting for open allowed/open allowed
   154     eDBWork, // Waiting to do/doing work on the DB thread
   155     eFiringEvents, // Waiting to fire/firing events on the main thread
   156     eSetVersionPending, // Waiting on a SetVersionHelper
   157     eSetVersionCompleted, // SetVersionHelper is done
   158     eDeletePending, // Waiting on a DeleteDatabaseHelper
   159     eDeleteCompleted, // DeleteDatabaseHelper is done
   160   };
   161   OpenDatabaseState mState;
   162   nsresult mResultCode;
   164   nsRefPtr<FileManager> mFileManager;
   166   nsRefPtr<DatabaseInfo> mDBInfo;
   167   bool mLoadDBMetadata;
   168   bool mTrackingQuota;
   169 };
   171 END_INDEXEDDB_NAMESPACE
   173 #endif // mozilla_dom_indexeddb_opendatabasehelper_h__

mercurial