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,
38 const std::string& in_highlighted_base_name)
40 is_directory(is_directory),
41 highlighted_base_name(in_highlighted_base_name) {}
43 // The two members are used to create FileEntry object.
47 // The base name to be displayed in the UI. The parts matched the search
48 // query are highlighted with <b> tag. Meta characters are escaped like <
50 // Why HTML? we could instead provide matched ranges using pairs of
51 // integers, but this is fragile as we'll eventually converting strings
52 // from UTF-8 (StringValue in base/values.h uses std::string) to UTF-16
53 // when sending strings from C++ to JavaScript.
55 // Why <b> instead of <strong>? Because <b> is shorter.
56 std::string highlighted_base_name;
59 typedef std::vector<MetadataSearchResult> MetadataSearchResultVector;
61 // Used to get a resource entry from the file system.
62 // If |error| is not FILE_ERROR_OK, |entry_info| is set to NULL.
63 typedef base::Callback<void(FileError error,
64 scoped_ptr<ResourceEntry> entry)>
65 GetResourceEntryCallback;
67 // Used to get files from the file system.
68 typedef base::Callback<void(FileError error,
69 const base::FilePath& file_path,
70 scoped_ptr<ResourceEntry> entry)> GetFileCallback;
72 // Used to get file content from the file system.
73 // If the file content is available in local cache, |local_file| is filled with
74 // the path to the cache file. If the file content starts to be downloaded from
75 // the server, |local_file| is empty.
76 typedef base::Callback<void(FileError error,
77 const base::FilePath& local_file,
78 scoped_ptr<ResourceEntry> entry)>
79 GetFileContentInitializedCallback;
81 // Used to get list of entries under a directory.
82 typedef base::Callback<void(scoped_ptr<ResourceEntryVector> entries)>
83 ReadDirectoryEntriesCallback;
85 // Used to get drive content search results.
86 // If |error| is not FILE_ERROR_OK, |result_paths| is empty.
87 typedef base::Callback<void(
89 const GURL& next_link,
90 scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback;
92 // Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and
93 // |result| contains the search result.
94 typedef base::Callback<void(
96 scoped_ptr<MetadataSearchResultVector> result)> SearchMetadataCallback;
98 // Used to open files from the file system. |file_path| is the path on the local
99 // file system for the opened file.
100 // If |close_callback| is not null, it must be called when the
101 // modification to the cache is done. Otherwise, Drive file system does not
102 // pick up the file for uploading.
103 // |close_callback| must not be called more than once.
104 typedef base::Callback<void(FileError error,
105 const base::FilePath& file_path,
106 const base::Closure& close_callback)>
109 // Used to get available space for the account from Drive.
110 typedef base::Callback<void(FileError error,
112 int64 bytes_used)> GetAvailableSpaceCallback;
114 // Used to get the url to the sharing dialog.
115 typedef base::Callback<void(FileError error,
116 const GURL& share_url)> GetShareUrlCallback;
118 // Used to get filesystem metadata.
119 typedef base::Callback<void(const FileSystemMetadata&)>
120 GetFilesystemMetadataCallback;
122 // Used to mark cached files mounted.
123 typedef base::Callback<void(FileError error,
124 const base::FilePath& file_path)>
127 // Callback for GetCacheEntry.
128 // |success| indicates if the operation was successful.
129 // |cache_entry| is the obtained cache entry.
130 typedef base::Callback<void(bool success, const FileCacheEntry& cache_entry)>
131 GetCacheEntryCallback;
133 // Used to get file path.
134 typedef base::Callback<void(FileError error, const base::FilePath& file_path)>
137 // The mode of opening a file.
139 // Open the file if exists. If not, failed.
142 // Create a new file if not exists, and then open it. If exists, failed.
145 // Open the file if exists. If not, create a new file and then open it.
149 // Option enum to control eligible entries for SearchMetadata().
150 // SEARCH_METADATA_ALL is the default to investigate all the entries.
151 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS excludes the hosted documents.
152 // SEARCH_METADATA_EXCLUDE_DIRECTORIES excludes the directories from the result.
153 // SEARCH_METADATA_SHARED_WITH_ME targets only "shared-with-me" entries.
154 // SEARCH_METADATA_OFFLINE targets only "offline" entries. This option can not
155 // be used with other options.
156 enum SearchMetadataOptions {
157 SEARCH_METADATA_ALL = 0,
158 SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS = 1,
159 SEARCH_METADATA_EXCLUDE_DIRECTORIES = 1 << 1,
160 SEARCH_METADATA_SHARED_WITH_ME = 1 << 2,
161 SEARCH_METADATA_OFFLINE = 1 << 3,
164 // Drive file system abstraction layer.
165 // The interface is defined to make FileSystem mockable.
166 class FileSystemInterface {
168 virtual ~FileSystemInterface() {}
170 // Adds and removes the observer.
171 virtual void AddObserver(FileSystemObserver* observer) = 0;
172 virtual void RemoveObserver(FileSystemObserver* observer) = 0;
174 // Checks for updates on the server.
175 virtual void CheckForUpdates() = 0;
177 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
178 // |local_src_file_path| must be a file from the local file system.
179 // |remote_dest_file_path| is the virtual destination path within Drive file
182 // |callback| must not be null.
183 virtual void TransferFileFromLocalToRemote(
184 const base::FilePath& local_src_file_path,
185 const base::FilePath& remote_dest_file_path,
186 const FileOperationCallback& callback) = 0;
188 // Retrieves a file at the virtual path |file_path| on the Drive file system
189 // onto the cache, and mark it dirty. The local path to the cache file is
190 // returned to |callback|. After opening the file, both read and write
191 // on the file can be done with normal local file operations.
192 // If |mime_type| is set and the file is newly created, the mime type is
193 // set to the specified value. If |mime_type| is empty, it is guessed from
196 // |callback| must not be null.
197 virtual void OpenFile(const base::FilePath& file_path,
199 const std::string& mime_type,
200 const OpenFileCallback& callback) = 0;
202 // Copies |src_file_path| to |dest_file_path| on the file system.
203 // |src_file_path| can be a hosted document (see limitations below).
204 // |dest_file_path| is expected to be of the same type of |src_file_path|
205 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
207 // If |preserve_last_modified| is set to true, the last modified time will be
208 // preserved. This feature is only supported on Drive API v2 protocol because
209 // GData WAPI doesn't support updating modification time.
211 // This method also has the following assumptions/limitations that may be
212 // relaxed or addressed later:
213 // - |src_file_path| cannot be a regular file (i.e. non-hosted document)
215 // - |dest_file_path| must not exist.
216 // - The parent of |dest_file_path| must already exist.
218 // The file entries represented by |src_file_path| and the parent directory
219 // of |dest_file_path| need to be present in the in-memory representation
220 // of the file system.
222 // |callback| must not be null.
223 virtual void Copy(const base::FilePath& src_file_path,
224 const base::FilePath& dest_file_path,
225 bool preserve_last_modified,
226 const FileOperationCallback& callback) = 0;
228 // Moves |src_file_path| to |dest_file_path| on the file system.
229 // |src_file_path| can be a file (regular or hosted document) or a directory.
230 // |dest_file_path| is expected to be of the same type of |src_file_path|
231 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
234 // This method also has the following assumptions/limitations that may be
235 // relaxed or addressed later:
236 // - |dest_file_path| must not exist.
237 // - The parent of |dest_file_path| must already exist.
239 // The file entries represented by |src_file_path| and the parent directory
240 // of |dest_file_path| need to be present in the in-memory representation
241 // of the file system.
243 // |callback| must not be null.
244 virtual void Move(const base::FilePath& src_file_path,
245 const base::FilePath& dest_file_path,
246 const FileOperationCallback& callback) = 0;
248 // Removes |file_path| from the file system. If |is_recursive| is set and
249 // |file_path| represents a directory, we will also delete all of its
250 // contained children elements. The file entry represented by |file_path|
251 // needs to be present in in-memory representation of the file system that
252 // in order to be removed.
254 // |callback| must not be null.
255 virtual void Remove(const base::FilePath& file_path,
257 const FileOperationCallback& callback) = 0;
259 // Creates new directory under |directory_path|. If |is_exclusive| is true,
260 // an error is raised in case a directory is already present at the
261 // |directory_path|. If |is_recursive| is true, the call creates parent
262 // directories as needed just like mkdir -p does.
264 // |callback| must not be null.
265 virtual void CreateDirectory(const base::FilePath& directory_path,
268 const FileOperationCallback& callback) = 0;
270 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an
271 // error is raised when a file already exists at the path. It is
272 // an error if a directory or a hosted document is already present at the
273 // path, or the parent directory of the path is not present yet.
274 // If |mime_type| is set and the file is newly created, the mime type is
275 // set to the specified value. If |mime_type| is empty, it is guessed from
278 // |callback| must not be null.
279 virtual void CreateFile(const base::FilePath& file_path,
281 const std::string& mime_type,
282 const FileOperationCallback& callback) = 0;
284 // Touches the file at |file_path| by updating the timestamp to
285 // |last_access_time| and |last_modified_time|.
286 // Upon completion, invokes |callback|.
287 // Note that, differently from unix touch command, this doesn't create a file
288 // if the target file doesn't exist.
290 // |last_access_time|, |last_modified_time| and |callback| must not be null.
291 virtual void TouchFile(const base::FilePath& file_path,
292 const base::Time& last_access_time,
293 const base::Time& last_modified_time,
294 const FileOperationCallback& callback) = 0;
296 // Truncates the file content at |file_path| to the |length|.
298 // |callback| must not be null.
299 virtual void TruncateFile(const base::FilePath& file_path,
301 const FileOperationCallback& callback) = 0;
303 // Pins a file at |file_path|.
305 // |callback| must not be null.
306 virtual void Pin(const base::FilePath& file_path,
307 const FileOperationCallback& callback) = 0;
309 // Unpins a file at |file_path|.
311 // |callback| must not be null.
312 virtual void Unpin(const base::FilePath& file_path,
313 const FileOperationCallback& callback) = 0;
315 // Makes sure that |file_path| in the file system is available in the local
316 // cache. If the file is not cached, the file will be downloaded. The entry
317 // needs to be present in the file system.
319 // Returns the cache path and entry info to |callback|. It must not be null.
320 virtual void GetFile(const base::FilePath& file_path,
321 const GetFileCallback& callback) = 0;
323 // Makes sure that |file_path| in the file system is available in the local
324 // cache, and mark it as dirty. The next modification to the cache file is
325 // watched and is automatically uploaded to the server. If the entry is not
326 // present in the file system, it is created.
328 // Returns the cache path and entry info to |callback|. It must not be null.
329 virtual void GetFileForSaving(const base::FilePath& file_path,
330 const GetFileCallback& callback) = 0;
332 // Gets a file by the given |file_path| and returns a closure to cancel the
334 // Calls |initialized_callback| when either:
335 // 1) The cached file (or JSON file for hosted file) is found, or
336 // 2) Starting to download the file from drive server.
337 // In case of 2), the given FilePath is empty, and |get_content_callback| is
338 // called repeatedly with downloaded content following the
339 // |initialized_callback| invocation.
340 // |completion_callback| is invoked if an error is found, or the operation
341 // is successfully done.
342 // |initialized_callback|, |get_content_callback| and |completion_callback|
344 virtual base::Closure GetFileContent(
345 const base::FilePath& file_path,
346 const GetFileContentInitializedCallback& initialized_callback,
347 const google_apis::GetContentCallback& get_content_callback,
348 const FileOperationCallback& completion_callback) = 0;
350 // Finds an entry (a file or a directory) by |file_path|. This call will also
351 // retrieve and refresh file system content from server and disk cache.
353 // |callback| must not be null.
354 virtual void GetResourceEntry(const base::FilePath& file_path,
355 const GetResourceEntryCallback& callback) = 0;
357 // Finds and reads a directory by |file_path|. This call will also retrieve
358 // and refresh file system content from server and disk cache.
359 // |entries_callback| can be a null callback when not interested in entries.
361 // |completion_callback| must not be null.
362 virtual void ReadDirectory(
363 const base::FilePath& file_path,
364 const ReadDirectoryEntriesCallback& entries_callback,
365 const FileOperationCallback& completion_callback) = 0;
367 // Does server side content search for |search_query|.
368 // If |next_link| is set, this is the search result url that will be
369 // fetched. Search results will be returned as a list of results'
370 // |SearchResultInfo| structs, which contains file's path and is_directory
373 // |callback| must not be null.
374 virtual void Search(const std::string& search_query,
375 const GURL& next_link,
376 const SearchCallback& callback) = 0;
378 // Searches the local resource metadata, and returns the entries
379 // |at_most_num_matches| that contain |query| in their base names. Search is
380 // done in a case-insensitive fashion. The eligible entries are selected based
381 // on the given |options|, which is a bit-wise OR of SearchMetadataOptions.
382 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS will be automatically added based
383 // on the preference. |callback| must not be null. Must be called on UI
384 // thread. Empty |query| matches any base name. i.e. returns everything.
385 virtual void SearchMetadata(const std::string& query,
387 int at_most_num_matches,
388 const SearchMetadataCallback& callback) = 0;
390 // Fetches the user's Account Metadata to find out current quota information
391 // and returns it to the callback.
392 virtual void GetAvailableSpace(const GetAvailableSpaceCallback& callback) = 0;
394 // Fetches the url to the sharing dialog to be embedded in |embed_origin|,
395 // for the specified file or directory. |callback| must not be null.
396 virtual void GetShareUrl(
397 const base::FilePath& file_path,
398 const GURL& embed_origin,
399 const GetShareUrlCallback& callback) = 0;
401 // Returns miscellaneous metadata of the file system like the largest
402 // timestamp. Used in chrome:drive-internals. |callback| must not be null.
403 virtual void GetMetadata(
404 const GetFilesystemMetadataCallback& callback) = 0;
406 // Marks the cached file as mounted, and runs |callback| upon completion.
407 // If succeeded, the cached file path will be passed to the |callback|.
408 // |callback| must not be null.
409 virtual void MarkCacheFileAsMounted(const base::FilePath& drive_file_path,
410 const MarkMountedCallback& callback) = 0;
412 // Marks the cached file as unmounted, and runs |callback| upon completion.
413 // Note that this method expects that the |cached_file_path| is the path
414 // returned by MarkCacheFileAsMounted().
415 // |callback| must not be null.
416 virtual void MarkCacheFileAsUnmounted(
417 const base::FilePath& cache_file_path,
418 const FileOperationCallback& callback) = 0;
420 // Gets the cache entry for file corresponding to |drive_file_path| and runs
421 // |callback| with true and the found entry if the entry exists in the cache
422 // map. Otherwise, runs |callback| with false.
423 // |callback| must not be null.
424 virtual void GetCacheEntry(const base::FilePath& drive_file_path,
425 const GetCacheEntryCallback& callback) = 0;
427 // Adds permission as |role| to |email| for the entry at |drive_file_path|.
428 // |callback| must not be null.
429 virtual void AddPermission(const base::FilePath& drive_file_path,
430 const std::string& email,
431 google_apis::drive::PermissionRole role,
432 const FileOperationCallback& callback) = 0;
434 // Resets local data.
435 virtual void Reset(const FileOperationCallback& callback) = 0;
437 // Finds a path of an entry (a file or a directory) by |resource_id|.
438 virtual void GetPathFromResourceId(const std::string& resource_id,
439 const GetFilePathCallback& callback) = 0;
444 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_