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