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 CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/chromeos/drive/drive.pb.h"
13 #include "chrome/browser/chromeos/drive/file_system_metadata.h"
14 #include "chrome/browser/chromeos/drive/resource_metadata.h"
15 #include "google_apis/drive/base_requests.h"
19 class FileSystemObserver;
21 // Information about search result returned by Search Async callback.
22 // This is data needed to create a file system entry that will be used by file
24 struct SearchResultInfo {
25 SearchResultInfo(const base::FilePath& path, bool is_directory)
27 is_directory(is_directory) {
34 // Struct to represent a search result for SearchMetadata().
35 struct MetadataSearchResult {
36 MetadataSearchResult(const base::FilePath& in_path,
37 const ResourceEntry& in_entry,
38 const std::string& in_highlighted_base_name)
41 highlighted_base_name(in_highlighted_base_name) {
44 // The two members are used to create FileEntry object.
48 // The base name to be displayed in the UI. The parts matched the search
49 // query are highlighted with <b> tag. Meta characters are escaped like <
51 // Why HTML? we could instead provide matched ranges using pairs of
52 // integers, but this is fragile as we'll eventually converting strings
53 // from UTF-8 (StringValue in base/values.h uses std::string) to UTF-16
54 // when sending strings from C++ to JavaScript.
56 // Why <b> instead of <strong>? Because <b> is shorter.
57 std::string highlighted_base_name;
60 typedef std::vector<MetadataSearchResult> MetadataSearchResultVector;
62 // Used to get files from the file system.
63 typedef base::Callback<void(FileError error,
64 const base::FilePath& file_path,
65 scoped_ptr<ResourceEntry> entry)> GetFileCallback;
67 // Used to get file content from the file system.
68 // If the file content is available in local cache, |local_file| is filled with
69 // the path to the cache file and |cancel_download_closure| is null. If the file
70 // content starts to be downloaded from the server, |local_file| is empty and
71 // |cancel_download_closure| is filled with a closure by calling which the
72 // download job can be cancelled.
73 // |cancel_download_closure| must be called on the UI thread.
74 typedef base::Callback<void(FileError error,
75 scoped_ptr<ResourceEntry> entry,
76 const base::FilePath& local_file,
77 const base::Closure& cancel_download_closure)>
78 GetFileContentInitializedCallback;
80 // Used to get list of entries under a directory.
81 // If |error| is not FILE_ERROR_OK, |entries| is null.
82 typedef base::Callback<void(FileError error,
83 scoped_ptr<ResourceEntryVector> entries,
85 ReadDirectoryCallback;
87 // Used to get drive content search results.
88 // If |error| is not FILE_ERROR_OK, |result_paths| is empty.
89 typedef base::Callback<void(
91 const GURL& next_link,
92 scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback;
94 // Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and
95 // |result| contains the search result.
96 typedef base::Callback<void(
98 scoped_ptr<MetadataSearchResultVector> result)> SearchMetadataCallback;
100 // Used to open files from the file system. |file_path| is the path on the local
101 // file system for the opened file.
102 // If |close_callback| is not null, it must be called when the
103 // modification to the cache is done. Otherwise, Drive file system does not
104 // pick up the file for uploading.
105 // |close_callback| must not be called more than once.
106 typedef base::Callback<void(FileError error,
107 const base::FilePath& file_path,
108 const base::Closure& close_callback)>
111 // Used to get available space for the account from Drive.
112 typedef base::Callback<void(FileError error,
114 int64 bytes_used)> GetAvailableSpaceCallback;
116 // Used to get the url to the sharing dialog.
117 typedef base::Callback<void(FileError error,
118 const GURL& share_url)> GetShareUrlCallback;
120 // Used to get filesystem metadata.
121 typedef base::Callback<void(const FileSystemMetadata&)>
122 GetFilesystemMetadataCallback;
124 // Used to mark cached files mounted.
125 typedef base::Callback<void(FileError error,
126 const base::FilePath& file_path)>
129 // Callback for GetCacheEntry.
130 // |success| indicates if the operation was successful.
131 // |cache_entry| is the obtained cache entry.
132 typedef base::Callback<void(bool success, const FileCacheEntry& cache_entry)>
133 GetCacheEntryCallback;
135 // The mode of opening a file.
137 // Open the file if exists. If not, failed.
140 // Create a new file if not exists, and then open it. If exists, failed.
143 // Open the file if exists. If not, create a new file and then open it.
147 // Priority of a job. Higher values are lower priority.
151 // Indicates the number of values of this enum.
155 struct ClientContext {
156 explicit ClientContext(ContextType in_type) : type(in_type) {}
160 // Option enum to control eligible entries for SearchMetadata().
161 // SEARCH_METADATA_ALL is the default to investigate all the entries.
162 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS excludes the hosted documents.
163 // SEARCH_METADATA_EXCLUDE_DIRECTORIES excludes the directories from the result.
164 // SEARCH_METADATA_SHARED_WITH_ME targets only "shared-with-me" entries.
165 // SEARCH_METADATA_OFFLINE targets only "offline" entries. This option can not
166 // be used with other options.
167 enum SearchMetadataOptions {
168 SEARCH_METADATA_ALL = 0,
169 SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS = 1,
170 SEARCH_METADATA_EXCLUDE_DIRECTORIES = 1 << 1,
171 SEARCH_METADATA_SHARED_WITH_ME = 1 << 2,
172 SEARCH_METADATA_OFFLINE = 1 << 3,
175 // Drive file system abstraction layer.
176 // The interface is defined to make FileSystem mockable.
177 class FileSystemInterface {
179 virtual ~FileSystemInterface() {}
181 // Adds and removes the observer.
182 virtual void AddObserver(FileSystemObserver* observer) = 0;
183 virtual void RemoveObserver(FileSystemObserver* observer) = 0;
185 // Checks for updates on the server.
186 virtual void CheckForUpdates() = 0;
188 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
189 // |local_src_file_path| must be a file from the local file system.
190 // |remote_dest_file_path| is the virtual destination path within Drive file
193 // |callback| must not be null.
194 virtual void TransferFileFromLocalToRemote(
195 const base::FilePath& local_src_file_path,
196 const base::FilePath& remote_dest_file_path,
197 const FileOperationCallback& callback) = 0;
199 // Retrieves a file at the virtual path |file_path| on the Drive file system
200 // onto the cache, and mark it dirty. The local path to the cache file is
201 // returned to |callback|. After opening the file, both read and write
202 // on the file can be done with normal local file operations.
203 // If |mime_type| is set and the file is newly created, the mime type is
204 // set to the specified value. If |mime_type| is empty, it is guessed from
207 // |callback| must not be null.
208 virtual void OpenFile(const base::FilePath& file_path,
210 const std::string& mime_type,
211 const OpenFileCallback& callback) = 0;
213 // Copies |src_file_path| to |dest_file_path| on the file system.
214 // |src_file_path| can be a hosted document (see limitations below).
215 // |dest_file_path| is expected to be of the same type of |src_file_path|
216 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
218 // If |preserve_last_modified| is set to true, the last modified time will be
219 // preserved. This feature is only supported on Drive API v2 protocol because
220 // GData WAPI doesn't support updating modification time.
222 // This method also has the following assumptions/limitations that may be
223 // relaxed or addressed later:
224 // - |src_file_path| cannot be a regular file (i.e. non-hosted document)
226 // - |dest_file_path| must not exist.
227 // - The parent of |dest_file_path| must already exist.
229 // The file entries represented by |src_file_path| and the parent directory
230 // of |dest_file_path| need to be present in the in-memory representation
231 // of the file system.
233 // |callback| must not be null.
234 virtual void Copy(const base::FilePath& src_file_path,
235 const base::FilePath& dest_file_path,
236 bool preserve_last_modified,
237 const FileOperationCallback& callback) = 0;
239 // Moves |src_file_path| to |dest_file_path| on the file system.
240 // |src_file_path| can be a file (regular or hosted document) or a directory.
241 // |dest_file_path| is expected to be of the same type of |src_file_path|
242 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
244 // If |preserve_last_modified| is set to true, the last modified time will be
245 // preserved. This feature is only supported on Drive API v2 protocol because
246 // GData WAPI doesn't support updating modification time.
248 // This method also has the following assumptions/limitations that may be
249 // relaxed or addressed later:
250 // - |dest_file_path| must not exist.
251 // - The parent of |dest_file_path| must already exist.
253 // The file entries represented by |src_file_path| and the parent directory
254 // of |dest_file_path| need to be present in the in-memory representation
255 // of the file system.
257 // |callback| must not be null.
258 virtual void Move(const base::FilePath& src_file_path,
259 const base::FilePath& dest_file_path,
260 bool preserve_last_modified,
261 const FileOperationCallback& callback) = 0;
263 // Removes |file_path| from the file system. If |is_recursive| is set and
264 // |file_path| represents a directory, we will also delete all of its
265 // contained children elements. The file entry represented by |file_path|
266 // needs to be present in in-memory representation of the file system that
267 // in order to be removed.
269 // |callback| must not be null.
270 virtual void Remove(const base::FilePath& file_path,
272 const FileOperationCallback& callback) = 0;
274 // Creates new directory under |directory_path|. If |is_exclusive| is true,
275 // an error is raised in case a directory is already present at the
276 // |directory_path|. If |is_recursive| is true, the call creates parent
277 // directories as needed just like mkdir -p does.
279 // |callback| must not be null.
280 virtual void CreateDirectory(const base::FilePath& directory_path,
283 const FileOperationCallback& callback) = 0;
285 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an
286 // error is raised when a file already exists at the path. It is
287 // an error if a directory or a hosted document is already present at the
288 // path, or the parent directory of the path is not present yet.
289 // If |mime_type| is set and the file is newly created, the mime type is
290 // set to the specified value. If |mime_type| is empty, it is guessed from
293 // |callback| must not be null.
294 virtual void CreateFile(const base::FilePath& file_path,
296 const std::string& mime_type,
297 const FileOperationCallback& callback) = 0;
299 // Touches the file at |file_path| by updating the timestamp to
300 // |last_access_time| and |last_modified_time|.
301 // Upon completion, invokes |callback|.
302 // Note that, differently from unix touch command, this doesn't create a file
303 // if the target file doesn't exist.
305 // |last_access_time|, |last_modified_time| and |callback| must not be null.
306 virtual void TouchFile(const base::FilePath& file_path,
307 const base::Time& last_access_time,
308 const base::Time& last_modified_time,
309 const FileOperationCallback& callback) = 0;
311 // Truncates the file content at |file_path| to the |length|.
313 // |callback| must not be null.
314 virtual void TruncateFile(const base::FilePath& file_path,
316 const FileOperationCallback& callback) = 0;
318 // Pins a file at |file_path|.
320 // |callback| must not be null.
321 virtual void Pin(const base::FilePath& file_path,
322 const FileOperationCallback& callback) = 0;
324 // Unpins a file at |file_path|.
326 // |callback| must not be null.
327 virtual void Unpin(const base::FilePath& file_path,
328 const FileOperationCallback& callback) = 0;
330 // Makes sure that |file_path| in the file system is available in the local
331 // cache. If the file is not cached, the file will be downloaded. The entry
332 // needs to be present in the file system.
334 // Returns the cache path and entry info to |callback|. It must not be null.
335 virtual void GetFile(const base::FilePath& file_path,
336 const GetFileCallback& callback) = 0;
338 // Makes sure that |file_path| in the file system is available in the local
339 // cache, and mark it as dirty. The next modification to the cache file is
340 // watched and is automatically uploaded to the server. If the entry is not
341 // present in the file system, it is created.
343 // Returns the cache path and entry info to |callback|. It must not be null.
344 virtual void GetFileForSaving(const base::FilePath& file_path,
345 const GetFileCallback& callback) = 0;
347 // Gets a file by the given |file_path|.
348 // Calls |initialized_callback| when either:
349 // 1) The cached file (or JSON file for hosted file) is found, or
350 // 2) Starting to download the file from drive server.
351 // In case of 2), the given FilePath is empty, and |get_content_callback| is
352 // called repeatedly with downloaded content following the
353 // |initialized_callback| invocation.
354 // |completion_callback| is invoked if an error is found, or the operation
355 // is successfully done.
356 // |initialized_callback|, |get_content_callback| and |completion_callback|
358 virtual void GetFileContent(
359 const base::FilePath& file_path,
360 const GetFileContentInitializedCallback& initialized_callback,
361 const google_apis::GetContentCallback& get_content_callback,
362 const FileOperationCallback& completion_callback) = 0;
364 // Finds an entry (a file or a directory) by |file_path|. This call will also
365 // retrieve and refresh file system content from server and disk cache.
367 // |callback| must not be null.
368 virtual void GetResourceEntry(const base::FilePath& file_path,
369 const GetResourceEntryCallback& callback) = 0;
371 // Finds and reads a directory by |file_path|. This call will also retrieve
372 // and refresh file system content from server and disk cache.
374 // |callback| must not be null.
375 virtual void ReadDirectory(const base::FilePath& file_path,
376 const ReadDirectoryCallback& callback) = 0;
378 // Does server side content search for |search_query|.
379 // If |next_link| is set, this is the search result url that will be
380 // fetched. Search results will be returned as a list of results'
381 // |SearchResultInfo| structs, which contains file's path and is_directory
384 // |callback| must not be null.
385 virtual void Search(const std::string& search_query,
386 const GURL& next_link,
387 const SearchCallback& callback) = 0;
389 // Searches the local resource metadata, and returns the entries
390 // |at_most_num_matches| that contain |query| in their base names. Search is
391 // done in a case-insensitive fashion. The eligible entries are selected based
392 // on the given |options|, which is a bit-wise OR of SearchMetadataOptions.
393 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS will be automatically added based
394 // on the preference. |callback| must not be null. Must be called on UI
395 // thread. Empty |query| matches any base name. i.e. returns everything.
396 virtual void SearchMetadata(const std::string& query,
398 int at_most_num_matches,
399 const SearchMetadataCallback& callback) = 0;
401 // Fetches the user's Account Metadata to find out current quota information
402 // and returns it to the callback.
403 virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0;
405 // Fetches the url to the sharing dialog to be embedded in |embed_origin|,
406 // for the specified file or directory. |callback| must not be null.
407 virtual void GetShareUrl(
408 const base::FilePath& file_path,
409 const GURL& embed_origin,
410 const GetShareUrlCallback& callback) = 0;
412 // Returns miscellaneous metadata of the file system like the largest
413 // timestamp. Used in chrome:drive-internals. |callback| must not be null.
414 virtual void GetMetadata(
415 const GetFilesystemMetadataCallback& callback) = 0;
417 // Marks the cached file as mounted, and runs |callback| upon completion.
418 // If succeeded, the cached file path will be passed to the |callback|.
419 // |callback| must not be null.
420 virtual void MarkCacheFileAsMounted(const base::FilePath& drive_file_path,
421 const MarkMountedCallback& callback) = 0;
423 // Marks the cached file as unmounted, and runs |callback| upon completion.
424 // Note that this method expects that the |cached_file_path| is the path
425 // returned by MarkCacheFileAsMounted().
426 // |callback| must not be null.
427 virtual void MarkCacheFileAsUnmounted(
428 const base::FilePath& cache_file_path,
429 const FileOperationCallback& callback) = 0;
431 // Gets the cache entry for file corresponding to |drive_file_path| and runs
432 // |callback| with true and the found entry if the entry exists in the cache
433 // map. Otherwise, runs |callback| with false.
434 // |callback| must not be null.
435 virtual void GetCacheEntry(const base::FilePath& drive_file_path,
436 const GetCacheEntryCallback& callback) = 0;
438 // Resets local data.
439 virtual void Reset(const FileOperationCallback& callback) = 0;
444 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_