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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
// 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.
#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATIONS_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATIONS_H_
#pragma once
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "base/stringprintf.h"
#include "chrome/browser/chromeos/gdata/gdata_operation_registry.h"
#include "chrome/browser/chromeos/gdata/gdata_params.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/net/gaia/oauth2_access_token_consumer.h"
#include "chrome/common/net/gaia/oauth2_access_token_fetcher.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/load_flags.h"
#include "net/http/http_response_headers.h"
namespace gdata {
// Template for optional OAuth2 authorization HTTP header.
const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
// Template for GData API version HTTP header.
const char kGDataVersionHeader[] = "GData-Version: 3.0";
// Maximum number of attempts for re-authentication per operation.
const int kMaxReAuthenticateAttemptsPerOperation = 1;
//================================ AuthOperation ===============================
// OAuth2 authorization token retrieval operation.
class AuthOperation : public GDataOperationRegistry::Operation,
public OAuth2AccessTokenConsumer {
public:
AuthOperation(GDataOperationRegistry* registry,
Profile* profile,
const AuthStatusCallback& callback,
const std::string& refresh_token);
virtual ~AuthOperation();
void Start();
// Overridden from OAuth2AccessTokenConsumer:
virtual void OnGetTokenSuccess(const std::string& access_token) OVERRIDE;
virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
// Overridden from GDataOpertionRegistry::Operation
virtual void DoCancel() OVERRIDE;
private:
Profile* profile_;
std::string token_;
AuthStatusCallback callback_;
scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_;
DISALLOW_COPY_AND_ASSIGN(AuthOperation);
};
//=========================== GDataOperationInterface ==========================
// An interface for implementing an operation used by DocumentsService.
class GDataOperationInterface {
public:
// Callback to DocumentsService upon for re-authentication.
typedef base::Callback<void(GDataOperationInterface* operation)>
ReAuthenticateCallback;
virtual ~GDataOperationInterface() {}
// Starts the actual operation after obtaining an authentication token
// |auth_token|.
virtual void Start(const std::string& auth_token) = 0;
// Invoked when the authentication failed with an error code |code|.
virtual void OnAuthFailed(GDataErrorCode code) = 0;
// Sets the callback to DocumentsService when the operation restarts due to
// an authentication failure.
virtual void SetReAuthenticateCallback(
const ReAuthenticateCallback& callback) = 0;
};
//============================== UrlFetchOperation =============================
// Base class for operations that are fetching URLs.
template <typename T>
class UrlFetchOperation : public GDataOperationInterface,
public GDataOperationRegistry::Operation,
public content::URLFetcherDelegate {
public:
typedef T CompletionCallback;
UrlFetchOperation(GDataOperationRegistry* registry,
Profile* profile,
const CompletionCallback& callback)
: GDataOperationRegistry::Operation(registry),
profile_(profile),
// Completion callback runs on the origin thread that creates
// this operation object.
callback_(callback),
// MessageLoopProxy is used to run |callback| on the origin thread.
relay_proxy_(base::MessageLoopProxy::current()),
re_authenticate_count_(0),
save_temp_file_(false) {
}
// Overridden from GDataOperationInterface.
virtual void Start(const std::string& auth_token) OVERRIDE {
DCHECK(!auth_token.empty());
GURL url = GetURL();
DCHECK(!url.is_empty());
DVLOG(1) << "URL: " << url.spec();
url_fetcher_.reset(content::URLFetcher::Create(url, GetRequestType(),
this));
url_fetcher_->SetRequestContext(profile_->GetRequestContext());
// Always set flags to neither send nor save cookies.
url_fetcher_->SetLoadFlags(
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
if (save_temp_file_) {
url_fetcher_->SaveResponseToTemporaryFile(
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::FILE));
}
// Add request headers.
// Note that SetExtraRequestHeaders clears the current headers and sets it
// to the passed-in headers, so calling it for each header will result in
// only the last header being set in request headers.
url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader);
url_fetcher_->AddExtraRequestHeader(
base::StringPrintf(kAuthorizationHeaderFormat, auth_token.data()));
std::vector<std::string> headers = GetExtraRequestHeaders();
for (size_t i = 0; i < headers.size(); ++i) {
url_fetcher_->AddExtraRequestHeader(headers[i]);
DVLOG(1) << "Extra header: " << headers[i];
}
// Set upload data if available.
std::string upload_content_type;
std::string upload_content;
if (GetContentData(&upload_content_type, &upload_content)) {
url_fetcher_->SetUploadData(upload_content_type, upload_content);
}
// Register to operation registry.
NotifyStart();
url_fetcher_->Start();
}
// Overridden from GDataOperationInterface.
virtual void SetReAuthenticateCallback(
const ReAuthenticateCallback& callback) OVERRIDE {
DCHECK(re_authenticate_callback_.is_null());
re_authenticate_callback_ = callback;
}
protected:
virtual ~UrlFetchOperation() {}
// Gets URL for the request.
virtual GURL GetURL() const = 0;
// Returns the request type. A derived class should override this method
// for a request type other than HTTP GET.
virtual content::URLFetcher::RequestType GetRequestType() const {
return content::URLFetcher::GET;
}
// Returns the extra HTTP headers for the request. A derived class should
// override this method to specify any extra headers needed for the request.
virtual std::vector<std::string> GetExtraRequestHeaders() const {
return std::vector<std::string>();
}
// Used by a derived class to add any content data to the request.
// Returns true if |upload_content_type| and |upload_content| are updated
// with the content type and data for the request.
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) {
return false;
}
// Invoked by OnURLFetchComplete when the operation completes without an
// authentication error. Must be implemented by a derived class.
virtual void ProcessURLFetchResults(const content::URLFetcher* source) = 0;
// Invoked by this base class upon an authentication error.
// Must be implemented by a derived class.
virtual void RunCallbackOnAuthFailed(GDataErrorCode code) = 0;
// Implement GDataOperationRegistry::Operation
virtual void DoCancel() OVERRIDE {
url_fetcher_.reset(NULL);
}
// Implement URLFetcherDelegate.
// TODO(kinaba): http://crosbug.com/27370
// Current URLFetcherDelegate notifies only the progress of "download"
// transfers, and no notification for upload progress in POST/PUT.
// For some GData operations, however, progress of uploading transfer makes
// more sense. We need to add a way to track upload status.
virtual void OnURLFetchDownloadProgress(const content::URLFetcher* source,
int64 current, int64 total) OVERRIDE {
NotifyProgress(current, total);
}
// Overridden from URLFetcherDelegate.
virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE {
GDataErrorCode code =
static_cast<GDataErrorCode>(source->GetResponseCode());
DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source);
if (code == HTTP_UNAUTHORIZED) {
if (!re_authenticate_callback_.is_null() &&
++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerOperation) {
re_authenticate_callback_.Run(this);
return;
}
OnAuthFailed(code);
return;
}
// Overridden by each specialization
ProcessURLFetchResults(source);
NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
}
// Overridden from GDataOperationInterface.
virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE {
RunCallbackOnAuthFailed(code);
NotifyFinish(GDataOperationRegistry::OPERATION_FAILED);
}
std::string GetResponseHeadersAsString(
const content::URLFetcher* url_fetcher) {
// net::HttpResponseHeaders::raw_headers(), as the name implies, stores
// all headers in their raw format, i.e each header is null-terminated.
// So logging raw_headers() only shows the first header, which is probably
// the status line. GetNormalizedHeaders, on the other hand, will show all
// the headers, one per line, which is probably what we want.
std::string headers;
// Check that response code indicates response headers are valid (i.e. not
// malformed) before we retrieve the headers.
if (url_fetcher->GetResponseCode() ==
content::URLFetcher::RESPONSE_CODE_INVALID) {
headers.assign("Response headers are malformed!!");
} else {
url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers);
}
return headers;
}
Profile* profile_;
CompletionCallback callback_;
ReAuthenticateCallback re_authenticate_callback_;
scoped_refptr<base::MessageLoopProxy> relay_proxy_;
int re_authenticate_count_;
bool save_temp_file_;
scoped_ptr<content::URLFetcher> url_fetcher_;
};
//============================ EntryActionOperation ============================
// This class performs a simple action over a given entry (document/file).
// It is meant to be used for operations that return no JSON blobs.
class EntryActionOperation : public UrlFetchOperation<EntryActionCallback> {
public:
EntryActionOperation(GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& document_url);
virtual ~EntryActionOperation();
protected:
// Overridden from UrlFetchOperation.
virtual GURL GetURL() const OVERRIDE;
virtual void ProcessURLFetchResults(const content::URLFetcher* source)
OVERRIDE;
virtual void RunCallbackOnAuthFailed(GDataErrorCode code) OVERRIDE;
const GURL& document_url() const { return document_url_; }
private:
GURL document_url_;
DISALLOW_COPY_AND_ASSIGN(EntryActionOperation);
};
//============================== GetDataOperation ==============================
// This class performs the operation for fetching and parsing JSON data content.
class GetDataOperation : public UrlFetchOperation<GetDataCallback> {
public:
GetDataOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback);
virtual ~GetDataOperation();
protected:
// Overridden from UrlFetchOperation.
virtual void ProcessURLFetchResults(const content::URLFetcher* source)
OVERRIDE;
virtual void RunCallbackOnAuthFailed(GDataErrorCode code) OVERRIDE;
// Parse GData JSON response.
static base::Value* ParseResponse(const std::string& data);
private:
DISALLOW_COPY_AND_ASSIGN(GetDataOperation);
};
//============================ GetDocumentsOperation ===========================
// This class performs the operation for fetching a document list.
class GetDocumentsOperation : public GetDataOperation {
public:
GetDocumentsOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback);
virtual ~GetDocumentsOperation();
// Sets |url| for document fetching operation. This URL should be set in use
// case when additional 'pages' of document lists are being fetched.
void SetUrl(const GURL& url);
protected:
// Overridden from GetDataOperation.
virtual GURL GetURL() const OVERRIDE;
private:
GURL override_url_;
DISALLOW_COPY_AND_ASSIGN(GetDocumentsOperation);
};
//========================= GetAccountMetadataOperation ========================
// This class performs the operation for fetching account metadata.
class GetAccountMetadataOperation : public GetDataOperation {
public:
GetAccountMetadataOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback);
virtual ~GetAccountMetadataOperation();
protected:
// Overridden from GetDataOperation.
virtual GURL GetURL() const OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(GetAccountMetadataOperation);
};
//============================ DownloadFileOperation ===========================
// This class performs the operation for downloading of a given document/file.
class DownloadFileOperation : public UrlFetchOperation<DownloadActionCallback> {
public:
DownloadFileOperation(GDataOperationRegistry* registry,
Profile* profile,
const DownloadActionCallback& callback,
const GURL& document_url);
virtual ~DownloadFileOperation();
protected:
// Overridden from UrlFetchOperation.
virtual GURL GetURL() const OVERRIDE;
virtual void ProcessURLFetchResults(const content::URLFetcher* source)
OVERRIDE;
virtual void RunCallbackOnAuthFailed(GDataErrorCode code) OVERRIDE;
private:
GURL document_url_;
DISALLOW_COPY_AND_ASSIGN(DownloadFileOperation);
};
//=========================== DeleteDocumentOperation ==========================
// This class performs the operation for deleting a document.
class DeleteDocumentOperation : public EntryActionOperation {
public:
DeleteDocumentOperation(GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& document_url);
virtual ~DeleteDocumentOperation();
protected:
// Overridden from EntryActionOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(DeleteDocumentOperation);
};
//========================== CreateDirectoryOperation ==========================
// This class performs the operation for creating a directory.
class CreateDirectoryOperation : public GetDataOperation {
public:
// Empty |parent_content_url| will create the directory in the root folder.
CreateDirectoryOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback,
const GURL& parent_content_url,
const FilePath::StringType& directory_name);
virtual ~CreateDirectoryOperation();
protected:
// Overridden from UrlFetchOperation.
virtual GURL GetURL() const OVERRIDE;
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
// Overridden from UrlFetchOperation.
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;
private:
GURL parent_content_url_;
FilePath::StringType directory_name_;
DISALLOW_COPY_AND_ASSIGN(CreateDirectoryOperation);
};
//============================ CopyDocumentOperation ===========================
// This class performs the operation for making a copy of a document.
class CopyDocumentOperation : public GetDataOperation {
public:
CopyDocumentOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback,
const GURL& document_url,
const FilePath::StringType& new_name);
virtual ~CopyDocumentOperation();
protected:
// Overridden from GetDataOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
// Overridden from UrlFetchOperation.
virtual GURL GetURL() const OVERRIDE;
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;
private:
GURL document_url_;
FilePath::StringType new_name_;
DISALLOW_COPY_AND_ASSIGN(CopyDocumentOperation);
};
//=========================== RenameResourceOperation ==========================
// This class performs the operation for renaming a document/file/directory.
class RenameResourceOperation : public EntryActionOperation {
public:
RenameResourceOperation(GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& document_url,
const FilePath::StringType& new_name);
virtual ~RenameResourceOperation();
protected:
// Overridden from EntryActionOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
// Overridden from UrlFetchOperation.
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;
private:
FilePath::StringType new_name_;
DISALLOW_COPY_AND_ASSIGN(RenameResourceOperation);
};
//======================= AddResourceToDirectoryOperation ======================
// This class performs the operation for adding a document/file/directory
// to a directory.
class AddResourceToDirectoryOperation : public EntryActionOperation {
public:
AddResourceToDirectoryOperation(GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& parent_content_url,
const GURL& document_url);
virtual ~AddResourceToDirectoryOperation();
protected:
// Overridden from EntryActionOperation.
virtual GURL GetURL() const OVERRIDE;
// Overridden from UrlFetchOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;
private:
GURL parent_content_url_;
DISALLOW_COPY_AND_ASSIGN(AddResourceToDirectoryOperation);
};
//==================== RemoveResourceFromDirectoryOperation ====================
// This class performs the operation for adding a document/file/directory
// from a directory.
class RemoveResourceFromDirectoryOperation : public EntryActionOperation {
public:
RemoveResourceFromDirectoryOperation(GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& parent_content_url,
const GURL& document_url,
const std::string& resource_id);
virtual ~RemoveResourceFromDirectoryOperation();
protected:
// Overridden from EntryActionOperation.
virtual GURL GetURL() const OVERRIDE;
// Overridden from UrlFetchOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
private:
std::string resource_id_;
GURL parent_content_url_;
DISALLOW_COPY_AND_ASSIGN(RemoveResourceFromDirectoryOperation);
};
//=========================== InitiateUploadOperation ==========================
// This class performs the operation for initiating the upload of a file.
class InitiateUploadOperation
: public UrlFetchOperation<InitiateUploadCallback> {
public:
InitiateUploadOperation(GDataOperationRegistry* registry,
Profile* profile,
const InitiateUploadCallback& callback,
const InitiateUploadParams& params);
virtual ~InitiateUploadOperation();
protected:
// Overridden from UrlFetchOperation.
virtual GURL GetURL() const OVERRIDE;
virtual void ProcessURLFetchResults(const content::URLFetcher* source)
OVERRIDE;
virtual void RunCallbackOnAuthFailed(GDataErrorCode code) OVERRIDE;
// Overridden from UrlFetchOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;
private:
InitiateUploadParams params_;
GURL initiate_upload_url_;
DISALLOW_COPY_AND_ASSIGN(InitiateUploadOperation);
};
//============================ ResumeUploadOperation ===========================
// This class performs the operation for resuming the upload of a file.
class ResumeUploadOperation : public UrlFetchOperation<ResumeUploadCallback> {
public:
ResumeUploadOperation(GDataOperationRegistry* registry,
Profile* profile,
const ResumeUploadCallback& callback,
const ResumeUploadParams& params);
virtual ~ResumeUploadOperation();
protected:
// Overridden from UrlFetchOperation.
virtual GURL GetURL() const OVERRIDE;
virtual void ProcessURLFetchResults(const content::URLFetcher* source)
OVERRIDE;
virtual void RunCallbackOnAuthFailed(GDataErrorCode code) OVERRIDE;
// Overridden from UrlFetchOperation.
virtual content::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE;
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;
private:
ResumeUploadParams params_;
DISALLOW_COPY_AND_ASSIGN(ResumeUploadOperation);
};
} // namespace gdata
#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_OPERATIONS_H_
|