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