1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/google_apis/gdata_wapi_service.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/message_loop_proxy.h"
#include "chrome/browser/google_apis/auth_service.h"
#include "chrome/browser/google_apis/drive_api_operations.h"
#include "chrome/browser/google_apis/gdata_operations.h"
#include "chrome/browser/google_apis/gdata_util.h"
#include "chrome/browser/google_apis/operation_runner.h"
#include "chrome/common/net/url_util.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace drive {
namespace {
const char* GetExportFormatParam(DocumentExportFormat format) {
switch (format) {
case PNG:
return "png";
case HTML:
return "html";
case TXT:
return "txt";
case DOC:
return "doc";
case ODT:
return "odt";
case RTF:
return "rtf";
case ZIP:
return "zip";
case JPEG:
return "jpeg";
case SVG:
return "svg";
case PPT:
return "ppt";
case XLS:
return "xls";
case CSV:
return "csv";
case ODS:
return "ods";
case TSV:
return "tsv";
default:
return "pdf";
}
}
// OAuth2 scopes for the documents API.
const char kDocsListScope[] = "https://docs.google.com/feeds/";
const char kSpreadsheetsScope[] = "https://spreadsheets.google.com/feeds/";
const char kUserContentScope[] = "https://docs.googleusercontent.com/";
const char kDriveAppsScope[] = "https://www.googleapis.com/auth/drive.apps";
} // namespace
GDataWapiService::GDataWapiService()
: runner_(NULL) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
GDataWapiService::~GDataWapiService() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (runner_.get()) {
runner_->operation_registry()->RemoveObserver(this);
runner_->auth_service()->RemoveObserver(this);
}
}
gdata::AuthService* GDataWapiService::auth_service_for_testing() {
return runner_->auth_service();
}
void GDataWapiService::Initialize(Profile* profile) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
std::vector<std::string> scopes;
scopes.push_back(kDocsListScope);
scopes.push_back(kSpreadsheetsScope);
scopes.push_back(kUserContentScope);
// Drive App scope is required for even WAPI v3 apps access.
scopes.push_back(kDriveAppsScope);
runner_.reset(new gdata::OperationRunner(profile, scopes));
runner_->Initialize();
runner_->auth_service()->AddObserver(this);
runner_->operation_registry()->AddObserver(this);
}
void GDataWapiService::AddObserver(DriveServiceObserver* observer) {
observers_.AddObserver(observer);
}
void GDataWapiService::RemoveObserver(DriveServiceObserver* observer) {
observers_.RemoveObserver(observer);
}
bool GDataWapiService::CanStartOperation() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return HasRefreshToken();
}
void GDataWapiService::CancelAll() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->CancelAll();
}
bool GDataWapiService::CancelForFilePath(const FilePath& file_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return operation_registry()->CancelForFilePath(file_path);
}
gdata::OperationProgressStatusList GDataWapiService::GetProgressStatusList()
const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return operation_registry()->GetProgressStatusList();
}
void GDataWapiService::Authenticate(const gdata::AuthStatusCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->Authenticate(callback);
}
void GDataWapiService::GetDocuments(const GURL& url,
int64 start_changestamp,
const std::string& search_query,
const std::string& directory_resource_id,
const gdata::GetDataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Drive V2 API defines changestamp in int64, while DocumentsList API uses
// int32. This narrowing should not cause any trouble.
gdata::GetDocumentsOperation* operation =
new gdata::GetDocumentsOperation(operation_registry(),
url,
static_cast<int>(start_changestamp),
search_query,
directory_resource_id,
callback);
runner_->StartOperationWithRetry(operation);
}
void GDataWapiService::GetDocumentEntry(
const std::string& resource_id,
const gdata::GetDataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
gdata::GetDocumentEntryOperation* operation =
new gdata::GetDocumentEntryOperation(operation_registry(),
resource_id,
callback);
runner_->StartOperationWithRetry(operation);
}
void GDataWapiService::GetAccountMetadata(
const gdata::GetDataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
gdata::GetAccountMetadataOperation* operation =
new gdata::GetAccountMetadataOperation(operation_registry(), callback);
runner_->StartOperationWithRetry(operation);
}
void GDataWapiService::GetApplicationInfo(
const gdata::GetDataCallback& callback) {
// For WAPI, AccountMetadata includes Drive application information.
GetAccountMetadata(callback);
}
void GDataWapiService::DownloadDocument(
const FilePath& virtual_path,
const FilePath& local_cache_path,
const GURL& document_url,
DocumentExportFormat format,
const gdata::DownloadActionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DownloadFile(
virtual_path,
local_cache_path,
chrome_common_net::AppendQueryParameter(document_url,
"exportFormat",
GetExportFormatParam(format)),
callback,
gdata::GetContentCallback());
}
void GDataWapiService::DownloadFile(
const FilePath& virtual_path,
const FilePath& local_cache_path,
const GURL& document_url,
const gdata::DownloadActionCallback& download_action_callback,
const gdata::GetContentCallback& get_content_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::DownloadFileOperation(operation_registry(),
download_action_callback,
get_content_callback, document_url,
virtual_path, local_cache_path));
}
void GDataWapiService::DeleteDocument(
const GURL& document_url,
const gdata::EntryActionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::DeleteDocumentOperation(operation_registry(), callback,
document_url));
}
void GDataWapiService::CreateDirectory(
const GURL& parent_content_url,
const FilePath::StringType& directory_name,
const gdata::GetDataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::CreateDirectoryOperation(operation_registry(), callback,
parent_content_url, directory_name));
}
void GDataWapiService::CopyDocument(const std::string& resource_id,
const FilePath::StringType& new_name,
const gdata::GetDataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::CopyDocumentOperation(operation_registry(), callback,
resource_id, new_name));
}
void GDataWapiService::RenameResource(
const GURL& resource_url,
const FilePath::StringType& new_name,
const gdata::EntryActionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::RenameResourceOperation(operation_registry(), callback,
resource_url, new_name));
}
void GDataWapiService::AddResourceToDirectory(
const GURL& parent_content_url,
const GURL& resource_url,
const gdata::EntryActionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::AddResourceToDirectoryOperation(operation_registry(),
callback,
parent_content_url,
resource_url));
}
void GDataWapiService::RemoveResourceFromDirectory(
const GURL& parent_content_url,
const GURL& resource_url,
const std::string& resource_id,
const gdata::EntryActionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::RemoveResourceFromDirectoryOperation(operation_registry(),
callback,
parent_content_url,
resource_url,
resource_id));
}
void GDataWapiService::InitiateUpload(
const gdata::InitiateUploadParams& params,
const gdata::InitiateUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (params.upload_location.is_empty()) {
if (!callback.is_null())
callback.Run(gdata::HTTP_BAD_REQUEST, GURL());
return;
}
runner_->StartOperationWithRetry(
new gdata::InitiateUploadOperation(
operation_registry(), callback, params));
}
void GDataWapiService::ResumeUpload(
const gdata::ResumeUploadParams& params,
const gdata::ResumeUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::ResumeUploadOperation(
operation_registry(), callback, params));
}
void GDataWapiService::AuthorizeApp(const GURL& resource_url,
const std::string& app_ids,
const gdata::GetDataCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
runner_->StartOperationWithRetry(
new gdata::AuthorizeAppsOperation(operation_registry(), callback,
resource_url, app_ids));
}
bool GDataWapiService::HasAccessToken() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return runner_->auth_service()->HasAccessToken();
}
bool GDataWapiService::HasRefreshToken() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return runner_->auth_service()->HasRefreshToken();
}
gdata::OperationRegistry* GDataWapiService::operation_registry() const {
return runner_->operation_registry();
}
void GDataWapiService::OnOAuth2RefreshTokenChanged() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (CanStartOperation()) {
FOR_EACH_OBSERVER(
DriveServiceObserver, observers_, OnReadyToPerformOperations());
}
}
void GDataWapiService::OnProgressUpdate(
const gdata::OperationProgressStatusList& list) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FOR_EACH_OBSERVER(
DriveServiceObserver, observers_, OnProgressUpdate(list));
}
void GDataWapiService::OnAuthenticationFailed(gdata::GDataErrorCode error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
FOR_EACH_OBSERVER(
DriveServiceObserver, observers_, OnAuthenticationFailed(error));
}
} // namespace drive
|