File: canned_syncable_file_system.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 (239 lines) | stat: -rw-r--r-- 10,282 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
// 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 CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/files/file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_forward.h"
#include "base/observer_list_threadsafe.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_status.h"
#include "chrome/browser/sync_file_system/sync_status_code.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/quota/quota_callbacks.h"
#include "storage/browser/test/mock_quota_manager.h"
#include "storage/browser/test/mock_quota_manager_proxy.h"
#include "storage/common/file_system/file_system_types.h"
#include "storage/common/file_system/file_system_util.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace storage {
class FileSystemContext;
class FileSystemOperationRunner;
class FileSystemURL;
}

namespace sync_file_system {

class FileChangeList;
class LocalFileSyncContext;
class SyncFileSystemBackend;

// A canned syncable filesystem for testing.
// This internally creates its own QuotaManager and FileSystemContext
// (as we do so for each isolated application).
class CannedSyncableFileSystem
    : public LocalFileSyncStatus::Observer {
 public:
  typedef base::OnceCallback<void(const storage::FileSystemURL& root,
                                  const std::string& name,
                                  base::File::Error result)>
      OpenFileSystemCallback;
  typedef base::OnceCallback<void(base::File::Error)> StatusCallback;
  typedef base::RepeatingCallback<void(int64_t)> WriteCallback;
  typedef storage::FileSystemOperation::FileEntryList FileEntryList;

  CannedSyncableFileSystem(
      const GURL& origin,
      bool in_memory_file_system,
      const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
      const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner);

  CannedSyncableFileSystem(const CannedSyncableFileSystem&) = delete;
  CannedSyncableFileSystem& operator=(const CannedSyncableFileSystem&) = delete;

  ~CannedSyncableFileSystem() override;

  // SetUp must be called before using this instance.
  void SetUp();

  // TearDown must be called before destructing this instance.
  void TearDown();

  // Creates a FileSystemURL for the given (utf8) path string.
  storage::FileSystemURL URL(const std::string& path) const;

  // Initialize this with given |sync_context| if it hasn't
  // been initialized.
  sync_file_system::SyncStatusCode MaybeInitializeFileSystemContext(
      LocalFileSyncContext* sync_context);

  // Opens a new syncable file system.
  base::File::Error OpenFileSystem();

  // Register sync status observers. Unlike original
  // LocalFileSyncStatus::Observer implementation the observer methods
  // are called on the same thread where AddSyncStatusObserver were called.
  void AddSyncStatusObserver(LocalFileSyncStatus::Observer* observer);
  void RemoveSyncStatusObserver(LocalFileSyncStatus::Observer* observer);

  // Accessors.
  storage::FileSystemContext* file_system_context() {
    return file_system_context_.get();
  }
  storage::MockQuotaManager* quota_manager() { return quota_manager_.get(); }
  GURL origin() const { return origin_; }
  storage::FileSystemType type() const { return type_; }

  // Helper routines to perform file system operations.
  // OpenFileSystem() must have been called before calling any of them.
  // They create an operation and run it on IO task runner, and the operation
  // posts a task on file runner.
  base::File::Error CreateDirectory(const storage::FileSystemURL& url);
  base::File::Error CreateFile(const storage::FileSystemURL& url);
  base::File::Error Copy(const storage::FileSystemURL& src_url,
                         const storage::FileSystemURL& dest_url);
  base::File::Error Move(const storage::FileSystemURL& src_url,
                         const storage::FileSystemURL& dest_url);
  base::File::Error TruncateFile(const storage::FileSystemURL& url,
                                 int64_t size);
  base::File::Error TouchFile(const storage::FileSystemURL& url,
                              const base::Time& last_access_time,
                              const base::Time& last_modified_time);
  base::File::Error Remove(const storage::FileSystemURL& url, bool recursive);
  base::File::Error FileExists(const storage::FileSystemURL& url);
  base::File::Error DirectoryExists(const storage::FileSystemURL& url);
  base::File::Error VerifyFile(const storage::FileSystemURL& url,
                               const std::string& expected_data);
  base::File::Error GetMetadataAndPlatformPath(
      const storage::FileSystemURL& url,
      base::File::Info* info,
      base::FilePath* platform_path);
  base::File::Error ReadDirectory(const storage::FileSystemURL& url,
                                  FileEntryList* entries);

  // Returns the # of bytes written (>=0) or an error code (<0).
  int64_t Write(const storage::FileSystemURL& url,
                std::unique_ptr<storage::BlobDataHandle> blob_data_handle);
  int64_t WriteString(const storage::FileSystemURL& url,
                      const std::string& data);

  // Purges the file system local storage.
  base::File::Error DeleteFileSystem();

  // Retrieves the quota and usage.
  blink::mojom::QuotaStatusCode GetUsageAndQuota(int64_t* usage,
                                                 int64_t* quota);

  // ChangeTracker related methods. They run on file task runner.
  void GetChangedURLsInTracker(storage::FileSystemURLSet* urls);
  void ClearChangeForURLInTracker(const storage::FileSystemURL& url);
  void GetChangesForURLInTracker(const storage::FileSystemURL& url,
                                 FileChangeList* changes);

  SyncFileSystemBackend* backend();
  storage::FileSystemOperationRunner* operation_runner();

  // LocalFileSyncStatus::Observer overrides.
  void OnSyncEnabled(const storage::FileSystemURL& url) override;
  void OnWriteEnabled(const storage::FileSystemURL& url) override;

  // Operation methods body.
  // They can be also called directly if the caller is already on IO thread.
  void DoOpenFileSystem(OpenFileSystemCallback callback);
  void DoCreateDirectory(const storage::FileSystemURL& url,
                         StatusCallback callback);
  void DoCreateFile(const storage::FileSystemURL& url, StatusCallback callback);
  void DoCopy(const storage::FileSystemURL& src_url,
              const storage::FileSystemURL& dest_url,
              StatusCallback callback);
  void DoMove(const storage::FileSystemURL& src_url,
              const storage::FileSystemURL& dest_url,
              StatusCallback callback);
  void DoTruncateFile(const storage::FileSystemURL& url,
                      int64_t size,
                      StatusCallback callback);
  void DoTouchFile(const storage::FileSystemURL& url,
                   const base::Time& last_access_time,
                   const base::Time& last_modified_time,
                   StatusCallback callback);
  void DoRemove(const storage::FileSystemURL& url,
                bool recursive,
                StatusCallback callback);
  void DoFileExists(const storage::FileSystemURL& url, StatusCallback callback);
  void DoDirectoryExists(const storage::FileSystemURL& url,
                         StatusCallback callback);
  void DoVerifyFile(const storage::FileSystemURL& url,
                    const std::string& expected_data,
                    StatusCallback callback);
  void DoGetMetadataAndPlatformPath(const storage::FileSystemURL& url,
                                    base::File::Info* info,
                                    base::FilePath* platform_path,
                                    StatusCallback callback);
  void DoReadDirectory(const storage::FileSystemURL& url,
                       FileEntryList* entries,
                       StatusCallback callback);
  void DoWrite(const storage::FileSystemURL& url,
               std::unique_ptr<storage::BlobDataHandle> blob_data_handle,
               WriteCallback callback);
  void DoWriteString(const storage::FileSystemURL& url,
                     const std::string& data,
                     WriteCallback callback);
  void DoGetUsageAndQuota(int64_t* usage,
                          int64_t* quota,
                          storage::StatusCallback callback);

 private:
  typedef base::ObserverListThreadSafe<LocalFileSyncStatus::Observer>
      ObserverList;

  // Callbacks.
  void DidOpenFileSystem(base::SingleThreadTaskRunner* original_task_runner,
                         base::OnceClosure quit_closure,
                         const storage::FileSystemURL& root,
                         const std::string& name,
                         base::File::Error result);
  void DidInitializeFileSystemContext(base::OnceClosure quit_closure,
                                      sync_file_system::SyncStatusCode status);

  void InitializeSyncStatusObserver();

  base::ScopedTempDir data_dir_;
  const std::string service_name_;

  scoped_refptr<storage::MockQuotaManager> quota_manager_;
  scoped_refptr<storage::MockQuotaManagerProxy> quota_manager_proxy_;
  scoped_refptr<storage::FileSystemContext> file_system_context_;
  const GURL origin_;
  const storage::FileSystemType type_;
  GURL root_url_;
  base::File::Error result_;
  sync_file_system::SyncStatusCode sync_status_;

  const bool in_memory_file_system_;
  scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;

  // Boolean flags mainly for helping debug.
  bool is_filesystem_set_up_;
  bool is_filesystem_opened_;  // Should be accessed only on the IO thread.

  scoped_refptr<ObserverList> sync_status_observers_;
};

}  // namespace sync_file_system

#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_