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_API_SERVICE_H_
      6 #define CHROME_BROWSER_DRIVE_DRIVE_API_SERVICE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/observer_list.h"
     13 #include "chrome/browser/drive/drive_service_interface.h"
     14 #include "google_apis/drive/auth_service_interface.h"
     15 #include "google_apis/drive/auth_service_observer.h"
     16 #include "google_apis/drive/drive_api_url_generator.h"
     17 #include "google_apis/drive/gdata_wapi_url_generator.h"
     18 
     19 class GURL;
     20 class OAuth2TokenService;
     21 
     22 namespace base {
     23 class FilePath;
     24 class SequencedTaskRunner;
     25 }
     26 
     27 namespace google_apis {
     28 class RequestSender;
     29 }
     30 
     31 namespace net {
     32 class URLRequestContextGetter;
     33 }  // namespace net
     34 
     35 namespace drive {
     36 
     37 // This class provides Drive request calls using Drive V2 API.
     38 // Details of API call are abstracted in each request class and this class
     39 // works as a thin wrapper for the API.
     40 class DriveAPIService : public DriveServiceInterface,
     41                         public google_apis::AuthServiceObserver {
     42  public:
     43   // |oauth2_token_service| is used for obtaining OAuth2 access tokens.
     44   // |url_request_context_getter| is used to initialize URLFetcher.
     45   // |blocking_task_runner| is used to run blocking tasks (like parsing JSON).
     46   // |base_url| is used to generate URLs for communication with the drive API.
     47   // |base_download_url| is used to generate URLs for downloading file from the
     48   // drive API.
     49   // |wapi_base_url| is used to generate URLs for shared_url. Unfortunately,
     50   // the share_url is not yet supported on Drive API v2, so as a fallback,
     51   // we use GData WAPI.
     52   // |custom_user_agent| will be used for the User-Agent header in HTTP
     53   // requests issues through the service if the value is not empty.
     54   DriveAPIService(
     55       OAuth2TokenService* oauth2_token_service,
     56       net::URLRequestContextGetter* url_request_context_getter,
     57       base::SequencedTaskRunner* blocking_task_runner,
     58       const GURL& base_url,
     59       const GURL& base_download_url,
     60       const GURL& wapi_base_url,
     61       const std::string& custom_user_agent);
     62   virtual ~DriveAPIService();
     63 
     64   // DriveServiceInterface Overrides
     65   virtual void Initialize(const std::string& account_id) OVERRIDE;
     66   virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE;
     67   virtual void RemoveObserver(DriveServiceObserver* observer) OVERRIDE;
     68   virtual bool CanSendRequest() const OVERRIDE;
     69   virtual ResourceIdCanonicalizer GetResourceIdCanonicalizer() const OVERRIDE;
     70   virtual bool HasAccessToken() const OVERRIDE;
     71   virtual void RequestAccessToken(
     72       const google_apis::AuthStatusCallback& callback) OVERRIDE;
     73   virtual bool HasRefreshToken() const OVERRIDE;
     74   virtual void ClearAccessToken() OVERRIDE;
     75   virtual void ClearRefreshToken() OVERRIDE;
     76   virtual std::string GetRootResourceId() const OVERRIDE;
     77   virtual google_apis::CancelCallback GetAllFileList(
     78       const google_apis::FileListCallback& callback) OVERRIDE;
     79   virtual google_apis::CancelCallback GetFileListInDirectory(
     80       const std::string& directory_resource_id,
     81       const google_apis::FileListCallback& callback) OVERRIDE;
     82   virtual google_apis::CancelCallback Search(
     83       const std::string& search_query,
     84       const google_apis::FileListCallback& callback) OVERRIDE;
     85   virtual google_apis::CancelCallback SearchByTitle(
     86       const std::string& title,
     87       const std::string& directory_resource_id,
     88       const google_apis::FileListCallback& callback) OVERRIDE;
     89   virtual google_apis::CancelCallback GetChangeList(
     90       int64 start_changestamp,
     91       const google_apis::ChangeListCallback& callback) OVERRIDE;
     92   virtual google_apis::CancelCallback GetRemainingChangeList(
     93       const GURL& next_link,
     94       const google_apis::ChangeListCallback& callback) OVERRIDE;
     95   virtual google_apis::CancelCallback GetRemainingFileList(
     96       const GURL& next_link,
     97       const google_apis::FileListCallback& callback) OVERRIDE;
     98   virtual google_apis::CancelCallback GetFileResource(
     99       const std::string& resource_id,
    100       const google_apis::FileResourceCallback& callback) OVERRIDE;
    101   virtual google_apis::CancelCallback GetShareUrl(
    102       const std::string& resource_id,
    103       const GURL& embed_origin,
    104       const google_apis::GetShareUrlCallback& callback) OVERRIDE;
    105   virtual google_apis::CancelCallback GetAboutResource(
    106       const google_apis::AboutResourceCallback& callback) OVERRIDE;
    107   virtual google_apis::CancelCallback GetAppList(
    108       const google_apis::AppListCallback& callback) OVERRIDE;
    109   virtual google_apis::CancelCallback DeleteResource(
    110       const std::string& resource_id,
    111       const std::string& etag,
    112       const google_apis::EntryActionCallback& callback) OVERRIDE;
    113   virtual google_apis::CancelCallback TrashResource(
    114       const std::string& resource_id,
    115       const google_apis::EntryActionCallback& callback) OVERRIDE;
    116   virtual google_apis::CancelCallback DownloadFile(
    117       const base::FilePath& local_cache_path,
    118       const std::string& resource_id,
    119       const google_apis::DownloadActionCallback& download_action_callback,
    120       const google_apis::GetContentCallback& get_content_callback,
    121       const google_apis::ProgressCallback& progress_callback) OVERRIDE;
    122   virtual google_apis::CancelCallback CopyResource(
    123       const std::string& resource_id,
    124       const std::string& parent_resource_id,
    125       const std::string& new_title,
    126       const base::Time& last_modified,
    127       const google_apis::FileResourceCallback& callback) OVERRIDE;
    128   virtual google_apis::CancelCallback UpdateResource(
    129       const std::string& resource_id,
    130       const std::string& parent_resource_id,
    131       const std::string& new_title,
    132       const base::Time& last_modified,
    133       const base::Time& last_viewed_by_me,
    134       const google_apis::FileResourceCallback& callback) OVERRIDE;
    135   virtual google_apis::CancelCallback RenameResource(
    136       const std::string& resource_id,
    137       const std::string& new_title,
    138       const google_apis::EntryActionCallback& callback) OVERRIDE;
    139   virtual google_apis::CancelCallback AddResourceToDirectory(
    140       const std::string& parent_resource_id,
    141       const std::string& resource_id,
    142       const google_apis::EntryActionCallback& callback) OVERRIDE;
    143   virtual google_apis::CancelCallback RemoveResourceFromDirectory(
    144       const std::string& parent_resource_id,
    145       const std::string& resource_id,
    146       const google_apis::EntryActionCallback& callback) OVERRIDE;
    147   virtual google_apis::CancelCallback AddNewDirectory(
    148       const std::string& parent_resource_id,
    149       const std::string& directory_title,
    150       const AddNewDirectoryOptions& options,
    151       const google_apis::FileResourceCallback& callback) OVERRIDE;
    152   virtual google_apis::CancelCallback InitiateUploadNewFile(
    153       const std::string& content_type,
    154       int64 content_length,
    155       const std::string& parent_resource_id,
    156       const std::string& title,
    157       const InitiateUploadNewFileOptions& options,
    158       const google_apis::InitiateUploadCallback& callback) OVERRIDE;
    159   virtual google_apis::CancelCallback InitiateUploadExistingFile(
    160       const std::string& content_type,
    161       int64 content_length,
    162       const std::string& resource_id,
    163       const InitiateUploadExistingFileOptions& options,
    164       const google_apis::InitiateUploadCallback& callback) OVERRIDE;
    165   virtual google_apis::CancelCallback ResumeUpload(
    166       const GURL& upload_url,
    167       int64 start_position,
    168       int64 end_position,
    169       int64 content_length,
    170       const std::string& content_type,
    171       const base::FilePath& local_file_path,
    172       const google_apis::drive::UploadRangeCallback& callback,
    173       const google_apis::ProgressCallback& progress_callback) OVERRIDE;
    174   virtual google_apis::CancelCallback GetUploadStatus(
    175       const GURL& upload_url,
    176       int64 content_length,
    177       const google_apis::drive::UploadRangeCallback& callback) OVERRIDE;
    178   virtual google_apis::CancelCallback AuthorizeApp(
    179       const std::string& resource_id,
    180       const std::string& app_id,
    181       const google_apis::AuthorizeAppCallback& callback) OVERRIDE;
    182   virtual google_apis::CancelCallback UninstallApp(
    183       const std::string& app_id,
    184       const google_apis::EntryActionCallback& callback) OVERRIDE;
    185   virtual google_apis::CancelCallback AddPermission(
    186       const std::string& resource_id,
    187       const std::string& email,
    188       google_apis::drive::PermissionRole role,
    189       const google_apis::EntryActionCallback& callback) OVERRIDE;
    190 
    191  private:
    192   // AuthServiceObserver override.
    193   virtual void OnOAuth2RefreshTokenChanged() OVERRIDE;
    194 
    195   OAuth2TokenService* oauth2_token_service_;
    196   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
    197   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    198   scoped_ptr<google_apis::RequestSender> sender_;
    199   ObserverList<DriveServiceObserver> observers_;
    200   google_apis::DriveApiUrlGenerator url_generator_;
    201   google_apis::GDataWapiUrlGenerator wapi_url_generator_;
    202   const std::string custom_user_agent_;
    203 
    204   DISALLOW_COPY_AND_ASSIGN(DriveAPIService);
    205 };
    206 
    207 }  // namespace drive
    208 
    209 #endif  // CHROME_BROWSER_DRIVE_DRIVE_API_SERVICE_H_
    210