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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
|
// 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/chromeos/gdata/gdata_operations.h"
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/metrics/histogram.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/gdata/gdata_file_system.h"
#include "chrome/browser/chromeos/gdata/gdata_util.h"
#include "chrome/common/libxml_utils.h"
#include "chrome/common/net/gaia/gaia_urls.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
#include "chrome/common/net/url_util.h"
#include "net/base/escape.h"
#include "net/http/http_util.h"
using content::BrowserThread;
using content::URLFetcher;
namespace {
// Used for success ratio histograms. 0 for failure, 1 for success,
// 2 for no connection (likely offline).
const int kSuccessRatioHistogramFailure = 0;
const int kSuccessRatioHistogramSuccess = 1;
const int kSuccessRatioHistogramNoConnection = 2;
const int kSuccessRatioHistogramMaxValue = 3; // The max value is exclusive.
// 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";
// etag matching header.
const char kIfMatchAllHeader[] = "If-Match: *";
const char kIfMatchHeaderFormat[] = "If-Match: %s";
// URL requesting documents list that belong to the authenticated user only
// (handled with '-/mine' part).
const char kGetDocumentListURL[] =
"https://docs.google.com/feeds/default/private/full/-/mine";
// URL requesting documents list of changes to documents collections.
const char kGetChangesListURL[] =
"https://docs.google.com/feeds/default/private/changes";
// Root document list url.
const char kDocumentListRootURL[] =
"https://docs.google.com/feeds/default/private/full";
// Metadata feed with things like user quota.
const char kAccountMetadataURL[] =
"https://docs.google.com/feeds/metadata/default";
const char kUploadContentRange[] = "Content-Range: bytes ";
const char kUploadContentType[] = "X-Upload-Content-Type: ";
const char kUploadContentLength[] = "X-Upload-Content-Length: ";
#ifndef NDEBUG
// Use smaller 'page' size while debugging to ensure we hit feed reload
// almost always. Be careful not to use something too small on account that
// have many items because server side 503 error might kick in.
const int kMaxDocumentsPerFeed = 1000;
#else
const int kMaxDocumentsPerFeed = 1000;
#endif
// Maximum number of attempts for re-authentication per operation.
const int kMaxReAuthenticateAttemptsPerOperation = 1;
const char kFeedField[] = "feed";
// Templates for file uploading.
const char kUploadParamConvertKey[] = "convert";
const char kUploadParamConvertValue[] = "false";
const char kUploadResponseLocation[] = "location";
const char kUploadResponseRange[] = "range";
// OAuth scope 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/";
// Adds additional parameters for API version, output content type and to show
// folders in the feed are added to document feed URLs.
GURL AddStandardUrlParams(const GURL& url) {
GURL result =
chrome_common_net::AppendOrReplaceQueryParameter(url, "v", "3");
result =
chrome_common_net::AppendOrReplaceQueryParameter(result, "alt", "json");
return result;
}
// Adds additional parameters to metadata feed to include installed 3rd party
// applications.
GURL AddMetadataUrlParams(const GURL& url) {
GURL result = AddStandardUrlParams(url);
result = chrome_common_net::AppendOrReplaceQueryParameter(
result, "include-installed-apps", "true");
return result;
}
// Adds additional parameters for API version, output content type and to show
// folders in the feed are added to document feed URLs.
GURL AddFeedUrlParams(const GURL& url,
int num_items_to_fetch,
int changestamp,
const std::string& search_string) {
GURL result = AddStandardUrlParams(url);
result = chrome_common_net::AppendOrReplaceQueryParameter(
result,
"showfolders",
"true");
result = chrome_common_net::AppendOrReplaceQueryParameter(
result,
"max-results",
base::StringPrintf("%d", num_items_to_fetch));
result = chrome_common_net::AppendOrReplaceQueryParameter(
result, "include-installed-apps", "true");
if (changestamp) {
result = chrome_common_net::AppendQueryParameter(
result,
"start-index",
base::StringPrintf("%d", changestamp));
}
if (!search_string.empty()) {
result = chrome_common_net::AppendOrReplaceQueryParameter(
result, "q", search_string);
}
return result;
}
} // namespace
namespace gdata {
//================================ AuthOperation ===============================
AuthOperation::AuthOperation(GDataOperationRegistry* registry,
Profile* profile,
const AuthStatusCallback& callback,
const std::string& refresh_token)
: GDataOperationRegistry::Operation(registry),
profile_(profile), token_(refresh_token), callback_(callback) {
}
AuthOperation::~AuthOperation() {}
void AuthOperation::Start() {
DCHECK(!token_.empty());
std::vector<std::string> scopes;
scopes.push_back(kDocsListScope);
scopes.push_back(kSpreadsheetsScope);
scopes.push_back(kUserContentScope);
oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher(
this, g_browser_process->system_request_context()));
NotifyStart();
oauth2_access_token_fetcher_->Start(
GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
token_,
scopes);
}
void AuthOperation::DoCancel() {
oauth2_access_token_fetcher_->CancelRequest();
if (!callback_.is_null())
callback_.Run(GDATA_CANCELLED, std::string());
}
// Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token
// used to start fetching user data.
void AuthOperation::OnGetTokenSuccess(const std::string& access_token) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess",
kSuccessRatioHistogramSuccess,
kSuccessRatioHistogramMaxValue);
callback_.Run(HTTP_SUCCESS, access_token);
NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
}
// Callback for OAuth2AccessTokenFetcher on failure.
void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
LOG(WARNING) << "AuthOperation: token request using refresh token failed"
<< error.ToString();
// There are many ways to fail, but if the failure is due to connection,
// it's likely that the device is off-line. We treat the error differently
// so that the file manager works while off-line.
if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) {
UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess",
kSuccessRatioHistogramNoConnection,
kSuccessRatioHistogramMaxValue);
callback_.Run(GDATA_NO_CONNECTION, std::string());
} else {
UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess",
kSuccessRatioHistogramFailure,
kSuccessRatioHistogramMaxValue);
callback_.Run(HTTP_UNAUTHORIZED, std::string());
}
NotifyFinish(GDataOperationRegistry::OPERATION_FAILED);
}
//============================ UrlFetchOperationBase ===========================
UrlFetchOperationBase::UrlFetchOperationBase(GDataOperationRegistry* registry,
Profile* profile)
: GDataOperationRegistry::Operation(registry),
profile_(profile),
re_authenticate_count_(0),
save_temp_file_(false),
started_(false) {
}
UrlFetchOperationBase::UrlFetchOperationBase(
GDataOperationRegistry* registry,
GDataOperationRegistry::OperationType type,
const FilePath& path,
Profile* profile)
: GDataOperationRegistry::Operation(registry, type, path),
profile_(profile),
re_authenticate_count_(0),
save_temp_file_(false) {
}
UrlFetchOperationBase::~UrlFetchOperationBase() {}
void UrlFetchOperationBase::Start(const std::string& auth_token) {
DCHECK(!auth_token.empty());
GURL url = GetURL();
DCHECK(!url.is_empty());
DVLOG(1) << "URL: " << url.spec();
url_fetcher_.reset(URLFetcher::Create(url, GetRequestType(), this));
url_fetcher_->SetRequestContext(g_browser_process->system_request_context());
// Always set flags to neither send nor save cookies.
url_fetcher_->SetLoadFlags(
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DISABLE_CACHE);
if (save_temp_file_) {
url_fetcher_->SaveResponseToTemporaryFile(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
} else if (!output_file_path_.empty()) {
url_fetcher_->SaveResponseToFileAtPath(output_file_path_,
BrowserThread::GetMessageLoopProxyForThread(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.
NotifyStartToOperationRegistry();
url_fetcher_->Start();
started_ = true;
}
void UrlFetchOperationBase::SetReAuthenticateCallback(
const ReAuthenticateCallback& callback) {
DCHECK(re_authenticate_callback_.is_null());
re_authenticate_callback_ = callback;
}
URLFetcher::RequestType UrlFetchOperationBase::GetRequestType() const {
return URLFetcher::GET;
}
std::vector<std::string> UrlFetchOperationBase::GetExtraRequestHeaders() const {
return std::vector<std::string>();
}
bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
return false;
}
void UrlFetchOperationBase::DoCancel() {
url_fetcher_.reset(NULL);
RunCallbackOnPrematureFailure(GDATA_CANCELLED);
}
void UrlFetchOperationBase::OnURLFetchComplete(const net::URLFetcher* source) {
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
bool success = ProcessURLFetchResults(source);
if (success)
NotifySuccessToOperationRegistry();
else
NotifyFinish(GDataOperationRegistry::OPERATION_FAILED);
}
void UrlFetchOperationBase::NotifySuccessToOperationRegistry() {
NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
}
void UrlFetchOperationBase::NotifyStartToOperationRegistry() {
NotifyStart();
}
void UrlFetchOperationBase::OnAuthFailed(GDataErrorCode code) {
RunCallbackOnPrematureFailure(code);
// Check if this failed before we even started fetching. If so, register
// for start so we can properly unregister with finish.
if (!started_)
NotifyStart();
NotifyFinish(GDataOperationRegistry::OPERATION_FAILED);
// Notify authentication failed.
NotifyAuthFailed();
}
std::string UrlFetchOperationBase::GetResponseHeadersAsString(
const net::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() == URLFetcher::RESPONSE_CODE_INVALID) {
headers.assign("Response headers are malformed!!");
} else {
url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers);
}
return headers;
}
//============================ EntryActionOperation ============================
EntryActionOperation::EntryActionOperation(GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& document_url)
: UrlFetchOperationBase(registry, profile),
callback_(callback),
document_url_(document_url) {
}
EntryActionOperation::~EntryActionOperation() {}
// Overridden from UrlFetchOperationBase.
GURL EntryActionOperation::GetURL() const {
return AddStandardUrlParams(document_url_);
}
bool EntryActionOperation::ProcessURLFetchResults(
const net::URLFetcher* source) {
if (!callback_.is_null()) {
GDataErrorCode code =
static_cast<GDataErrorCode>(source->GetResponseCode());
callback_.Run(code, document_url_);
}
return true;
}
void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
if (!callback_.is_null())
callback_.Run(code, document_url_);
}
//============================== GetDataOperation ==============================
GetDataOperation::GetDataOperation(GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback)
: UrlFetchOperationBase(registry, profile), callback_(callback) {
}
GetDataOperation::~GetDataOperation() {}
bool GetDataOperation::ProcessURLFetchResults(const net::URLFetcher* source) {
std::string data;
source->GetResponseAsString(&data);
scoped_ptr<base::Value> root_value;
GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode());
switch (code) {
case HTTP_SUCCESS:
case HTTP_CREATED: {
root_value.reset(ParseResponse(data));
if (!root_value.get())
code = GDATA_PARSE_ERROR;
break;
}
default:
break;
}
if (!callback_.is_null())
callback_.Run(code, root_value.Pass());
return root_value.get() != NULL;
}
void GetDataOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
if (!callback_.is_null()) {
scoped_ptr<base::Value> root_value;
callback_.Run(code, root_value.Pass());
}
}
// static
base::Value* GetDataOperation::ParseResponse(const std::string& data) {
int error_code = -1;
std::string error_message;
scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError(
data, base::JSON_PARSE_RFC, &error_code, &error_message));
if (!root_value.get()) {
LOG(ERROR) << "Error while parsing entry response: "
<< error_message
<< ", code: "
<< error_code
<< ", data:\n"
<< data;
return NULL;
}
return root_value.release();
}
//============================ GetDocumentsOperation ===========================
GetDocumentsOperation::GetDocumentsOperation(GDataOperationRegistry* registry,
Profile* profile,
int start_changestamp,
const std::string& search_string,
const GetDataCallback& callback)
: GetDataOperation(registry, profile, callback),
start_changestamp_(start_changestamp),
search_string_(search_string) {
}
GetDocumentsOperation::~GetDocumentsOperation() {}
void GetDocumentsOperation::SetUrl(const GURL& url) {
override_url_ = url;
}
GURL GetDocumentsOperation::GetURL() const {
if (!override_url_.is_empty())
return AddFeedUrlParams(override_url_,
kMaxDocumentsPerFeed,
0,
std::string());
if (start_changestamp_ == 0) {
return AddFeedUrlParams(GURL(kGetDocumentListURL),
kMaxDocumentsPerFeed,
0,
search_string_);
}
return AddFeedUrlParams(GURL(kGetChangesListURL),
kMaxDocumentsPerFeed,
start_changestamp_,
std::string());
}
//========================= GetAccountMetadataOperation ========================
GetAccountMetadataOperation::GetAccountMetadataOperation(
GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback)
: GetDataOperation(registry, profile, callback) {
}
GetAccountMetadataOperation::~GetAccountMetadataOperation() {}
GURL GetAccountMetadataOperation::GetURL() const {
return AddMetadataUrlParams(GURL(kAccountMetadataURL));
}
//============================ DownloadFileOperation ===========================
DownloadFileOperation::DownloadFileOperation(
GDataOperationRegistry* registry,
Profile* profile,
const DownloadActionCallback& download_action_callback,
const GetDownloadDataCallback& get_download_data_callback,
const GURL& document_url,
const FilePath& virtual_path,
const FilePath& output_file_path)
: UrlFetchOperationBase(registry,
GDataOperationRegistry::OPERATION_DOWNLOAD,
virtual_path,
profile),
download_action_callback_(download_action_callback),
get_download_data_callback_(get_download_data_callback),
document_url_(document_url) {
// Make sure we download the content into a temp file.
if (output_file_path.empty())
save_temp_file_ = true;
else
output_file_path_ = output_file_path;
}
DownloadFileOperation::~DownloadFileOperation() {}
// Overridden from UrlFetchOperationBase.
GURL DownloadFileOperation::GetURL() const {
return document_url_;
}
void DownloadFileOperation::OnURLFetchDownloadProgress(
const net::URLFetcher* source,
int64 current,
int64 total) {
NotifyProgress(current, total);
}
bool DownloadFileOperation::ShouldSendDownloadData() {
return !get_download_data_callback_.is_null();
}
void DownloadFileOperation::OnURLFetchDownloadData(
const net::URLFetcher* source,
scoped_ptr<std::string> download_data) {
if (!get_download_data_callback_.is_null())
get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass());
}
bool DownloadFileOperation::ProcessURLFetchResults(
const net::URLFetcher* source) {
GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode());
// Take over the ownership of the the downloaded temp file.
FilePath temp_file;
if (code == HTTP_SUCCESS &&
!source->GetResponseAsFilePath(true, // take_ownership
&temp_file)) {
code = GDATA_FILE_ERROR;
}
if (!download_action_callback_.is_null())
download_action_callback_.Run(code, document_url_, temp_file);
return code == HTTP_SUCCESS;
}
void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
if (!download_action_callback_.is_null())
download_action_callback_.Run(code, document_url_, FilePath());
}
//=========================== DeleteDocumentOperation ==========================
DeleteDocumentOperation::DeleteDocumentOperation(
GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& document_url)
: EntryActionOperation(registry, profile, callback, document_url) {
}
DeleteDocumentOperation::~DeleteDocumentOperation() {}
URLFetcher::RequestType DeleteDocumentOperation::GetRequestType() const {
return URLFetcher::DELETE_REQUEST;
}
std::vector<std::string>
DeleteDocumentOperation::GetExtraRequestHeaders() const {
std::vector<std::string> headers;
headers.push_back(kIfMatchAllHeader);
return headers;
}
//========================== CreateDirectoryOperation ==========================
CreateDirectoryOperation::CreateDirectoryOperation(
GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback,
const GURL& parent_content_url,
const FilePath::StringType& directory_name)
: GetDataOperation(registry, profile, callback),
parent_content_url_(parent_content_url),
directory_name_(directory_name) {
}
CreateDirectoryOperation::~CreateDirectoryOperation() {}
GURL CreateDirectoryOperation::GetURL() const {
if (!parent_content_url_.is_empty())
return AddStandardUrlParams(parent_content_url_);
return AddStandardUrlParams(GURL(kDocumentListRootURL));
}
URLFetcher::RequestType
CreateDirectoryOperation::GetRequestType() const {
return URLFetcher::POST;
}
bool CreateDirectoryOperation::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
upload_content_type->assign("application/atom+xml");
XmlWriter xml_writer;
xml_writer.StartWriting();
xml_writer.StartElement("entry");
xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom");
xml_writer.StartElement("category");
xml_writer.AddAttribute("scheme",
"http://schemas.google.com/g/2005#kind");
xml_writer.AddAttribute("term",
"http://schemas.google.com/docs/2007#folder");
xml_writer.EndElement(); // Ends "category" element.
xml_writer.WriteElement("title", directory_name_);
xml_writer.EndElement(); // Ends "entry" element.
xml_writer.StopWriting();
upload_content->assign(xml_writer.GetWrittenString());
DVLOG(1) << "CreateDirectory data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//============================ CopyDocumentOperation ===========================
CopyDocumentOperation::CopyDocumentOperation(
GDataOperationRegistry* registry,
Profile* profile,
const GetDataCallback& callback,
const std::string& resource_id,
const FilePath::StringType& new_name)
: GetDataOperation(registry, profile, callback),
resource_id_(resource_id),
new_name_(new_name) {
}
CopyDocumentOperation::~CopyDocumentOperation() {}
URLFetcher::RequestType CopyDocumentOperation::GetRequestType() const {
return URLFetcher::POST;
}
GURL CopyDocumentOperation::GetURL() const {
return AddStandardUrlParams(GURL(kDocumentListRootURL));
}
bool CopyDocumentOperation::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
upload_content_type->assign("application/atom+xml");
XmlWriter xml_writer;
xml_writer.StartWriting();
xml_writer.StartElement("entry");
xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom");
xml_writer.WriteElement("id", resource_id_);
xml_writer.WriteElement("title", new_name_);
xml_writer.EndElement(); // Ends "entry" element.
xml_writer.StopWriting();
upload_content->assign(xml_writer.GetWrittenString());
DVLOG(1) << "CopyDocumentOperation data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//=========================== RenameResourceOperation ==========================
RenameResourceOperation::RenameResourceOperation(
GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& document_url,
const FilePath::StringType& new_name)
: EntryActionOperation(registry, profile, callback, document_url),
new_name_(new_name) {
}
RenameResourceOperation::~RenameResourceOperation() {}
URLFetcher::RequestType RenameResourceOperation::GetRequestType() const {
return URLFetcher::PUT;
}
std::vector<std::string>
RenameResourceOperation::GetExtraRequestHeaders() const {
std::vector<std::string> headers;
headers.push_back(kIfMatchAllHeader);
return headers;
}
bool RenameResourceOperation::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
upload_content_type->assign("application/atom+xml");
XmlWriter xml_writer;
xml_writer.StartWriting();
xml_writer.StartElement("entry");
xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom");
xml_writer.WriteElement("title", new_name_);
xml_writer.EndElement(); // Ends "entry" element.
xml_writer.StopWriting();
upload_content->assign(xml_writer.GetWrittenString());
DVLOG(1) << "RenameResourceOperation data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//======================= AddResourceToDirectoryOperation ======================
AddResourceToDirectoryOperation::AddResourceToDirectoryOperation(
GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& parent_content_url,
const GURL& document_url)
: EntryActionOperation(registry, profile, callback, document_url),
parent_content_url_(parent_content_url) {
}
AddResourceToDirectoryOperation::~AddResourceToDirectoryOperation() {}
GURL AddResourceToDirectoryOperation::GetURL() const {
if (!parent_content_url_.is_empty())
return AddStandardUrlParams(parent_content_url_);
return AddStandardUrlParams(GURL(kDocumentListRootURL));
}
URLFetcher::RequestType
AddResourceToDirectoryOperation::GetRequestType() const {
return URLFetcher::POST;
}
bool AddResourceToDirectoryOperation::GetContentData(
std::string* upload_content_type, std::string* upload_content) {
upload_content_type->assign("application/atom+xml");
XmlWriter xml_writer;
xml_writer.StartWriting();
xml_writer.StartElement("entry");
xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom");
xml_writer.WriteElement("id", document_url().spec());
xml_writer.EndElement(); // Ends "entry" element.
xml_writer.StopWriting();
upload_content->assign(xml_writer.GetWrittenString());
DVLOG(1) << "AddResourceToDirectoryOperation data: " << *upload_content_type
<< ", [" << *upload_content << "]";
return true;
}
//==================== RemoveResourceFromDirectoryOperation ====================
RemoveResourceFromDirectoryOperation::RemoveResourceFromDirectoryOperation(
GDataOperationRegistry* registry,
Profile* profile,
const EntryActionCallback& callback,
const GURL& parent_content_url,
const GURL& document_url,
const std::string& document_resource_id)
: EntryActionOperation(registry, profile, callback, document_url),
resource_id_(document_resource_id),
parent_content_url_(parent_content_url) {
}
RemoveResourceFromDirectoryOperation::~RemoveResourceFromDirectoryOperation() {
}
GURL RemoveResourceFromDirectoryOperation::GetURL() const {
std::string escaped_resource_id = net::EscapePath(resource_id_);
GURL edit_url(base::StringPrintf("%s/%s",
parent_content_url_.spec().c_str(),
escaped_resource_id.c_str()));
return AddStandardUrlParams(edit_url);
}
URLFetcher::RequestType
RemoveResourceFromDirectoryOperation::GetRequestType() const {
return URLFetcher::DELETE_REQUEST;
}
std::vector<std::string>
RemoveResourceFromDirectoryOperation::GetExtraRequestHeaders() const {
std::vector<std::string> headers;
headers.push_back(kIfMatchAllHeader);
return headers;
}
//=========================== InitiateUploadOperation ==========================
InitiateUploadOperation::InitiateUploadOperation(
GDataOperationRegistry* registry,
Profile* profile,
const InitiateUploadCallback& callback,
const InitiateUploadParams& params)
: UrlFetchOperationBase(registry,
GDataOperationRegistry::OPERATION_UPLOAD,
params.virtual_path,
profile),
callback_(callback),
params_(params),
initiate_upload_url_(chrome_common_net::AppendOrReplaceQueryParameter(
params.resumable_create_media_link,
kUploadParamConvertKey,
kUploadParamConvertValue)) {
}
InitiateUploadOperation::~InitiateUploadOperation() {}
GURL InitiateUploadOperation::GetURL() const {
return initiate_upload_url_;
}
bool InitiateUploadOperation::ProcessURLFetchResults(
const net::URLFetcher* source) {
GDataErrorCode code =
static_cast<GDataErrorCode>(source->GetResponseCode());
std::string upload_location;
if (code == HTTP_SUCCESS) {
// Retrieve value of the first "Location" header.
source->GetResponseHeaders()->EnumerateHeader(NULL,
kUploadResponseLocation,
&upload_location);
}
VLOG(1) << "Got response for [" << params_.title
<< "]: code=" << code
<< ", location=[" << upload_location << "]";
if (!callback_.is_null())
callback_.Run(code, GURL(upload_location));
return code == HTTP_SUCCESS;
}
void InitiateUploadOperation::NotifySuccessToOperationRegistry() {
NotifySuspend();
}
void InitiateUploadOperation::RunCallbackOnPrematureFailure(
GDataErrorCode code) {
if (!callback_.is_null())
callback_.Run(code, GURL());
}
URLFetcher::RequestType InitiateUploadOperation::GetRequestType() const {
return URLFetcher::POST;
}
std::vector<std::string>
InitiateUploadOperation::GetExtraRequestHeaders() const {
std::vector<std::string> headers;
if (!params_.content_type.empty())
headers.push_back(kUploadContentType + params_.content_type);
headers.push_back(
kUploadContentLength + base::Int64ToString(params_.content_length));
return headers;
}
bool InitiateUploadOperation::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
upload_content_type->assign("application/atom+xml");
XmlWriter xml_writer;
xml_writer.StartWriting();
xml_writer.StartElement("entry");
xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom");
xml_writer.AddAttribute("xmlns:docs",
"http://schemas.google.com/docs/2007");
xml_writer.WriteElement("title", params_.title);
xml_writer.EndElement(); // Ends "entry" element.
xml_writer.StopWriting();
upload_content->assign(xml_writer.GetWrittenString());
DVLOG(1) << "Upload data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//============================ ResumeUploadOperation ===========================
ResumeUploadOperation::ResumeUploadOperation(
GDataOperationRegistry* registry,
Profile* profile,
const ResumeUploadCallback& callback,
const ResumeUploadParams& params)
: UrlFetchOperationBase(registry,
GDataOperationRegistry::OPERATION_UPLOAD,
params.virtual_path,
profile),
callback_(callback),
params_(params),
last_chunk_completed_(false) {
}
ResumeUploadOperation::~ResumeUploadOperation() {}
GURL ResumeUploadOperation::GetURL() const {
return params_.upload_location;
}
bool ResumeUploadOperation::ProcessURLFetchResults(
const net::URLFetcher* source) {
GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode());
net::HttpResponseHeaders* hdrs = source->GetResponseHeaders();
int64 start_range_received = -1;
int64 end_range_received = -1;
scoped_ptr<DocumentEntry> entry;
if (code == HTTP_RESUME_INCOMPLETE) {
// Retrieve value of the first "Range" header.
std::string range_received;
hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received);
if (!range_received.empty()) { // Parse the range header.
std::vector<net::HttpByteRange> ranges;
if (net::HttpUtil::ParseRangeHeader(range_received, &ranges) &&
!ranges.empty() ) {
// We only care about the first start-end pair in the range.
start_range_received = ranges[0].first_byte_position();
end_range_received = ranges[0].last_byte_position();
}
}
DVLOG(1) << "Got response for [" << params_.title
<< "]: code=" << code
<< ", range_hdr=[" << range_received
<< "], range_parsed=" << start_range_received
<< "," << end_range_received;
} else {
// There might be explanation of unexpected error code in response.
std::string response_content;
source->GetResponseAsString(&response_content);
DVLOG(1) << "Got response for [" << params_.title
<< "]: code=" << code
<< ", content=[\n" << response_content << "\n]";
// Parse entry XML.
XmlReader xml_reader;
if (xml_reader.Load(response_content)) {
while (xml_reader.Read()) {
if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) {
entry.reset(DocumentEntry::CreateFromXml(&xml_reader));
break;
}
}
}
if (!entry.get())
LOG(WARNING) << "Invalid entry received on upload:\n" << response_content;
}
if (!callback_.is_null()) {
callback_.Run(ResumeUploadResponse(code,
start_range_received,
end_range_received),
entry.Pass());
}
if (code == HTTP_CREATED)
last_chunk_completed_ = true;
return code == HTTP_CREATED || code == HTTP_RESUME_INCOMPLETE;
}
void ResumeUploadOperation::NotifyStartToOperationRegistry() {
NotifyResume();
}
void ResumeUploadOperation::NotifySuccessToOperationRegistry() {
if (last_chunk_completed_)
NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
else
NotifySuspend();
}
void ResumeUploadOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
scoped_ptr<DocumentEntry> entry;
if (!callback_.is_null())
callback_.Run(ResumeUploadResponse(code, 0, 0), entry.Pass());
}
URLFetcher::RequestType ResumeUploadOperation::GetRequestType() const {
return URLFetcher::PUT;
}
std::vector<std::string> ResumeUploadOperation::GetExtraRequestHeaders() const {
// The header looks like
// Content-Range: bytes <start_range>-<end_range>/<content_length>
// for example:
// Content-Range: bytes 7864320-8388607/13851821
// Use * for unknown/streaming content length.
DCHECK_GE(params_.start_range, 0);
DCHECK_GE(params_.end_range, 0);
DCHECK_GE(params_.content_length, -1);
std::vector<std::string> headers;
headers.push_back(
std::string(kUploadContentRange) +
base::Int64ToString(params_.start_range) + "-" +
base::Int64ToString(params_.end_range) + "/" +
(params_.content_length == -1 ? "*" :
base::Int64ToString(params_.content_length)));
return headers;
}
bool ResumeUploadOperation::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
*upload_content_type = params_.content_type;
*upload_content = std::string(params_.buf->data(),
params_.end_range - params_.start_range + 1);
return true;
}
void ResumeUploadOperation::OnURLFetchUploadProgress(
const net::URLFetcher* source, int64 current, int64 total) {
// Adjust the progress values according to the range currently uploaded.
NotifyProgress(params_.start_range + current, params_.content_length);
}
} // namespace gdata
|