/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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 DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATAMANAGER_H_ #define DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATAMANAGER_H_ #include "FileSystemParentTypes.h" #include "ResultConnection.h" #include "mozilla/NotNull.h" #include "mozilla/TaskQueue.h" #include "mozilla/ThreadBound.h" #include "mozilla/dom/FileSystemHelpers.h" #include "mozilla/dom/FileSystemTypes.h" #include "mozilla/dom/quota/CheckedUnsafePtr.h" #include "mozilla/dom/quota/CommonMetadata.h" #include "mozilla/dom/quota/ForwardDecls.h" #include "nsCOMPtr.h" #include "nsISupportsUtils.h" #include "nsString.h" #include "nsTHashSet.h" namespace mozilla { template class Result; namespace dom { class FileSystemAccessHandle; class FileSystemManagerParent; namespace fs { struct FileId; class FileSystemChildMetadata; } // namespace fs namespace quota { class DirectoryLock; class QuotaManager; } // namespace quota namespace fs::data { class FileSystemDatabaseManager; Result GetRootHandle(const Origin& origin); Result GetEntryHandle( const FileSystemChildMetadata& aHandle); Result GetStorageConnection( const quota::OriginMetadata& aOriginMetadata, const int64_t aDirectoryLockId); class FileSystemDataManager : public SupportsCheckedUnsafePtr> { public: enum struct State : uint8_t { Initial = 0, Opening, Open, Closing, Closed }; FileSystemDataManager(const quota::OriginMetadata& aOriginMetadata, RefPtr aQuotaManager, MovingNotNull> aIOTarget, MovingNotNull> aIOTaskQueue); // IsExclusive is true because we want to allow the move operations. There's // always just one consumer anyway. using CreatePromise = MozPromise, nsresult, /* IsExclusive */ true>; static RefPtr GetOrCreateFileSystemDataManager( const quota::OriginMetadata& aOriginMetadata); static void AbortOperationsForLocks( const quota::Client::DirectoryLockIdTable& aDirectoryLockIds); static void InitiateShutdown(); static bool IsShutdownCompleted(); NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FileSystemDataManager) void AssertIsOnIOTarget() const; const quota::OriginMetadata& OriginMetadataRef() const { return mOriginMetadata; } nsISerialEventTarget* MutableBackgroundTargetPtr() const { return mBackgroundTarget.get(); } nsIEventTarget* MutableIOTargetPtr() const { return mIOTarget.get(); } nsISerialEventTarget* MutableIOTaskQueuePtr() const { return mIOTaskQueue.get(); } Maybe MaybeDirectoryLockRef() const { return ToMaybeRef(mDirectoryLock.get()); } FileSystemDatabaseManager* MutableDatabaseManagerPtr() const { MOZ_ASSERT(mDatabaseManager); return mDatabaseManager.get(); } void Register(); void Unregister(); void RegisterActor(NotNull aActor); void UnregisterActor(NotNull aActor); void RegisterAccessHandle(NotNull aAccessHandle); void UnregisterAccessHandle(NotNull aAccessHandle); bool IsOpen() const { return mState == State::Open; } RefPtr OnOpen(); RefPtr OnClose(); Result IsLocked(const FileId& aFileId) const; Result IsLocked(const EntryId& aEntryId) const; Result LockExclusive(const EntryId& aEntryId); void UnlockExclusive(const EntryId& aEntryId); Result LockShared(const EntryId& aEntryId); void UnlockShared(const EntryId& aEntryId, const FileId& aFileId, bool aAbort); FileMode GetMode(bool aKeepData) const; protected: virtual ~FileSystemDataManager(); bool IsInactive() const; bool IsOpening() const { return mState == State::Opening; } bool IsClosing() const { return mState == State::Closing; } void RequestAllowToClose(); RefPtr BeginOpen(); RefPtr BeginClose(); // Things touched on background thread only. struct BackgroundThreadAccessible { nsTHashSet mActors; nsTHashSet mAccessHandles; }; ThreadBound mBackgroundThreadAccessible; const quota::OriginMetadata mOriginMetadata; nsTHashSet mExclusiveLocks; nsTHashMap mSharedLocks; NS_DECL_OWNINGEVENTTARGET const RefPtr mQuotaManager; const NotNull> mBackgroundTarget; const NotNull> mIOTarget; const NotNull> mIOTaskQueue; RefPtr mDirectoryLock; UniquePtr mDatabaseManager; MozPromiseHolder mOpenPromiseHolder; MozPromiseHolder mClosePromiseHolder; uint32_t mRegCount; DatabaseVersion mVersion; State mState; }; } // namespace fs::data } // namespace dom } // namespace mozilla #endif // DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATAMANAGER_H_