summaryrefslogtreecommitdiffstats
path: root/dom/fs/parent/datamodel/FileSystemDatabaseManagerVersion001.h
blob: d15e4f5cc9437748699e540752a13382f9c52133 (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
/* -*- 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_FILESYSTEMDATABASEMANAGERVERSION001_H_
#define DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGERVERSION001_H_

#include "FileSystemDatabaseManager.h"
#include "mozilla/dom/quota/CommonMetadata.h"
#include "nsString.h"

namespace mozilla::dom::fs::data {

class FileSystemDataManager;
class FileSystemFileManager;
using FileSystemConnection = fs::ResultConnection;

/**
 * @brief Versioned implementation of database interface enables backwards
 * support after the schema has changed. Version number 0 refers to
 * uninitialized database, and versions after that are sequential upgrades.
 *
 * To change the schema to the next version x,
 * - a new implementation FileSystemDatabaseManagerVersion00x is derived from
 * the previous version and the required methods are overridden
 * - a new apppropriate schema initialization class SchemaVersion00x is created
 * or derived
 * - the factory method of FileSystemDatabaseManager is extended to try to
 * migrate the data from the previous version to version x, and to return
 * FileSystemDatabaseManagerVersion00x implementation if the database version
 * after the migrations is x
 * - note that if the migration fails at some old version, the corresponding
 * old implementation should be returned: this way the users whose migrations
 * fail systematically due to hardware or other issues will not get locked out
 */
class FileSystemDatabaseManagerVersion001 : public FileSystemDatabaseManager {
 public:
  FileSystemDatabaseManagerVersion001(
      FileSystemDataManager* aDataManager, FileSystemConnection&& aConnection,
      UniquePtr<FileSystemFileManager>&& aFileManager,
      const EntryId& aRootEntry);

  /* Static to allow use by quota client without instantiation */
  static nsresult RescanTrackedUsages(
      const FileSystemConnection& aConnection,
      const quota::OriginMetadata& aOriginMetadata);

  /* Static to allow use by quota client without instantiation */
  static Result<Usage, QMResult> GetFileUsage(
      const FileSystemConnection& aConnection);

  virtual nsresult UpdateUsage(const EntryId& aEntry) override;

  virtual Result<EntryId, QMResult> GetOrCreateDirectory(
      const FileSystemChildMetadata& aHandle, bool aCreate) override;

  virtual Result<EntryId, QMResult> GetOrCreateFile(
      const FileSystemChildMetadata& aHandle, const ContentType& aType,
      bool aCreate) override;

  virtual nsresult GetFile(const EntryId& aEntryId, ContentType& aType,
                           TimeStamp& lastModifiedMilliSeconds, Path& aPath,
                           nsCOMPtr<nsIFile>& aFile) const override;

  virtual Result<FileSystemDirectoryListing, QMResult> GetDirectoryEntries(
      const EntryId& aParent, PageNumber aPage) const override;

  virtual Result<bool, QMResult> RenameEntry(
      const FileSystemEntryMetadata& aHandle, const Name& aNewName) override;

  virtual Result<bool, QMResult> MoveEntry(
      const FileSystemEntryMetadata& aHandle,
      const FileSystemChildMetadata& aNewDesignation) override;

  virtual Result<bool, QMResult> RemoveDirectory(
      const FileSystemChildMetadata& aHandle, bool aRecursive) override;

  virtual Result<bool, QMResult> RemoveFile(
      const FileSystemChildMetadata& aHandle) override;

  virtual Result<Path, QMResult> Resolve(
      const FileSystemEntryPair& aEndpoints) const override;

  virtual void Close() override;

  virtual nsresult BeginUsageTracking(const EntryId& aEntryId) override;

  virtual nsresult EndUsageTracking(const EntryId& aEntryId) override;

  virtual ~FileSystemDatabaseManagerVersion001() = default;

 private:
  nsresult UpdateUsageInDatabase(const EntryId& aEntry, Usage aNewDiskUsage);

  Result<Ok, QMResult> EnsureUsageIsKnown(const EntryId& aEntryId);

  void DecreaseCachedQuotaUsage(int64_t aDelta);

  nsresult UpdateCachedQuotaUsage(const EntryId& aEntryId, Usage aOldUsage,
                                  Usage aNewUsage);

  nsresult ClearDestinationIfNotLocked(
      const FileSystemConnection& aConnection,
      const FileSystemDataManager* const aDataManager,
      const FileSystemEntryMetadata& aHandle,
      const FileSystemChildMetadata& aNewDesignation);

  nsresult PrepareMoveEntry(const FileSystemConnection& aConnection,
                            const FileSystemDataManager* const aDataManager,
                            const FileSystemEntryMetadata& aHandle,
                            const FileSystemChildMetadata& aNewDesignation,
                            bool aIsFile);

  nsresult PrepareRenameEntry(const FileSystemConnection& aConnection,
                              const FileSystemDataManager* const aDataManager,
                              const FileSystemEntryMetadata& aHandle,
                              const Name& aNewName, bool aIsFile);

  // This is a raw pointer since we're owned by the FileSystemDataManager.
  FileSystemDataManager* MOZ_NON_OWNING_REF mDataManager;

  FileSystemConnection mConnection;

  UniquePtr<FileSystemFileManager> mFileManager;

  const EntryId mRootEntry;

  const quota::ClientMetadata mClientMetadata;

  int32_t mFilesOfUnknownUsage;
};

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

#endif  // DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGERVERSION001_H_