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