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 #include "chrome/browser/chromeos/gdata/gdata_wapi_service.h"
10 #include "base/bind.h"
11 #include "base/message_loop_proxy.h"
12 #include "chrome/browser/chromeos/gdata/drive_api_operations.h"
13 #include "chrome/browser/chromeos/gdata/gdata_operations.h"
14 #include "chrome/browser/chromeos/gdata/gdata_util.h"
15 #include "chrome/browser/chromeos/gdata/operation_runner.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/net/url_util.h"
18 #include "content/public/browser/browser_thread.h"
20 using content::BrowserThread
;
26 const char* GetExportFormatParam(DocumentExportFormat format
) {
61 // OAuth2 scopes for the documents API.
62 const char kDocsListScope
[] = "https://docs.google.com/feeds/";
63 const char kSpreadsheetsScope
[] = "https://spreadsheets.google.com/feeds/";
64 const char kUserContentScope
[] = "https://docs.googleusercontent.com/";
65 const char kDriveAppsScope
[] = "https://www.googleapis.com/auth/drive.apps";
69 GDataWapiService::GDataWapiService()
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
75 GDataWapiService::~GDataWapiService() {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
79 AuthService
* GDataWapiService::auth_service_for_testing() {
80 return runner_
->auth_service();
83 void GDataWapiService::Initialize(Profile
* profile
) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
87 std::vector
<std::string
> scopes
;
88 scopes
.push_back(kDocsListScope
);
89 scopes
.push_back(kSpreadsheetsScope
);
90 scopes
.push_back(kUserContentScope
);
91 // Drive App scope is required for even WAPI v3 apps access.
92 scopes
.push_back(kDriveAppsScope
);
93 runner_
.reset(new OperationRunner(profile
, scopes
));
94 runner_
->Initialize();
97 OperationRegistry
* GDataWapiService::operation_registry() const {
98 return runner_
->operation_registry();
101 void GDataWapiService::CancelAll() {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
103 runner_
->CancelAll();
106 void GDataWapiService::Authenticate(const AuthStatusCallback
& callback
) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
108 runner_
->Authenticate(callback
);
111 void GDataWapiService::GetDocuments(const GURL
& url
,
112 int64 start_changestamp
,
113 const std::string
& search_query
,
114 const std::string
& directory_resource_id
,
115 const GetDataCallback
& callback
) {
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
118 // Drive V2 API defines changestamp in int64, while DocumentsList API uses
119 // int32. This narrowing should not cause any trouble.
120 GetDocumentsOperation
* operation
=
121 new GetDocumentsOperation(operation_registry(),
123 static_cast<int>(start_changestamp
),
125 directory_resource_id
,
127 runner_
->StartOperationWithRetry(operation
);
130 void GDataWapiService::GetDocumentEntry(const std::string
& resource_id
,
131 const GetDataCallback
& callback
) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
134 GetDocumentEntryOperation
* operation
=
135 new GetDocumentEntryOperation(operation_registry(),
138 runner_
->StartOperationWithRetry(operation
);
141 void GDataWapiService::GetAccountMetadata(const GetDataCallback
& callback
) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
144 GetAccountMetadataOperation
* operation
=
145 new GetAccountMetadataOperation(operation_registry(), callback
);
146 runner_
->StartOperationWithRetry(operation
);
149 void GDataWapiService::GetApplicationInfo(const GetDataCallback
& callback
) {
150 // For WAPI, AccountMetadata includes Drive application information.
151 GetAccountMetadata(callback
);
154 void GDataWapiService::DownloadDocument(
155 const FilePath
& virtual_path
,
156 const FilePath
& local_cache_path
,
157 const GURL
& document_url
,
158 DocumentExportFormat format
,
159 const DownloadActionCallback
& callback
) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
165 chrome_common_net::AppendQueryParameter(document_url
,
167 GetExportFormatParam(format
)),
169 GetContentCallback());
172 void GDataWapiService::DownloadFile(
173 const FilePath
& virtual_path
,
174 const FilePath
& local_cache_path
,
175 const GURL
& document_url
,
176 const DownloadActionCallback
& download_action_callback
,
177 const GetContentCallback
& get_content_callback
) {
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
180 runner_
->StartOperationWithRetry(
181 new DownloadFileOperation(operation_registry(),
182 download_action_callback
,
183 get_content_callback
, document_url
,
184 virtual_path
, local_cache_path
));
187 void GDataWapiService::DeleteDocument(const GURL
& document_url
,
188 const EntryActionCallback
& callback
) {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
191 runner_
->StartOperationWithRetry(
192 new DeleteDocumentOperation(operation_registry(), callback
,
196 void GDataWapiService::CreateDirectory(
197 const GURL
& parent_content_url
,
198 const FilePath::StringType
& directory_name
,
199 const GetDataCallback
& callback
) {
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
202 runner_
->StartOperationWithRetry(
203 new CreateDirectoryOperation(operation_registry(), callback
,
204 parent_content_url
, directory_name
));
207 void GDataWapiService::CopyDocument(const std::string
& resource_id
,
208 const FilePath::StringType
& new_name
,
209 const GetDataCallback
& callback
) {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
212 runner_
->StartOperationWithRetry(
213 new CopyDocumentOperation(operation_registry(), callback
,
214 resource_id
, new_name
));
217 void GDataWapiService::RenameResource(const GURL
& resource_url
,
218 const FilePath::StringType
& new_name
,
219 const EntryActionCallback
& callback
) {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
222 runner_
->StartOperationWithRetry(
223 new RenameResourceOperation(operation_registry(), callback
,
224 resource_url
, new_name
));
227 void GDataWapiService::AddResourceToDirectory(
228 const GURL
& parent_content_url
,
229 const GURL
& resource_url
,
230 const EntryActionCallback
& callback
) {
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
233 runner_
->StartOperationWithRetry(
234 new AddResourceToDirectoryOperation(operation_registry(),
240 void GDataWapiService::RemoveResourceFromDirectory(
241 const GURL
& parent_content_url
,
242 const GURL
& resource_url
,
243 const std::string
& resource_id
,
244 const EntryActionCallback
& callback
) {
245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
247 runner_
->StartOperationWithRetry(
248 new RemoveResourceFromDirectoryOperation(operation_registry(),
255 void GDataWapiService::InitiateUpload(const InitiateUploadParams
& params
,
256 const InitiateUploadCallback
& callback
) {
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
259 if (params
.upload_location
.is_empty()) {
260 if (!callback
.is_null())
261 callback
.Run(HTTP_BAD_REQUEST
, GURL());
265 runner_
->StartOperationWithRetry(
266 new InitiateUploadOperation(operation_registry(), callback
, params
));
269 void GDataWapiService::ResumeUpload(const ResumeUploadParams
& params
,
270 const ResumeUploadCallback
& callback
) {
271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
273 runner_
->StartOperationWithRetry(
274 new ResumeUploadOperation(operation_registry(), callback
, params
));
278 void GDataWapiService::AuthorizeApp(const GURL
& resource_url
,
279 const std::string
& app_ids
,
280 const GetDataCallback
& callback
) {
281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
283 runner_
->StartOperationWithRetry(
284 new AuthorizeAppsOperation(operation_registry(), callback
,
285 resource_url
, app_ids
));
288 bool GDataWapiService::HasAccessToken() const {
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
291 return runner_
->auth_service()->HasAccessToken();
294 bool GDataWapiService::HasRefreshToken() const {
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
297 return runner_
->auth_service()->HasRefreshToken();