OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/content/browser/wallet/wallet_client.h" | 5 #include "components/autofill/content/browser/wallet/wallet_client.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/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 251 |
252 WalletClient::FullWalletRequest::~FullWalletRequest() {} | 252 WalletClient::FullWalletRequest::~FullWalletRequest() {} |
253 | 253 |
254 WalletClient::WalletClient(net::URLRequestContextGetter* context_getter, | 254 WalletClient::WalletClient(net::URLRequestContextGetter* context_getter, |
255 WalletClientDelegate* delegate, | 255 WalletClientDelegate* delegate, |
256 const GURL& source_url) | 256 const GURL& source_url) |
257 : context_getter_(context_getter), | 257 : context_getter_(context_getter), |
258 delegate_(delegate), | 258 delegate_(delegate), |
259 user_index_(0U), | 259 user_index_(0U), |
260 source_url_(source_url), | 260 source_url_(source_url), |
261 request_type_(NO_PENDING_REQUEST), | 261 request_type_(NO_REQUEST), |
262 one_time_pad_(kOneTimePadLength), | 262 one_time_pad_(kOneTimePadLength), |
263 weak_ptr_factory_(this) { | 263 weak_ptr_factory_(this) { |
264 DCHECK(context_getter_.get()); | 264 DCHECK(context_getter_.get()); |
265 DCHECK(delegate_); | 265 DCHECK(delegate_); |
266 } | 266 } |
267 | 267 |
268 WalletClient::~WalletClient() {} | 268 WalletClient::~WalletClient() {} |
269 | 269 |
270 void WalletClient::AcceptLegalDocuments( | 270 void WalletClient::AcceptLegalDocuments( |
271 const std::vector<WalletItems::LegalDocument*>& documents, | 271 const std::vector<WalletItems::LegalDocument*>& documents, |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 MakeWalletRequest(GetGetWalletItemsUrl(user_index_), | 469 MakeWalletRequest(GetGetWalletItemsUrl(user_index_), |
470 post_body, | 470 post_body, |
471 kJsonMimeType, | 471 kJsonMimeType, |
472 GET_WALLET_ITEMS); | 472 GET_WALLET_ITEMS); |
473 } | 473 } |
474 | 474 |
475 bool WalletClient::HasRequestInProgress() const { | 475 bool WalletClient::HasRequestInProgress() const { |
476 return request_; | 476 return request_; |
477 } | 477 } |
478 | 478 |
479 void WalletClient::CancelRequests() { | 479 void WalletClient::CancelRequest() { |
480 request_.reset(); | 480 request_.reset(); |
481 request_type_ = NO_PENDING_REQUEST; | 481 request_type_ = NO_REQUEST; |
482 while (!pending_requests_.empty()) { | |
483 pending_requests_.pop(); | |
484 } | |
485 } | 482 } |
486 | 483 |
487 void WalletClient::SetUserIndex(size_t user_index) { | 484 void WalletClient::SetUserIndex(size_t user_index) { |
488 CancelRequests(); | 485 CancelRequest(); |
489 user_index_ = user_index; | 486 user_index_ = user_index; |
490 } | 487 } |
491 | 488 |
492 void WalletClient::DoAcceptLegalDocuments( | 489 void WalletClient::DoAcceptLegalDocuments( |
493 const std::vector<std::string>& document_ids, | 490 const std::vector<std::string>& document_ids, |
494 const std::string& google_transaction_id) { | 491 const std::string& google_transaction_id) { |
495 base::DictionaryValue request_dict; | 492 base::DictionaryValue request_dict; |
496 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey()); | 493 request_dict.SetString(kApiKeyKey, google_apis::GetAPIKey()); |
497 request_dict.SetString(kGoogleTransactionIdKey, google_transaction_id); | 494 request_dict.SetString(kGoogleTransactionIdKey, google_transaction_id); |
498 request_dict.SetString(kMerchantDomainKey, | 495 request_dict.SetString(kMerchantDomainKey, |
(...skipping 12 matching lines...) Expand all Loading... |
511 MakeWalletRequest(GetAcceptLegalDocumentsUrl(user_index_), | 508 MakeWalletRequest(GetAcceptLegalDocumentsUrl(user_index_), |
512 post_body, | 509 post_body, |
513 kJsonMimeType, | 510 kJsonMimeType, |
514 ACCEPT_LEGAL_DOCUMENTS); | 511 ACCEPT_LEGAL_DOCUMENTS); |
515 } | 512 } |
516 | 513 |
517 void WalletClient::MakeWalletRequest(const GURL& url, | 514 void WalletClient::MakeWalletRequest(const GURL& url, |
518 const std::string& post_body, | 515 const std::string& post_body, |
519 const std::string& mime_type, | 516 const std::string& mime_type, |
520 RequestType request_type) { | 517 RequestType request_type) { |
521 if (HasRequestInProgress()) { | 518 DCHECK_EQ(request_type_, NO_REQUEST); |
522 pending_requests_.push(base::Bind(&WalletClient::MakeWalletRequest, | |
523 base::Unretained(this), | |
524 url, | |
525 post_body, | |
526 mime_type, | |
527 request_type)); | |
528 return; | |
529 } | |
530 | |
531 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
532 request_type_ = request_type; | 519 request_type_ = request_type; |
533 | 520 |
534 request_.reset(net::URLFetcher::Create( | 521 request_.reset(net::URLFetcher::Create( |
535 0, url, net::URLFetcher::POST, this)); | 522 0, url, net::URLFetcher::POST, this)); |
536 request_->SetRequestContext(context_getter_.get()); | 523 request_->SetRequestContext(context_getter_.get()); |
537 DVLOG(1) << "Making request to " << url << " with post_body=" << post_body; | 524 DVLOG(1) << "Making request to " << url << " with post_body=" << post_body; |
538 request_->SetUploadData(mime_type, post_body); | 525 request_->SetUploadData(mime_type, post_body); |
539 request_->AddExtraRequestHeader("Authorization: GoogleLogin auth=" + | 526 request_->AddExtraRequestHeader("Authorization: GoogleLogin auth=" + |
540 delegate_->GetWalletCookieValue()); | 527 delegate_->GetWalletCookieValue()); |
541 DVLOG(1) << "Setting authorization header value to " | 528 DVLOG(1) << "Setting authorization header value to " |
(...skipping 14 matching lines...) Expand all Loading... |
556 RequestTypeToUmaMetric(request_type_), | 543 RequestTypeToUmaMetric(request_type_), |
557 base::Time::Now() - request_started_timestamp_); | 544 base::Time::Now() - request_started_timestamp_); |
558 | 545 |
559 DCHECK_EQ(source, request_.get()); | 546 DCHECK_EQ(source, request_.get()); |
560 DVLOG(1) << "Got response from " << source->GetOriginalURL(); | 547 DVLOG(1) << "Got response from " << source->GetOriginalURL(); |
561 | 548 |
562 // |request_|, which is aliased to |source|, might continue to be used in this | 549 // |request_|, which is aliased to |source|, might continue to be used in this |
563 // |method, but should be freed once control leaves the method. | 550 // |method, but should be freed once control leaves the method. |
564 scoped_ptr<net::URLFetcher> scoped_request(request_.Pass()); | 551 scoped_ptr<net::URLFetcher> scoped_request(request_.Pass()); |
565 | 552 |
566 // Prepare to start the next pending request. This is queued up as an | |
567 // asynchronous message because |this| WalletClient instance can be destroyed | |
568 // before the end of the method in response to the current incoming request. | |
569 base::MessageLoop::current()->PostTask( | |
570 FROM_HERE, | |
571 base::Bind(&WalletClient::StartNextPendingRequest, | |
572 weak_ptr_factory_.GetWeakPtr()));; | |
573 | |
574 std::string data; | 553 std::string data; |
575 source->GetResponseAsString(&data); | 554 source->GetResponseAsString(&data); |
576 DVLOG(1) << "Response body: " << data; | 555 DVLOG(1) << "Response body: " << data; |
577 | 556 |
578 scoped_ptr<base::DictionaryValue> response_dict; | 557 scoped_ptr<base::DictionaryValue> response_dict; |
579 | 558 |
580 int response_code = source->GetResponseCode(); | 559 int response_code = source->GetResponseCode(); |
581 delegate_->GetMetricLogger().LogWalletResponseCode(response_code); | 560 delegate_->GetMetricLogger().LogWalletResponseCode(response_code); |
582 | 561 |
583 switch (response_code) { | 562 switch (response_code) { |
584 // HTTP_BAD_REQUEST means the arguments are invalid. No point retrying. | 563 // HTTP_BAD_REQUEST means the arguments are invalid. No point retrying. |
585 case net::HTTP_BAD_REQUEST: { | 564 case net::HTTP_BAD_REQUEST: { |
586 request_type_ = NO_PENDING_REQUEST; | 565 request_type_ = NO_REQUEST; |
587 HandleWalletError(BAD_REQUEST); | 566 HandleWalletError(BAD_REQUEST); |
588 return; | 567 return; |
589 } | 568 } |
590 // HTTP_OK holds a valid response and HTTP_INTERNAL_SERVER_ERROR holds an | 569 // HTTP_OK holds a valid response and HTTP_INTERNAL_SERVER_ERROR holds an |
591 // error code and message for the user. | 570 // error code and message for the user. |
592 case net::HTTP_OK: | 571 case net::HTTP_OK: |
593 case net::HTTP_INTERNAL_SERVER_ERROR: { | 572 case net::HTTP_INTERNAL_SERVER_ERROR: { |
594 scoped_ptr<base::Value> message_value(base::JSONReader::Read(data)); | 573 scoped_ptr<base::Value> message_value(base::JSONReader::Read(data)); |
595 if (message_value.get() && | 574 if (message_value.get() && |
596 message_value->IsType(base::Value::TYPE_DICTIONARY)) { | 575 message_value->IsType(base::Value::TYPE_DICTIONARY)) { |
597 response_dict.reset( | 576 response_dict.reset( |
598 static_cast<base::DictionaryValue*>(message_value.release())); | 577 static_cast<base::DictionaryValue*>(message_value.release())); |
599 } | 578 } |
600 if (response_code == net::HTTP_INTERNAL_SERVER_ERROR) { | 579 if (response_code == net::HTTP_INTERNAL_SERVER_ERROR) { |
601 request_type_ = NO_PENDING_REQUEST; | 580 request_type_ = NO_REQUEST; |
602 | 581 |
603 std::string error_type_string; | 582 std::string error_type_string; |
604 if (!response_dict->GetString(kErrorTypeKey, &error_type_string)) { | 583 if (!response_dict->GetString(kErrorTypeKey, &error_type_string)) { |
605 HandleWalletError(UNKNOWN_ERROR); | 584 HandleWalletError(UNKNOWN_ERROR); |
606 return; | 585 return; |
607 } | 586 } |
608 WalletClient::ErrorType error_type = | 587 WalletClient::ErrorType error_type = |
609 StringToErrorType(error_type_string); | 588 StringToErrorType(error_type_string); |
610 if (error_type == BUYER_ACCOUNT_ERROR) { | 589 if (error_type == BUYER_ACCOUNT_ERROR) { |
611 // If the error_type is |BUYER_ACCOUNT_ERROR|, then | 590 // If the error_type is |BUYER_ACCOUNT_ERROR|, then |
612 // message_type_for_buyer field contains more specific information | 591 // message_type_for_buyer field contains more specific information |
613 // about the error. | 592 // about the error. |
614 std::string message_type_for_buyer_string; | 593 std::string message_type_for_buyer_string; |
615 if (response_dict->GetString(kMessageTypeForBuyerKey, | 594 if (response_dict->GetString(kMessageTypeForBuyerKey, |
616 &message_type_for_buyer_string)) { | 595 &message_type_for_buyer_string)) { |
617 error_type = BuyerErrorStringToErrorType( | 596 error_type = BuyerErrorStringToErrorType( |
618 message_type_for_buyer_string); | 597 message_type_for_buyer_string); |
619 } | 598 } |
620 } | 599 } |
621 | 600 |
622 HandleWalletError(error_type); | 601 HandleWalletError(error_type); |
623 return; | 602 return; |
624 } | 603 } |
625 break; | 604 break; |
626 } | 605 } |
627 | 606 |
628 // Anything else is an error. | 607 // Anything else is an error. |
629 default: | 608 default: |
630 request_type_ = NO_PENDING_REQUEST; | 609 request_type_ = NO_REQUEST; |
631 HandleWalletError(NETWORK_ERROR); | 610 HandleWalletError(NETWORK_ERROR); |
632 return; | 611 return; |
633 } | 612 } |
634 | 613 |
635 RequestType type = request_type_; | 614 RequestType type = request_type_; |
636 request_type_ = NO_PENDING_REQUEST; | 615 request_type_ = NO_REQUEST; |
637 | 616 |
638 if (type != ACCEPT_LEGAL_DOCUMENTS && !response_dict) { | 617 if (type != ACCEPT_LEGAL_DOCUMENTS && !response_dict) { |
639 HandleMalformedResponse(type, scoped_request.get()); | 618 HandleMalformedResponse(type, scoped_request.get()); |
640 return; | 619 return; |
641 } | 620 } |
642 | 621 |
643 switch (type) { | 622 switch (type) { |
644 case ACCEPT_LEGAL_DOCUMENTS: | 623 case ACCEPT_LEGAL_DOCUMENTS: |
645 delegate_->OnDidAcceptLegalDocuments(); | 624 delegate_->OnDidAcceptLegalDocuments(); |
646 break; | 625 break; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 } else { | 680 } else { |
702 LogRequiredActions(required_actions); | 681 LogRequiredActions(required_actions); |
703 delegate_->OnDidSaveToWallet(instrument_id, | 682 delegate_->OnDidSaveToWallet(instrument_id, |
704 shipping_address_id, | 683 shipping_address_id, |
705 required_actions, | 684 required_actions, |
706 form_errors); | 685 form_errors); |
707 } | 686 } |
708 break; | 687 break; |
709 } | 688 } |
710 | 689 |
711 case NO_PENDING_REQUEST: | 690 case NO_REQUEST: |
712 NOTREACHED(); | 691 NOTREACHED(); |
713 } | 692 } |
714 } | 693 } |
715 | 694 |
716 void WalletClient::StartNextPendingRequest() { | |
717 if (pending_requests_.empty()) | |
718 return; | |
719 | |
720 base::Closure next_request = pending_requests_.front(); | |
721 pending_requests_.pop(); | |
722 next_request.Run(); | |
723 } | |
724 | |
725 void WalletClient::HandleMalformedResponse(RequestType request_type, | 695 void WalletClient::HandleMalformedResponse(RequestType request_type, |
726 net::URLFetcher* request) { | 696 net::URLFetcher* request) { |
727 // Called to inform exponential backoff logic of the error. | 697 // Called to inform exponential backoff logic of the error. |
728 request->ReceivedContentWasMalformed(); | 698 request->ReceivedContentWasMalformed(); |
729 // Record failed API call in metrics. | 699 // Record failed API call in metrics. |
730 delegate_->GetMetricLogger().LogWalletMalformedResponseMetric( | 700 delegate_->GetMetricLogger().LogWalletMalformedResponseMetric( |
731 RequestTypeToUmaMetric(request_type)); | 701 RequestTypeToUmaMetric(request_type)); |
732 HandleWalletError(MALFORMED_RESPONSE); | 702 HandleWalletError(MALFORMED_RESPONSE); |
733 } | 703 } |
734 | 704 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 case ACCEPT_LEGAL_DOCUMENTS: | 765 case ACCEPT_LEGAL_DOCUMENTS: |
796 return AutofillMetrics::ACCEPT_LEGAL_DOCUMENTS; | 766 return AutofillMetrics::ACCEPT_LEGAL_DOCUMENTS; |
797 case AUTHENTICATE_INSTRUMENT: | 767 case AUTHENTICATE_INSTRUMENT: |
798 return AutofillMetrics::AUTHENTICATE_INSTRUMENT; | 768 return AutofillMetrics::AUTHENTICATE_INSTRUMENT; |
799 case GET_FULL_WALLET: | 769 case GET_FULL_WALLET: |
800 return AutofillMetrics::GET_FULL_WALLET; | 770 return AutofillMetrics::GET_FULL_WALLET; |
801 case GET_WALLET_ITEMS: | 771 case GET_WALLET_ITEMS: |
802 return AutofillMetrics::GET_WALLET_ITEMS; | 772 return AutofillMetrics::GET_WALLET_ITEMS; |
803 case SAVE_TO_WALLET: | 773 case SAVE_TO_WALLET: |
804 return AutofillMetrics::SAVE_TO_WALLET; | 774 return AutofillMetrics::SAVE_TO_WALLET; |
805 case NO_PENDING_REQUEST: | 775 case NO_REQUEST: |
806 NOTREACHED(); | 776 NOTREACHED(); |
807 return AutofillMetrics::UNKNOWN_API_CALL; | 777 return AutofillMetrics::UNKNOWN_API_CALL; |
808 } | 778 } |
809 | 779 |
810 NOTREACHED(); | 780 NOTREACHED(); |
811 return AutofillMetrics::UNKNOWN_API_CALL; | 781 return AutofillMetrics::UNKNOWN_API_CALL; |
812 } | 782 } |
813 | 783 |
814 } // namespace wallet | 784 } // namespace wallet |
815 } // namespace autofill | 785 } // namespace autofill |
OLD | NEW |