File: sandbox_file_system_backend_delegate.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (303 lines) | stat: -rw-r--r-- 11,583 bytes parent folder | download | duplicates (5)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_
#define STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_

#include <stdint.h>

#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/component_export.h"
#include "base/files/file_error_or.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "storage/browser/file_system/file_system_backend.h"
#include "storage/browser/file_system/file_system_options.h"
#include "storage/browser/file_system/file_system_quota_util.h"
#include "storage/browser/file_system/task_runner_bound_observer_list.h"
#include "storage/browser/quota/quota_manager_proxy.h"

namespace base {
class SequencedTaskRunner;
}  // namespace base

namespace blink {
class StorageKey;
}  // namespace blink

namespace storage {
class SandboxFileSystemBackendDelegateTest;
class SandboxFileSystemTestHelper;
}  // namespace storage

namespace leveldb {
class Env;
}  // namespace leveldb

namespace url {
class Origin;
}  // namespace url

namespace storage {

class AsyncFileUtil;
class FileStreamWriter;
class FileSystemFileUtil;
class FileSystemOperationContext;
class FileStreamReader;
class FileSystemURL;
class FileSystemUsageCache;
class ObfuscatedFileUtil;
class ObfuscatedFileUtilMemoryDelegate;
class QuotaManagerProxy;
class QuotaReservationManager;
class SandboxQuotaObserver;
class SpecialStoragePolicy;

// Delegate implementation of the some methods in Sandbox/SyncFileSystemBackend.
// An instance of this class is created and owned by FileSystemContext.
class COMPONENT_EXPORT(STORAGE_BROWSER) SandboxFileSystemBackendDelegate
    : public FileSystemQuotaUtil {
 public:
  using OpenFileSystemCallback = FileSystemBackend::OpenFileSystemCallback;
  using ResolveURLCallback = FileSystemBackend::ResolveURLCallback;

  // StorageKey enumerator interface.
  // An instance of this interface is assumed to be called on the file thread.
  class StorageKeyEnumerator {
   public:
    StorageKeyEnumerator(const StorageKeyEnumerator&) = delete;
    StorageKeyEnumerator& operator=(const StorageKeyEnumerator&) = delete;
    virtual ~StorageKeyEnumerator() = default;

    // Returns the next StorageKey.  Returns std::nullopt if there are no more
    // StorageKey.
    virtual std::optional<blink::StorageKey> Next() = 0;

    // Returns the current StorageKey's information.
    virtual bool HasFileSystemType(FileSystemType type) const = 0;

   protected:
    StorageKeyEnumerator() = default;
  };

  // Returns the type directory name in sandbox directory for given |type|.
  static std::string GetTypeString(FileSystemType type);

  SandboxFileSystemBackendDelegate(
      scoped_refptr<QuotaManagerProxy> quota_manager_proxy,
      scoped_refptr<base::SequencedTaskRunner> file_task_runner,
      const base::FilePath& profile_path,
      scoped_refptr<SpecialStoragePolicy> special_storage_policy,
      const FileSystemOptions& file_system_options,
      leveldb::Env* env_override);

  SandboxFileSystemBackendDelegate(const SandboxFileSystemBackendDelegate&) =
      delete;
  SandboxFileSystemBackendDelegate& operator=(
      const SandboxFileSystemBackendDelegate&) = delete;
  ~SandboxFileSystemBackendDelegate() override;

  // Returns a StorageKey enumerator of sandbox filesystem.
  // This method can only be called on the file thread.
  StorageKeyEnumerator* CreateStorageKeyEnumerator();

  // Gets a base directory path of the sandboxed filesystem that is
  // specified by `storage_key` and `type`.
  // (The path is similar to the origin's root path but doesn't contain
  // the 'unique' part.)
  // Returns an empty path if the given type is invalid.
  // This method can only be called on the file thread.
  base::FilePath GetBaseDirectoryForStorageKeyAndType(
      const blink::StorageKey& storage_key,
      FileSystemType type,
      bool create);

  // Gets a base directory path of the sandboxed filesystem that is specified by
  // `bucket_locator` and `type`. Returns an empty path if invalid or directory
  // does not exist when `create` is false.
  base::FilePath GetBaseDirectoryForBucketAndType(
      const BucketLocator& bucket_locator,
      FileSystemType type,
      bool create);

  // FileSystemBackend helpers.
  void OpenFileSystem(const BucketLocator& bucket_locator,
                      FileSystemType type,
                      OpenFileSystemMode mode,
                      ResolveURLCallback callback,
                      const GURL& root_url);
  std::unique_ptr<FileSystemOperationContext> CreateFileSystemOperationContext(
      const FileSystemURL& url,
      FileSystemContext* context,
      base::File::Error* error_code) const;
  std::unique_ptr<FileStreamReader> CreateFileStreamReader(
      const FileSystemURL& url,
      int64_t offset,
      const base::Time& expected_modification_time,
      FileSystemContext* context) const;
  std::unique_ptr<FileStreamWriter> CreateFileStreamWriter(
      const FileSystemURL& url,
      int64_t offset,
      FileSystemContext* context,
      FileSystemType type) const;

  // FileSystemQuotaUtil overrides.
  void DeleteCachedDefaultBucket(const blink::StorageKey& storage_key) override;
  base::File::Error DeleteBucketDataOnFileTaskRunner(
      FileSystemContext* context,
      QuotaManagerProxy* proxy,
      const BucketLocator& bucket_locator,
      FileSystemType type) override;
  void PerformStorageCleanupOnFileTaskRunner(FileSystemContext* context,
                                             QuotaManagerProxy* proxy,
                                             FileSystemType type) override;
  std::vector<blink::StorageKey> GetDefaultStorageKeysOnFileTaskRunner(
      FileSystemType type) override;
  int64_t GetBucketUsageOnFileTaskRunner(FileSystemContext* context,
                                         const BucketLocator& bucket_locator,
                                         FileSystemType type) override;
  scoped_refptr<QuotaReservation> CreateQuotaReservationOnFileTaskRunner(
      const blink::StorageKey& storage_key,
      FileSystemType type) override;

  // Adds an observer for the secified |type| of a file system, bound to
  // |task_runner|.
  virtual void AddFileUpdateObserver(FileSystemType type,
                                     FileUpdateObserver* observer,
                                     base::SequencedTaskRunner* task_runner);
  virtual void AddFileChangeObserver(FileSystemType type,
                                     FileChangeObserver* observer,
                                     base::SequencedTaskRunner* task_runner);
  virtual void AddFileAccessObserver(FileSystemType type,
                                     FileAccessObserver* observer,
                                     base::SequencedTaskRunner* task_runner);

  // Returns observer lists for the specified |type| of a file system.
  virtual const UpdateObserverList* GetUpdateObservers(
      FileSystemType type) const;
  virtual const ChangeObserverList* GetChangeObservers(
      FileSystemType type) const;
  virtual const AccessObserverList* GetAccessObservers(
      FileSystemType type) const;

  // Registers quota observer for file updates on filesystem of |type|.
  void RegisterQuotaUpdateObserver(FileSystemType type);

  void InvalidateUsageCache(const blink::StorageKey& storage_key,
                            FileSystemType type);
  void StickyInvalidateUsageCache(const blink::StorageKey& storage_key,
                                  FileSystemType type);

  void CollectOpenFileSystemMetrics(base::File::Error error_code);

  base::SequencedTaskRunner* file_task_runner() {
    return file_task_runner_.get();
  }

  AsyncFileUtil* file_util() { return sandbox_file_util_.get(); }
  FileSystemUsageCache* usage_cache() { return file_system_usage_cache_.get(); }
  SandboxQuotaObserver* quota_observer() { return quota_observer_.get(); }

  SpecialStoragePolicy* special_storage_policy() {
    return special_storage_policy_.get();
  }

  const FileSystemOptions& file_system_options() const {
    return file_system_options_;
  }

  const scoped_refptr<QuotaManagerProxy> quota_manager_proxy() const {
    return quota_manager_proxy_;
  }

  FileSystemFileUtil* sync_file_util();

  base::WeakPtr<ObfuscatedFileUtilMemoryDelegate> memory_file_util_delegate();

 private:
  friend class QuotaBackendImpl;
  friend class SandboxQuotaObserver;
  friend class SandboxFileSystemBackendDelegateTest;
  friend class SandboxFileSystemTestHelper;

  // Performs API-specific validity checks on the given path |url|.
  // Returns true if access to |url| is valid in this filesystem.
  bool IsAccessValid(const FileSystemURL& url) const;

  // Returns true if the given |url|'s scheme is allowed to access
  // filesystem.
  bool IsAllowedScheme(const GURL& url) const;

  // Returns a path to the usage cache file.
  base::FileErrorOr<base::FilePath> GetUsageCachePathForStorageKeyAndType(
      const blink::StorageKey& storage_key,
      FileSystemType type);

  // Returns a path to the usage cache file (static version).
  static base::FileErrorOr<base::FilePath>
  GetUsageCachePathForStorageKeyAndType(ObfuscatedFileUtil* sandbox_file_util,
                                        const blink::StorageKey& storage_key,
                                        FileSystemType type);

  // Returns a path to the usage cache file for a given bucket and type.
  base::FileErrorOr<base::FilePath> GetUsageCachePathForBucketAndType(
      const BucketLocator& bucket_locator,
      FileSystemType type);

  // Returns a path to the usage cache file for a given bucket and type(static
  // version).
  static base::FileErrorOr<base::FilePath> GetUsageCachePathForBucketAndType(
      ObfuscatedFileUtil* sandbox_file_util,
      const BucketLocator& bucket_locator,
      FileSystemType type);

  int64_t RecalculateBucketUsage(FileSystemContext* context,
                                 const BucketLocator& bucket_locator,
                                 FileSystemType type);

  ObfuscatedFileUtil* obfuscated_file_util();

  const scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
  const scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;

  std::unique_ptr<AsyncFileUtil> sandbox_file_util_;
  std::unique_ptr<FileSystemUsageCache> file_system_usage_cache_;
  std::unique_ptr<SandboxQuotaObserver> quota_observer_;
  std::unique_ptr<QuotaReservationManager> quota_reservation_manager_;

  const scoped_refptr<SpecialStoragePolicy> special_storage_policy_;

  FileSystemOptions file_system_options_;

  bool is_filesystem_opened_;
  THREAD_CHECKER(io_thread_checker_);

  // Accessed only on the file thread.
  std::set<url::Origin> visited_origins_;

  std::set<std::pair<url::Origin, FileSystemType>> sticky_dirty_origins_;

  std::map<FileSystemType, UpdateObserverList> update_observers_;
  std::map<FileSystemType, ChangeObserverList> change_observers_;
  std::map<FileSystemType, AccessObserverList> access_observers_;

  base::Time next_release_time_for_open_filesystem_stat_;

  base::WeakPtrFactory<SandboxFileSystemBackendDelegate> weak_factory_{this};
};

}  // namespace storage

#endif  // STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_SYSTEM_BACKEND_DELEGATE_H_