summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/DatabaseFileManager.h
blob: 62d11e95a5d6c5492cceb4dece0ad35921dd4492 (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
/* -*- 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_INDEXEDDB_DATABASEFILEMANAGER_H_
#define DOM_INDEXEDDB_DATABASEFILEMANAGER_H_

#include "FileInfoManager.h"
#include "IndexedDBCipherKeyManager.h"
#include "mozilla/dom/FlippedOnce.h"
#include "mozilla/dom/quota/CommonMetadata.h"
#include "mozilla/dom/quota/PersistenceType.h"
#include "mozilla/dom/quota/UsageInfo.h"
#include "mozilla/InitializedOnce.h"

class nsIFile;
class mozIStorageConnection;

namespace mozilla::dom::indexedDB {

// Implemented in ActorsParent.cpp.
class DatabaseFileManager final
    : public FileInfoManager<DatabaseFileManager>,
      public AtomicSafeRefCounted<DatabaseFileManager> {
  using PersistenceType = mozilla::dom::quota::PersistenceType;
  using FileInfoManager<DatabaseFileManager>::MutexType;

  const PersistenceType mPersistenceType;
  const quota::OriginMetadata mOriginMetadata;
  const nsString mDatabaseName;
  const nsCString mDatabaseID;

  RefPtr<IndexedDBCipherKeyManager> mCipherKeyManager;

  LazyInitializedOnce<const nsString> mDirectoryPath;
  LazyInitializedOnce<const nsString> mJournalDirectoryPath;

  const bool mEnforcingQuota;
  const bool mIsInPrivateBrowsingMode;

  FlippedOnce<false> mInitialized;

  // Lock protecting DatabaseFileManager.mFileInfos.
  // It's s also used to atomically update DatabaseFileInfo.mRefCnt and
  // DatabaseFileInfo.mDBRefCnt
  static MutexType sMutex;

 public:
  [[nodiscard]] static nsCOMPtr<nsIFile> GetFileForId(nsIFile* aDirectory,
                                                      int64_t aId);

  [[nodiscard]] static nsCOMPtr<nsIFile> GetCheckedFileForId(
      nsIFile* aDirectory, int64_t aId);

  static nsresult InitDirectory(nsIFile& aDirectory, nsIFile& aDatabaseFile,
                                const nsACString& aOrigin,
                                uint32_t aTelemetryId);

  template <typename KnownDirEntryOp, typename UnknownDirEntryOp>
  static Result<Ok, nsresult> TraverseFiles(
      nsIFile& aDirectory, KnownDirEntryOp&& aKnownDirEntryOp,
      UnknownDirEntryOp&& aUnknownDirEntryOp);

  static Result<quota::FileUsageType, nsresult> GetUsage(nsIFile* aDirectory);

  DatabaseFileManager(PersistenceType aPersistenceType,
                      const quota::OriginMetadata& aOriginMetadata,
                      const nsAString& aDatabaseName,
                      const nsCString& aDatabaseID, bool aEnforcingQuota,
                      bool aIsInPrivateBrowsingMode);

  PersistenceType Type() const { return mPersistenceType; }

  const quota::OriginMetadata& OriginMetadata() const {
    return mOriginMetadata;
  }

  const nsACString& Origin() const { return mOriginMetadata.mOrigin; }

  const nsAString& DatabaseName() const { return mDatabaseName; }

  const nsCString& DatabaseID() const { return mDatabaseID; }

  IndexedDBCipherKeyManager& MutableCipherKeyManagerRef() const {
    MOZ_ASSERT(mIsInPrivateBrowsingMode);
    MOZ_ASSERT(mCipherKeyManager);

    return *mCipherKeyManager;
  }

  auto IsInPrivateBrowsingMode() const { return mIsInPrivateBrowsingMode; }

  bool EnforcingQuota() const { return mEnforcingQuota; }

  bool Initialized() const { return mInitialized; }

  nsresult Init(nsIFile* aDirectory, mozIStorageConnection& aConnection);

  [[nodiscard]] nsCOMPtr<nsIFile> GetDirectory();

  [[nodiscard]] nsCOMPtr<nsIFile> GetCheckedDirectory();

  [[nodiscard]] nsCOMPtr<nsIFile> GetJournalDirectory();

  [[nodiscard]] nsCOMPtr<nsIFile> EnsureJournalDirectory();

  [[nodiscard]] nsresult SyncDeleteFile(int64_t aId);

  // XXX When getting rid of FileHelper, this method should be removed/made
  // private.
  [[nodiscard]] nsresult SyncDeleteFile(nsIFile& aFile,
                                        nsIFile& aJournalFile) const;

  [[nodiscard]] nsresult AsyncDeleteFile(int64_t aFileId);

  nsresult Invalidate() override;

  MOZ_DECLARE_REFCOUNTED_TYPENAME(DatabaseFileManager)

  static StaticMutex& Mutex() { return sMutex; }

  ~DatabaseFileManager() = default;
};

}  // namespace mozilla::dom::indexedDB

#endif  // DOM_INDEXEDDB_DATABASEFILEMANAGER_H_