file_system_interface.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_
7
8#include <string>
9#include <vector>
10
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"
16
17namespace drive {
18
19class FileSystemObserver;
20
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
23// browser.
24struct SearchResultInfo {
25  SearchResultInfo(const base::FilePath& path, bool is_directory)
26      : path(path),
27        is_directory(is_directory) {
28  }
29
30  base::FilePath path;
31  bool is_directory;
32};
33
34// Struct to represent a search result for SearchMetadata().
35struct MetadataSearchResult {
36  MetadataSearchResult(const base::FilePath& in_path,
37                       bool is_directory,
38                       const std::string& in_highlighted_base_name)
39      : path(in_path),
40        is_directory(is_directory),
41        highlighted_base_name(in_highlighted_base_name) {}
42
43  // The two members are used to create FileEntry object.
44  base::FilePath path;
45  bool is_directory;
46
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 &lt;
49  //
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.
54  //
55  // Why <b> instead of <strong>? Because <b> is shorter.
56  std::string highlighted_base_name;
57};
58
59typedef std::vector<MetadataSearchResult> MetadataSearchResultVector;
60
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.
63typedef base::Callback<void(FileError error,
64                            scoped_ptr<ResourceEntry> entry)>
65    GetResourceEntryCallback;
66
67// Used to get files from the file system.
68typedef base::Callback<void(FileError error,
69                            const base::FilePath& file_path,
70                            scoped_ptr<ResourceEntry> entry)> GetFileCallback;
71
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.
76typedef base::Callback<void(FileError error,
77                            const base::FilePath& local_file,
78                            scoped_ptr<ResourceEntry> entry)>
79    GetFileContentInitializedCallback;
80
81// Used to get list of entries under a directory.
82typedef base::Callback<void(scoped_ptr<ResourceEntryVector> entries)>
83    ReadDirectoryEntriesCallback;
84
85// Used to get drive content search results.
86// If |error| is not FILE_ERROR_OK, |result_paths| is empty.
87typedef base::Callback<void(
88    FileError error,
89    const GURL& next_link,
90    scoped_ptr<std::vector<SearchResultInfo> > result_paths)> SearchCallback;
91
92// Callback for SearchMetadata(). On success, |error| is FILE_ERROR_OK, and
93// |result| contains the search result.
94typedef base::Callback<void(
95    FileError error,
96    scoped_ptr<MetadataSearchResultVector> result)> SearchMetadataCallback;
97
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.
104typedef base::Callback<void(FileError error,
105                            const base::FilePath& file_path,
106                            const base::Closure& close_callback)>
107    OpenFileCallback;
108
109// Used to get available space for the account from Drive.
110typedef base::Callback<void(FileError error,
111                            int64 bytes_total,
112                            int64 bytes_used)> GetAvailableSpaceCallback;
113
114// Used to get the url to the sharing dialog.
115typedef base::Callback<void(FileError error,
116                            const GURL& share_url)> GetShareUrlCallback;
117
118// Used to get filesystem metadata.
119typedef base::Callback<void(const FileSystemMetadata&)>
120    GetFilesystemMetadataCallback;
121
122// Used to mark cached files mounted.
123typedef base::Callback<void(FileError error,
124                            const base::FilePath& file_path)>
125    MarkMountedCallback;
126
127// Callback for GetCacheEntry.
128// |success| indicates if the operation was successful.
129// |cache_entry| is the obtained cache entry.
130typedef base::Callback<void(bool success, const FileCacheEntry& cache_entry)>
131    GetCacheEntryCallback;
132
133// Used to get file path.
134typedef base::Callback<void(FileError error, const base::FilePath& file_path)>
135    GetFilePathCallback;
136
137// The mode of opening a file.
138enum OpenMode {
139  // Open the file if exists. If not, failed.
140  OPEN_FILE,
141
142  // Create a new file if not exists, and then open it. If exists, failed.
143  CREATE_FILE,
144
145  // Open the file if exists. If not, create a new file and then open it.
146  OPEN_OR_CREATE_FILE,
147};
148
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.
156enum 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,
162};
163
164// Drive file system abstraction layer.
165// The interface is defined to make FileSystem mockable.
166class FileSystemInterface {
167 public:
168  virtual ~FileSystemInterface() {}
169
170  // Adds and removes the observer.
171  virtual void AddObserver(FileSystemObserver* observer) = 0;
172  virtual void RemoveObserver(FileSystemObserver* observer) = 0;
173
174  // Checks for updates on the server.
175  virtual void CheckForUpdates() = 0;
176
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
180  // system.
181  //
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;
187
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
194  // |file_path|.
195  //
196  // |callback| must not be null.
197  virtual void OpenFile(const base::FilePath& file_path,
198                        OpenMode open_mode,
199                        const std::string& mime_type,
200                        const OpenFileCallback& callback) = 0;
201
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
206  // a file).
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.
210  //
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)
214  //   or a directory.
215  // - |dest_file_path| must not exist.
216  // - The parent of |dest_file_path| must already exist.
217  //
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.
221  //
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;
227
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
232  // a file).
233  //
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.
238  //
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.
242  //
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;
247
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.
253  //
254  // |callback| must not be null.
255  virtual void Remove(const base::FilePath& file_path,
256                      bool is_recursive,
257                      const FileOperationCallback& callback) = 0;
258
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.
263  //
264  // |callback| must not be null.
265  virtual void CreateDirectory(const base::FilePath& directory_path,
266                               bool is_exclusive,
267                               bool is_recursive,
268                               const FileOperationCallback& callback) = 0;
269
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
276  // |file_path|.
277  //
278  // |callback| must not be null.
279  virtual void CreateFile(const base::FilePath& file_path,
280                          bool is_exclusive,
281                          const std::string& mime_type,
282                          const FileOperationCallback& callback) = 0;
283
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.
289  //
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;
295
296  // Truncates the file content at |file_path| to the |length|.
297  //
298  // |callback| must not be null.
299  virtual void TruncateFile(const base::FilePath& file_path,
300                            int64 length,
301                            const FileOperationCallback& callback) = 0;
302
303  // Pins a file at |file_path|.
304  //
305  // |callback| must not be null.
306  virtual void Pin(const base::FilePath& file_path,
307                   const FileOperationCallback& callback) = 0;
308
309  // Unpins a file at |file_path|.
310  //
311  // |callback| must not be null.
312  virtual void Unpin(const base::FilePath& file_path,
313                     const FileOperationCallback& callback) = 0;
314
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.
318  //
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;
322
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.
327  //
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;
331
332  // Gets a file by the given |file_path| and returns a closure to cancel the
333  // task.
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|
343  // must not be null.
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;
349
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.
352  //
353  // |callback| must not be null.
354  virtual void GetResourceEntry(const base::FilePath& file_path,
355                                const GetResourceEntryCallback& callback) = 0;
356
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.
360  //
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;
366
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
371  // flag.
372  //
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;
377
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,
386                              int options,
387                              int at_most_num_matches,
388                              const SearchMetadataCallback& callback) = 0;
389
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;
393
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;
400
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;
405
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;
411
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;
419
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;
426
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;
433
434  // Resets local data.
435  virtual void Reset(const FileOperationCallback& callback) = 0;
436
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;
440};
441
442}  // namespace drive
443
444#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_INTERFACE_H_
445