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.
5 #ifndef COMPONENTS_DRIVE_SERVICE_DRIVE_API_SERVICE_H_
6 #define COMPONENTS_DRIVE_SERVICE_DRIVE_API_SERVICE_H_
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 "base/threading/thread_checker.h"
15 #include "components/drive/service/drive_service_interface.h"
16 #include "google_apis/drive/auth_service_interface.h"
17 #include "google_apis/drive/auth_service_observer.h"
18 #include "google_apis/drive/drive_api_url_generator.h"
21 class OAuth2TokenService
;
25 class SequencedTaskRunner
;
28 namespace google_apis
{
29 class FilesListRequestRunner
;
32 class BatchUploadRequest
;
34 } // namespace google_apis
37 class URLRequestContextGetter
;
42 // Builder for batch request returned by |DriveAPIService|.
43 class BatchRequestConfigurator
: public BatchRequestConfiguratorInterface
,
44 public base::NonThreadSafe
{
46 BatchRequestConfigurator(
47 const base::WeakPtr
<google_apis::drive::BatchUploadRequest
>&
49 base::SequencedTaskRunner
* task_runner
,
50 const google_apis::DriveApiUrlGenerator
& url_generator
,
51 const google_apis::CancelCallback
& cancel_callback
);
52 ~BatchRequestConfigurator() override
;
54 // BatchRequestConfiguratorInterface overrides.
55 google_apis::CancelCallback
MultipartUploadNewFile(
56 const std::string
& content_type
,
58 const std::string
& parent_resource_id
,
59 const std::string
& title
,
60 const base::FilePath
& local_file_path
,
61 const UploadNewFileOptions
& options
,
62 const google_apis::FileResourceCallback
& callback
,
63 const google_apis::ProgressCallback
& progress_callback
) override
;
64 google_apis::CancelCallback
MultipartUploadExistingFile(
65 const std::string
& content_type
,
67 const std::string
& resource_id
,
68 const base::FilePath
& local_file_path
,
69 const UploadExistingFileOptions
& options
,
70 const google_apis::FileResourceCallback
& callback
,
71 const google_apis::ProgressCallback
& progress_callback
) override
;
72 void Commit() override
;
75 // Reference to batch request. It turns to null after committing.
76 base::WeakPtr
<google_apis::drive::BatchUploadRequest
> batch_request_
;
77 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
78 google_apis::DriveApiUrlGenerator url_generator_
;
79 google_apis::CancelCallback cancel_callback_
;
81 DISALLOW_COPY_AND_ASSIGN(BatchRequestConfigurator
);
84 // This class provides Drive request calls using Drive V2 API.
85 // Details of API call are abstracted in each request class and this class
86 // works as a thin wrapper for the API.
87 class DriveAPIService
: public DriveServiceInterface
,
88 public google_apis::AuthServiceObserver
{
90 // |oauth2_token_service| is used for obtaining OAuth2 access tokens.
91 // |url_request_context_getter| is used to initialize URLFetcher.
92 // |blocking_task_runner| is used to run blocking tasks (like parsing JSON).
93 // |base_url| is used to generate URLs for communication with the drive API.
94 // |base_download_url| is used to generate URLs for downloading file from the
96 // |custom_user_agent| will be used for the User-Agent header in HTTP
97 // requests issues through the service if the value is not empty.
99 OAuth2TokenService
* oauth2_token_service
,
100 net::URLRequestContextGetter
* url_request_context_getter
,
101 base::SequencedTaskRunner
* blocking_task_runner
,
102 const GURL
& base_url
,
103 const GURL
& base_download_url
,
104 const std::string
& custom_user_agent
);
105 ~DriveAPIService() override
;
107 // DriveServiceInterface Overrides
108 void Initialize(const std::string
& account_id
) override
;
109 void AddObserver(DriveServiceObserver
* observer
) override
;
110 void RemoveObserver(DriveServiceObserver
* observer
) override
;
111 bool CanSendRequest() const override
;
112 bool HasAccessToken() const override
;
113 void RequestAccessToken(
114 const google_apis::AuthStatusCallback
& callback
) override
;
115 bool HasRefreshToken() const override
;
116 void ClearAccessToken() override
;
117 void ClearRefreshToken() override
;
118 std::string
GetRootResourceId() const override
;
119 google_apis::CancelCallback
GetAllFileList(
120 const google_apis::FileListCallback
& callback
) override
;
121 google_apis::CancelCallback
GetFileListInDirectory(
122 const std::string
& directory_resource_id
,
123 const google_apis::FileListCallback
& callback
) override
;
124 google_apis::CancelCallback
Search(
125 const std::string
& search_query
,
126 const google_apis::FileListCallback
& callback
) override
;
127 google_apis::CancelCallback
SearchByTitle(
128 const std::string
& title
,
129 const std::string
& directory_resource_id
,
130 const google_apis::FileListCallback
& callback
) override
;
131 google_apis::CancelCallback
GetChangeList(
132 int64 start_changestamp
,
133 const google_apis::ChangeListCallback
& callback
) override
;
134 google_apis::CancelCallback
GetRemainingChangeList(
135 const GURL
& next_link
,
136 const google_apis::ChangeListCallback
& callback
) override
;
137 google_apis::CancelCallback
GetRemainingFileList(
138 const GURL
& next_link
,
139 const google_apis::FileListCallback
& callback
) override
;
140 google_apis::CancelCallback
GetFileResource(
141 const std::string
& resource_id
,
142 const google_apis::FileResourceCallback
& callback
) override
;
143 google_apis::CancelCallback
GetShareUrl(
144 const std::string
& resource_id
,
145 const GURL
& embed_origin
,
146 const google_apis::GetShareUrlCallback
& callback
) override
;
147 google_apis::CancelCallback
GetAboutResource(
148 const google_apis::AboutResourceCallback
& callback
) override
;
149 google_apis::CancelCallback
GetAppList(
150 const google_apis::AppListCallback
& callback
) override
;
151 google_apis::CancelCallback
DeleteResource(
152 const std::string
& resource_id
,
153 const std::string
& etag
,
154 const google_apis::EntryActionCallback
& callback
) override
;
155 google_apis::CancelCallback
TrashResource(
156 const std::string
& resource_id
,
157 const google_apis::EntryActionCallback
& callback
) override
;
158 google_apis::CancelCallback
DownloadFile(
159 const base::FilePath
& local_cache_path
,
160 const std::string
& resource_id
,
161 const google_apis::DownloadActionCallback
& download_action_callback
,
162 const google_apis::GetContentCallback
& get_content_callback
,
163 const google_apis::ProgressCallback
& progress_callback
) override
;
164 google_apis::CancelCallback
CopyResource(
165 const std::string
& resource_id
,
166 const std::string
& parent_resource_id
,
167 const std::string
& new_title
,
168 const base::Time
& last_modified
,
169 const google_apis::FileResourceCallback
& callback
) override
;
170 google_apis::CancelCallback
UpdateResource(
171 const std::string
& resource_id
,
172 const std::string
& parent_resource_id
,
173 const std::string
& new_title
,
174 const base::Time
& last_modified
,
175 const base::Time
& last_viewed_by_me
,
176 const google_apis::drive::Properties
& properties
,
177 const google_apis::FileResourceCallback
& callback
) override
;
178 google_apis::CancelCallback
AddResourceToDirectory(
179 const std::string
& parent_resource_id
,
180 const std::string
& resource_id
,
181 const google_apis::EntryActionCallback
& callback
) override
;
182 google_apis::CancelCallback
RemoveResourceFromDirectory(
183 const std::string
& parent_resource_id
,
184 const std::string
& resource_id
,
185 const google_apis::EntryActionCallback
& callback
) override
;
186 google_apis::CancelCallback
AddNewDirectory(
187 const std::string
& parent_resource_id
,
188 const std::string
& directory_title
,
189 const AddNewDirectoryOptions
& options
,
190 const google_apis::FileResourceCallback
& callback
) override
;
191 google_apis::CancelCallback
InitiateUploadNewFile(
192 const std::string
& content_type
,
193 int64 content_length
,
194 const std::string
& parent_resource_id
,
195 const std::string
& title
,
196 const UploadNewFileOptions
& options
,
197 const google_apis::InitiateUploadCallback
& callback
) override
;
198 google_apis::CancelCallback
InitiateUploadExistingFile(
199 const std::string
& content_type
,
200 int64 content_length
,
201 const std::string
& resource_id
,
202 const UploadExistingFileOptions
& options
,
203 const google_apis::InitiateUploadCallback
& callback
) override
;
204 google_apis::CancelCallback
ResumeUpload(
205 const GURL
& upload_url
,
206 int64 start_position
,
208 int64 content_length
,
209 const std::string
& content_type
,
210 const base::FilePath
& local_file_path
,
211 const google_apis::drive::UploadRangeCallback
& callback
,
212 const google_apis::ProgressCallback
& progress_callback
) override
;
213 google_apis::CancelCallback
GetUploadStatus(
214 const GURL
& upload_url
,
215 int64 content_length
,
216 const google_apis::drive::UploadRangeCallback
& callback
) override
;
217 google_apis::CancelCallback
MultipartUploadNewFile(
218 const std::string
& content_type
,
219 int64 content_length
,
220 const std::string
& parent_resource_id
,
221 const std::string
& title
,
222 const base::FilePath
& local_file_path
,
223 const drive::UploadNewFileOptions
& options
,
224 const google_apis::FileResourceCallback
& callback
,
225 const google_apis::ProgressCallback
& progress_callback
) override
;
226 google_apis::CancelCallback
MultipartUploadExistingFile(
227 const std::string
& content_type
,
228 int64 content_length
,
229 const std::string
& resource_id
,
230 const base::FilePath
& local_file_path
,
231 const drive::UploadExistingFileOptions
& options
,
232 const google_apis::FileResourceCallback
& callback
,
233 const google_apis::ProgressCallback
& progress_callback
) override
;
234 google_apis::CancelCallback
AuthorizeApp(
235 const std::string
& resource_id
,
236 const std::string
& app_id
,
237 const google_apis::AuthorizeAppCallback
& callback
) override
;
238 google_apis::CancelCallback
UninstallApp(
239 const std::string
& app_id
,
240 const google_apis::EntryActionCallback
& callback
) override
;
241 google_apis::CancelCallback
AddPermission(
242 const std::string
& resource_id
,
243 const std::string
& email
,
244 google_apis::drive::PermissionRole role
,
245 const google_apis::EntryActionCallback
& callback
) override
;
246 scoped_ptr
<BatchRequestConfiguratorInterface
> StartBatchRequest() override
;
249 // AuthServiceObserver override.
250 void OnOAuth2RefreshTokenChanged() override
;
252 // The class is expected to run on UI thread.
253 base::ThreadChecker thread_checker_
;
255 OAuth2TokenService
* oauth2_token_service_
;
256 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
257 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
258 scoped_ptr
<google_apis::RequestSender
> sender_
;
259 scoped_ptr
<google_apis::FilesListRequestRunner
> files_list_request_runner_
;
260 base::ObserverList
<DriveServiceObserver
> observers_
;
261 google_apis::DriveApiUrlGenerator url_generator_
;
262 const std::string custom_user_agent_
;
264 DISALLOW_COPY_AND_ASSIGN(DriveAPIService
);
269 #endif // COMPONENTS_DRIVE_SERVICE_DRIVE_API_SERVICE_H_