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/file_system_metadata.h"
13 #include "components/drive/drive.pb.h"
14 #include "components/drive/resource_metadata.h"
15 #include "google_apis/drive/base_requests.h"
16 #include "google_apis/drive/drive_api_requests.h"
20 class FileSystemObserver
;
22 // Information about search result returned by Search Async callback.
23 // This is data needed to create a file system entry that will be used by file
25 struct SearchResultInfo
{
26 SearchResultInfo(const base::FilePath
& path
, bool is_directory
)
27 : path(path
), is_directory(is_directory
) {}
33 // File path and its MD5 hash obtained from drive API.
34 struct HashAndFilePath
{
39 // Struct to represent a search result for SearchMetadata().
40 struct MetadataSearchResult
{
41 MetadataSearchResult(const base::FilePath
& path
,
43 const std::string
& highlighted_base_name
,
44 const std::string
& md5
);
46 // The two members are used to create FileEntry object.
50 // The base name to be displayed in the UI. The parts matched the search
51 // query are highlighted with <b> tag. Meta characters are escaped like <
53 // Why HTML? we could instead provide matched ranges using pairs of
54 // integers, but this is fragile as we'll eventually converting strings
55 // from UTF-8 (StringValue in base/values.h uses std::string) to UTF-16
56 // when sending strings from C++ to JavaScript.
58 // Why <b> instead of <strong>? Because <b> is shorter.
59 std::string highlighted_base_name
;
61 // MD5 hash of the file.
65 typedef std::vector
<MetadataSearchResult
> MetadataSearchResultVector
;
67 // Used to get a resource entry from the file system.
68 // If |error| is not FILE_ERROR_OK, |entry_info| is set to NULL.
69 typedef base::Callback
<void(FileError error
,
70 scoped_ptr
<ResourceEntry
> entry
)>
71 GetResourceEntryCallback
;
73 // Used to get files from the file system.
74 typedef base::Callback
<void(FileError error
,
75 const base::FilePath
& file_path
,
76 scoped_ptr
<ResourceEntry
> entry
)> GetFileCallback
;
78 // Used to get file content from the file system.
79 // If the file content is available in local cache, |local_file| is filled with
80 // the path to the cache file. If the file content starts to be downloaded from
81 // the server, |local_file| is empty.
82 typedef base::Callback
<void(FileError error
,
83 const base::FilePath
& local_file
,
84 scoped_ptr
<ResourceEntry
> entry
)>
85 GetFileContentInitializedCallback
;
87 // Used to get list of entries under a directory.
88 typedef base::Callback
<void(scoped_ptr
<ResourceEntryVector
> entries
)>
89 ReadDirectoryEntriesCallback
;
91 // Used to get drive content search results.
92 // If |error| is not FILE_ERROR_OK, |result_paths| is empty.
93 typedef base::Callback
<void(
95 const GURL
& next_link
,
96 scoped_ptr
<std::vector
<SearchResultInfo
> > result_paths
)> SearchCallback
;
98 // Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and
99 // |result| contains the search result.
100 typedef base::Callback
<void(
102 scoped_ptr
<MetadataSearchResultVector
> result
)> SearchMetadataCallback
;
104 // Callback for SearchByHashesCallback. On success, vector contains hash and
105 // corresponding files. The vector can include multiple entries for one hash.
106 typedef base::Callback
<void(FileError
, const std::vector
<HashAndFilePath
>&)>
107 SearchByHashesCallback
;
109 // Used to open files from the file system. |file_path| is the path on the local
110 // file system for the opened file.
111 // If |close_callback| is not null, it must be called when the
112 // modification to the cache is done. Otherwise, Drive file system does not
113 // pick up the file for uploading.
114 // |close_callback| must not be called more than once.
115 typedef base::Callback
<void(FileError error
,
116 const base::FilePath
& file_path
,
117 const base::Closure
& close_callback
)>
120 // Used to get available space for the account from Drive.
121 typedef base::Callback
<void(FileError error
,
123 int64 bytes_used
)> GetAvailableSpaceCallback
;
125 // Used to get the url to the sharing dialog.
126 typedef base::Callback
<void(FileError error
,
127 const GURL
& share_url
)> GetShareUrlCallback
;
129 // Used to get filesystem metadata.
130 typedef base::Callback
<void(const FileSystemMetadata
&)>
131 GetFilesystemMetadataCallback
;
133 // Used to mark cached files mounted.
134 typedef base::Callback
<void(FileError error
,
135 const base::FilePath
& file_path
)>
138 // Used to get file path.
139 typedef base::Callback
<void(FileError error
, const base::FilePath
& file_path
)>
142 // Used to free space.
143 typedef base::Callback
<void(bool)> FreeDiskSpaceCallback
;
145 // The mode of opening a file.
147 // Open the file if exists. If not, failed.
150 // Create a new file if not exists, and then open it. If exists, failed.
153 // Open the file if exists. If not, create a new file and then open it.
157 // Option enum to control eligible entries for SearchMetadata().
158 // SEARCH_METADATA_ALL is the default to investigate all the entries.
159 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS excludes the hosted documents.
160 // SEARCH_METADATA_EXCLUDE_DIRECTORIES excludes the directories from the result.
161 // SEARCH_METADATA_SHARED_WITH_ME targets only "shared-with-me" entries.
162 // SEARCH_METADATA_OFFLINE targets only "offline" entries. This option can not
163 // be used with other options.
164 enum SearchMetadataOptions
{
165 SEARCH_METADATA_ALL
= 0,
166 SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS
= 1,
167 SEARCH_METADATA_EXCLUDE_DIRECTORIES
= 1 << 1,
168 SEARCH_METADATA_SHARED_WITH_ME
= 1 << 2,
169 SEARCH_METADATA_OFFLINE
= 1 << 3,
172 // Drive file system abstraction layer.
173 // The interface is defined to make FileSystem mockable.
174 class FileSystemInterface
{
176 virtual ~FileSystemInterface() {}
178 // Adds and removes the observer.
179 virtual void AddObserver(FileSystemObserver
* observer
) = 0;
180 virtual void RemoveObserver(FileSystemObserver
* observer
) = 0;
182 // Checks for updates on the server.
183 virtual void CheckForUpdates() = 0;
185 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
186 // |local_src_file_path| must be a file from the local file system.
187 // |remote_dest_file_path| is the virtual destination path within Drive file
190 // |callback| must not be null.
191 virtual void TransferFileFromLocalToRemote(
192 const base::FilePath
& local_src_file_path
,
193 const base::FilePath
& remote_dest_file_path
,
194 const FileOperationCallback
& callback
) = 0;
196 // Retrieves a file at the virtual path |file_path| on the Drive file system
197 // onto the cache, and mark it dirty. The local path to the cache file is
198 // returned to |callback|. After opening the file, both read and write
199 // on the file can be done with normal local file operations.
200 // If |mime_type| is set and the file is newly created, the mime type is
201 // set to the specified value. If |mime_type| is empty, it is guessed from
204 // |callback| must not be null.
205 virtual void OpenFile(const base::FilePath
& file_path
,
207 const std::string
& mime_type
,
208 const OpenFileCallback
& callback
) = 0;
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
215 // If |preserve_last_modified| is set to true, the last modified time will be
216 // preserved. This feature is only supported on Drive API v2 protocol because
217 // GData WAPI doesn't support updating modification time.
219 // This method also has the following assumptions/limitations that may be
220 // relaxed or addressed later:
221 // - |src_file_path| cannot be a regular file (i.e. non-hosted document)
223 // - |dest_file_path| must not exist.
224 // - The parent of |dest_file_path| must already exist.
226 // The file entries represented by |src_file_path| and the parent directory
227 // of |dest_file_path| need to be present in the in-memory representation
228 // of the file system.
230 // |callback| must not be null.
231 virtual void Copy(const base::FilePath
& src_file_path
,
232 const base::FilePath
& dest_file_path
,
233 bool preserve_last_modified
,
234 const FileOperationCallback
& callback
) = 0;
236 // Moves |src_file_path| to |dest_file_path| on the file system.
237 // |src_file_path| can be a file (regular or hosted document) or a directory.
238 // |dest_file_path| is expected to be of the same type of |src_file_path|
239 // (i.e. if |src_file_path| is a file, |dest_file_path| will be created as
242 // This method also has the following assumptions/limitations that may be
243 // relaxed or addressed later:
244 // - |dest_file_path| must not exist.
245 // - The parent of |dest_file_path| must already exist.
247 // The file entries represented by |src_file_path| and the parent directory
248 // of |dest_file_path| need to be present in the in-memory representation
249 // of the file system.
251 // |callback| must not be null.
252 virtual void Move(const base::FilePath
& src_file_path
,
253 const base::FilePath
& dest_file_path
,
254 const FileOperationCallback
& callback
) = 0;
256 // Removes |file_path| from the file system. If |is_recursive| is set and
257 // |file_path| represents a directory, we will also delete all of its
258 // contained children elements. The file entry represented by |file_path|
259 // needs to be present in in-memory representation of the file system that
260 // in order to be removed.
262 // |callback| must not be null.
263 virtual void Remove(const base::FilePath
& file_path
,
265 const FileOperationCallback
& callback
) = 0;
267 // Creates new directory under |directory_path|. If |is_exclusive| is true,
268 // an error is raised in case a directory is already present at the
269 // |directory_path|. If |is_recursive| is true, the call creates parent
270 // directories as needed just like mkdir -p does.
272 // |callback| must not be null.
273 virtual void CreateDirectory(const base::FilePath
& directory_path
,
276 const FileOperationCallback
& callback
) = 0;
278 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an
279 // error is raised when a file already exists at the path. It is
280 // an error if a directory or a hosted document is already present at the
281 // path, or the parent directory of the path is not present yet.
282 // If |mime_type| is set and the file is newly created, the mime type is
283 // set to the specified value. If |mime_type| is empty, it is guessed from
286 // |callback| must not be null.
287 virtual void CreateFile(const base::FilePath
& file_path
,
289 const std::string
& mime_type
,
290 const FileOperationCallback
& callback
) = 0;
292 // Touches the file at |file_path| by updating the timestamp to
293 // |last_access_time| and |last_modified_time|.
294 // Upon completion, invokes |callback|.
295 // Note that, differently from unix touch command, this doesn't create a file
296 // if the target file doesn't exist.
298 // |last_access_time|, |last_modified_time| and |callback| must not be null.
299 virtual void TouchFile(const base::FilePath
& file_path
,
300 const base::Time
& last_access_time
,
301 const base::Time
& last_modified_time
,
302 const FileOperationCallback
& callback
) = 0;
304 // Truncates the file content at |file_path| to the |length|.
306 // |callback| must not be null.
307 virtual void TruncateFile(const base::FilePath
& file_path
,
309 const FileOperationCallback
& callback
) = 0;
311 // Pins a file at |file_path|.
313 // |callback| must not be null.
314 virtual void Pin(const base::FilePath
& file_path
,
315 const FileOperationCallback
& callback
) = 0;
317 // Unpins a file at |file_path|.
319 // |callback| must not be null.
320 virtual void Unpin(const base::FilePath
& file_path
,
321 const FileOperationCallback
& callback
) = 0;
323 // Makes sure that |file_path| in the file system is available in the local
324 // cache. If the file is not cached, the file will be downloaded. The entry
325 // needs to be present in the file system.
327 // Returns the cache path and entry info to |callback|. It must not be null.
328 virtual void GetFile(const base::FilePath
& file_path
,
329 const GetFileCallback
& callback
) = 0;
331 // Makes sure that |file_path| in the file system is available in the local
332 // cache, and mark it as dirty. The next modification to the cache file is
333 // watched and is automatically uploaded to the server. If the entry is not
334 // present in the file system, it is created.
336 // Returns the cache path and entry info to |callback|. It must not be null.
337 virtual void GetFileForSaving(const base::FilePath
& file_path
,
338 const GetFileCallback
& callback
) = 0;
340 // Gets a file by the given |file_path| and returns a closure to cancel the
342 // Calls |initialized_callback| when either:
343 // 1) The cached file (or JSON file for hosted file) is found, or
344 // 2) Starting to download the file from drive server.
345 // In case of 2), the given FilePath is empty, and |get_content_callback| is
346 // called repeatedly with downloaded content following the
347 // |initialized_callback| invocation.
348 // |completion_callback| is invoked if an error is found, or the operation
349 // is successfully done.
350 // |initialized_callback|, |get_content_callback| and |completion_callback|
352 virtual base::Closure
GetFileContent(
353 const base::FilePath
& file_path
,
354 const GetFileContentInitializedCallback
& initialized_callback
,
355 const google_apis::GetContentCallback
& get_content_callback
,
356 const FileOperationCallback
& completion_callback
) = 0;
358 // Finds an entry (a file or a directory) by |file_path|. This call will also
359 // retrieve and refresh file system content from server and disk cache.
361 // |callback| must not be null.
362 virtual void GetResourceEntry(const base::FilePath
& file_path
,
363 const GetResourceEntryCallback
& callback
) = 0;
365 // Finds and reads a directory by |file_path|. This call will also retrieve
366 // and refresh file system content from server and disk cache.
367 // |entries_callback| can be a null callback when not interested in entries.
369 // |completion_callback| must not be null.
370 virtual void ReadDirectory(
371 const base::FilePath
& file_path
,
372 const ReadDirectoryEntriesCallback
& entries_callback
,
373 const FileOperationCallback
& completion_callback
) = 0;
375 // Does server side content search for |search_query|.
376 // If |next_link| is set, this is the search result url that will be
377 // fetched. Search results will be returned as a list of results'
378 // |SearchResultInfo| structs, which contains file's path and is_directory
381 // |callback| must not be null.
382 virtual void Search(const std::string
& search_query
,
383 const GURL
& next_link
,
384 const SearchCallback
& callback
) = 0;
386 // Searches the local resource metadata, and returns the entries
387 // |at_most_num_matches| that contain |query| in their base names. Search is
388 // done in a case-insensitive fashion. The eligible entries are selected based
389 // on the given |options|, which is a bit-wise OR of SearchMetadataOptions.
390 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS will be automatically added based
391 // on the preference. |callback| must not be null. Must be called on UI
392 // thread. Empty |query| matches any base name. i.e. returns everything.
393 virtual void SearchMetadata(const std::string
& query
,
395 int at_most_num_matches
,
396 const SearchMetadataCallback
& callback
) = 0;
398 // Searches the local resource metadata, and returns the entries that have the
399 // given |hashes|. The list of resource entries are passed to |callback|. The
400 // item of the list can be null if the corresponding file is not found.
401 // |callback| must not be null.
402 virtual void SearchByHashes(const std::set
<std::string
>& hashes
,
403 const SearchByHashesCallback
& callback
) = 0;
405 // Fetches the user's Account Metadata to find out current quota information
406 // and returns it to the callback.
407 virtual void GetAvailableSpace(const GetAvailableSpaceCallback
& callback
) = 0;
409 // Fetches the url to the sharing dialog to be embedded in |embed_origin|,
410 // for the specified file or directory. |callback| must not be null.
411 virtual void GetShareUrl(
412 const base::FilePath
& file_path
,
413 const GURL
& embed_origin
,
414 const GetShareUrlCallback
& callback
) = 0;
416 // Returns miscellaneous metadata of the file system like the largest
417 // timestamp. Used in chrome:drive-internals. |callback| must not be null.
418 virtual void GetMetadata(
419 const GetFilesystemMetadataCallback
& callback
) = 0;
421 // Marks the cached file as mounted, and runs |callback| upon completion.
422 // If succeeded, the cached file path will be passed to the |callback|.
423 // |callback| must not be null.
424 virtual void MarkCacheFileAsMounted(const base::FilePath
& drive_file_path
,
425 const MarkMountedCallback
& callback
) = 0;
427 // Marks the cached file as unmounted, and runs |callback| upon completion.
428 // Note that this method expects that the |cached_file_path| is the path
429 // returned by MarkCacheFileAsMounted().
430 // |callback| must not be null.
431 virtual void MarkCacheFileAsUnmounted(
432 const base::FilePath
& cache_file_path
,
433 const FileOperationCallback
& callback
) = 0;
435 // Adds permission as |role| to |email| for the entry at |drive_file_path|.
436 // |callback| must not be null.
437 virtual void AddPermission(const base::FilePath
& drive_file_path
,
438 const std::string
& email
,
439 google_apis::drive::PermissionRole role
,
440 const FileOperationCallback
& callback
) = 0;
442 // Sets the |key| property on the file or directory at |drive_file_path| with
443 // the specified |visibility|. If already exists, then it will be overwritten.
444 virtual void SetProperty(const base::FilePath
& drive_file_path
,
445 google_apis::drive::Property::Visibility visibility
,
446 const std::string
& key
,
447 const std::string
& value
,
448 const FileOperationCallback
& callback
) = 0;
450 // Resets local data.
451 virtual void Reset(const FileOperationCallback
& callback
) = 0;
453 // Finds a path of an entry (a file or a directory) by |resource_id|.
454 virtual void GetPathFromResourceId(const std::string
& resource_id
,
455 const GetFilePathCallback
& callback
) = 0;
457 // Free drive caches if needed to secure given available spaces. |callback|
458 // takes whether given bytes are available or not.
459 virtual void FreeDiskSpaceIfNeededFor(
461 const FreeDiskSpaceCallback
& callback
) = 0;
466 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_