canned_syncable_file_system.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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 storage {
31class FileSystemContext;
32class FileSystemOperationRunner;
33class FileSystemURL;
34}
35
36namespace leveldb {
37class Env;
38}
39
40namespace net {
41class URLRequestContext;
42}
43
44namespace storage {
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 storage::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  storage::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  storage::FileSystemContext* file_system_context() {
104    return file_system_context_.get();
105  }
106  storage::QuotaManager* quota_manager() { return quota_manager_.get(); }
107  GURL origin() const { return origin_; }
108  storage::FileSystemType type() const { return type_; }
109  storage::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 storage::FileSystemURL& url);
118  base::File::Error CreateFile(const storage::FileSystemURL& url);
119  base::File::Error Copy(const storage::FileSystemURL& src_url,
120                         const storage::FileSystemURL& dest_url);
121  base::File::Error Move(const storage::FileSystemURL& src_url,
122                         const storage::FileSystemURL& dest_url);
123  base::File::Error TruncateFile(const storage::FileSystemURL& url, int64 size);
124  base::File::Error TouchFile(const storage::FileSystemURL& url,
125                              const base::Time& last_access_time,
126                              const base::Time& last_modified_time);
127  base::File::Error Remove(const storage::FileSystemURL& url, bool recursive);
128  base::File::Error FileExists(const storage::FileSystemURL& url);
129  base::File::Error DirectoryExists(const storage::FileSystemURL& url);
130  base::File::Error VerifyFile(const storage::FileSystemURL& url,
131                               const std::string& expected_data);
132  base::File::Error GetMetadataAndPlatformPath(
133      const storage::FileSystemURL& url,
134      base::File::Info* info,
135      base::FilePath* platform_path);
136  base::File::Error ReadDirectory(const storage::FileSystemURL& url,
137                                  FileEntryList* entries);
138
139  // Returns the # of bytes written (>=0) or an error code (<0).
140  int64 Write(net::URLRequestContext* url_request_context,
141              const storage::FileSystemURL& url,
142              scoped_ptr<storage::BlobDataHandle> blob_data_handle);
143  int64 WriteString(const storage::FileSystemURL& url, const std::string& data);
144
145  // Purges the file system local storage.
146  base::File::Error DeleteFileSystem();
147
148  // Retrieves the quota and usage.
149  storage::QuotaStatusCode GetUsageAndQuota(int64* usage, int64* quota);
150
151  // ChangeTracker related methods. They run on file task runner.
152  void GetChangedURLsInTracker(storage::FileSystemURLSet* urls);
153  void ClearChangeForURLInTracker(const storage::FileSystemURL& url);
154  void GetChangesForURLInTracker(const storage::FileSystemURL& url,
155                                 FileChangeList* changes);
156
157  SyncFileSystemBackend* backend();
158  storage::FileSystemOperationRunner* operation_runner();
159
160  // LocalFileSyncStatus::Observer overrides.
161  virtual void OnSyncEnabled(const storage::FileSystemURL& url) OVERRIDE;
162  virtual void OnWriteEnabled(const storage::FileSystemURL& url) OVERRIDE;
163
164  // Operation methods body.
165  // They can be also called directly if the caller is already on IO thread.
166  void DoOpenFileSystem(const OpenFileSystemCallback& callback);
167  void DoCreateDirectory(const storage::FileSystemURL& url,
168                         const StatusCallback& callback);
169  void DoCreateFile(const storage::FileSystemURL& url,
170                    const StatusCallback& callback);
171  void DoCopy(const storage::FileSystemURL& src_url,
172              const storage::FileSystemURL& dest_url,
173              const StatusCallback& callback);
174  void DoMove(const storage::FileSystemURL& src_url,
175              const storage::FileSystemURL& dest_url,
176              const StatusCallback& callback);
177  void DoTruncateFile(const storage::FileSystemURL& url,
178                      int64 size,
179                      const StatusCallback& callback);
180  void DoTouchFile(const storage::FileSystemURL& url,
181                   const base::Time& last_access_time,
182                   const base::Time& last_modified_time,
183                   const StatusCallback& callback);
184  void DoRemove(const storage::FileSystemURL& url,
185                bool recursive,
186                const StatusCallback& callback);
187  void DoFileExists(const storage::FileSystemURL& url,
188                    const StatusCallback& callback);
189  void DoDirectoryExists(const storage::FileSystemURL& url,
190                         const StatusCallback& callback);
191  void DoVerifyFile(const storage::FileSystemURL& url,
192                    const std::string& expected_data,
193                    const StatusCallback& callback);
194  void DoGetMetadataAndPlatformPath(const storage::FileSystemURL& url,
195                                    base::File::Info* info,
196                                    base::FilePath* platform_path,
197                                    const StatusCallback& callback);
198  void DoReadDirectory(const storage::FileSystemURL& url,
199                       FileEntryList* entries,
200                       const StatusCallback& callback);
201  void DoWrite(net::URLRequestContext* url_request_context,
202               const storage::FileSystemURL& url,
203               scoped_ptr<storage::BlobDataHandle> blob_data_handle,
204               const WriteCallback& callback);
205  void DoWriteString(const storage::FileSystemURL& url,
206                     const std::string& data,
207                     const WriteCallback& callback);
208  void DoGetUsageAndQuota(int64* usage,
209                          int64* quota,
210                          const storage::StatusCallback& callback);
211
212 private:
213  typedef ObserverListThreadSafe<LocalFileSyncStatus::Observer> ObserverList;
214
215  // Callbacks.
216  void DidOpenFileSystem(base::SingleThreadTaskRunner* original_task_runner,
217                         const base::Closure& quit_closure,
218                         const GURL& root,
219                         const std::string& name,
220                         base::File::Error result);
221  void DidInitializeFileSystemContext(const base::Closure& quit_closure,
222                                      sync_file_system::SyncStatusCode status);
223
224  void InitializeSyncStatusObserver();
225
226  base::ScopedTempDir data_dir_;
227  const std::string service_name_;
228
229  scoped_refptr<storage::QuotaManager> quota_manager_;
230  scoped_refptr<storage::FileSystemContext> file_system_context_;
231  const GURL origin_;
232  const storage::FileSystemType type_;
233  GURL root_url_;
234  base::File::Error result_;
235  sync_file_system::SyncStatusCode sync_status_;
236
237  leveldb::Env* env_override_;
238  scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
239  scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
240
241  // Boolean flags mainly for helping debug.
242  bool is_filesystem_set_up_;
243  bool is_filesystem_opened_;  // Should be accessed only on the IO thread.
244
245  scoped_refptr<ObserverList> sync_status_observers_;
246
247  DISALLOW_COPY_AND_ASSIGN(CannedSyncableFileSystem);
248};
249
250}  // namespace sync_file_system
251
252#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_CANNED_SYNCABLE_FILE_SYSTEM_H_
253