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