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 "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"
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(
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  virtual ~CannedSyncableFileSystem();
79
80  // SetUp must be called before using this instance.
81  void SetUp(QuotaMode quota_mode);
82
83  // TearDown must be called before destructing this instance.
84  void TearDown();
85
86  // Creates a FileSystemURL for the given (utf8) path string.
87  storage::FileSystemURL URL(const std::string& path) const;
88
89  // Initialize this with given |sync_context| if it hasn't
90  // been initialized.
91  sync_file_system::SyncStatusCode MaybeInitializeFileSystemContext(
92      LocalFileSyncContext* sync_context);
93
94  // Opens a new syncable file system.
95  base::File::Error OpenFileSystem();
96
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);
102
103  // Accessors.
104  storage::FileSystemContext* file_system_context() {
105    return file_system_context_.get();
106  }
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_);
112  }
113
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);
139
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);
145
146  // Purges the file system local storage.
147  base::File::Error DeleteFileSystem();
148
149  // Retrieves the quota and usage.
150  storage::QuotaStatusCode GetUsageAndQuota(int64* usage, int64* quota);
151
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);
157
158  SyncFileSystemBackend* backend();
159  storage::FileSystemOperationRunner* operation_runner();
160
161  // LocalFileSyncStatus::Observer overrides.
162  virtual void OnSyncEnabled(const storage::FileSystemURL& url) OVERRIDE;
163  virtual void OnWriteEnabled(const storage::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 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);
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<storage::QuotaManager> quota_manager_;
231  scoped_refptr<storage::FileSystemContext> file_system_context_;
232  const GURL origin_;
233  const storage::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