canned_syncable_file_system.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/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 "webkit/browser/blob/blob_data_handle.h"
18#include "webkit/browser/fileapi/file_system_operation.h"
19#include "webkit/browser/fileapi/file_system_url.h"
20#include "webkit/browser/quota/quota_callbacks.h"
21#include "webkit/common/fileapi/file_system_types.h"
22#include "webkit/common/fileapi/file_system_util.h"
23#include "webkit/common/quota/quota_types.h"
24
25namespace base {
26class SingleThreadTaskRunner;
27class Thread;
28}
29
30namespace fileapi {
31class FileSystemContext;
32class FileSystemOperationRunner;
33class FileSystemURL;
34}
35
36namespace leveldb {
37class Env;
38}
39
40namespace net {
41class URLRequestContext;
42}
43
44namespace quota {
45class QuotaManager;
46}
47
48namespace sync_file_system {
49
50class FileChangeList;
51class LocalFileSyncContext;
52class SyncFileSystemBackend;
53
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).
57class 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 fileapi::FileSystemOperation::FileEntryList FileEntryList;
67
68  enum QuotaMode {
69    QUOTA_ENABLED,
70    QUOTA_DISABLED,
71  };
72
73  CannedSyncableFileSystem(const GURL& origin,
74                           leveldb::Env* env_override,
75                           base::SingleThreadTaskRunner* io_task_runner,
76                           base::SingleThreadTaskRunner* file_task_runner);
77  virtual ~CannedSyncableFileSystem();
78
79  // SetUp must be called before using this instance.
80  void SetUp(QuotaMode quota_mode);
81
82  // TearDown must be called before destructing this instance.
83  void TearDown();
84
85  // Creates a FileSystemURL for the given (utf8) path string.
86  fileapi::FileSystemURL URL(const std::string& path) const;
87
88  // Initialize this with given |sync_context| if it hasn't
89  // been initialized.
90  sync_file_system::SyncStatusCode MaybeInitializeFileSystemContext(
91      LocalFileSyncContext* sync_context);
92
93  // Opens a new syncable file system.
94  base::File::Error OpenFileSystem();
95
96  // Register sync status observers. Unlike original
97  // LocalFileSyncStatus::Observer implementation the observer methods
98  // are called on the same thread where AddSyncStatusObserver were called.
99  void AddSyncStatusObserver(LocalFileSyncStatus::Observer* observer);
100  void RemoveSyncStatusObserver(LocalFileSyncStatus::Observer* observer);
101
102  // Accessors.
103  fileapi::FileSystemContext* file_system_context() {
104    return file_system_context_.get();
105  }
106  quota::QuotaManager* quota_manager() { return quota_manager_.get(); }
107  GURL origin() const { return origin_; }
108  fileapi::FileSystemType type() const { return type_; }
109  quota::StorageType storage_type() const {
110    return FileSystemTypeToQuotaStorageType(type_);
111  }
112
113  // Helper routines to perform file system operations.
114  // OpenFileSystem() must have been called before calling any of them.
115  // They create an operation and run it on IO task runner, and the operation
116  // posts a task on file runner.
117  base::File::Error CreateDirectory(const fileapi::FileSystemURL& url);
118  base::File::Error CreateFile(const fileapi::FileSystemURL& url);
119  base::File::Error Copy(const fileapi::FileSystemURL& src_url,
120                         const fileapi::FileSystemURL& dest_url);
121  base::File::Error Move(const fileapi::FileSystemURL& src_url,
122                         const fileapi::FileSystemURL& dest_url);
123  base::File::Error TruncateFile(const fileapi::FileSystemURL& url,
124                                 int64 size);
125  base::File::Error TouchFile(const fileapi::FileSystemURL& url,
126                              const base::Time& last_access_time,
127                              const base::Time& last_modified_time);
128  base::File::Error Remove(const fileapi::FileSystemURL& url, bool recursive);
129  base::File::Error FileExists(const fileapi::FileSystemURL& url);
130  base::File::Error DirectoryExists(const fileapi::FileSystemURL& url);
131  base::File::Error VerifyFile(const fileapi::FileSystemURL& url,
132                               const std::string& expected_data);
133  base::File::Error GetMetadataAndPlatformPath(
134      const fileapi::FileSystemURL& url,
135      base::File::Info* info,
136      base::FilePath* platform_path);
137  base::File::Error ReadDirectory(const fileapi::FileSystemURL& url,
138                                  FileEntryList* entries);
139
140  // Returns the # of bytes written (>=0) or an error code (<0).
141  int64 Write(net::URLRequestContext* url_request_context,
142              const fileapi::FileSystemURL& url,
143              scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle);
144  int64 WriteString(const fileapi::FileSystemURL& url, const std::string& data);
145
146  // Purges the file system local storage.
147  base::File::Error DeleteFileSystem();
148
149  // Retrieves the quota and usage.
150  quota::QuotaStatusCode GetUsageAndQuota(int64* usage, int64* quota);
151
152  // ChangeTracker related methods. They run on file task runner.
153  void GetChangedURLsInTracker(fileapi::FileSystemURLSet* urls);
154  void ClearChangeForURLInTracker(const fileapi::FileSystemURL& url);
155  void GetChangesForURLInTracker(const fileapi::FileSystemURL& url,
156                                 FileChangeList* changes);
157
158  SyncFileSystemBackend* backend();
159  fileapi::FileSystemOperationRunner* operation_runner();
160
161  // LocalFileSyncStatus::Observer overrides.
162  virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) OVERRIDE;
163  virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) OVERRIDE;
164
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 fileapi::FileSystemURL& url,
169                         const StatusCallback& callback);
170  void DoCreateFile(const fileapi::FileSystemURL& url,
171                    const StatusCallback& callback);
172  void DoCopy(const fileapi::FileSystemURL& src_url,
173              const fileapi::FileSystemURL& dest_url,
174              const StatusCallback& callback);
175  void DoMove(const fileapi::FileSystemURL& src_url,
176              const fileapi::FileSystemURL& dest_url,
177              const StatusCallback& callback);
178  void DoTruncateFile(const fileapi::FileSystemURL& url,
179                      int64 size,
180                      const StatusCallback& callback);
181  void DoTouchFile(const fileapi::FileSystemURL& url,
182                   const base::Time& last_access_time,
183                   const base::Time& last_modified_time,
184                   const StatusCallback& callback);
185  void DoRemove(const fileapi::FileSystemURL& url,
186                bool recursive,
187                const StatusCallback& callback);
188  void DoFileExists(const fileapi::FileSystemURL& url,
189                    const StatusCallback& callback);
190  void DoDirectoryExists(const fileapi::FileSystemURL& url,
191                         const StatusCallback& callback);
192  void DoVerifyFile(const fileapi::FileSystemURL& url,
193                    const std::string& expected_data,
194                    const StatusCallback& callback);
195  void DoGetMetadataAndPlatformPath(const fileapi::FileSystemURL& url,
196                                    base::File::Info* info,
197                                    base::FilePath* platform_path,
198                                    const StatusCallback& callback);
199  void DoReadDirectory(const fileapi::FileSystemURL& url,
200                       FileEntryList* entries,
201                       const StatusCallback& callback);
202  void DoWrite(net::URLRequestContext* url_request_context,
203               const fileapi::FileSystemURL& url,
204               scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle,
205               const WriteCallback& callback);
206  void DoWriteString(const fileapi::FileSystemURL& url,
207                     const std::string& data,
208                     const WriteCallback& callback);
209  void DoGetUsageAndQuota(int64* usage,
210                          int64* quota,
211                          const quota::StatusCallback& callback);
212
213 private:
214  typedef ObserverListThreadSafe<LocalFileSyncStatus::Observer> ObserverList;
215
216  // Callbacks.
217  void DidOpenFileSystem(base::SingleThreadTaskRunner* original_task_runner,
218                         const base::Closure& quit_closure,
219                         const GURL& root,
220                         const std::string& name,
221                         base::File::Error result);
222  void DidInitializeFileSystemContext(const base::Closure& quit_closure,
223                                      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