summaryrefslogtreecommitdiffstats
path: root/dom/fs/parent/datamodel/FileSystemDataManager.h
blob: e23e4dd95b0ca0b0d8cfd1440b6b9b00f5f42519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- 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 "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 <typename V, typename E>
class Result;

namespace dom {

class FileSystemAccessHandle;
class FileSystemManagerParent;

namespace fs {
class FileSystemChildMetadata;
}  // namespace fs

namespace quota {
class DirectoryLock;
class QuotaManager;
}  // namespace quota

namespace fs::data {

class FileSystemDatabaseManager;

Result<EntryId, QMResult> GetRootHandle(const Origin& origin);

Result<EntryId, QMResult> GetEntryHandle(
    const FileSystemChildMetadata& aHandle);

class FileSystemDataManager
    : public SupportsCheckedUnsafePtr<CheckIf<DiagnosticAssertEnabled>> {
 public:
  enum struct State : uint8_t { Initial = 0, Opening, Open, Closing, Closed };

  FileSystemDataManager(const quota::OriginMetadata& aOriginMetadata,
                        RefPtr<quota::QuotaManager> aQuotaManager,
                        MovingNotNull<nsCOMPtr<nsIEventTarget>> aIOTarget,
                        MovingNotNull<RefPtr<TaskQueue>> aIOTaskQueue);

  // IsExclusive is true because we want to allow the move operations. There's
  // always just one consumer anyway.
  using CreatePromise = MozPromise<Registered<FileSystemDataManager>, nsresult,
                                   /* IsExclusive */ true>;
  static RefPtr<CreatePromise> 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<quota::DirectoryLock&> MaybeDirectoryLockRef() const {
    return ToMaybeRef(mDirectoryLock.get());
  }

  FileSystemDatabaseManager* MutableDatabaseManagerPtr() const {
    MOZ_ASSERT(mDatabaseManager);

    return mDatabaseManager.get();
  }

  void Register();

  void Unregister();

  void RegisterActor(NotNull<FileSystemManagerParent*> aActor);

  void UnregisterActor(NotNull<FileSystemManagerParent*> aActor);

  void RegisterAccessHandle(NotNull<FileSystemAccessHandle*> aAccessHandle);

  void UnregisterAccessHandle(NotNull<FileSystemAccessHandle*> aAccessHandle);

  bool IsOpen() const { return mState == State::Open; }

  RefPtr<BoolPromise> OnOpen();

  RefPtr<BoolPromise> OnClose();

  bool IsLocked(const EntryId& aEntryId) const;

  nsresult LockExclusive(const EntryId& aEntryId);

  void UnlockExclusive(const EntryId& aEntryId);

  nsresult LockShared(const EntryId& aEntryId);

  void UnlockShared(const EntryId& aEntryId);

 protected:
  virtual ~FileSystemDataManager();

  bool IsInactive() const;

  bool IsOpening() const { return mState == State::Opening; }

  bool IsClosing() const { return mState == State::Closing; }

  void RequestAllowToClose();

  RefPtr<BoolPromise> BeginOpen();

  RefPtr<BoolPromise> BeginClose();

  // Things touched on background thread only.
  struct BackgroundThreadAccessible {
    nsTHashSet<FileSystemManagerParent*> mActors;
    nsTHashSet<FileSystemAccessHandle*> mAccessHandles;
  };
  ThreadBound<BackgroundThreadAccessible> mBackgroundThreadAccessible;

  const quota::OriginMetadata mOriginMetadata;
  nsTHashSet<EntryId> mExclusiveLocks;
  nsTHashMap<EntryId, uint32_t> mSharedLocks;
  NS_DECL_OWNINGEVENTTARGET
  const RefPtr<quota::QuotaManager> mQuotaManager;
  const NotNull<nsCOMPtr<nsISerialEventTarget>> mBackgroundTarget;
  const NotNull<nsCOMPtr<nsIEventTarget>> mIOTarget;
  const NotNull<RefPtr<TaskQueue>> mIOTaskQueue;
  RefPtr<quota::DirectoryLock> mDirectoryLock;
  UniquePtr<FileSystemDatabaseManager> mDatabaseManager;
  MozPromiseHolder<BoolPromise> mOpenPromiseHolder;
  MozPromiseHolder<BoolPromise> mClosePromiseHolder;
  uint32_t mRegCount;
  State mState;
};

}  // namespace fs::data
}  // namespace dom
}  // namespace mozilla

#endif  // DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATAMANAGER_H_