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