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_DRIVE_DRIVE_SERVICE_INTERFACE_H_
      6 #define CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/time/time.h"
     11 #include "google_apis/drive/auth_service_interface.h"
     12 #include "google_apis/drive/base_requests.h"
     13 #include "google_apis/drive/drive_api_requests.h"
     14 #include "google_apis/drive/drive_common_callbacks.h"
     15 
     16 namespace base {
     17 class Time;
     18 }
     19 
     20 namespace drive {
     21 
     22 // Function which converts the given resource ID into the desired format.
     23 typedef base::Callback<std::string(
     24     const std::string& resource_id)> ResourceIdCanonicalizer;
     25 
     26 // Observer interface for DriveServiceInterface.
     27 class DriveServiceObserver {
     28  public:
     29   // Triggered when the service gets ready to send requests.
     30   virtual void OnReadyToSendRequests() {}
     31 
     32   // Called when the refresh token was found to be invalid.
     33   virtual void OnRefreshTokenInvalid() {}
     34 
     35  protected:
     36   virtual ~DriveServiceObserver() {}
     37 };
     38 
     39 // This defines an interface for sharing by DriveService and MockDriveService
     40 // so that we can do testing of clients of DriveService.
     41 //
     42 // All functions must be called on UI thread. DriveService is built on top of
     43 // URLFetcher that runs on UI thread.
     44 class DriveServiceInterface {
     45  public:
     46   // Optional parameters for AddNewDirectory().
     47   struct AddNewDirectoryOptions {
     48     AddNewDirectoryOptions();
     49     ~AddNewDirectoryOptions();
     50 
     51     // modified_date of the directory.
     52     // Pass the null Time if you are not interested in setting this property.
     53     base::Time modified_date;
     54 
     55     // last_viewed_by_me_date of the directory.
     56     // Pass the null Time if you are not interested in setting this property.
     57     base::Time last_viewed_by_me_date;
     58   };
     59 
     60   // Optional parameters for InitiateUploadNewFile().
     61   struct InitiateUploadNewFileOptions {
     62     InitiateUploadNewFileOptions();
     63     ~InitiateUploadNewFileOptions();
     64 
     65     // modified_date of the file.
     66     // Pass the null Time if you are not interested in setting this property.
     67     base::Time modified_date;
     68 
     69     // last_viewed_by_me_date of the file.
     70     // Pass the null Time if you are not interested in setting this property.
     71     base::Time last_viewed_by_me_date;
     72   };
     73 
     74   // Optional parameters for InitiateUploadExistingFile().
     75   struct InitiateUploadExistingFileOptions {
     76     InitiateUploadExistingFileOptions();
     77     ~InitiateUploadExistingFileOptions();
     78 
     79     // Expected ETag of the file. UPLOAD_ERROR_CONFLICT error is generated when
     80     // matching fails.
     81     // Pass the empty string to disable this behavior.
     82     std::string etag;
     83 
     84     // New parent of the file.
     85     // Pass the empty string to keep the property unchanged.
     86     std::string parent_resource_id;
     87 
     88     // New title of the file.
     89     // Pass the empty string to keep the property unchanged.
     90     std::string title;
     91 
     92     // New modified_date of the file.
     93     // Pass the null Time if you are not interested in setting this property.
     94     base::Time modified_date;
     95 
     96     // New last_viewed_by_me_date of the file.
     97     // Pass the null Time if you are not interested in setting this property.
     98     base::Time last_viewed_by_me_date;
     99   };
    100 
    101   virtual ~DriveServiceInterface() {}
    102 
    103   // Common service:
    104 
    105   // Initializes the documents service with |account_id|.
    106   virtual void Initialize(const std::string& account_id) = 0;
    107 
    108   // Adds an observer.
    109   virtual void AddObserver(DriveServiceObserver* observer) = 0;
    110 
    111   // Removes an observer.
    112   virtual void RemoveObserver(DriveServiceObserver* observer) = 0;
    113 
    114   // True if ready to send requests.
    115   virtual bool CanSendRequest() const = 0;
    116 
    117   // Returns a function which converts the given resource ID into the desired
    118   // format.
    119   virtual ResourceIdCanonicalizer GetResourceIdCanonicalizer() const = 0;
    120 
    121   // Authentication service:
    122 
    123   // True if OAuth2 access token is retrieved and believed to be fresh.
    124   virtual bool HasAccessToken() const = 0;
    125 
    126   // Gets the cached OAuth2 access token or if empty, then fetches a new one.
    127   virtual void RequestAccessToken(
    128       const google_apis::AuthStatusCallback& callback) = 0;
    129 
    130   // True if OAuth2 refresh token is present.
    131   virtual bool HasRefreshToken() const = 0;
    132 
    133   // Clears OAuth2 access token.
    134   virtual void ClearAccessToken() = 0;
    135 
    136   // Clears OAuth2 refresh token.
    137   virtual void ClearRefreshToken() = 0;
    138 
    139   // Document access:
    140 
    141   // Returns the resource id for the root directory.
    142   virtual std::string GetRootResourceId() const = 0;
    143 
    144   // Fetches a file list of the account. |callback| will be called upon
    145   // completion.
    146   // If the list is too long, it may be paged. In such a case, a URL to fetch
    147   // remaining results will be included in the returned result. See also
    148   // GetRemainingFileList.
    149   //
    150   // |callback| must not be null.
    151   virtual google_apis::CancelCallback GetAllFileList(
    152       const google_apis::FileListCallback& callback) = 0;
    153 
    154   // Fetches a file list in the directory with |directory_resource_id|.
    155   // |callback| will be called upon completion.
    156   // If the list is too long, it may be paged. In such a case, a URL to fetch
    157   // remaining results will be included in the returned result. See also
    158   // GetRemainingFileList.
    159   //
    160   // |directory_resource_id| must not be empty.
    161   // |callback| must not be null.
    162   virtual google_apis::CancelCallback GetFileListInDirectory(
    163       const std::string& directory_resource_id,
    164       const google_apis::FileListCallback& callback) = 0;
    165 
    166   // Searches the resources for the |search_query| from all the user's
    167   // resources. |callback| will be called upon completion.
    168   // If the list is too long, it may be paged. In such a case, a URL to fetch
    169   // remaining results will be included in the returned result. See also
    170   // GetRemainingFileList.
    171   //
    172   // |search_query| must not be empty.
    173   // |callback| must not be null.
    174   virtual google_apis::CancelCallback Search(
    175       const std::string& search_query,
    176       const google_apis::FileListCallback& callback) = 0;
    177 
    178   // Searches the resources with the |title|.
    179   // |directory_resource_id| is an optional parameter. If it is empty,
    180   // the search target is all the existing resources. Otherwise, it is
    181   // the resources directly under the directory with |directory_resource_id|.
    182   // If the list is too long, it may be paged. In such a case, a URL to fetch
    183   // remaining results will be included in the returned result. See also
    184   // GetRemainingFileList.
    185   //
    186   // |title| must not be empty, and |callback| must not be null.
    187   virtual google_apis::CancelCallback SearchByTitle(
    188       const std::string& title,
    189       const std::string& directory_resource_id,
    190       const google_apis::FileListCallback& callback) = 0;
    191 
    192   // Fetches change list since |start_changestamp|. |callback| will be
    193   // called upon completion.
    194   // If the list is too long, it may be paged. In such a case, a URL to fetch
    195   // remaining results will be included in the returned result. See also
    196   // GetRemainingChangeList.
    197   //
    198   // |callback| must not be null.
    199   virtual google_apis::CancelCallback GetChangeList(
    200       int64 start_changestamp,
    201       const google_apis::ChangeListCallback& callback) = 0;
    202 
    203   // The result of GetChangeList() may be paged.
    204   // In such a case, a next link to fetch remaining result is returned.
    205   // The page token can be used for this method. |callback| will be called upon
    206   // completion.
    207   //
    208   // |next_link| must not be empty. |callback| must not be null.
    209   virtual google_apis::CancelCallback GetRemainingChangeList(
    210       const GURL& next_link,
    211       const google_apis::ChangeListCallback& callback) = 0;
    212 
    213   // The result of GetAllFileList(), GetFileListInDirectory(), Search()
    214   // and SearchByTitle() may be paged. In such a case, a next link to fetch
    215   // remaining result is returned. The page token can be used for this method.
    216   // |callback| will be called upon completion.
    217   //
    218   // |next_link| must not be empty. |callback| must not be null.
    219   virtual google_apis::CancelCallback GetRemainingFileList(
    220       const GURL& next_link,
    221       const google_apis::FileListCallback& callback) = 0;
    222 
    223   // Fetches single entry metadata from server. The entry's file id equals
    224   // |resource_id|.
    225   // Upon completion, invokes |callback| with results on the calling thread.
    226   // |callback| must not be null.
    227   virtual google_apis::CancelCallback GetFileResource(
    228       const std::string& resource_id,
    229       const google_apis::FileResourceCallback& callback) = 0;
    230 
    231   // Fetches an url for the sharing dialog for a single entry with id
    232   // |resource_id|, to be embedded in a webview or an iframe with origin
    233   // |embed_origin|. The url is returned via |callback| with results on the
    234   // calling thread. |callback| must not be null.
    235   virtual google_apis::CancelCallback GetShareUrl(
    236       const std::string& resource_id,
    237       const GURL& embed_origin,
    238       const google_apis::GetShareUrlCallback& callback) = 0;
    239 
    240   // Gets the about resource information from the server.
    241   // Upon completion, invokes |callback| with results on the calling thread.
    242   // |callback| must not be null.
    243   virtual google_apis::CancelCallback GetAboutResource(
    244       const google_apis::AboutResourceCallback& callback) = 0;
    245 
    246   // Gets the application information from the server.
    247   // Upon completion, invokes |callback| with results on the calling thread.
    248   // |callback| must not be null.
    249   virtual google_apis::CancelCallback GetAppList(
    250       const google_apis::AppListCallback& callback) = 0;
    251 
    252   // Permanently deletes a resource identified by its |resource_id|.
    253   // If |etag| is not empty and did not match, the deletion fails with
    254   // HTTP_PRECONDITION error.
    255   // Upon completion, invokes |callback| with results on the calling thread.
    256   // |callback| must not be null.
    257   virtual google_apis::CancelCallback DeleteResource(
    258       const std::string& resource_id,
    259       const std::string& etag,
    260       const google_apis::EntryActionCallback& callback) = 0;
    261 
    262   // Trashes a resource identified by its |resource_id|.
    263   // Upon completion, invokes |callback| with results on the calling thread.
    264   // |callback| must not be null.
    265   virtual google_apis::CancelCallback TrashResource(
    266       const std::string& resource_id,
    267       const google_apis::EntryActionCallback& callback) = 0;
    268 
    269   // Makes a copy of a resource with |resource_id|.
    270   // The new resource will be put under a directory with |parent_resource_id|,
    271   // and it'll be named |new_title|.
    272   // If |last_modified| is not null, the modified date of the resource on the
    273   // server will be set to the date.
    274   // This request is supported only on DriveAPIService, because GData WAPI
    275   // doesn't support the function unfortunately.
    276   // Upon completion, invokes |callback| with results on the calling thread.
    277   // |callback| must not be null.
    278   virtual google_apis::CancelCallback CopyResource(
    279       const std::string& resource_id,
    280       const std::string& parent_resource_id,
    281       const std::string& new_title,
    282       const base::Time& last_modified,
    283       const google_apis::FileResourceCallback& callback) = 0;
    284 
    285   // Updates a resource with |resource_id| to the directory of
    286   // |parent_resource_id| with renaming to |new_title|.
    287   // If |last_modified| or |last_accessed| is not null, the modified/accessed
    288   // date of the resource on the server will be set to the date.
    289   // This request is supported only on DriveAPIService, because GData WAPI
    290   // doesn't support the function unfortunately.
    291   // Upon completion, invokes |callback| with results on the calling thread.
    292   // |callback| must not be null.
    293   virtual google_apis::CancelCallback UpdateResource(
    294       const std::string& resource_id,
    295       const std::string& parent_resource_id,
    296       const std::string& new_title,
    297       const base::Time& last_modified,
    298       const base::Time& last_viewed_by_me,
    299       const google_apis::FileResourceCallback& callback) = 0;
    300 
    301   // Renames a document or collection identified by its |resource_id|
    302   // to the UTF-8 encoded |new_title|. Upon completion,
    303   // invokes |callback| with results on the calling thread.
    304   // |callback| must not be null.
    305   virtual google_apis::CancelCallback RenameResource(
    306       const std::string& resource_id,
    307       const std::string& new_title,
    308       const google_apis::EntryActionCallback& callback) = 0;
    309 
    310   // Adds a resource (document, file, or collection) identified by its
    311   // |resource_id| to a collection represented by the |parent_resource_id|.
    312   // Upon completion, invokes |callback| with results on the calling thread.
    313   // |callback| must not be null.
    314   virtual google_apis::CancelCallback AddResourceToDirectory(
    315       const std::string& parent_resource_id,
    316       const std::string& resource_id,
    317       const google_apis::EntryActionCallback& callback) = 0;
    318 
    319   // Removes a resource (document, file, collection) identified by its
    320   // |resource_id| from a collection represented by the |parent_resource_id|.
    321   // Upon completion, invokes |callback| with results on the calling thread.
    322   // |callback| must not be null.
    323   virtual google_apis::CancelCallback RemoveResourceFromDirectory(
    324       const std::string& parent_resource_id,
    325       const std::string& resource_id,
    326       const google_apis::EntryActionCallback& callback) = 0;
    327 
    328   // Adds new collection with |directory_title| under parent directory
    329   // identified with |parent_resource_id|. |parent_resource_id| can be the
    330   // value returned by GetRootResourceId to represent the root directory.
    331   // Upon completion, invokes |callback| and passes newly created entry on
    332   // the calling thread.
    333   // This function cannot be named as "CreateDirectory" as it conflicts with
    334   // a macro on Windows.
    335   // |callback| must not be null.
    336   virtual google_apis::CancelCallback AddNewDirectory(
    337       const std::string& parent_resource_id,
    338       const std::string& directory_title,
    339       const AddNewDirectoryOptions& options,
    340       const google_apis::FileResourceCallback& callback) = 0;
    341 
    342   // Downloads a file with |resourced_id|. The downloaded file will
    343   // be stored at |local_cache_path| location. Upon completion, invokes
    344   // |download_action_callback| with results on the calling thread.
    345   // If |get_content_callback| is not empty,
    346   // URLFetcherDelegate::OnURLFetchDownloadData will be called, which will in
    347   // turn invoke |get_content_callback| on the calling thread.
    348   // If |progress_callback| is not empty, it is invoked periodically when
    349   // the download made some progress.
    350   //
    351   // |download_action_callback| must not be null.
    352   // |get_content_callback| and |progress_callback| may be null.
    353   virtual google_apis::CancelCallback DownloadFile(
    354       const base::FilePath& local_cache_path,
    355       const std::string& resource_id,
    356       const google_apis::DownloadActionCallback& download_action_callback,
    357       const google_apis::GetContentCallback& get_content_callback,
    358       const google_apis::ProgressCallback& progress_callback) = 0;
    359 
    360   // Initiates uploading of a new document/file.
    361   // |content_type| and |content_length| should be the ones of the file to be
    362   // uploaded.
    363   // |callback| must not be null.
    364   virtual google_apis::CancelCallback InitiateUploadNewFile(
    365       const std::string& content_type,
    366       int64 content_length,
    367       const std::string& parent_resource_id,
    368       const std::string& title,
    369       const InitiateUploadNewFileOptions& options,
    370       const google_apis::InitiateUploadCallback& callback) = 0;
    371 
    372   // Initiates uploading of an existing document/file.
    373   // |content_type| and |content_length| should be the ones of the file to be
    374   // uploaded.
    375   // |callback| must not be null.
    376   virtual google_apis::CancelCallback InitiateUploadExistingFile(
    377       const std::string& content_type,
    378       int64 content_length,
    379       const std::string& resource_id,
    380       const InitiateUploadExistingFileOptions& options,
    381       const google_apis::InitiateUploadCallback& callback) = 0;
    382 
    383   // Resumes uploading of a document/file on the calling thread.
    384   // |callback| must not be null. |progress_callback| may be null.
    385   virtual google_apis::CancelCallback ResumeUpload(
    386       const GURL& upload_url,
    387       int64 start_position,
    388       int64 end_position,
    389       int64 content_length,
    390       const std::string& content_type,
    391       const base::FilePath& local_file_path,
    392       const google_apis::drive::UploadRangeCallback& callback,
    393       const google_apis::ProgressCallback& progress_callback) = 0;
    394 
    395   // Gets the current status of the uploading to |upload_url| from the server.
    396   // |drive_file_path| and |content_length| should be set to the same value
    397   // which is used for ResumeUpload.
    398   // |callback| must not be null.
    399   virtual google_apis::CancelCallback GetUploadStatus(
    400       const GURL& upload_url,
    401       int64 content_length,
    402       const google_apis::drive::UploadRangeCallback& callback) = 0;
    403 
    404   // Authorizes a Drive app with the id |app_id| to open the given file.
    405   // Upon completion, invokes |callback| with the link to open the file with
    406   // the provided app. |callback| must not be null.
    407   virtual google_apis::CancelCallback AuthorizeApp(
    408       const std::string& resource_id,
    409       const std::string& app_id,
    410       const google_apis::AuthorizeAppCallback& callback) = 0;
    411 
    412   // Uninstalls a Drive app with the id |app_id|. |callback| must not be null.
    413   virtual google_apis::CancelCallback UninstallApp(
    414       const std::string& app_id,
    415       const google_apis::EntryActionCallback& callback) = 0;
    416 
    417   // Authorizes the account |email| to access |resource_id| as a |role|.
    418   //
    419   // |callback| must not be null.
    420   virtual google_apis::CancelCallback AddPermission(
    421       const std::string& resource_id,
    422       const std::string& email,
    423       google_apis::drive::PermissionRole role,
    424       const google_apis::EntryActionCallback& callback) = 0;
    425 };
    426 
    427 }  // namespace drive
    428 
    429 #endif  // CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
    430