123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- #ifndef mozilla_dom_idbdatabase_h__
- #define mozilla_dom_idbdatabase_h__
- #include "mozilla/Attributes.h"
- #include "mozilla/dom/IDBTransactionBinding.h"
- #include "mozilla/dom/StorageTypeBinding.h"
- #include "mozilla/dom/IDBWrapperCache.h"
- #include "mozilla/dom/quota/PersistenceType.h"
- #include "nsAutoPtr.h"
- #include "nsDataHashtable.h"
- #include "nsHashKeys.h"
- #include "nsString.h"
- #include "nsTHashtable.h"
- class nsIDocument;
- class nsPIDOMWindowInner;
- namespace mozilla {
- class ErrorResult;
- class EventChainPostVisitor;
- namespace dom {
- class Blob;
- class DOMStringList;
- class IDBFactory;
- class IDBMutableFile;
- class IDBObjectStore;
- struct IDBObjectStoreParameters;
- class IDBOpenDBRequest;
- class IDBRequest;
- class IDBTransaction;
- template <class> class Optional;
- class StringOrStringSequence;
- namespace indexedDB {
- class BackgroundDatabaseChild;
- class DatabaseSpec;
- class PBackgroundIDBDatabaseFileChild;
- }
- class IDBDatabase final
- : public IDBWrapperCache
- {
- typedef mozilla::dom::indexedDB::DatabaseSpec DatabaseSpec;
- typedef mozilla::dom::StorageType StorageType;
- typedef mozilla::dom::quota::PersistenceType PersistenceType;
- class Observer;
- friend class Observer;
- friend class IDBObjectStore;
- friend class IDBIndex;
- // The factory must be kept alive when IndexedDB is used in multiple
- // processes. If it dies then the entire actor tree will be destroyed with it
- // and the world will explode.
- RefPtr<IDBFactory> mFactory;
- nsAutoPtr<DatabaseSpec> mSpec;
- // Normally null except during a versionchange transaction.
- nsAutoPtr<DatabaseSpec> mPreviousSpec;
- indexedDB::BackgroundDatabaseChild* mBackgroundActor;
- nsTHashtable<nsPtrHashKey<IDBTransaction>> mTransactions;
- nsDataHashtable<nsISupportsHashKey, indexedDB::PBackgroundIDBDatabaseFileChild*>
- mFileActors;
- nsTHashtable<nsISupportsHashKey> mReceivedBlobs;
- RefPtr<Observer> mObserver;
- // Weak refs, IDBMutableFile strongly owns this IDBDatabase object.
- nsTArray<IDBMutableFile*> mLiveMutableFiles;
- const bool mFileHandleDisabled;
- bool mClosed;
- bool mInvalidated;
- bool mQuotaExceeded;
- public:
- static already_AddRefed<IDBDatabase>
- Create(IDBOpenDBRequest* aRequest,
- IDBFactory* aFactory,
- indexedDB::BackgroundDatabaseChild* aActor,
- DatabaseSpec* aSpec);
- #ifdef DEBUG
- void
- AssertIsOnOwningThread() const;
- PRThread*
- OwningThread() const;
- #else
- void
- AssertIsOnOwningThread() const
- { }
- #endif
- const nsString&
- Name() const;
- void
- GetName(nsAString& aName) const
- {
- AssertIsOnOwningThread();
- aName = Name();
- }
- uint64_t
- Version() const;
- already_AddRefed<nsIDocument>
- GetOwnerDocument() const;
- void
- Close()
- {
- AssertIsOnOwningThread();
- CloseInternal();
- }
- bool
- IsClosed() const
- {
- AssertIsOnOwningThread();
- return mClosed;
- }
- void
- Invalidate();
- // Whether or not the database has been invalidated. If it has then no further
- // transactions for this database will be allowed to run.
- bool
- IsInvalidated() const
- {
- AssertIsOnOwningThread();
- return mInvalidated;
- }
- void
- SetQuotaExceeded()
- {
- mQuotaExceeded = true;
- }
- void
- EnterSetVersionTransaction(uint64_t aNewVersion);
- void
- ExitSetVersionTransaction();
- // Called when a versionchange transaction is aborted to reset the
- // DatabaseInfo.
- void
- RevertToPreviousState();
- IDBFactory*
- Factory() const
- {
- AssertIsOnOwningThread();
- return mFactory;
- }
- void
- RegisterTransaction(IDBTransaction* aTransaction);
- void
- UnregisterTransaction(IDBTransaction* aTransaction);
- void
- AbortTransactions(bool aShouldWarn);
- indexedDB::PBackgroundIDBDatabaseFileChild*
- GetOrCreateFileActorForBlob(Blob* aBlob);
- void
- NoteFinishedFileActor(indexedDB::PBackgroundIDBDatabaseFileChild* aFileActor);
- void
- NoteReceivedBlob(Blob* aBlob);
- void
- DelayedMaybeExpireFileActors();
- // XXX This doesn't really belong here... It's only needed for IDBMutableFile
- // serialization and should be removed or fixed someday.
- nsresult
- GetQuotaInfo(nsACString& aOrigin, PersistenceType* aPersistenceType);
- bool
- IsFileHandleDisabled() const
- {
- return mFileHandleDisabled;
- }
- void
- NoteLiveMutableFile(IDBMutableFile* aMutableFile);
- void
- NoteFinishedMutableFile(IDBMutableFile* aMutableFile);
- nsPIDOMWindowInner*
- GetParentObject() const;
- already_AddRefed<DOMStringList>
- ObjectStoreNames() const;
- already_AddRefed<IDBObjectStore>
- CreateObjectStore(const nsAString& aName,
- const IDBObjectStoreParameters& aOptionalParameters,
- ErrorResult& aRv);
- void
- DeleteObjectStore(const nsAString& name, ErrorResult& aRv);
- // This will be called from the DOM.
- already_AddRefed<IDBTransaction>
- Transaction(JSContext* aCx,
- const StringOrStringSequence& aStoreNames,
- IDBTransactionMode aMode,
- ErrorResult& aRv);
- // This can be called from C++ to avoid JS exception.
- nsresult
- Transaction(JSContext* aCx,
- const StringOrStringSequence& aStoreNames,
- IDBTransactionMode aMode,
- IDBTransaction** aTransaction);
- StorageType
- Storage() const;
- IMPL_EVENT_HANDLER(abort)
- IMPL_EVENT_HANDLER(close)
- IMPL_EVENT_HANDLER(error)
- IMPL_EVENT_HANDLER(versionchange)
- already_AddRefed<IDBRequest>
- CreateMutableFile(JSContext* aCx,
- const nsAString& aName,
- const Optional<nsAString>& aType,
- ErrorResult& aRv);
- already_AddRefed<IDBRequest>
- MozCreateFileHandle(JSContext* aCx,
- const nsAString& aName,
- const Optional<nsAString>& aType,
- ErrorResult& aRv)
- {
- return CreateMutableFile(aCx, aName, aType, aRv);
- }
- void
- ClearBackgroundActor()
- {
- AssertIsOnOwningThread();
- mBackgroundActor = nullptr;
- }
- const DatabaseSpec*
- Spec() const
- {
- return mSpec;
- }
- NS_DECL_ISUPPORTS_INHERITED
- NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase, IDBWrapperCache)
- // nsIDOMEventTarget
- virtual void
- LastRelease() override;
- virtual nsresult
- PostHandleEvent(EventChainPostVisitor& aVisitor) override;
- // nsWrapperCache
- virtual JSObject*
- WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
- private:
- IDBDatabase(IDBOpenDBRequest* aRequest,
- IDBFactory* aFactory,
- indexedDB::BackgroundDatabaseChild* aActor,
- DatabaseSpec* aSpec);
- ~IDBDatabase();
- void
- CloseInternal();
- void
- InvalidateInternal();
- bool
- RunningVersionChangeTransaction() const
- {
- AssertIsOnOwningThread();
- return !!mPreviousSpec;
- }
- void
- RefreshSpec(bool aMayDelete);
- void
- ExpireFileActors(bool aExpireAll);
- void
- InvalidateMutableFiles();
- void
- LogWarning(const char* aMessageName,
- const nsAString& aFilename,
- uint32_t aLineNumber,
- uint32_t aColumnNumber);
- // Only accessed by IDBObjectStore.
- nsresult
- RenameObjectStore(int64_t aObjectStoreId, const nsAString& aName);
- // Only accessed by IDBIndex.
- nsresult
- RenameIndex(int64_t aObjectStoreId, int64_t aIndexId, const nsAString& aName);
- };
- } // namespace dom
- } // namespace mozilla
- #endif // mozilla_dom_idbdatabase_h__
|