1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/OpenDatabaseHelper.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 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 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef mozilla_dom_indexeddb_opendatabasehelper_h__ 1.9 +#define mozilla_dom_indexeddb_opendatabasehelper_h__ 1.10 + 1.11 +#include "AsyncConnectionHelper.h" 1.12 + 1.13 +#include "nsIRunnable.h" 1.14 + 1.15 +#include "mozilla/dom/quota/StoragePrivilege.h" 1.16 + 1.17 +#include "DatabaseInfo.h" 1.18 +#include "IDBDatabase.h" 1.19 +#include "IDBRequest.h" 1.20 + 1.21 +class mozIStorageConnection; 1.22 + 1.23 +namespace mozilla { 1.24 +namespace dom { 1.25 +class ContentParent; 1.26 +} 1.27 +} 1.28 + 1.29 +BEGIN_INDEXEDDB_NAMESPACE 1.30 + 1.31 +class CheckPermissionsHelper; 1.32 + 1.33 +class OpenDatabaseHelper : public HelperBase 1.34 +{ 1.35 + friend class CheckPermissionsHelper; 1.36 + 1.37 + typedef mozilla::dom::quota::PersistenceType PersistenceType; 1.38 + typedef mozilla::dom::quota::StoragePrivilege StoragePrivilege; 1.39 + 1.40 +public: 1.41 + OpenDatabaseHelper(IDBOpenDBRequest* aRequest, 1.42 + const nsAString& aName, 1.43 + const nsACString& aGroup, 1.44 + const nsACString& aASCIIOrigin, 1.45 + uint64_t aRequestedVersion, 1.46 + PersistenceType aPersistenceType, 1.47 + bool aForDeletion, 1.48 + mozilla::dom::ContentParent* aContentParent, 1.49 + StoragePrivilege aPrivilege) 1.50 + : HelperBase(aRequest), mOpenDBRequest(aRequest), mName(aName), 1.51 + mGroup(aGroup), mASCIIOrigin(aASCIIOrigin), 1.52 + mRequestedVersion(aRequestedVersion), mPersistenceType(aPersistenceType), 1.53 + mForDeletion(aForDeletion), mPrivilege(aPrivilege), 1.54 + mContentParent(aContentParent), mCurrentVersion(0), mLastObjectStoreId(0), 1.55 + mLastIndexId(0), mState(eCreated), mResultCode(NS_OK), 1.56 + mLoadDBMetadata(false), 1.57 + mTrackingQuota(aPrivilege != mozilla::dom::quota::Chrome) 1.58 + { 1.59 + NS_ASSERTION(!aForDeletion || !aRequestedVersion, 1.60 + "Can't be for deletion and request a version!"); 1.61 + } 1.62 + 1.63 + NS_DECL_THREADSAFE_ISUPPORTS 1.64 + NS_DECL_NSIRUNNABLE 1.65 + 1.66 + nsresult Init(); 1.67 + 1.68 + nsresult WaitForOpenAllowed(); 1.69 + nsresult Dispatch(nsIEventTarget* aDatabaseThread); 1.70 + nsresult DispatchToIOThread(); 1.71 + nsresult RunImmediately(); 1.72 + 1.73 + void SetError(nsresult rv) 1.74 + { 1.75 + NS_ASSERTION(NS_FAILED(rv), "Why are you telling me?"); 1.76 + mResultCode = rv; 1.77 + } 1.78 + 1.79 + virtual nsresult GetResultCode() MOZ_OVERRIDE 1.80 + { 1.81 + return mResultCode; 1.82 + } 1.83 + 1.84 + nsresult NotifySetVersionFinished(); 1.85 + nsresult NotifyDeleteFinished(); 1.86 + void BlockDatabase(); 1.87 + 1.88 + const nsACString& Id() const 1.89 + { 1.90 + return mDatabaseId; 1.91 + } 1.92 + 1.93 + IDBDatabase* Database() const 1.94 + { 1.95 + NS_ASSERTION(mDatabase, "Calling at the wrong time!"); 1.96 + return mDatabase; 1.97 + } 1.98 + 1.99 + const StoragePrivilege& Privilege() const 1.100 + { 1.101 + return mPrivilege; 1.102 + } 1.103 + 1.104 + static 1.105 + nsresult CreateDatabaseConnection(nsIFile* aDBFile, 1.106 + nsIFile* aFMDirectory, 1.107 + const nsAString& aName, 1.108 + PersistenceType aPersistenceType, 1.109 + const nsACString& aGroup, 1.110 + const nsACString& aOrigin, 1.111 + mozIStorageConnection** aConnection); 1.112 + 1.113 +protected: 1.114 + // Methods only called on the main thread 1.115 + nsresult EnsureSuccessResult(); 1.116 + nsresult StartSetVersion(); 1.117 + nsresult StartDelete(); 1.118 + virtual nsresult GetSuccessResult(JSContext* aCx, 1.119 + JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE; 1.120 + void DispatchSuccessEvent(); 1.121 + void DispatchErrorEvent(); 1.122 + virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE; 1.123 + 1.124 + // Called by CheckPermissionsHelper on the main thread before dispatch. 1.125 + void SetUnlimitedQuotaAllowed() 1.126 + { 1.127 + mTrackingQuota = false; 1.128 + } 1.129 + 1.130 + // Methods only called on the DB thread 1.131 + nsresult DoDatabaseWork(); 1.132 + 1.133 + // In-params. 1.134 + nsRefPtr<IDBOpenDBRequest> mOpenDBRequest; 1.135 + nsString mName; 1.136 + nsCString mGroup; 1.137 + nsCString mASCIIOrigin; 1.138 + uint64_t mRequestedVersion; 1.139 + PersistenceType mPersistenceType; 1.140 + bool mForDeletion; 1.141 + StoragePrivilege mPrivilege; 1.142 + nsCString mDatabaseId; 1.143 + mozilla::dom::ContentParent* mContentParent; 1.144 + 1.145 + // Out-params. 1.146 + nsTArray<nsRefPtr<ObjectStoreInfo> > mObjectStores; 1.147 + uint64_t mCurrentVersion; 1.148 + nsString mDatabaseFilePath; 1.149 + int64_t mLastObjectStoreId; 1.150 + int64_t mLastIndexId; 1.151 + nsRefPtr<IDBDatabase> mDatabase; 1.152 + 1.153 + // State variables 1.154 + enum OpenDatabaseState { 1.155 + eCreated = 0, // Not yet dispatched to the DB thread 1.156 + eOpenPending, // Waiting for open allowed/open allowed 1.157 + eDBWork, // Waiting to do/doing work on the DB thread 1.158 + eFiringEvents, // Waiting to fire/firing events on the main thread 1.159 + eSetVersionPending, // Waiting on a SetVersionHelper 1.160 + eSetVersionCompleted, // SetVersionHelper is done 1.161 + eDeletePending, // Waiting on a DeleteDatabaseHelper 1.162 + eDeleteCompleted, // DeleteDatabaseHelper is done 1.163 + }; 1.164 + OpenDatabaseState mState; 1.165 + nsresult mResultCode; 1.166 + 1.167 + nsRefPtr<FileManager> mFileManager; 1.168 + 1.169 + nsRefPtr<DatabaseInfo> mDBInfo; 1.170 + bool mLoadDBMetadata; 1.171 + bool mTrackingQuota; 1.172 +}; 1.173 + 1.174 +END_INDEXEDDB_NAMESPACE 1.175 + 1.176 +#endif // mozilla_dom_indexeddb_opendatabasehelper_h__