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