1 // Copyright (c) 2012 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.
5 #ifndef WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_
6 #define WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_
11 #include "base/callback_forward.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/message_loop.h"
14 #include "base/observer_list_threadsafe.h"
15 #include "base/platform_file.h"
16 #include "webkit/fileapi/file_system_types.h"
17 #include "webkit/fileapi/file_system_url.h"
18 #include "webkit/fileapi/file_system_util.h"
19 #include "webkit/fileapi/syncable/local_file_sync_status.h"
20 #include "webkit/fileapi/syncable/sync_status_code.h"
21 #include "webkit/quota/quota_types.h"
24 class MessageLoopProxy
;
25 class SingleThreadTaskRunner
;
30 class FileSystemContext
;
31 class FileSystemOperation
;
36 class URLRequestContext
;
43 namespace sync_file_system
{
45 class LocalFileSyncContext
;
47 // A canned syncable filesystem for testing.
48 // This internally creates its own QuotaManager and FileSystemContext
49 // (as we do so for each isolated application).
50 class CannedSyncableFileSystem
51 : public LocalFileSyncStatus::Observer
{
53 typedef base::Callback
<void(base::PlatformFileError
)> StatusCallback
;
54 typedef base::Callback
<void(int64
)> WriteCallback
;
56 CannedSyncableFileSystem(const GURL
& origin
,
57 const std::string
& service
,
58 base::SingleThreadTaskRunner
* io_task_runner
,
59 base::SingleThreadTaskRunner
* file_task_runner
);
60 virtual ~CannedSyncableFileSystem();
62 // SetUp must be called before using this instance.
65 // TearDown must be called before destructing this instance.
68 // Creates a FileSystemURL for the given (utf8) path string.
69 fileapi::FileSystemURL
URL(const std::string
& path
) const;
71 // Initialize this with given |sync_context| if it hasn't
73 sync_file_system::SyncStatusCode
MaybeInitializeFileSystemContext(
74 LocalFileSyncContext
* sync_context
);
76 // Opens a new syncable file system.
77 base::PlatformFileError
OpenFileSystem();
79 // Register sync status observers. Unlike original
80 // LocalFileSyncStatus::Observer implementation the observer methods
81 // are called on the same thread where AddSyncStatusObserver were called.
82 void AddSyncStatusObserver(LocalFileSyncStatus::Observer
* observer
);
83 void RemoveSyncStatusObserver(LocalFileSyncStatus::Observer
* observer
);
86 fileapi::FileSystemContext
* file_system_context() {
87 return file_system_context_
.get();
89 quota::QuotaManager
* quota_manager() { return quota_manager_
.get(); }
90 GURL
origin() const { return origin_
; }
91 fileapi::FileSystemType
type() const { return type_
; }
92 quota::StorageType
storage_type() const {
93 return FileSystemTypeToQuotaStorageType(type_
);
96 // Helper routines to perform file system operations.
97 // OpenFileSystem() must have been called before calling any of them.
98 // They create an operation and run it on IO task runner, and the operation
99 // posts a task on file runner.
100 base::PlatformFileError
CreateDirectory(const fileapi::FileSystemURL
& url
);
101 base::PlatformFileError
CreateFile(const fileapi::FileSystemURL
& url
);
102 base::PlatformFileError
Copy(const fileapi::FileSystemURL
& src_url
,
103 const fileapi::FileSystemURL
& dest_url
);
104 base::PlatformFileError
Move(const fileapi::FileSystemURL
& src_url
,
105 const fileapi::FileSystemURL
& dest_url
);
106 base::PlatformFileError
TruncateFile(const fileapi::FileSystemURL
& url
,
108 base::PlatformFileError
TouchFile(const fileapi::FileSystemURL
& url
,
109 const base::Time
& last_access_time
,
110 const base::Time
& last_modified_time
);
111 base::PlatformFileError
Remove(const fileapi::FileSystemURL
& url
,
113 base::PlatformFileError
FileExists(const fileapi::FileSystemURL
& url
);
114 base::PlatformFileError
DirectoryExists(const fileapi::FileSystemURL
& url
);
115 base::PlatformFileError
VerifyFile(const fileapi::FileSystemURL
& url
,
116 const std::string
& expected_data
);
117 base::PlatformFileError
GetMetadata(const fileapi::FileSystemURL
& url
,
118 base::PlatformFileInfo
* info
,
119 base::FilePath
* platform_path
);
121 // Returns the # of bytes written (>=0) or an error code (<0).
122 int64
Write(net::URLRequestContext
* url_request_context
,
123 const fileapi::FileSystemURL
& url
, const GURL
& blob_url
);
124 int64
WriteString(const fileapi::FileSystemURL
& url
, const std::string
& data
);
126 // Purges the file system local storage.
127 base::PlatformFileError
DeleteFileSystem();
129 // Retrieves the quota and usage.
130 quota::QuotaStatusCode
GetUsageAndQuota(int64
* usage
, int64
* quota
);
132 // ChangeTracker related methods. They run on file task runner.
133 void GetChangedURLsInTracker(fileapi::FileSystemURLSet
* urls
);
134 void ClearChangeForURLInTracker(const fileapi::FileSystemURL
& url
);
136 // Returns new FileSystemOperation.
137 fileapi::FileSystemOperation
* NewOperation();
139 // LocalFileSyncStatus::Observer overrides.
140 virtual void OnSyncEnabled(const fileapi::FileSystemURL
& url
) OVERRIDE
;
141 virtual void OnWriteEnabled(const fileapi::FileSystemURL
& url
) OVERRIDE
;
143 // Operation methods body.
144 // They can be also called directly if the caller is already on IO thread.
145 void DoCreateDirectory(const fileapi::FileSystemURL
& url
,
146 const StatusCallback
& callback
);
147 void DoCreateFile(const fileapi::FileSystemURL
& url
,
148 const StatusCallback
& callback
);
149 void DoCopy(const fileapi::FileSystemURL
& src_url
,
150 const fileapi::FileSystemURL
& dest_url
,
151 const StatusCallback
& callback
);
152 void DoMove(const fileapi::FileSystemURL
& src_url
,
153 const fileapi::FileSystemURL
& dest_url
,
154 const StatusCallback
& callback
);
155 void DoTruncateFile(const fileapi::FileSystemURL
& url
,
157 const StatusCallback
& callback
);
158 void DoTouchFile(const fileapi::FileSystemURL
& url
,
159 const base::Time
& last_access_time
,
160 const base::Time
& last_modified_time
,
161 const StatusCallback
& callback
);
162 void DoRemove(const fileapi::FileSystemURL
& url
,
164 const StatusCallback
& callback
);
165 void DoFileExists(const fileapi::FileSystemURL
& url
,
166 const StatusCallback
& callback
);
167 void DoDirectoryExists(const fileapi::FileSystemURL
& url
,
168 const StatusCallback
& callback
);
169 void DoVerifyFile(const fileapi::FileSystemURL
& url
,
170 const std::string
& expected_data
,
171 const StatusCallback
& callback
);
172 void DoGetMetadata(const fileapi::FileSystemURL
& url
,
173 base::PlatformFileInfo
* info
,
174 base::FilePath
* platform_path
,
175 const StatusCallback
& callback
);
176 void DoWrite(net::URLRequestContext
* url_request_context
,
177 const fileapi::FileSystemURL
& url
,
178 const GURL
& blob_url
,
179 const WriteCallback
& callback
);
180 void DoWriteString(const fileapi::FileSystemURL
& url
,
181 const std::string
& data
,
182 const WriteCallback
& callback
);
183 void DoGetUsageAndQuota(int64
* usage
,
185 const quota::StatusCallback
& callback
);
188 typedef ObserverListThreadSafe
<LocalFileSyncStatus::Observer
> ObserverList
;
191 void DidOpenFileSystem(base::PlatformFileError result
,
192 const std::string
& name
,
194 void DidInitializeFileSystemContext(sync_file_system::SyncStatusCode status
);
196 void InitializeSyncStatusObserver();
198 base::ScopedTempDir data_dir_
;
199 const std::string service_name_
;
201 scoped_refptr
<quota::QuotaManager
> quota_manager_
;
202 scoped_refptr
<fileapi::FileSystemContext
> file_system_context_
;
204 const fileapi::FileSystemType type_
;
206 base::PlatformFileError result_
;
207 sync_file_system::SyncStatusCode sync_status_
;
209 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
210 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner_
;
212 // Boolean flags mainly for helping debug.
213 bool is_filesystem_set_up_
;
214 bool is_filesystem_opened_
;
216 scoped_refptr
<ObserverList
> sync_status_observers_
;
218 DISALLOW_COPY_AND_ASSIGN(CannedSyncableFileSystem
);
221 } // namespace sync_file_system
223 #endif // WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_