OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_ | |
6 #define WEBKIT_FILEAPI_SYNCABLE_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.h" | |
14 #include "base/observer_list_threadsafe.h" | |
15 #include "base/platform_file.h" | |
16 #include "webkit/browser/fileapi/file_system_url.h" | |
17 #include "webkit/common/fileapi/file_system_types.h" | |
18 #include "webkit/common/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" | |
22 | |
23 namespace base { | |
24 class MessageLoopProxy; | |
25 class SingleThreadTaskRunner; | |
26 class Thread; | |
27 } | |
28 | |
29 namespace fileapi { | |
30 class FileSystemContext; | |
31 class FileSystemOperation; | |
32 class FileSystemURL; | |
33 } | |
34 | |
35 namespace net { | |
36 class URLRequestContext; | |
37 } | |
38 | |
39 namespace quota { | |
40 class QuotaManager; | |
41 } | |
42 | |
43 namespace sync_file_system { | |
44 | |
45 class LocalFileSyncContext; | |
46 | |
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 { | |
52 public: | |
53 typedef base::Callback<void(base::PlatformFileError)> StatusCallback; | |
54 typedef base::Callback<void(int64)> WriteCallback; | |
55 | |
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(); | |
61 | |
62 // SetUp must be called before using this instance. | |
63 void SetUp(); | |
64 | |
65 // TearDown must be called before destructing this instance. | |
66 void TearDown(); | |
67 | |
68 // Creates a FileSystemURL for the given (utf8) path string. | |
69 fileapi::FileSystemURL URL(const std::string& path) const; | |
70 | |
71 // Initialize this with given |sync_context| if it hasn't | |
72 // been initialized. | |
73 sync_file_system::SyncStatusCode MaybeInitializeFileSystemContext( | |
74 LocalFileSyncContext* sync_context); | |
75 | |
76 // Opens a new syncable file system. | |
77 base::PlatformFileError OpenFileSystem(); | |
78 | |
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); | |
84 | |
85 // Accessors. | |
86 fileapi::FileSystemContext* file_system_context() { | |
87 return file_system_context_.get(); | |
88 } | |
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_); | |
94 } | |
95 | |
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, | |
107 int64 size); | |
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, | |
112 bool recursive); | |
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); | |
120 | |
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); | |
125 | |
126 // Purges the file system local storage. | |
127 base::PlatformFileError DeleteFileSystem(); | |
128 | |
129 // Retrieves the quota and usage. | |
130 quota::QuotaStatusCode GetUsageAndQuota(int64* usage, int64* quota); | |
131 | |
132 // ChangeTracker related methods. They run on file task runner. | |
133 void GetChangedURLsInTracker(fileapi::FileSystemURLSet* urls); | |
134 void ClearChangeForURLInTracker(const fileapi::FileSystemURL& url); | |
135 | |
136 // Returns new FileSystemOperation. | |
137 fileapi::FileSystemOperation* NewOperation(); | |
138 | |
139 // LocalFileSyncStatus::Observer overrides. | |
140 virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) OVERRIDE; | |
141 virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) OVERRIDE; | |
142 | |
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, | |
156 int64 size, | |
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, | |
163 bool recursive, | |
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, | |
184 int64* quota, | |
185 const quota::StatusCallback& callback); | |
186 | |
187 private: | |
188 typedef ObserverListThreadSafe<LocalFileSyncStatus::Observer> ObserverList; | |
189 | |
190 // Callbacks. | |
191 void DidOpenFileSystem(base::PlatformFileError result, | |
192 const std::string& name, | |
193 const GURL& root); | |
194 void DidInitializeFileSystemContext(sync_file_system::SyncStatusCode status); | |
195 | |
196 void InitializeSyncStatusObserver(); | |
197 | |
198 base::ScopedTempDir data_dir_; | |
199 const std::string service_name_; | |
200 | |
201 scoped_refptr<quota::QuotaManager> quota_manager_; | |
202 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | |
203 const GURL origin_; | |
204 const fileapi::FileSystemType type_; | |
205 GURL root_url_; | |
206 base::PlatformFileError result_; | |
207 sync_file_system::SyncStatusCode sync_status_; | |
208 | |
209 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
210 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
211 | |
212 // Boolean flags mainly for helping debug. | |
213 bool is_filesystem_set_up_; | |
214 bool is_filesystem_opened_; | |
215 | |
216 scoped_refptr<ObserverList> sync_status_observers_; | |
217 | |
218 DISALLOW_COPY_AND_ASSIGN(CannedSyncableFileSystem); | |
219 }; | |
220 | |
221 } // namespace sync_file_system | |
222 | |
223 #endif // WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_ | |
OLD | NEW |