Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / canned_syncable_file_system.h
blobcb95f7a3135e018119a8d4c8036922ec7511bb2a
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/files/file.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/observer_list_threadsafe.h"
15 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h"
16 #include "chrome/browser/sync_file_system/sync_status_code.h"
17 #include "storage/browser/blob/blob_data_handle.h"
18 #include "storage/browser/fileapi/file_system_operation.h"
19 #include "storage/browser/fileapi/file_system_url.h"
20 #include "storage/browser/quota/quota_callbacks.h"
21 #include "storage/common/fileapi/file_system_types.h"
22 #include "storage/common/fileapi/file_system_util.h"
23 #include "storage/common/quota/quota_types.h"
25 namespace base {
26 class SingleThreadTaskRunner;
27 class Thread;
30 namespace storage {
31 class FileSystemContext;
32 class FileSystemOperationRunner;
33 class FileSystemURL;
36 namespace leveldb {
37 class Env;
40 namespace net {
41 class URLRequestContext;
44 namespace storage {
45 class QuotaManager;
48 namespace sync_file_system {
50 class FileChangeList;
51 class LocalFileSyncContext;
52 class SyncFileSystemBackend;
54 // A canned syncable filesystem for testing.
55 // This internally creates its own QuotaManager and FileSystemContext
56 // (as we do so for each isolated application).
57 class CannedSyncableFileSystem
58 : public LocalFileSyncStatus::Observer {
59 public:
60 typedef base::Callback<void(const GURL& root,
61 const std::string& name,
62 base::File::Error result)>
63 OpenFileSystemCallback;
64 typedef base::Callback<void(base::File::Error)> StatusCallback;
65 typedef base::Callback<void(int64)> WriteCallback;
66 typedef storage::FileSystemOperation::FileEntryList FileEntryList;
68 enum QuotaMode {
69 QUOTA_ENABLED,
70 QUOTA_DISABLED,
73 CannedSyncableFileSystem(
74 const GURL& origin,
75 leveldb::Env* env_override,
76 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
77 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner);
78 ~CannedSyncableFileSystem() override;
80 // SetUp must be called before using this instance.
81 void SetUp(QuotaMode quota_mode);
83 // TearDown must be called before destructing this instance.
84 void TearDown();
86 // Creates a FileSystemURL for the given (utf8) path string.
87 storage::FileSystemURL URL(const std::string& path) const;
89 // Initialize this with given |sync_context| if it hasn't
90 // been initialized.
91 sync_file_system::SyncStatusCode MaybeInitializeFileSystemContext(
92 LocalFileSyncContext* sync_context);
94 // Opens a new syncable file system.
95 base::File::Error OpenFileSystem();
97 // Register sync status observers. Unlike original
98 // LocalFileSyncStatus::Observer implementation the observer methods
99 // are called on the same thread where AddSyncStatusObserver were called.
100 void AddSyncStatusObserver(LocalFileSyncStatus::Observer* observer);
101 void RemoveSyncStatusObserver(LocalFileSyncStatus::Observer* observer);
103 // Accessors.
104 storage::FileSystemContext* file_system_context() {
105 return file_system_context_.get();
107 storage::QuotaManager* quota_manager() { return quota_manager_.get(); }
108 GURL origin() const { return origin_; }
109 storage::FileSystemType type() const { return type_; }
110 storage::StorageType storage_type() const {
111 return FileSystemTypeToQuotaStorageType(type_);
114 // Helper routines to perform file system operations.
115 // OpenFileSystem() must have been called before calling any of them.
116 // They create an operation and run it on IO task runner, and the operation
117 // posts a task on file runner.
118 base::File::Error CreateDirectory(const storage::FileSystemURL& url);
119 base::File::Error CreateFile(const storage::FileSystemURL& url);
120 base::File::Error Copy(const storage::FileSystemURL& src_url,
121 const storage::FileSystemURL& dest_url);
122 base::File::Error Move(const storage::FileSystemURL& src_url,
123 const storage::FileSystemURL& dest_url);
124 base::File::Error TruncateFile(const storage::FileSystemURL& url, int64 size);
125 base::File::Error TouchFile(const storage::FileSystemURL& url,
126 const base::Time& last_access_time,
127 const base::Time& last_modified_time);
128 base::File::Error Remove(const storage::FileSystemURL& url, bool recursive);
129 base::File::Error FileExists(const storage::FileSystemURL& url);
130 base::File::Error DirectoryExists(const storage::FileSystemURL& url);
131 base::File::Error VerifyFile(const storage::FileSystemURL& url,
132 const std::string& expected_data);
133 base::File::Error GetMetadataAndPlatformPath(
134 const storage::FileSystemURL& url,
135 base::File::Info* info,
136 base::FilePath* platform_path);
137 base::File::Error ReadDirectory(const storage::FileSystemURL& url,
138 FileEntryList* entries);
140 // Returns the # of bytes written (>=0) or an error code (<0).
141 int64 Write(net::URLRequestContext* url_request_context,
142 const storage::FileSystemURL& url,
143 scoped_ptr<storage::BlobDataHandle> blob_data_handle);
144 int64 WriteString(const storage::FileSystemURL& url, const std::string& data);
146 // Purges the file system local storage.
147 base::File::Error DeleteFileSystem();
149 // Retrieves the quota and usage.
150 storage::QuotaStatusCode GetUsageAndQuota(int64* usage, int64* quota);
152 // ChangeTracker related methods. They run on file task runner.
153 void GetChangedURLsInTracker(storage::FileSystemURLSet* urls);
154 void ClearChangeForURLInTracker(const storage::FileSystemURL& url);
155 void GetChangesForURLInTracker(const storage::FileSystemURL& url,
156 FileChangeList* changes);
158 SyncFileSystemBackend* backend();
159 storage::FileSystemOperationRunner* operation_runner();
161 // LocalFileSyncStatus::Observer overrides.
162 void OnSyncEnabled(const storage::FileSystemURL& url) override;
163 void OnWriteEnabled(const storage::FileSystemURL& url) override;
165 // Operation methods body.
166 // They can be also called directly if the caller is already on IO thread.
167 void DoOpenFileSystem(const OpenFileSystemCallback& callback);
168 void DoCreateDirectory(const storage::FileSystemURL& url,
169 const StatusCallback& callback);
170 void DoCreateFile(const storage::FileSystemURL& url,
171 const StatusCallback& callback);
172 void DoCopy(const storage::FileSystemURL& src_url,
173 const storage::FileSystemURL& dest_url,
174 const StatusCallback& callback);
175 void DoMove(const storage::FileSystemURL& src_url,
176 const storage::FileSystemURL& dest_url,
177 const StatusCallback& callback);
178 void DoTruncateFile(const storage::FileSystemURL& url,
179 int64 size,
180 const StatusCallback& callback);
181 void DoTouchFile(const storage::FileSystemURL& url,
182 const base::Time& last_access_time,
183 const base::Time& last_modified_time,
184 const StatusCallback& callback);
185 void DoRemove(const storage::FileSystemURL& url,
186 bool recursive,
187 const StatusCallback& callback);
188 void DoFileExists(const storage::FileSystemURL& url,
189 const StatusCallback& callback);
190 void DoDirectoryExists(const storage::FileSystemURL& url,
191 const StatusCallback& callback);
192 void DoVerifyFile(const storage::FileSystemURL& url,
193 const std::string& expected_data,
194 const StatusCallback& callback);
195 void DoGetMetadataAndPlatformPath(const storage::FileSystemURL& url,
196 base::File::Info* info,
197 base::FilePath* platform_path,
198 const StatusCallback& callback);
199 void DoReadDirectory(const storage::FileSystemURL& url,
200 FileEntryList* entries,
201 const StatusCallback& callback);
202 void DoWrite(net::URLRequestContext* url_request_context,
203 const storage::FileSystemURL& url,
204 scoped_ptr<storage::BlobDataHandle> blob_data_handle,
205 const WriteCallback& callback);
206 void DoWriteString(const storage::FileSystemURL& url,
207 const std::string& data,
208 const WriteCallback& callback);
209 void DoGetUsageAndQuota(int64* usage,
210 int64* quota,
211 const storage::StatusCallback& callback);
213 private:
214 typedef base::ObserverListThreadSafe<LocalFileSyncStatus::Observer>
215 ObserverList;
217 // Callbacks.
218 void DidOpenFileSystem(base::SingleThreadTaskRunner* original_task_runner,
219 const base::Closure& quit_closure,
220 const GURL& root,
221 const std::string& name,
222 base::File::Error result);
223 void DidInitializeFileSystemContext(const base::Closure& quit_closure,
224 sync_file_system::SyncStatusCode status);
226 void InitializeSyncStatusObserver();
228 base::ScopedTempDir data_dir_;
229 const std::string service_name_;
231 scoped_refptr<storage::QuotaManager> quota_manager_;
232 scoped_refptr<storage::FileSystemContext> file_system_context_;
233 const GURL origin_;
234 const storage::FileSystemType type_;
235 GURL root_url_;
236 base::File::Error result_;
237 sync_file_system::SyncStatusCode sync_status_;
239 leveldb::Env* env_override_;
240 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
241 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
243 // Boolean flags mainly for helping debug.
244 bool is_filesystem_set_up_;
245 bool is_filesystem_opened_; // Should be accessed only on the IO thread.
247 scoped_refptr<ObserverList> sync_status_observers_;
249 DISALLOW_COPY_AND_ASSIGN(CannedSyncableFileSystem);
252 } // namespace sync_file_system
254 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_