OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ |
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
17 #include "base/timer.h" | 17 #include "base/timer.h" |
18 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
19 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
| 20 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" |
20 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 21 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
21 #include "chrome/browser/prefs/pref_change_registrar.h" | 22 #include "chrome/browser/prefs/pref_change_registrar.h" |
22 #include "content/public/browser/notification_observer.h" | 23 #include "content/public/browser/notification_observer.h" |
23 | 24 |
24 namespace gdata { | 25 namespace gdata { |
25 | 26 |
26 class DocumentsServiceInterface; | 27 class DocumentsServiceInterface; |
27 class DriveWebAppsRegistryInterface; | 28 class DriveWebAppsRegistryInterface; |
28 class GDataFileProto; | 29 class GDataFileProto; |
29 struct UploadFileInfo; | 30 struct UploadFileInfo; |
30 | 31 |
31 namespace { | 32 namespace { |
32 struct LoadRootFeedParams; | 33 struct LoadRootFeedParams; |
33 } // namespace | 34 } // namespace |
34 | 35 |
35 // Information about search result returned by Search Async callback. | |
36 // This is data needed to create a file system entry that will be used by file | |
37 // browser. | |
38 struct SearchResultInfo { | |
39 SearchResultInfo(const FilePath& in_path, bool in_is_directory) | |
40 : path(in_path), | |
41 is_directory(in_is_directory) { | |
42 } | |
43 | |
44 FilePath path; | |
45 bool is_directory; | |
46 }; | |
47 | |
48 // Used for file operations like removing files. | |
49 typedef base::Callback<void(base::PlatformFileError error)> | |
50 FileOperationCallback; | |
51 | |
52 // Used to get files from the file system. | |
53 typedef base::Callback<void(base::PlatformFileError error, | |
54 const FilePath& file_path, | |
55 const std::string& mime_type, | |
56 GDataFileType file_type)> | |
57 GetFileCallback; | |
58 | |
59 // Used to get file info from the file system. | |
60 // If |error| is not PLATFORM_FILE_OK, |file_info| is set to NULL. | |
61 typedef base::Callback<void(base::PlatformFileError error, | |
62 scoped_ptr<GDataFileProto> file_proto)> | |
63 GetFileInfoCallback; | |
64 | |
65 // Used to get file info from the file system, with the gdata file path. | |
66 // If |error| is not PLATFORM_FILE_OK, |file_info| is set to NULL. | |
67 // | |
68 // |gdata_file_path| parameter is provided as GDataFileProto does not contain | |
69 // the gdata file path (i.e. only contains the base name without parent | |
70 // directory names). | |
71 typedef base::Callback<void(base::PlatformFileError error, | |
72 const FilePath& gdata_file_path, | |
73 scoped_ptr<GDataFileProto> file_proto)> | |
74 GetFileInfoWithFilePathCallback; | |
75 | |
76 // Used to get entry info from the file system. | |
77 // If |error| is not PLATFORM_FILE_OK, |entry_info| is set to NULL. | |
78 typedef base::Callback<void(base::PlatformFileError error, | |
79 const FilePath& entry_path, | |
80 scoped_ptr<GDataEntryProto> entry_proto)> | |
81 GetEntryInfoCallback; | |
82 | |
83 // Used to read a directory from the file system. | |
84 // If |error| is not PLATFORM_FILE_OK, |directory_info| is set to NULL. | |
85 typedef base::Callback<void(base::PlatformFileError error, | |
86 bool hide_hosted_documents, | |
87 scoped_ptr<GDataDirectoryProto> directory_proto)> | |
88 ReadDirectoryCallback; | |
89 | |
90 // Used to get drive content search results. | |
91 // If |error| is not PLATFORM_FILE_OK, |result_paths| is empty. | |
92 typedef base::Callback<void( | |
93 base::PlatformFileError error, | |
94 scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback; | |
95 | |
96 // Used to open files from the file system. |file_path| is the path on the local | |
97 // file system for the opened file. | |
98 typedef base::Callback<void(base::PlatformFileError error, | |
99 const FilePath& file_path)> | |
100 OpenFileCallback; | |
101 | |
102 // Used for file operations like removing files. | |
103 typedef base::Callback<void(base::PlatformFileError error, | |
104 base::ListValue* feed_list)> | |
105 GetJsonDocumentCallback; | |
106 | |
107 // Used to get available space for the account from GData. | |
108 typedef base::Callback<void(base::PlatformFileError error, | |
109 int64 bytes_total, | |
110 int64 bytes_used)> | |
111 GetAvailableSpaceCallback; | |
112 | |
113 // Used by GDataFileSystem::GetDocumentResourceIdOnBlockingPool to return | |
114 // the resource ID read from a document JSON file on the local file system. | |
115 typedef base::Callback<void(const std::string& resource_id)> | |
116 GetDocumentResourceIdCallback; | |
117 | |
118 // GData file system abstraction layer. | |
119 // The interface is defined to make GDataFileSystem mockable. | |
120 class GDataFileSystemInterface { | |
121 public: | |
122 virtual ~GDataFileSystemInterface() {} | |
123 | |
124 // Used to notify events on the file system. | |
125 // All events are notified on UI thread. | |
126 class Observer { | |
127 public: | |
128 // Triggered when a content of a directory has been changed. | |
129 // |directory_path| is a virtual directory path (/gdata/...) representing | |
130 // changed directory. | |
131 virtual void OnDirectoryChanged(const FilePath& directory_path) {} | |
132 | |
133 // Triggered when the file system is initially loaded. | |
134 virtual void OnInitialLoadFinished() {} | |
135 | |
136 // Triggered when a document feed is fetched. |num_accumulated_entries| | |
137 // tells the number of entries fetched so far. | |
138 virtual void OnDocumentFeedFetched(int num_accumulated_entries) {} | |
139 | |
140 // Triggered when the feed from the server is loaded. | |
141 virtual void OnFeedFromServerLoaded() {} | |
142 | |
143 protected: | |
144 virtual ~Observer() {} | |
145 }; | |
146 | |
147 // Initializes the object. This function should be called before any | |
148 // other functions. | |
149 virtual void Initialize() = 0; | |
150 | |
151 // Adds and removes the observer. | |
152 virtual void AddObserver(Observer* observer) = 0; | |
153 virtual void RemoveObserver(Observer* observer) = 0; | |
154 | |
155 // Starts and stops periodic updates. | |
156 virtual void StartUpdates() = 0; | |
157 virtual void StopUpdates() = 0; | |
158 | |
159 // Checks for updates on the server. | |
160 virtual void CheckForUpdates() = 0; | |
161 | |
162 // Finds a file (not directory) by using |resource_id|. This call does not | |
163 // initiate content refreshing. | |
164 // | |
165 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
166 virtual void GetFileInfoByResourceId( | |
167 const std::string& resource_id, | |
168 const GetFileInfoWithFilePathCallback& callback) = 0; | |
169 | |
170 // Initiates transfer of |remote_src_file_path| to |local_dest_file_path|. | |
171 // |remote_src_file_path| is the virtual source path on the gdata file system. | |
172 // |local_dest_file_path| is the destination path on the local file system. | |
173 // | |
174 // Must be called from *UI* thread. |callback| is run on the calling thread. | |
175 virtual void TransferFileFromRemoteToLocal( | |
176 const FilePath& remote_src_file_path, | |
177 const FilePath& local_dest_file_path, | |
178 const FileOperationCallback& callback) = 0; | |
179 | |
180 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|. | |
181 // |local_src_file_path| must be a file from the local file system. | |
182 // |remote_dest_file_path| is the virtual destination path within gdata file | |
183 // system. | |
184 // | |
185 // Must be called from *UI* thread. |callback| is run on the calling thread. | |
186 virtual void TransferFileFromLocalToRemote( | |
187 const FilePath& local_src_file_path, | |
188 const FilePath& remote_dest_file_path, | |
189 const FileOperationCallback& callback) = 0; | |
190 | |
191 // Retrieves a file at the virtual path |file_path| on the gdata file system | |
192 // onto the cache, and mark it dirty. The local path to the cache file is | |
193 // returned to |callback|. After opening the file, both read and write | |
194 // on the file can be done with normal local file operations. | |
195 // | |
196 // |CloseFile| must be called when the modification to the cache is done. | |
197 // Otherwise, GData file system does not pick up the file for uploading. | |
198 // | |
199 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
200 virtual void OpenFile(const FilePath& file_path, | |
201 const OpenFileCallback& callback) = 0; | |
202 | |
203 // Closes a file at the virtual path |file_path| on the gdata file system, | |
204 // which is opened via OpenFile(). It commits the dirty flag on the cache. | |
205 // | |
206 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
207 virtual void CloseFile(const FilePath& file_path, | |
208 const FileOperationCallback& callback) = 0; | |
209 | |
210 // Copies |src_file_path| to |dest_file_path| on the file system. | |
211 // |src_file_path| can be a hosted document (see limitations below). | |
212 // |dest_file_path| is expected to be of the same type of |src_file_path| | |
213 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as | |
214 // a file). | |
215 // | |
216 // This method also has the following assumptions/limitations that may be | |
217 // relaxed or addressed later: | |
218 // - |src_file_path| cannot be a regular file (i.e. non-hosted document) | |
219 // or a directory. | |
220 // - |dest_file_path| must not exist. | |
221 // - The parent of |dest_file_path| must already exist. | |
222 // | |
223 // The file entries represented by |src_file_path| and the parent directory | |
224 // of |dest_file_path| need to be present in the in-memory representation | |
225 // of the file system. | |
226 // | |
227 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
228 virtual void Copy(const FilePath& src_file_path, | |
229 const FilePath& dest_file_path, | |
230 const FileOperationCallback& callback) = 0; | |
231 | |
232 // Moves |src_file_path| to |dest_file_path| on the file system. | |
233 // |src_file_path| can be a file (regular or hosted document) or a directory. | |
234 // |dest_file_path| is expected to be of the same type of |src_file_path| | |
235 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as | |
236 // a file). | |
237 // | |
238 // This method also has the following assumptions/limitations that may be | |
239 // relaxed or addressed later: | |
240 // - |dest_file_path| must not exist. | |
241 // - The parent of |dest_file_path| must already exist. | |
242 // | |
243 // The file entries represented by |src_file_path| and the parent directory | |
244 // of |dest_file_path| need to be present in the in-memory representation | |
245 // of the file system. | |
246 // | |
247 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
248 virtual void Move(const FilePath& src_file_path, | |
249 const FilePath& dest_file_path, | |
250 const FileOperationCallback& callback) = 0; | |
251 | |
252 // Removes |file_path| from the file system. If |is_recursive| is set and | |
253 // |file_path| represents a directory, we will also delete all of its | |
254 // contained children elements. The file entry represented by |file_path| | |
255 // needs to be present in in-memory representation of the file system that | |
256 // in order to be removed. | |
257 // | |
258 // TODO(zelidrag): Wire |is_recursive| through gdata api | |
259 // (find appropriate calls for it). | |
260 // | |
261 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
262 virtual void Remove(const FilePath& file_path, | |
263 bool is_recursive, | |
264 const FileOperationCallback& callback) = 0; | |
265 | |
266 // Creates new directory under |directory_path|. If |is_exclusive| is true, | |
267 // an error is raised in case a directory is already present at the | |
268 // |directory_path|. If |is_recursive| is true, the call creates parent | |
269 // directories as needed just like mkdir -p does. | |
270 // | |
271 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
272 virtual void CreateDirectory(const FilePath& directory_path, | |
273 bool is_exclusive, | |
274 bool is_recursive, | |
275 const FileOperationCallback& callback) = 0; | |
276 | |
277 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an | |
278 // error is raised when a file already exists at the path. It is | |
279 // an error if a directory or a hosted document is already present at the | |
280 // path, or the parent directory of the path is not present yet. | |
281 // | |
282 // Can be called from UI/IO thread. |callback| is run on the calling thread | |
283 virtual void CreateFile(const FilePath& file_path, | |
284 bool is_exclusive, | |
285 const FileOperationCallback& callback) = 0; | |
286 | |
287 // Gets |file_path| from the file system. The file entry represented by | |
288 // |file_path| needs to be present in in-memory representation of the file | |
289 // system in order to be retrieved. If the file is not cached, the file | |
290 // will be downloaded through gdata api. | |
291 // | |
292 // Can be called from UI/IO thread. |get_file_callback| and | |
293 // |get_download_data| are run on the calling thread. | |
294 virtual void GetFileByPath( | |
295 const FilePath& file_path, | |
296 const GetFileCallback& get_file_callback, | |
297 const GetDownloadDataCallback& get_download_data_callback) = 0; | |
298 | |
299 // Gets a file by the given |resource_id| from the gdata server. Used for | |
300 // fetching pinned-but-not-fetched files. | |
301 // | |
302 // Can be called from UI/IO thread. |get_file_callback| and | |
303 // |get_download_data_callback| are run on the calling thread. | |
304 virtual void GetFileByResourceId( | |
305 const std::string& resource_id, | |
306 const GetFileCallback& get_file_callback, | |
307 const GetDownloadDataCallback& get_download_data_callback) = 0; | |
308 | |
309 // Updates a file by the given |resource_id| on the gdata server by | |
310 // uploading an updated version. Used for uploading dirty files. The file | |
311 // should already be present in the cache. | |
312 // | |
313 // TODO(satorux): As of now, the function only handles files with the dirty | |
314 // bit committed. We should eliminate the restriction. crbug.com/134558. | |
315 // | |
316 // Can be called from UI/IO thread. |callback| and is run on the calling | |
317 // thread. | |
318 virtual void UpdateFileByResourceId( | |
319 const std::string& resource_id, | |
320 const FileOperationCallback& callback) = 0; | |
321 | |
322 // Finds an entry (a file or a directory) by |file_path|. This call will also | |
323 // retrieve and refresh file system content from server and disk cache. | |
324 // | |
325 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
326 virtual void GetEntryInfoByPath(const FilePath& file_path, | |
327 const GetEntryInfoCallback& callback) = 0; | |
328 | |
329 // Finds a file (not a directory) by |file_path|. This call will also | |
330 // retrieve and refresh file system content from server and disk cache. | |
331 // | |
332 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
333 virtual void GetFileInfoByPath(const FilePath& file_path, | |
334 const GetFileInfoCallback& callback) = 0; | |
335 | |
336 // Finds and reads a directory by |file_path|. This call will also retrieve | |
337 // and refresh file system content from server and disk cache. | |
338 // | |
339 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
340 virtual void ReadDirectoryByPath(const FilePath& file_path, | |
341 const ReadDirectoryCallback& callback) = 0; | |
342 | |
343 // Requests a refresh of the directory pointed by |file_path| (i.e. fetches | |
344 // the latest metadata of files in the target directory). | |
345 // | |
346 // In particular, this function is used to get the latest thumbnail | |
347 // URLs. Thumbnail URLs change periodically even if contents of files are | |
348 // not changed, hence we should get the new thumbnail URLs manually if we | |
349 // detect that the existing thumnail URLs are stale. | |
350 // | |
351 // Upon success, the metadata of files in the target directory is updated, | |
352 // and the change is notified via Observer::OnDirectoryChanged(). Note that | |
353 // this function ignores changes in directories in the target | |
354 // directory. Changes in directories are handled via the delta feeds. | |
355 // | |
356 // Can be called from UI/IO thread. | |
357 virtual void RequestDirectoryRefresh(const FilePath& file_path) = 0; | |
358 | |
359 // Does server side content search for |search_query|. | |
360 // Search results will be returned as a list of results' |SearchResultInfo| | |
361 // structs, which contains file's path and is_directory flag. | |
362 // | |
363 // Can be called from UI/IO thread. |callback| is run on the calling thread. | |
364 virtual void Search(const std::string& search_query, | |
365 const SearchCallback& callback) = 0; | |
366 | |
367 // Fetches the user's Account Metadata to find out current quota information | |
368 // and returns it to the callback. | |
369 virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0; | |
370 | |
371 // Adds a file entry from |entry| under |virtual_dir_path|, and modifies | |
372 // the cache state. | |
373 // | |
374 // When uploading a new file, adds a new file entry, and store its content | |
375 // from |file_content_path| into the cache. | |
376 // | |
377 // When uploading an existing file, replaces the file entry with a new one, | |
378 // and clears the dirty bit in the cache. | |
379 // | |
380 // |callback| will be called on the UI thread upon completion of operation. | |
381 virtual void AddUploadedFile(UploadMode upload_mode, | |
382 const FilePath& virtual_dir_path, | |
383 scoped_ptr<DocumentEntry> entry, | |
384 const FilePath& file_content_path, | |
385 GDataCache::FileOperationType cache_operation, | |
386 const base::Closure& callback) = 0; | |
387 }; | |
388 | |
389 // The production implementation of GDataFileSystemInterface. | 36 // The production implementation of GDataFileSystemInterface. |
390 class GDataFileSystem : public GDataFileSystemInterface, | 37 class GDataFileSystem : public GDataFileSystemInterface, |
391 public content::NotificationObserver { | 38 public content::NotificationObserver { |
392 public: | 39 public: |
393 GDataFileSystem( | 40 GDataFileSystem( |
394 Profile* profile, | 41 Profile* profile, |
395 GDataCache* cache, | 42 GDataCache* cache, |
396 DocumentsServiceInterface* documents_service, | 43 DocumentsServiceInterface* documents_service, |
397 GDataUploaderInterface* uploader, | 44 GDataUploaderInterface* uploader, |
398 DriveWebAppsRegistryInterface* webapps_registry, | 45 DriveWebAppsRegistryInterface* webapps_registry, |
(...skipping 869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 | 915 |
1269 ObserverList<Observer> observers_; | 916 ObserverList<Observer> observers_; |
1270 | 917 |
1271 // The token is used to post tasks to the blocking pool in sequence. | 918 // The token is used to post tasks to the blocking pool in sequence. |
1272 const base::SequencedWorkerPool::SequenceToken sequence_token_; | 919 const base::SequencedWorkerPool::SequenceToken sequence_token_; |
1273 }; | 920 }; |
1274 | 921 |
1275 } // namespace gdata | 922 } // namespace gdata |
1276 | 923 |
1277 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ | 924 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ |
OLD | NEW |