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/payments/payment_request.h" | 5 #include "components/payments/payment_request.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/autofill/core/browser/personal_data_manager.h" | 8 #include "components/autofill/core/browser/personal_data_manager.h" |
| 9 #include "components/payments/payment_details_validation.h" | 9 #include "components/payments/payment_details_validation.h" |
| 10 #include "components/payments/payment_request_delegate.h" | 10 #include "components/payments/payment_request_delegate.h" |
| 11 #include "components/payments/payment_request_web_contents_manager.h" | 11 #include "components/payments/payment_request_web_contents_manager.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 | 14 |
| 15 namespace payments { | 15 namespace payments { |
| 16 | 16 |
| 17 PaymentRequest::PaymentRequest( | 17 PaymentRequest::PaymentRequest( |
| 18 content::WebContents* web_contents, | 18 content::WebContents* web_contents, |
| 19 std::unique_ptr<PaymentRequestDelegate> delegate, | 19 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 20 PaymentRequestWebContentsManager* manager, | 20 PaymentRequestWebContentsManager* manager, |
| 21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | 21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
| 22 : web_contents_(web_contents), | 22 : web_contents_(web_contents), |
| 23 delegate_(std::move(delegate)), | 23 delegate_(std::move(delegate)), |
| 24 manager_(manager), | 24 manager_(manager), |
| 25 binding_(this, std::move(request)) { | 25 binding_(this, std::move(request)), |
| 26 selected_shipping_profile_(nullptr), | |
| 27 selected_contact_profile_(nullptr) { | |
| 26 binding_.set_connection_error_handler( | 28 binding_.set_connection_error_handler( |
| 27 base::Bind(&PaymentRequest::OnError, base::Unretained(this))); | 29 base::Bind(&PaymentRequest::OnError, base::Unretained(this))); |
| 28 } | 30 } |
| 29 | 31 |
| 30 PaymentRequest::~PaymentRequest() {} | 32 PaymentRequest::~PaymentRequest() {} |
| 31 | 33 |
| 32 void PaymentRequest::Init( | 34 void PaymentRequest::Init( |
| 33 payments::mojom::PaymentRequestClientPtr client, | 35 payments::mojom::PaymentRequestClientPtr client, |
| 34 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, | 36 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, |
| 35 payments::mojom::PaymentDetailsPtr details, | 37 payments::mojom::PaymentDetailsPtr details, |
| 36 payments::mojom::PaymentOptionsPtr options) { | 38 payments::mojom::PaymentOptionsPtr options) { |
| 37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 38 std::string error; | 40 std::string error; |
| 39 if (!payments::validatePaymentDetails(details, &error)) { | 41 if (!payments::validatePaymentDetails(details, &error)) { |
| 40 LOG(ERROR) << error; | 42 LOG(ERROR) << error; |
| 41 OnError(); | 43 OnError(); |
| 42 client_.reset(); | 44 client_.reset(); |
| 43 return; | 45 return; |
| 44 } | 46 } |
| 45 client_ = std::move(client); | 47 client_ = std::move(client); |
| 46 details_ = std::move(details); | 48 details_ = std::move(details); |
| 49 PopulateProfileCache(); | |
| 50 SetDefaultProfileSelections(); | |
| 47 } | 51 } |
| 48 | 52 |
| 49 void PaymentRequest::Show() { | 53 void PaymentRequest::Show() { |
| 50 if (!client_.is_bound() || !binding_.is_bound()) { | 54 if (!client_.is_bound() || !binding_.is_bound()) { |
| 51 OnError(); | 55 OnError(); |
| 52 return; | 56 return; |
| 53 } | 57 } |
| 54 delegate_->ShowPaymentRequestDialog(this); | 58 delegate_->ShowPaymentRequestDialog(this); |
| 55 } | 59 } |
| 56 | 60 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 68 const base::Optional<std::string> currency_system, | 72 const base::Optional<std::string> currency_system, |
| 69 const std::string& locale_name) { | 73 const std::string& locale_name) { |
| 70 if (!currency_formatter_) { | 74 if (!currency_formatter_) { |
| 71 currency_formatter_.reset( | 75 currency_formatter_.reset( |
| 72 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 76 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 73 } | 77 } |
| 74 | 78 |
| 75 return currency_formatter_.get(); | 79 return currency_formatter_.get(); |
| 76 } | 80 } |
| 77 | 81 |
| 78 autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { | 82 const std::vector<autofill::AutofillProfile*>& |
| 79 // TODO(tmartino): Implement more sophisticated algorithm for populating | 83 PaymentRequest::shipping_profiles() { |
| 80 // this when it starts empty. | 84 return shipping_profiles_; |
| 81 if (!profile_) { | 85 } |
| 82 autofill::PersonalDataManager* data_manager = | 86 |
| 83 delegate_->GetPersonalDataManager(); | 87 const std::vector<autofill::AutofillProfile*>& |
| 84 auto profiles = data_manager->GetProfiles(); | 88 PaymentRequest::contact_profiles() { |
| 85 if (!profiles.empty()) | 89 return contact_profiles_; |
| 86 profile_ = base::MakeUnique<autofill::AutofillProfile>(*profiles[0]); | |
| 87 } | |
| 88 return profile_ ? profile_.get() : nullptr; | |
| 89 } | 90 } |
| 90 | 91 |
| 91 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { | 92 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { |
| 92 // TODO(anthonyvd): Change this code to prioritize server cards and implement | 93 // TODO(anthonyvd): Change this code to prioritize server cards and implement |
| 93 // a way to modify this function's return value. | 94 // a way to modify this function's return value. |
| 94 autofill::PersonalDataManager* data_manager = | 95 autofill::PersonalDataManager* data_manager = |
| 95 delegate_->GetPersonalDataManager(); | 96 delegate_->GetPersonalDataManager(); |
| 96 | 97 |
| 97 const std::vector<autofill::CreditCard*> cards = | 98 const std::vector<autofill::CreditCard*> cards = |
| 98 data_manager->GetCreditCardsToSuggest(); | 99 data_manager->GetCreditCardsToSuggest(); |
| 99 | 100 |
| 100 auto first_complete_card = std::find_if( | 101 auto first_complete_card = std::find_if( |
| 101 cards.begin(), | 102 cards.begin(), |
| 102 cards.end(), | 103 cards.end(), |
| 103 [] (autofill::CreditCard* card) { | 104 [] (autofill::CreditCard* card) { |
| 104 return card->IsValid(); | 105 return card->IsValid(); |
| 105 }); | 106 }); |
| 106 | 107 |
| 107 return first_complete_card == cards.end() ? nullptr : *first_complete_card; | 108 return first_complete_card == cards.end() ? nullptr : *first_complete_card; |
| 108 } | 109 } |
| 109 | 110 |
| 111 void PaymentRequest::PopulateProfileCache() { | |
| 112 autofill::PersonalDataManager* data_manager = | |
| 113 delegate_->GetPersonalDataManager(); | |
| 114 std::vector<autofill::AutofillProfile*> profiles = | |
| 115 data_manager->GetProfilesToSuggest(); | |
| 116 | |
| 117 // PaymentRequest may outlive the Profiles returned by the Data Manager. | |
| 118 // Thus, we store copies, and return a vector of pointers to these copies | |
| 119 // whenever Profiles are requested. | |
| 120 for (size_t i = 0; i < profiles.size(); i++) { | |
| 121 profile_cache_.push_back( | |
| 122 base::MakeUnique<autofill::AutofillProfile>(*profiles[0])); | |
|
please use gerrit instead
2017/01/25 15:07:47
profiles[i]
tmartino
2017/01/25 18:48:10
Good catch, thanks.
| |
| 123 | |
| 124 // TODO(tmartino): Implement deduplication rules specific to shipping and | |
| 125 // contact profiles. | |
| 126 shipping_profiles_.push_back(profile_cache_[i].get()); | |
| 127 contact_profiles_.push_back(profile_cache_[i].get()); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void PaymentRequest::SetDefaultProfileSelections() { | |
| 132 if (!shipping_profiles().empty()) | |
| 133 set_selected_shipping_profile(shipping_profiles()[0]); | |
| 134 | |
| 135 if (!contact_profiles().empty()) | |
| 136 set_selected_contact_profile(contact_profiles()[0]); | |
| 137 } | |
| 138 | |
| 110 } // namespace payments | 139 } // namespace payments |
| OLD | NEW |