michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_indexeddb_opendatabasehelper_h__ michael@0: #define mozilla_dom_indexeddb_opendatabasehelper_h__ michael@0: michael@0: #include "AsyncConnectionHelper.h" michael@0: michael@0: #include "nsIRunnable.h" michael@0: michael@0: #include "mozilla/dom/quota/StoragePrivilege.h" michael@0: michael@0: #include "DatabaseInfo.h" michael@0: #include "IDBDatabase.h" michael@0: #include "IDBRequest.h" michael@0: michael@0: class mozIStorageConnection; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: class ContentParent; michael@0: } michael@0: } michael@0: michael@0: BEGIN_INDEXEDDB_NAMESPACE michael@0: michael@0: class CheckPermissionsHelper; michael@0: michael@0: class OpenDatabaseHelper : public HelperBase michael@0: { michael@0: friend class CheckPermissionsHelper; michael@0: michael@0: typedef mozilla::dom::quota::PersistenceType PersistenceType; michael@0: typedef mozilla::dom::quota::StoragePrivilege StoragePrivilege; michael@0: michael@0: public: michael@0: OpenDatabaseHelper(IDBOpenDBRequest* aRequest, michael@0: const nsAString& aName, michael@0: const nsACString& aGroup, michael@0: const nsACString& aASCIIOrigin, michael@0: uint64_t aRequestedVersion, michael@0: PersistenceType aPersistenceType, michael@0: bool aForDeletion, michael@0: mozilla::dom::ContentParent* aContentParent, michael@0: StoragePrivilege aPrivilege) michael@0: : HelperBase(aRequest), mOpenDBRequest(aRequest), mName(aName), michael@0: mGroup(aGroup), mASCIIOrigin(aASCIIOrigin), michael@0: mRequestedVersion(aRequestedVersion), mPersistenceType(aPersistenceType), michael@0: mForDeletion(aForDeletion), mPrivilege(aPrivilege), michael@0: mContentParent(aContentParent), mCurrentVersion(0), mLastObjectStoreId(0), michael@0: mLastIndexId(0), mState(eCreated), mResultCode(NS_OK), michael@0: mLoadDBMetadata(false), michael@0: mTrackingQuota(aPrivilege != mozilla::dom::quota::Chrome) michael@0: { michael@0: NS_ASSERTION(!aForDeletion || !aRequestedVersion, michael@0: "Can't be for deletion and request a version!"); michael@0: } michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIRUNNABLE michael@0: michael@0: nsresult Init(); michael@0: michael@0: nsresult WaitForOpenAllowed(); michael@0: nsresult Dispatch(nsIEventTarget* aDatabaseThread); michael@0: nsresult DispatchToIOThread(); michael@0: nsresult RunImmediately(); michael@0: michael@0: void SetError(nsresult rv) michael@0: { michael@0: NS_ASSERTION(NS_FAILED(rv), "Why are you telling me?"); michael@0: mResultCode = rv; michael@0: } michael@0: michael@0: virtual nsresult GetResultCode() MOZ_OVERRIDE michael@0: { michael@0: return mResultCode; michael@0: } michael@0: michael@0: nsresult NotifySetVersionFinished(); michael@0: nsresult NotifyDeleteFinished(); michael@0: void BlockDatabase(); michael@0: michael@0: const nsACString& Id() const michael@0: { michael@0: return mDatabaseId; michael@0: } michael@0: michael@0: IDBDatabase* Database() const michael@0: { michael@0: NS_ASSERTION(mDatabase, "Calling at the wrong time!"); michael@0: return mDatabase; michael@0: } michael@0: michael@0: const StoragePrivilege& Privilege() const michael@0: { michael@0: return mPrivilege; michael@0: } michael@0: michael@0: static michael@0: nsresult CreateDatabaseConnection(nsIFile* aDBFile, michael@0: nsIFile* aFMDirectory, michael@0: const nsAString& aName, michael@0: PersistenceType aPersistenceType, michael@0: const nsACString& aGroup, michael@0: const nsACString& aOrigin, michael@0: mozIStorageConnection** aConnection); michael@0: michael@0: protected: michael@0: // Methods only called on the main thread michael@0: nsresult EnsureSuccessResult(); michael@0: nsresult StartSetVersion(); michael@0: nsresult StartDelete(); michael@0: virtual nsresult GetSuccessResult(JSContext* aCx, michael@0: JS::MutableHandle aVal) MOZ_OVERRIDE; michael@0: void DispatchSuccessEvent(); michael@0: void DispatchErrorEvent(); michael@0: virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE; michael@0: michael@0: // Called by CheckPermissionsHelper on the main thread before dispatch. michael@0: void SetUnlimitedQuotaAllowed() michael@0: { michael@0: mTrackingQuota = false; michael@0: } michael@0: michael@0: // Methods only called on the DB thread michael@0: nsresult DoDatabaseWork(); michael@0: michael@0: // In-params. michael@0: nsRefPtr mOpenDBRequest; michael@0: nsString mName; michael@0: nsCString mGroup; michael@0: nsCString mASCIIOrigin; michael@0: uint64_t mRequestedVersion; michael@0: PersistenceType mPersistenceType; michael@0: bool mForDeletion; michael@0: StoragePrivilege mPrivilege; michael@0: nsCString mDatabaseId; michael@0: mozilla::dom::ContentParent* mContentParent; michael@0: michael@0: // Out-params. michael@0: nsTArray > mObjectStores; michael@0: uint64_t mCurrentVersion; michael@0: nsString mDatabaseFilePath; michael@0: int64_t mLastObjectStoreId; michael@0: int64_t mLastIndexId; michael@0: nsRefPtr mDatabase; michael@0: michael@0: // State variables michael@0: enum OpenDatabaseState { michael@0: eCreated = 0, // Not yet dispatched to the DB thread michael@0: eOpenPending, // Waiting for open allowed/open allowed michael@0: eDBWork, // Waiting to do/doing work on the DB thread michael@0: eFiringEvents, // Waiting to fire/firing events on the main thread michael@0: eSetVersionPending, // Waiting on a SetVersionHelper michael@0: eSetVersionCompleted, // SetVersionHelper is done michael@0: eDeletePending, // Waiting on a DeleteDatabaseHelper michael@0: eDeleteCompleted, // DeleteDatabaseHelper is done michael@0: }; michael@0: OpenDatabaseState mState; michael@0: nsresult mResultCode; michael@0: michael@0: nsRefPtr mFileManager; michael@0: michael@0: nsRefPtr mDBInfo; michael@0: bool mLoadDBMetadata; michael@0: bool mTrackingQuota; michael@0: }; michael@0: michael@0: END_INDEXEDDB_NAMESPACE michael@0: michael@0: #endif // mozilla_dom_indexeddb_opendatabasehelper_h__