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