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_GDATA_WAPI_SERVICE_H_ 6 #define CHROME_BROWSER_DRIVE_GDATA_WAPI_SERVICE_H_ 7 8 #include <string> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/observer_list.h" 14 #include "chrome/browser/drive/drive_service_interface.h" 15 #include "chrome/browser/google_apis/auth_service_interface.h" 16 #include "chrome/browser/google_apis/auth_service_observer.h" 17 #include "chrome/browser/google_apis/gdata_wapi_requests.h" 18 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" 19 20 class GURL; 21 class OAuth2TokenService; 22 23 namespace base { 24 class FilePath; 25 class TaskRunner; 26 } 27 28 namespace google_apis { 29 class RequestSender; 30 } 31 32 namespace net { 33 class URLRequestContextGetter; 34 } // namespace net 35 36 namespace drive { 37 38 // This class provides documents feed service calls for WAPI (codename for 39 // DocumentsList API). 40 // Details of API call are abstracted in each request class and this class 41 // works as a thin wrapper for the API. 42 class GDataWapiService : public DriveServiceInterface, 43 public google_apis::AuthServiceObserver { 44 public: 45 // |oauth2_token_service| is used for obtaining OAuth2 access tokens. 46 // |url_request_context_getter| is used to initialize URLFetcher. 47 // |blocking_task_runner| is used to run blocking tasks (like parsing JSON). 48 // |base_url| is used to generate URLs for communicating with the WAPI 49 // |base_download_url| is used to generate URLs for downloading file with WAPI 50 // |custom_user_agent| is used for the User-Agent header in HTTP 51 // requests issued through the service if the value is not empty. 52 GDataWapiService(OAuth2TokenService* oauth2_token_service, 53 net::URLRequestContextGetter* url_request_context_getter, 54 base::TaskRunner* blocking_task_runner, 55 const GURL& base_url, 56 const GURL& base_download_url, 57 const std::string& custom_user_agent); 58 virtual ~GDataWapiService(); 59 60 // DriveServiceInterface Overrides 61 virtual void Initialize() OVERRIDE; 62 virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE; 63 virtual void RemoveObserver(DriveServiceObserver* observer) OVERRIDE; 64 virtual bool CanSendRequest() const OVERRIDE; 65 virtual std::string CanonicalizeResourceId( 66 const std::string& resource_id) const OVERRIDE; 67 virtual bool HasAccessToken() const OVERRIDE; 68 virtual void RequestAccessToken( 69 const google_apis::AuthStatusCallback& callback) OVERRIDE; 70 virtual bool HasRefreshToken() const OVERRIDE; 71 virtual void ClearAccessToken() OVERRIDE; 72 virtual void ClearRefreshToken() OVERRIDE; 73 virtual std::string GetRootResourceId() const OVERRIDE; 74 virtual google_apis::CancelCallback GetAllResourceList( 75 const google_apis::GetResourceListCallback& callback) OVERRIDE; 76 virtual google_apis::CancelCallback GetResourceListInDirectory( 77 const std::string& directory_resource_id, 78 const google_apis::GetResourceListCallback& callback) OVERRIDE; 79 virtual google_apis::CancelCallback Search( 80 const std::string& search_query, 81 const google_apis::GetResourceListCallback& callback) OVERRIDE; 82 virtual google_apis::CancelCallback SearchByTitle( 83 const std::string& title, 84 const std::string& directory_resource_id, 85 const google_apis::GetResourceListCallback& callback) OVERRIDE; 86 virtual google_apis::CancelCallback GetChangeList( 87 int64 start_changestamp, 88 const google_apis::GetResourceListCallback& callback) OVERRIDE; 89 virtual google_apis::CancelCallback ContinueGetResourceList( 90 const GURL& override_url, 91 const google_apis::GetResourceListCallback& callback) OVERRIDE; 92 virtual google_apis::CancelCallback GetResourceEntry( 93 const std::string& resource_id, 94 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 95 virtual google_apis::CancelCallback GetShareUrl( 96 const std::string& resource_id, 97 const GURL& embed_origin, 98 const google_apis::GetShareUrlCallback& callback) OVERRIDE; 99 virtual google_apis::CancelCallback GetAboutResource( 100 const google_apis::GetAboutResourceCallback& callback) OVERRIDE; 101 virtual google_apis::CancelCallback GetAppList( 102 const google_apis::GetAppListCallback& callback) OVERRIDE; 103 virtual google_apis::CancelCallback DeleteResource( 104 const std::string& resource_id, 105 const std::string& etag, 106 const google_apis::EntryActionCallback& callback) OVERRIDE; 107 virtual google_apis::CancelCallback DownloadFile( 108 const base::FilePath& local_cache_path, 109 const std::string& resource_id, 110 const google_apis::DownloadActionCallback& download_action_callback, 111 const google_apis::GetContentCallback& get_content_callback, 112 const google_apis::ProgressCallback& progress_callback) OVERRIDE; 113 virtual google_apis::CancelCallback CopyResource( 114 const std::string& resource_id, 115 const std::string& parent_resource_id, 116 const std::string& new_title, 117 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 118 virtual google_apis::CancelCallback CopyHostedDocument( 119 const std::string& resource_id, 120 const std::string& new_title, 121 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 122 virtual google_apis::CancelCallback RenameResource( 123 const std::string& resource_id, 124 const std::string& new_title, 125 const google_apis::EntryActionCallback& callback) OVERRIDE; 126 virtual google_apis::CancelCallback TouchResource( 127 const std::string& resource_id, 128 const base::Time& modified_date, 129 const base::Time& last_viewed_by_me_date, 130 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 131 virtual google_apis::CancelCallback AddResourceToDirectory( 132 const std::string& parent_resource_id, 133 const std::string& resource_id, 134 const google_apis::EntryActionCallback& callback) OVERRIDE; 135 virtual google_apis::CancelCallback RemoveResourceFromDirectory( 136 const std::string& parent_resource_id, 137 const std::string& resource_id, 138 const google_apis::EntryActionCallback& callback) OVERRIDE; 139 virtual google_apis::CancelCallback AddNewDirectory( 140 const std::string& parent_resource_id, 141 const std::string& directory_title, 142 const google_apis::GetResourceEntryCallback& callback) OVERRIDE; 143 virtual google_apis::CancelCallback InitiateUploadNewFile( 144 const std::string& content_type, 145 int64 content_length, 146 const std::string& parent_resource_id, 147 const std::string& title, 148 const google_apis::InitiateUploadCallback& callback) OVERRIDE; 149 virtual google_apis::CancelCallback InitiateUploadExistingFile( 150 const std::string& content_type, 151 int64 content_length, 152 const std::string& resource_id, 153 const std::string& etag, 154 const google_apis::InitiateUploadCallback& callback) OVERRIDE; 155 virtual google_apis::CancelCallback ResumeUpload( 156 const GURL& upload_url, 157 int64 start_position, 158 int64 end_position, 159 int64 content_length, 160 const std::string& content_type, 161 const base::FilePath& local_file_path, 162 const google_apis::UploadRangeCallback& callback, 163 const google_apis::ProgressCallback& progress_callback) OVERRIDE; 164 virtual google_apis::CancelCallback GetUploadStatus( 165 const GURL& upload_url, 166 int64 content_length, 167 const google_apis::UploadRangeCallback& callback) OVERRIDE; 168 virtual google_apis::CancelCallback AuthorizeApp( 169 const std::string& resource_id, 170 const std::string& app_id, 171 const google_apis::AuthorizeAppCallback& callback) OVERRIDE; 172 173 private: 174 // AuthService::Observer override. 175 virtual void OnOAuth2RefreshTokenChanged() OVERRIDE; 176 177 OAuth2TokenService* oauth2_token_service_; // Not owned. 178 net::URLRequestContextGetter* url_request_context_getter_; // Not owned. 179 scoped_refptr<base::TaskRunner> blocking_task_runner_; 180 scoped_ptr<google_apis::RequestSender> sender_; 181 ObserverList<DriveServiceObserver> observers_; 182 // Request objects should hold a copy of this, rather than a const 183 // reference, as they may outlive this object. 184 const google_apis::GDataWapiUrlGenerator url_generator_; 185 const std::string custom_user_agent_; 186 187 DISALLOW_COPY_AND_ASSIGN(GDataWapiService); 188 }; 189 190 } // namespace drive 191 192 #endif // CHROME_BROWSER_DRIVE_GDATA_WAPI_SERVICE_H_ 193