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/google_apis/base_requests.h" | 5 #include "chrome/browser/google_apis/base_requests.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 void ParseJson(const std::string& json, const ParseJsonCallback& callback) { | 83 void ParseJson(const std::string& json, const ParseJsonCallback& callback) { |
84 base::PostTaskAndReplyWithResult( | 84 base::PostTaskAndReplyWithResult( |
85 BrowserThread::GetBlockingPool(), | 85 BrowserThread::GetBlockingPool(), |
86 FROM_HERE, | 86 FROM_HERE, |
87 base::Bind(&ParseJsonOnBlockingPool, json), | 87 base::Bind(&ParseJsonOnBlockingPool, json), |
88 callback); | 88 callback); |
89 } | 89 } |
90 | 90 |
91 //============================ UrlFetchRequestBase =========================== | 91 //============================ UrlFetchRequestBase =========================== |
92 | 92 |
93 UrlFetchRequestBase::UrlFetchRequestBase( | 93 UrlFetchRequestBase::UrlFetchRequestBase(RequestSender* sender) |
94 RequestSender* sender, | 94 : re_authenticate_count_(0), |
95 net::URLRequestContextGetter* url_request_context_getter) | |
96 : url_request_context_getter_(url_request_context_getter), | |
97 re_authenticate_count_(0), | |
98 sender_(sender), | 95 sender_(sender), |
99 save_temp_file_(false), | 96 save_temp_file_(false), |
100 weak_ptr_factory_(this) { | 97 weak_ptr_factory_(this) { |
101 } | 98 } |
102 | 99 |
103 UrlFetchRequestBase::~UrlFetchRequestBase() {} | 100 UrlFetchRequestBase::~UrlFetchRequestBase() {} |
104 | 101 |
105 void UrlFetchRequestBase::Start(const std::string& access_token, | 102 void UrlFetchRequestBase::Start(const std::string& access_token, |
106 const std::string& custom_user_agent, | 103 const std::string& custom_user_agent, |
107 const ReAuthenticateCallback& callback) { | 104 const ReAuthenticateCallback& callback) { |
108 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
109 DCHECK(url_request_context_getter_); | |
110 DCHECK(!access_token.empty()); | 106 DCHECK(!access_token.empty()); |
111 DCHECK(!callback.is_null()); | 107 DCHECK(!callback.is_null()); |
112 DCHECK(re_authenticate_callback_.is_null()); | 108 DCHECK(re_authenticate_callback_.is_null()); |
113 | 109 |
114 re_authenticate_callback_ = callback; | 110 re_authenticate_callback_ = callback; |
115 | 111 |
116 GURL url = GetURL(); | 112 GURL url = GetURL(); |
117 if (url.is_empty()) { | 113 if (url.is_empty()) { |
118 // Error is found on generating the url. Send the error message to the | 114 // Error is found on generating the url. Send the error message to the |
119 // callback, and then return immediately without trying to connect | 115 // callback, and then return immediately without trying to connect |
120 // to the server. | 116 // to the server. |
121 RunCallbackOnPrematureFailure(GDATA_OTHER_ERROR); | 117 RunCallbackOnPrematureFailure(GDATA_OTHER_ERROR); |
122 return; | 118 return; |
123 } | 119 } |
124 DVLOG(1) << "URL: " << url.spec(); | 120 DVLOG(1) << "URL: " << url.spec(); |
125 | 121 |
126 URLFetcher::RequestType request_type = GetRequestType(); | 122 URLFetcher::RequestType request_type = GetRequestType(); |
127 url_fetcher_.reset( | 123 url_fetcher_.reset( |
128 URLFetcher::Create(url, request_type, this)); | 124 URLFetcher::Create(url, request_type, this)); |
129 url_fetcher_->SetRequestContext(url_request_context_getter_); | 125 url_fetcher_->SetRequestContext(sender_->url_request_context_getter()); |
130 // Always set flags to neither send nor save cookies. | 126 // Always set flags to neither send nor save cookies. |
131 url_fetcher_->SetLoadFlags( | 127 url_fetcher_->SetLoadFlags( |
132 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | | 128 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES | |
133 net::LOAD_DISABLE_CACHE); | 129 net::LOAD_DISABLE_CACHE); |
134 if (save_temp_file_) { | 130 if (save_temp_file_) { |
135 url_fetcher_->SaveResponseToTemporaryFile( | 131 url_fetcher_->SaveResponseToTemporaryFile( |
136 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
137 } else if (!output_file_path_.empty()) { | 133 } else if (!output_file_path_.empty()) { |
138 url_fetcher_->SaveResponseToFileAtPath( | 134 url_fetcher_->SaveResponseToFileAtPath( |
139 output_file_path_, | 135 output_file_path_, |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 sender_->RequestFinished(this); | 261 sender_->RequestFinished(this); |
266 } | 262 } |
267 | 263 |
268 base::WeakPtr<AuthenticatedRequestInterface> | 264 base::WeakPtr<AuthenticatedRequestInterface> |
269 UrlFetchRequestBase::GetWeakPtr() { | 265 UrlFetchRequestBase::GetWeakPtr() { |
270 return weak_ptr_factory_.GetWeakPtr(); | 266 return weak_ptr_factory_.GetWeakPtr(); |
271 } | 267 } |
272 | 268 |
273 //============================ EntryActionRequest ============================ | 269 //============================ EntryActionRequest ============================ |
274 | 270 |
275 EntryActionRequest::EntryActionRequest( | 271 EntryActionRequest::EntryActionRequest(RequestSender* sender, |
276 RequestSender* runner, | 272 const EntryActionCallback& callback) |
277 net::URLRequestContextGetter* url_request_context_getter, | 273 : UrlFetchRequestBase(sender), |
278 const EntryActionCallback& callback) | |
279 : UrlFetchRequestBase(runner, url_request_context_getter), | |
280 callback_(callback) { | 274 callback_(callback) { |
281 DCHECK(!callback_.is_null()); | 275 DCHECK(!callback_.is_null()); |
282 } | 276 } |
283 | 277 |
284 EntryActionRequest::~EntryActionRequest() {} | 278 EntryActionRequest::~EntryActionRequest() {} |
285 | 279 |
286 void EntryActionRequest::ProcessURLFetchResults(const URLFetcher* source) { | 280 void EntryActionRequest::ProcessURLFetchResults(const URLFetcher* source) { |
287 GDataErrorCode code = GetErrorCode(source); | 281 GDataErrorCode code = GetErrorCode(source); |
288 callback_.Run(code); | 282 callback_.Run(code); |
289 const bool success = true; | 283 const bool success = true; |
290 OnProcessURLFetchResultsComplete(success); | 284 OnProcessURLFetchResultsComplete(success); |
291 } | 285 } |
292 | 286 |
293 void EntryActionRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 287 void EntryActionRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
294 callback_.Run(code); | 288 callback_.Run(code); |
295 } | 289 } |
296 | 290 |
297 //============================== GetDataRequest ============================== | 291 //============================== GetDataRequest ============================== |
298 | 292 |
299 GetDataRequest::GetDataRequest( | 293 GetDataRequest::GetDataRequest(RequestSender* sender, |
300 RequestSender* runner, | 294 const GetDataCallback& callback) |
301 net::URLRequestContextGetter* url_request_context_getter, | 295 : UrlFetchRequestBase(sender), |
302 const GetDataCallback& callback) | |
303 : UrlFetchRequestBase(runner, url_request_context_getter), | |
304 callback_(callback), | 296 callback_(callback), |
305 weak_ptr_factory_(this) { | 297 weak_ptr_factory_(this) { |
306 DCHECK(!callback_.is_null()); | 298 DCHECK(!callback_.is_null()); |
307 } | 299 } |
308 | 300 |
309 GetDataRequest::~GetDataRequest() {} | 301 GetDataRequest::~GetDataRequest() {} |
310 | 302 |
311 void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code, | 303 void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code, |
312 const std::string& data) { | 304 const std::string& data) { |
313 DCHECK(CalledOnValidThread()); | 305 DCHECK(CalledOnValidThread()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 | 354 |
363 void GetDataRequest::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, | 355 void GetDataRequest::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, |
364 scoped_ptr<base::Value> value) { | 356 scoped_ptr<base::Value> value) { |
365 DCHECK(CalledOnValidThread()); | 357 DCHECK(CalledOnValidThread()); |
366 callback_.Run(fetch_error_code, value.Pass()); | 358 callback_.Run(fetch_error_code, value.Pass()); |
367 } | 359 } |
368 | 360 |
369 //========================= InitiateUploadRequestBase ======================== | 361 //========================= InitiateUploadRequestBase ======================== |
370 | 362 |
371 InitiateUploadRequestBase::InitiateUploadRequestBase( | 363 InitiateUploadRequestBase::InitiateUploadRequestBase( |
372 RequestSender* runner, | 364 RequestSender* sender, |
373 net::URLRequestContextGetter* url_request_context_getter, | |
374 const InitiateUploadCallback& callback, | 365 const InitiateUploadCallback& callback, |
375 const std::string& content_type, | 366 const std::string& content_type, |
376 int64 content_length) | 367 int64 content_length) |
377 : UrlFetchRequestBase(runner, url_request_context_getter), | 368 : UrlFetchRequestBase(sender), |
378 callback_(callback), | 369 callback_(callback), |
379 content_type_(content_type), | 370 content_type_(content_type), |
380 content_length_(content_length) { | 371 content_length_(content_length) { |
381 DCHECK(!callback_.is_null()); | 372 DCHECK(!callback_.is_null()); |
382 DCHECK(!content_type_.empty()); | 373 DCHECK(!content_type_.empty()); |
383 DCHECK_GE(content_length_, 0); | 374 DCHECK_GE(content_length_, 0); |
384 } | 375 } |
385 | 376 |
386 InitiateUploadRequestBase::~InitiateUploadRequestBase() {} | 377 InitiateUploadRequestBase::~InitiateUploadRequestBase() {} |
387 | 378 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 : code(code), | 420 : code(code), |
430 start_position_received(start_position_received), | 421 start_position_received(start_position_received), |
431 end_position_received(end_position_received) { | 422 end_position_received(end_position_received) { |
432 } | 423 } |
433 | 424 |
434 UploadRangeResponse::~UploadRangeResponse() { | 425 UploadRangeResponse::~UploadRangeResponse() { |
435 } | 426 } |
436 | 427 |
437 //========================== UploadRangeRequestBase ========================== | 428 //========================== UploadRangeRequestBase ========================== |
438 | 429 |
439 UploadRangeRequestBase::UploadRangeRequestBase( | 430 UploadRangeRequestBase::UploadRangeRequestBase(RequestSender* sender, |
440 RequestSender* runner, | 431 const GURL& upload_url) |
441 net::URLRequestContextGetter* url_request_context_getter, | 432 : UrlFetchRequestBase(sender), |
442 const GURL& upload_url) | |
443 : UrlFetchRequestBase(runner, url_request_context_getter), | |
444 upload_url_(upload_url), | 433 upload_url_(upload_url), |
445 weak_ptr_factory_(this) { | 434 weak_ptr_factory_(this) { |
446 } | 435 } |
447 | 436 |
448 UploadRangeRequestBase::~UploadRangeRequestBase() {} | 437 UploadRangeRequestBase::~UploadRangeRequestBase() {} |
449 | 438 |
450 GURL UploadRangeRequestBase::GetURL() const { | 439 GURL UploadRangeRequestBase::GetURL() const { |
451 // This is very tricky to get json from this request. To do that, &alt=json | 440 // This is very tricky to get json from this request. To do that, &alt=json |
452 // has to be appended not here but in InitiateUploadRequestBase::GetURL(). | 441 // has to be appended not here but in InitiateUploadRequestBase::GetURL(). |
453 return upload_url_; | 442 return upload_url_; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 | 506 |
518 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( | 507 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( |
519 GDataErrorCode code) { | 508 GDataErrorCode code) { |
520 OnRangeRequestComplete( | 509 OnRangeRequestComplete( |
521 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); | 510 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); |
522 } | 511 } |
523 | 512 |
524 //========================== ResumeUploadRequestBase ========================= | 513 //========================== ResumeUploadRequestBase ========================= |
525 | 514 |
526 ResumeUploadRequestBase::ResumeUploadRequestBase( | 515 ResumeUploadRequestBase::ResumeUploadRequestBase( |
527 RequestSender* runner, | 516 RequestSender* sender, |
528 net::URLRequestContextGetter* url_request_context_getter, | |
529 const GURL& upload_location, | 517 const GURL& upload_location, |
530 int64 start_position, | 518 int64 start_position, |
531 int64 end_position, | 519 int64 end_position, |
532 int64 content_length, | 520 int64 content_length, |
533 const std::string& content_type, | 521 const std::string& content_type, |
534 const base::FilePath& local_file_path) | 522 const base::FilePath& local_file_path) |
535 : UploadRangeRequestBase(runner, | 523 : UploadRangeRequestBase(sender, upload_location), |
536 url_request_context_getter, | |
537 upload_location), | |
538 start_position_(start_position), | 524 start_position_(start_position), |
539 end_position_(end_position), | 525 end_position_(end_position), |
540 content_length_(content_length), | 526 content_length_(content_length), |
541 content_type_(content_type), | 527 content_type_(content_type), |
542 local_file_path_(local_file_path) { | 528 local_file_path_(local_file_path) { |
543 DCHECK_LE(start_position_, end_position_); | 529 DCHECK_LE(start_position_, end_position_); |
544 } | 530 } |
545 | 531 |
546 ResumeUploadRequestBase::~ResumeUploadRequestBase() {} | 532 ResumeUploadRequestBase::~ResumeUploadRequestBase() {} |
547 | 533 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 | 570 |
585 *local_file_path = local_file_path_; | 571 *local_file_path = local_file_path_; |
586 *range_offset = start_position_; | 572 *range_offset = start_position_; |
587 *range_length = end_position_ - start_position_; | 573 *range_length = end_position_ - start_position_; |
588 *upload_content_type = content_type_; | 574 *upload_content_type = content_type_; |
589 return true; | 575 return true; |
590 } | 576 } |
591 | 577 |
592 //======================== GetUploadStatusRequestBase ======================== | 578 //======================== GetUploadStatusRequestBase ======================== |
593 | 579 |
594 GetUploadStatusRequestBase::GetUploadStatusRequestBase( | 580 GetUploadStatusRequestBase::GetUploadStatusRequestBase(RequestSender* sender, |
595 RequestSender* runner, | 581 const GURL& upload_url, |
596 net::URLRequestContextGetter* url_request_context_getter, | 582 int64 content_length) |
597 const GURL& upload_url, | 583 : UploadRangeRequestBase(sender, upload_url), |
598 int64 content_length) | |
599 : UploadRangeRequestBase(runner, | |
600 url_request_context_getter, | |
601 upload_url), | |
602 content_length_(content_length) {} | 584 content_length_(content_length) {} |
603 | 585 |
604 GetUploadStatusRequestBase::~GetUploadStatusRequestBase() {} | 586 GetUploadStatusRequestBase::~GetUploadStatusRequestBase() {} |
605 | 587 |
606 std::vector<std::string> | 588 std::vector<std::string> |
607 GetUploadStatusRequestBase::GetExtraRequestHeaders() const { | 589 GetUploadStatusRequestBase::GetExtraRequestHeaders() const { |
608 // The header looks like | 590 // The header looks like |
609 // Content-Range: bytes */<content_length> | 591 // Content-Range: bytes */<content_length> |
610 // for example: | 592 // for example: |
611 // Content-Range: bytes */13851821 | 593 // Content-Range: bytes */13851821 |
612 DCHECK_GE(content_length_, 0); | 594 DCHECK_GE(content_length_, 0); |
613 | 595 |
614 std::vector<std::string> headers; | 596 std::vector<std::string> headers; |
615 headers.push_back( | 597 headers.push_back( |
616 std::string(kUploadContentRange) + "*/" + | 598 std::string(kUploadContentRange) + "*/" + |
617 base::Int64ToString(content_length_)); | 599 base::Int64ToString(content_length_)); |
618 return headers; | 600 return headers; |
619 } | 601 } |
620 | 602 |
621 //============================ DownloadFileRequest =========================== | 603 //============================ DownloadFileRequest =========================== |
622 | 604 |
623 DownloadFileRequest::DownloadFileRequest( | 605 DownloadFileRequest::DownloadFileRequest( |
624 RequestSender* runner, | 606 RequestSender* sender, |
625 net::URLRequestContextGetter* url_request_context_getter, | |
626 const DownloadActionCallback& download_action_callback, | 607 const DownloadActionCallback& download_action_callback, |
627 const GetContentCallback& get_content_callback, | 608 const GetContentCallback& get_content_callback, |
628 const ProgressCallback& progress_callback, | 609 const ProgressCallback& progress_callback, |
629 const GURL& download_url, | 610 const GURL& download_url, |
630 const base::FilePath& output_file_path) | 611 const base::FilePath& output_file_path) |
631 : UrlFetchRequestBase(runner, url_request_context_getter), | 612 : UrlFetchRequestBase(sender), |
632 download_action_callback_(download_action_callback), | 613 download_action_callback_(download_action_callback), |
633 get_content_callback_(get_content_callback), | 614 get_content_callback_(get_content_callback), |
634 progress_callback_(progress_callback), | 615 progress_callback_(progress_callback), |
635 download_url_(download_url) { | 616 download_url_(download_url) { |
636 DCHECK(!download_action_callback_.is_null()); | 617 DCHECK(!download_action_callback_.is_null()); |
637 // get_content_callback may be null. | 618 // get_content_callback may be null. |
638 | 619 |
639 // Make sure we download the content into a temp file. | 620 // Make sure we download the content into a temp file. |
640 if (output_file_path.empty()) | 621 if (output_file_path.empty()) |
641 set_save_temp_file(true); | 622 set_save_temp_file(true); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 | 662 |
682 download_action_callback_.Run(code, temp_file); | 663 download_action_callback_.Run(code, temp_file); |
683 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 664 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
684 } | 665 } |
685 | 666 |
686 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 667 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
687 download_action_callback_.Run(code, base::FilePath()); | 668 download_action_callback_.Run(code, base::FilePath()); |
688 } | 669 } |
689 | 670 |
690 } // namespace google_apis | 671 } // namespace google_apis |
OLD | NEW |