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 COMPONENTS_DRIVE_FILE_SYSTEM_INTERFACE_H_
6 #define COMPONENTS_DRIVE_FILE_SYSTEM_INTERFACE_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "components/drive/drive.pb.h"
13 #include "components/drive/file_system_metadata.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 // Used for returning result of calculated cache size.
146 typedef base::Callback
<void(uint64_t)> EvictableCacheSizeCallback
;
148 // The mode of opening a file.
150 // Open the file if exists. If not, failed.
153 // Create a new file if not exists, and then open it. If exists, failed.
156 // Open the file if exists. If not, create a new file and then open it.
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
245 // This method also has the following assumptions/limitations that may be
246 // relaxed or addressed later:
247 // - |dest_file_path| must not exist.
248 // - The parent of |dest_file_path| must already exist.
250 // The file entries represented by |src_file_path| and the parent directory
251 // of |dest_file_path| need to be present in the in-memory representation
252 // of the file system.
254 // |callback| must not be null.
255 virtual void Move(const base::FilePath
& src_file_path
,
256 const base::FilePath
& dest_file_path
,
257 const FileOperationCallback
& callback
) = 0;
259 // Removes |file_path| from the file system. If |is_recursive| is set and
260 // |file_path| represents a directory, we will also delete all of its
261 // contained children elements. The file entry represented by |file_path|
262 // needs to be present in in-memory representation of the file system that
263 // in order to be removed.
265 // |callback| must not be null.
266 virtual void Remove(const base::FilePath
& file_path
,
268 const FileOperationCallback
& callback
) = 0;
270 // Creates new directory under |directory_path|. If |is_exclusive| is true,
271 // an error is raised in case a directory is already present at the
272 // |directory_path|. If |is_recursive| is true, the call creates parent
273 // directories as needed just like mkdir -p does.
275 // |callback| must not be null.
276 virtual void CreateDirectory(const base::FilePath
& directory_path
,
279 const FileOperationCallback
& callback
) = 0;
281 // Creates a file at |file_path|. If the flag |is_exclusive| is true, an
282 // error is raised when a file already exists at the path. It is
283 // an error if a directory or a hosted document is already present at the
284 // path, or the parent directory of the path is not present yet.
285 // If |mime_type| is set and the file is newly created, the mime type is
286 // set to the specified value. If |mime_type| is empty, it is guessed from
289 // |callback| must not be null.
290 virtual void CreateFile(const base::FilePath
& file_path
,
292 const std::string
& mime_type
,
293 const FileOperationCallback
& callback
) = 0;
295 // Touches the file at |file_path| by updating the timestamp to
296 // |last_access_time| and |last_modified_time|.
297 // Upon completion, invokes |callback|.
298 // Note that, differently from unix touch command, this doesn't create a file
299 // if the target file doesn't exist.
301 // |last_access_time|, |last_modified_time| and |callback| must not be null.
302 virtual void TouchFile(const base::FilePath
& file_path
,
303 const base::Time
& last_access_time
,
304 const base::Time
& last_modified_time
,
305 const FileOperationCallback
& callback
) = 0;
307 // Truncates the file content at |file_path| to the |length|.
309 // |callback| must not be null.
310 virtual void TruncateFile(const base::FilePath
& file_path
,
312 const FileOperationCallback
& callback
) = 0;
314 // Pins a file at |file_path|.
316 // |callback| must not be null.
317 virtual void Pin(const base::FilePath
& file_path
,
318 const FileOperationCallback
& callback
) = 0;
320 // Unpins a file at |file_path|.
322 // |callback| must not be null.
323 virtual void Unpin(const base::FilePath
& file_path
,
324 const FileOperationCallback
& callback
) = 0;
326 // Makes sure that |file_path| in the file system is available in the local
327 // cache. If the file is not cached, the file will be downloaded. The entry
328 // needs to be present in the file system.
330 // Returns the cache path and entry info to |callback|. It must not be null.
331 virtual void GetFile(const base::FilePath
& file_path
,
332 const GetFileCallback
& callback
) = 0;
334 // Makes sure that |file_path| in the file system is available in the local
335 // cache, and mark it as dirty. The next modification to the cache file is
336 // watched and is automatically uploaded to the server. If the entry is not
337 // present in the file system, it is created.
339 // Returns the cache path and entry info to |callback|. It must not be null.
340 virtual void GetFileForSaving(const base::FilePath
& file_path
,
341 const GetFileCallback
& callback
) = 0;
343 // Gets a file by the given |file_path| and returns a closure to cancel the
345 // Calls |initialized_callback| when either:
346 // 1) The cached file (or JSON file for hosted file) is found, or
347 // 2) Starting to download the file from drive server.
348 // In case of 2), the given FilePath is empty, and |get_content_callback| is
349 // called repeatedly with downloaded content following the
350 // |initialized_callback| invocation.
351 // |completion_callback| is invoked if an error is found, or the operation
352 // is successfully done.
353 // |initialized_callback|, |get_content_callback| and |completion_callback|
355 virtual base::Closure
GetFileContent(
356 const base::FilePath
& file_path
,
357 const GetFileContentInitializedCallback
& initialized_callback
,
358 const google_apis::GetContentCallback
& get_content_callback
,
359 const FileOperationCallback
& completion_callback
) = 0;
361 // Finds an entry (a file or a directory) by |file_path|. This call will also
362 // retrieve and refresh file system content from server and disk cache.
364 // |callback| must not be null.
365 virtual void GetResourceEntry(const base::FilePath
& file_path
,
366 const GetResourceEntryCallback
& callback
) = 0;
368 // Finds and reads a directory by |file_path|. This call will also retrieve
369 // and refresh file system content from server and disk cache.
370 // |entries_callback| can be a null callback when not interested in entries.
372 // |completion_callback| must not be null.
373 virtual void ReadDirectory(
374 const base::FilePath
& file_path
,
375 const ReadDirectoryEntriesCallback
& entries_callback
,
376 const FileOperationCallback
& completion_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 // Searches the local resource metadata, and returns the entries that have the
402 // given |hashes|. The list of resource entries are passed to |callback|. The
403 // item of the list can be null if the corresponding file is not found.
404 // |callback| must not be null.
405 virtual void SearchByHashes(const std::set
<std::string
>& hashes
,
406 const SearchByHashesCallback
& callback
) = 0;
408 // Fetches the user's Account Metadata to find out current quota information
409 // and returns it to the callback.
410 virtual void GetAvailableSpace(const GetAvailableSpaceCallback
& callback
) = 0;
412 // Fetches the url to the sharing dialog to be embedded in |embed_origin|,
413 // for the specified file or directory. |callback| must not be null.
414 virtual void GetShareUrl(
415 const base::FilePath
& file_path
,
416 const GURL
& embed_origin
,
417 const GetShareUrlCallback
& callback
) = 0;
419 // Returns miscellaneous metadata of the file system like the largest
420 // timestamp. Used in chrome:drive-internals. |callback| must not be null.
421 virtual void GetMetadata(
422 const GetFilesystemMetadataCallback
& callback
) = 0;
424 // Marks the cached file as mounted, and runs |callback| upon completion.
425 // If succeeded, the cached file path will be passed to the |callback|.
426 // |callback| must not be null.
427 virtual void MarkCacheFileAsMounted(const base::FilePath
& drive_file_path
,
428 const MarkMountedCallback
& callback
) = 0;
430 // Marks the cached file as unmounted, and runs |callback| upon completion.
431 // Note that this method expects that the |cached_file_path| is the path
432 // returned by MarkCacheFileAsMounted().
433 // |callback| must not be null.
434 virtual void MarkCacheFileAsUnmounted(
435 const base::FilePath
& cache_file_path
,
436 const FileOperationCallback
& callback
) = 0;
438 // Adds permission as |role| to |email| for the entry at |drive_file_path|.
439 // |callback| must not be null.
440 virtual void AddPermission(const base::FilePath
& drive_file_path
,
441 const std::string
& email
,
442 google_apis::drive::PermissionRole role
,
443 const FileOperationCallback
& callback
) = 0;
445 // Sets the |key| property on the file or directory at |drive_file_path| with
446 // the specified |visibility|. If already exists, then it will be overwritten.
447 virtual void SetProperty(const base::FilePath
& drive_file_path
,
448 google_apis::drive::Property::Visibility visibility
,
449 const std::string
& key
,
450 const std::string
& value
,
451 const FileOperationCallback
& callback
) = 0;
453 // Resets local data.
454 virtual void Reset(const FileOperationCallback
& callback
) = 0;
456 // Finds a path of an entry (a file or a directory) by |resource_id|.
457 virtual void GetPathFromResourceId(const std::string
& resource_id
,
458 const GetFilePathCallback
& callback
) = 0;
460 // Free drive caches if needed to secure given available spaces. |callback|
461 // takes whether given bytes are available or not.
462 virtual void FreeDiskSpaceIfNeededFor(
464 const FreeDiskSpaceCallback
& callback
) = 0;
466 // Calculates evictable cache size.
467 // |callback| must not be null.
468 virtual void CalculateEvictableCacheSize(
469 const EvictableCacheSizeCallback
& callback
) = 0;
474 #endif // COMPONENTS_DRIVE_FILE_SYSTEM_INTERFACE_H_