Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/core/browser/payments/full_card_request.h" | 5 #include "components/autofill/core/browser/payments/full_card_request.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_client.h" | 10 #include "components/autofill/core/browser/autofill_client.h" |
| 11 #include "components/autofill/core/browser/autofill_metrics.h" | 11 #include "components/autofill/core/browser/autofill_metrics.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" | 13 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 | 14 |
| 15 namespace autofill { | 15 namespace autofill { |
| 16 namespace payments { | 16 namespace payments { |
| 17 | 17 |
| 18 FullCardRequest::FullCardRequest(AutofillClient* autofill_client, | 18 FullCardRequest::FullCardRequest(AutofillClient* autofill_client, |
| 19 payments::PaymentsClient* payments_client, | 19 payments::PaymentsClient* payments_client, |
| 20 PersonalDataManager* personal_data_manager) | 20 PersonalDataManager* personal_data_manager) |
| 21 : autofill_client_(autofill_client), | 21 : autofill_client_(autofill_client), |
| 22 payments_client_(payments_client), | 22 payments_client_(payments_client), |
| 23 personal_data_manager_(personal_data_manager), | 23 personal_data_manager_(personal_data_manager), |
| 24 delegate_(nullptr), | 24 delegate_(nullptr), |
| 25 should_unmask_card_(false), | |
| 25 weak_ptr_factory_(this) { | 26 weak_ptr_factory_(this) { |
| 26 DCHECK(autofill_client_); | 27 DCHECK(autofill_client_); |
| 28 DCHECK(payments_client_); | |
| 27 DCHECK(personal_data_manager_); | 29 DCHECK(personal_data_manager_); |
| 28 } | 30 } |
| 29 | 31 |
| 30 FullCardRequest::~FullCardRequest() {} | 32 FullCardRequest::~FullCardRequest() {} |
| 31 | 33 |
| 32 void FullCardRequest::GetFullCard(const CreditCard& card, | 34 void FullCardRequest::GetFullCard(const CreditCard& card, |
| 33 AutofillClient::UnmaskCardReason reason, | 35 AutofillClient::UnmaskCardReason reason, |
| 34 base::WeakPtr<Delegate> delegate) { | 36 base::WeakPtr<Delegate> delegate) { |
| 35 DCHECK(delegate); | 37 DCHECK(delegate); |
| 36 | 38 |
| 37 // Only request can be active a time. If the member variable |delegate_| is | 39 // Only request can be active a time. If the member variable |delegate_| is |
| 38 // already set, then immediately reject the new request through the method | 40 // already set, then immediately reject the new request through the method |
| 39 // parameter |delegate|. | 41 // parameter |delegate|. |
| 40 if (delegate_) { | 42 if (delegate_) { |
| 41 delegate->OnFullCardError(); | 43 delegate->OnFullCardError(); |
| 42 return; | 44 return; |
| 43 } | 45 } |
| 44 | 46 |
| 45 delegate_ = delegate; | 47 delegate_ = delegate; |
| 46 request_.reset(new payments::PaymentsClient::UnmaskRequestDetails); | 48 request_.reset(new payments::PaymentsClient::UnmaskRequestDetails); |
| 47 request_->card = card; | 49 request_->card = card; |
| 48 bool is_masked = card.record_type() == CreditCard::MASKED_SERVER_CARD; | 50 should_unmask_card_ = card.record_type() == CreditCard::MASKED_SERVER_CARD || |
| 49 if (is_masked) | 51 (card.record_type() == CreditCard::FULL_SERVER_CARD && |
| 52 card.ShouldUpdateExpiration()); | |
| 53 if (should_unmask_card_) | |
| 50 payments_client_->Prepare(); | 54 payments_client_->Prepare(); |
| 51 | 55 |
| 52 autofill_client_->ShowUnmaskPrompt(request_->card, reason, | 56 autofill_client_->ShowUnmaskPrompt(request_->card, reason, |
| 53 weak_ptr_factory_.GetWeakPtr()); | 57 weak_ptr_factory_.GetWeakPtr()); |
| 54 | 58 |
| 55 if (is_masked) { | 59 if (should_unmask_card_) { |
| 56 autofill_client_->LoadRiskData( | 60 autofill_client_->LoadRiskData( |
| 57 base::Bind(&FullCardRequest::OnDidGetUnmaskRiskData, | 61 base::Bind(&FullCardRequest::OnDidGetUnmaskRiskData, |
| 58 weak_ptr_factory_.GetWeakPtr())); | 62 weak_ptr_factory_.GetWeakPtr())); |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 bool FullCardRequest::IsGettingFullCard() const { | 66 bool FullCardRequest::IsGettingFullCard() const { |
| 63 return !!request_; | 67 return !!request_; |
| 64 } | 68 } |
| 65 | 69 |
| 66 void FullCardRequest::OnUnmaskResponse(const UnmaskResponse& response) { | 70 void FullCardRequest::OnUnmaskResponse(const UnmaskResponse& response) { |
| 67 // TODO(rouslan): Update the expiration date of the card on disk. | |
| 68 // http://crbug.com/606008 | |
| 69 if (!response.exp_month.empty()) | 71 if (!response.exp_month.empty()) |
| 70 request_->card.SetRawInfo(CREDIT_CARD_EXP_MONTH, response.exp_month); | 72 request_->card.SetRawInfo(CREDIT_CARD_EXP_MONTH, response.exp_month); |
| 71 | 73 |
| 72 if (!response.exp_year.empty()) | 74 if (!response.exp_year.empty()) |
| 73 request_->card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, response.exp_year); | 75 request_->card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, response.exp_year); |
| 74 | 76 |
| 75 if (request_->card.record_type() != CreditCard::MASKED_SERVER_CARD) { | 77 if (request_->card.record_type() == CreditCard::LOCAL_CARD && |
|
sebsg
2016/04/27 14:59:02
Quick question for my understanding. In what circu
please use gerrit instead
2016/04/27 18:05:44
PaymentRequest uses FullCardRequest to prompt the
| |
| 78 (!response.exp_month.empty() || !response.exp_year.empty())) { | |
| 79 personal_data_manager_->UpdateCreditCard(request_->card); | |
| 80 } | |
| 81 | |
| 82 if (!should_unmask_card_) { | |
| 76 if (delegate_) | 83 if (delegate_) |
| 77 delegate_->OnFullCardDetails(request_->card, response.cvc); | 84 delegate_->OnFullCardDetails(request_->card, response.cvc); |
| 78 delegate_ = nullptr; | 85 Reset(); |
| 79 autofill_client_->OnUnmaskVerificationResult(AutofillClient::SUCCESS); | 86 autofill_client_->OnUnmaskVerificationResult(AutofillClient::SUCCESS); |
| 80 request_.reset(); | |
| 81 return; | 87 return; |
| 82 } | 88 } |
| 83 | 89 |
| 84 request_->user_response = response; | 90 request_->user_response = response; |
| 85 if (!request_->risk_data.empty()) { | 91 if (!request_->risk_data.empty()) { |
| 86 real_pan_request_timestamp_ = base::Time::Now(); | 92 real_pan_request_timestamp_ = base::Time::Now(); |
| 87 payments_client_->UnmaskCard(*request_); | 93 payments_client_->UnmaskCard(*request_); |
| 88 } | 94 } |
| 89 } | 95 } |
| 90 | 96 |
| 91 void FullCardRequest::OnUnmaskPromptClosed() { | 97 void FullCardRequest::OnUnmaskPromptClosed() { |
| 92 if (delegate_) | 98 if (delegate_) |
| 93 delegate_->OnFullCardError(); | 99 delegate_->OnFullCardError(); |
| 94 | 100 |
| 95 delegate_ = nullptr; | 101 Reset(); |
| 96 payments_client_->CancelRequest(); | 102 payments_client_->CancelRequest(); |
| 97 request_.reset(); | |
| 98 } | 103 } |
| 99 | 104 |
| 100 void FullCardRequest::OnDidGetUnmaskRiskData(const std::string& risk_data) { | 105 void FullCardRequest::OnDidGetUnmaskRiskData(const std::string& risk_data) { |
| 101 request_->risk_data = risk_data; | 106 request_->risk_data = risk_data; |
| 102 if (!request_->user_response.cvc.empty()) { | 107 if (!request_->user_response.cvc.empty()) { |
| 103 real_pan_request_timestamp_ = base::Time::Now(); | 108 real_pan_request_timestamp_ = base::Time::Now(); |
| 104 payments_client_->UnmaskCard(*request_); | 109 payments_client_->UnmaskCard(*request_); |
| 105 } | 110 } |
| 106 } | 111 } |
| 107 | 112 |
| 108 void FullCardRequest::OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, | 113 void FullCardRequest::OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, |
| 109 const std::string& real_pan) { | 114 const std::string& real_pan) { |
| 110 AutofillMetrics::LogRealPanDuration( | 115 AutofillMetrics::LogRealPanDuration( |
| 111 base::Time::Now() - real_pan_request_timestamp_, result); | 116 base::Time::Now() - real_pan_request_timestamp_, result); |
| 112 | 117 |
| 113 if (!real_pan.empty()) { | 118 if (!real_pan.empty()) { |
| 114 DCHECK_EQ(AutofillClient::SUCCESS, result); | 119 DCHECK_EQ(AutofillClient::SUCCESS, result); |
| 115 request_->card.set_record_type(CreditCard::FULL_SERVER_CARD); | 120 request_->card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 116 request_->card.SetNumber(base::UTF8ToUTF16(real_pan)); | 121 request_->card.SetNumber(base::UTF8ToUTF16(real_pan)); |
| 122 request_->card.SetServerStatus(CreditCard::OK); | |
| 117 | 123 |
| 118 if (request_->user_response.should_store_pan) | 124 if (request_->user_response.should_store_pan) |
| 119 personal_data_manager_->UpdateServerCreditCard(request_->card); | 125 personal_data_manager_->UpdateServerCreditCard(request_->card); |
| 120 | 126 |
| 121 if (delegate_) | 127 if (delegate_) |
| 122 delegate_->OnFullCardDetails(request_->card, request_->user_response.cvc); | 128 delegate_->OnFullCardDetails(request_->card, request_->user_response.cvc); |
| 123 } else { | 129 } else { |
| 124 if (delegate_) | 130 if (delegate_) |
| 125 delegate_->OnFullCardError(); | 131 delegate_->OnFullCardError(); |
| 126 } | 132 } |
| 127 | 133 |
| 134 Reset(); | |
| 135 autofill_client_->OnUnmaskVerificationResult(result); | |
| 136 } | |
| 137 | |
| 138 void FullCardRequest::Reset() { | |
| 128 delegate_ = nullptr; | 139 delegate_ = nullptr; |
| 129 autofill_client_->OnUnmaskVerificationResult(result); | |
| 130 request_.reset(); | 140 request_.reset(); |
| 141 should_unmask_card_ = false; | |
| 131 } | 142 } |
| 132 | 143 |
| 133 } // namespace payments | 144 } // namespace payments |
| 134 } // namespace autofill | 145 } // namespace autofill |
| OLD | NEW |