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