OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/gdata/gdata_operations.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_operations.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type, | 302 bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type, |
303 std::string* upload_content) { | 303 std::string* upload_content) { |
304 return false; | 304 return false; |
305 } | 305 } |
306 | 306 |
307 void UrlFetchOperationBase::DoCancel() { | 307 void UrlFetchOperationBase::DoCancel() { |
308 url_fetcher_.reset(NULL); | 308 url_fetcher_.reset(NULL); |
309 RunCallbackOnPrematureFailure(GDATA_CANCELLED); | 309 RunCallbackOnPrematureFailure(GDATA_CANCELLED); |
310 } | 310 } |
311 | 311 |
312 void UrlFetchOperationBase::OnURLFetchComplete(const URLFetcher* source) { | 312 void UrlFetchOperationBase::OnURLFetchComplete(const net::URLFetcher* source) { |
313 GDataErrorCode code = | 313 GDataErrorCode code = |
314 static_cast<GDataErrorCode>(source->GetResponseCode()); | 314 static_cast<GDataErrorCode>(source->GetResponseCode()); |
315 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); | 315 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); |
316 | 316 |
317 if (code == HTTP_UNAUTHORIZED) { | 317 if (code == HTTP_UNAUTHORIZED) { |
318 if (!re_authenticate_callback_.is_null() && | 318 if (!re_authenticate_callback_.is_null() && |
319 ++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerOperation) { | 319 ++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerOperation) { |
320 re_authenticate_callback_.Run(this); | 320 re_authenticate_callback_.Run(this); |
321 return; | 321 return; |
322 } | 322 } |
(...skipping 22 matching lines...) Expand all Loading... |
345 RunCallbackOnPrematureFailure(code); | 345 RunCallbackOnPrematureFailure(code); |
346 // Check if this failed before we even started fetching. If so, register | 346 // Check if this failed before we even started fetching. If so, register |
347 // for start so we can properly unregister with finish. | 347 // for start so we can properly unregister with finish. |
348 if (!started_) | 348 if (!started_) |
349 NotifyStart(); | 349 NotifyStart(); |
350 | 350 |
351 NotifyFinish(GDataOperationRegistry::OPERATION_FAILED); | 351 NotifyFinish(GDataOperationRegistry::OPERATION_FAILED); |
352 } | 352 } |
353 | 353 |
354 std::string UrlFetchOperationBase::GetResponseHeadersAsString( | 354 std::string UrlFetchOperationBase::GetResponseHeadersAsString( |
355 const URLFetcher* url_fetcher) { | 355 const net::URLFetcher* url_fetcher) { |
356 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores | 356 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores |
357 // all headers in their raw format, i.e each header is null-terminated. | 357 // all headers in their raw format, i.e each header is null-terminated. |
358 // So logging raw_headers() only shows the first header, which is probably | 358 // So logging raw_headers() only shows the first header, which is probably |
359 // the status line. GetNormalizedHeaders, on the other hand, will show all | 359 // the status line. GetNormalizedHeaders, on the other hand, will show all |
360 // the headers, one per line, which is probably what we want. | 360 // the headers, one per line, which is probably what we want. |
361 std::string headers; | 361 std::string headers; |
362 // Check that response code indicates response headers are valid (i.e. not | 362 // Check that response code indicates response headers are valid (i.e. not |
363 // malformed) before we retrieve the headers. | 363 // malformed) before we retrieve the headers. |
364 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { | 364 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { |
365 headers.assign("Response headers are malformed!!"); | 365 headers.assign("Response headers are malformed!!"); |
(...skipping 14 matching lines...) Expand all Loading... |
380 document_url_(document_url) { | 380 document_url_(document_url) { |
381 } | 381 } |
382 | 382 |
383 EntryActionOperation::~EntryActionOperation() {} | 383 EntryActionOperation::~EntryActionOperation() {} |
384 | 384 |
385 // Overridden from UrlFetchOperationBase. | 385 // Overridden from UrlFetchOperationBase. |
386 GURL EntryActionOperation::GetURL() const { | 386 GURL EntryActionOperation::GetURL() const { |
387 return AddStandardUrlParams(document_url_); | 387 return AddStandardUrlParams(document_url_); |
388 } | 388 } |
389 | 389 |
390 bool EntryActionOperation::ProcessURLFetchResults(const URLFetcher* source) { | 390 bool EntryActionOperation::ProcessURLFetchResults( |
| 391 const net::URLFetcher* source) { |
391 if (!callback_.is_null()) { | 392 if (!callback_.is_null()) { |
392 GDataErrorCode code = | 393 GDataErrorCode code = |
393 static_cast<GDataErrorCode>(source->GetResponseCode()); | 394 static_cast<GDataErrorCode>(source->GetResponseCode()); |
394 callback_.Run(code, document_url_); | 395 callback_.Run(code, document_url_); |
395 } | 396 } |
396 return true; | 397 return true; |
397 } | 398 } |
398 | 399 |
399 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 400 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
400 if (!callback_.is_null()) | 401 if (!callback_.is_null()) |
401 callback_.Run(code, document_url_); | 402 callback_.Run(code, document_url_); |
402 } | 403 } |
403 | 404 |
404 //============================== GetDataOperation ============================== | 405 //============================== GetDataOperation ============================== |
405 | 406 |
406 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, | 407 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, |
407 Profile* profile, | 408 Profile* profile, |
408 const GetDataCallback& callback) | 409 const GetDataCallback& callback) |
409 : UrlFetchOperationBase(registry, profile), callback_(callback) { | 410 : UrlFetchOperationBase(registry, profile), callback_(callback) { |
410 } | 411 } |
411 | 412 |
412 GetDataOperation::~GetDataOperation() {} | 413 GetDataOperation::~GetDataOperation() {} |
413 | 414 |
414 bool GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { | 415 bool GetDataOperation::ProcessURLFetchResults(const net::URLFetcher* source) { |
415 std::string data; | 416 std::string data; |
416 source->GetResponseAsString(&data); | 417 source->GetResponseAsString(&data); |
417 scoped_ptr<base::Value> root_value; | 418 scoped_ptr<base::Value> root_value; |
418 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 419 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
419 | 420 |
420 switch (code) { | 421 switch (code) { |
421 case HTTP_SUCCESS: | 422 case HTTP_SUCCESS: |
422 case HTTP_CREATED: { | 423 case HTTP_CREATED: { |
423 root_value.reset(ParseResponse(data)); | 424 root_value.reset(ParseResponse(data)); |
424 if (!root_value.get()) | 425 if (!root_value.get()) |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 output_file_path_ = output_file_path; | 538 output_file_path_ = output_file_path; |
538 } | 539 } |
539 | 540 |
540 DownloadFileOperation::~DownloadFileOperation() {} | 541 DownloadFileOperation::~DownloadFileOperation() {} |
541 | 542 |
542 // Overridden from UrlFetchOperationBase. | 543 // Overridden from UrlFetchOperationBase. |
543 GURL DownloadFileOperation::GetURL() const { | 544 GURL DownloadFileOperation::GetURL() const { |
544 return document_url_; | 545 return document_url_; |
545 } | 546 } |
546 | 547 |
547 void DownloadFileOperation::OnURLFetchDownloadProgress(const URLFetcher* source, | 548 void DownloadFileOperation::OnURLFetchDownloadProgress( |
548 int64 current, | 549 const net::URLFetcher* source, |
549 int64 total) { | 550 int64 current, |
| 551 int64 total) { |
550 NotifyProgress(current, total); | 552 NotifyProgress(current, total); |
551 } | 553 } |
552 | 554 |
553 bool DownloadFileOperation::ShouldSendDownloadData() { | 555 bool DownloadFileOperation::ShouldSendDownloadData() { |
554 return !get_download_data_callback_.is_null(); | 556 return !get_download_data_callback_.is_null(); |
555 } | 557 } |
556 | 558 |
557 void DownloadFileOperation::OnURLFetchDownloadData( | 559 void DownloadFileOperation::OnURLFetchDownloadData( |
558 const URLFetcher* source, | 560 const net::URLFetcher* source, |
559 scoped_ptr<std::string> download_data) { | 561 scoped_ptr<std::string> download_data) { |
560 if (!get_download_data_callback_.is_null()) | 562 if (!get_download_data_callback_.is_null()) |
561 get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass()); | 563 get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass()); |
562 } | 564 } |
563 | 565 |
564 bool DownloadFileOperation::ProcessURLFetchResults(const URLFetcher* source) { | 566 bool DownloadFileOperation::ProcessURLFetchResults( |
| 567 const net::URLFetcher* source) { |
565 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 568 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
566 | 569 |
567 // Take over the ownership of the the downloaded temp file. | 570 // Take over the ownership of the the downloaded temp file. |
568 FilePath temp_file; | 571 FilePath temp_file; |
569 if (code == HTTP_SUCCESS && | 572 if (code == HTTP_SUCCESS && |
570 !source->GetResponseAsFilePath(true, // take_ownership | 573 !source->GetResponseAsFilePath(true, // take_ownership |
571 &temp_file)) { | 574 &temp_file)) { |
572 code = GDATA_FILE_ERROR; | 575 code = GDATA_FILE_ERROR; |
573 } | 576 } |
574 | 577 |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 kUploadParamConvertKey, | 844 kUploadParamConvertKey, |
842 kUploadParamConvertValue)) { | 845 kUploadParamConvertValue)) { |
843 } | 846 } |
844 | 847 |
845 InitiateUploadOperation::~InitiateUploadOperation() {} | 848 InitiateUploadOperation::~InitiateUploadOperation() {} |
846 | 849 |
847 GURL InitiateUploadOperation::GetURL() const { | 850 GURL InitiateUploadOperation::GetURL() const { |
848 return initiate_upload_url_; | 851 return initiate_upload_url_; |
849 } | 852 } |
850 | 853 |
851 bool InitiateUploadOperation::ProcessURLFetchResults(const URLFetcher* source) { | 854 bool InitiateUploadOperation::ProcessURLFetchResults( |
| 855 const net::URLFetcher* source) { |
852 GDataErrorCode code = | 856 GDataErrorCode code = |
853 static_cast<GDataErrorCode>(source->GetResponseCode()); | 857 static_cast<GDataErrorCode>(source->GetResponseCode()); |
854 | 858 |
855 std::string upload_location; | 859 std::string upload_location; |
856 if (code == HTTP_SUCCESS) { | 860 if (code == HTTP_SUCCESS) { |
857 // Retrieve value of the first "Location" header. | 861 // Retrieve value of the first "Location" header. |
858 source->GetResponseHeaders()->EnumerateHeader(NULL, | 862 source->GetResponseHeaders()->EnumerateHeader(NULL, |
859 kUploadResponseLocation, | 863 kUploadResponseLocation, |
860 &upload_location); | 864 &upload_location); |
861 } | 865 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 params_(params), | 930 params_(params), |
927 last_chunk_completed_(false) { | 931 last_chunk_completed_(false) { |
928 } | 932 } |
929 | 933 |
930 ResumeUploadOperation::~ResumeUploadOperation() {} | 934 ResumeUploadOperation::~ResumeUploadOperation() {} |
931 | 935 |
932 GURL ResumeUploadOperation::GetURL() const { | 936 GURL ResumeUploadOperation::GetURL() const { |
933 return params_.upload_location; | 937 return params_.upload_location; |
934 } | 938 } |
935 | 939 |
936 bool ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) { | 940 bool ResumeUploadOperation::ProcessURLFetchResults( |
| 941 const net::URLFetcher* source) { |
937 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 942 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
938 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); | 943 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); |
939 int64 start_range_received = -1; | 944 int64 start_range_received = -1; |
940 int64 end_range_received = -1; | 945 int64 end_range_received = -1; |
941 scoped_ptr<DocumentEntry> entry; | 946 scoped_ptr<DocumentEntry> entry; |
942 | 947 |
943 if (code == HTTP_RESUME_INCOMPLETE) { | 948 if (code == HTTP_RESUME_INCOMPLETE) { |
944 // Retrieve value of the first "Range" header. | 949 // Retrieve value of the first "Range" header. |
945 std::string range_received; | 950 std::string range_received; |
946 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); | 951 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1036 | 1041 |
1037 bool ResumeUploadOperation::GetContentData(std::string* upload_content_type, | 1042 bool ResumeUploadOperation::GetContentData(std::string* upload_content_type, |
1038 std::string* upload_content) { | 1043 std::string* upload_content) { |
1039 *upload_content_type = params_.content_type; | 1044 *upload_content_type = params_.content_type; |
1040 *upload_content = std::string(params_.buf->data(), | 1045 *upload_content = std::string(params_.buf->data(), |
1041 params_.end_range - params_.start_range + 1); | 1046 params_.end_range - params_.start_range + 1); |
1042 return true; | 1047 return true; |
1043 } | 1048 } |
1044 | 1049 |
1045 void ResumeUploadOperation::OnURLFetchUploadProgress( | 1050 void ResumeUploadOperation::OnURLFetchUploadProgress( |
1046 const content::URLFetcher* source, int64 current, int64 total) { | 1051 const net::URLFetcher* source, int64 current, int64 total) { |
1047 // Adjust the progress values according to the range currently uploaded. | 1052 // Adjust the progress values according to the range currently uploaded. |
1048 NotifyProgress(params_.start_range + current, params_.content_length); | 1053 NotifyProgress(params_.start_range + current, params_.content_length); |
1049 } | 1054 } |
1050 | 1055 |
1051 } // namespace gdata | 1056 } // namespace gdata |
OLD | NEW |