Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/payments/content/payment_request_state.cc

Issue 2775553004: [WebPayments] Implementing Profile filter and dedupe (Closed)
Patch Set: Adding omitted files Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/content/payment_request_state.h" 5 #include "components/payments/content/payment_request_state.h"
6 6
7 #include "components/autofill/core/browser/autofill_data_util.h" 7 #include "components/autofill/core/browser/autofill_data_util.h"
8 #include "components/autofill/core/browser/autofill_profile.h" 8 #include "components/autofill/core/browser/autofill_profile.h"
9 #include "components/autofill/core/browser/credit_card.h" 9 #include "components/autofill/core/browser/credit_card.h"
10 #include "components/autofill/core/browser/personal_data_manager.h" 10 #include "components/autofill/core/browser/personal_data_manager.h"
11 #include "components/payments/content/payment_request_spec.h" 11 #include "components/payments/content/payment_request_spec.h"
12 #include "components/payments/content/profile_util.h"
12 #include "components/payments/core/autofill_payment_instrument.h" 13 #include "components/payments/core/autofill_payment_instrument.h"
13 14
14 namespace payments { 15 namespace payments {
15 16
16 PaymentRequestState::PaymentRequestState( 17 PaymentRequestState::PaymentRequestState(
17 PaymentRequestSpec* spec, 18 PaymentRequestSpec* spec,
18 Delegate* delegate, 19 Delegate* delegate,
19 const std::string& app_locale, 20 const std::string& app_locale,
20 autofill::PersonalDataManager* personal_data_manager) 21 autofill::PersonalDataManager* personal_data_manager)
21 : is_ready_to_pay_(false), 22 : is_ready_to_pay_(false),
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 std::vector<autofill::AutofillProfile*> profiles = 90 std::vector<autofill::AutofillProfile*> profiles =
90 personal_data_manager_->GetProfilesToSuggest(); 91 personal_data_manager_->GetProfilesToSuggest();
91 92
92 // PaymentRequest may outlive the Profiles returned by the Data Manager. 93 // PaymentRequest may outlive the Profiles returned by the Data Manager.
93 // Thus, we store copies, and return a vector of pointers to these copies 94 // Thus, we store copies, and return a vector of pointers to these copies
94 // whenever Profiles are requested. The same is true for credit cards. 95 // whenever Profiles are requested. The same is true for credit cards.
95 for (size_t i = 0; i < profiles.size(); i++) { 96 for (size_t i = 0; i < profiles.size(); i++) {
96 profile_cache_.push_back( 97 profile_cache_.push_back(
97 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); 98 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
98 99
99 // TODO(tmartino): Implement deduplication rules specific to shipping and 100 // TODO(tmartino): Implement deduplication rules specific to shipping
100 // contact profiles. 101 // profiles.
101 shipping_profiles_.push_back(profile_cache_[i].get()); 102 shipping_profiles_.push_back(profile_cache_[i].get());
102 contact_profiles_.push_back(profile_cache_[i].get());
103 } 103 }
104 104
105 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering(
106 profile_cache_.size());
107 std::transform(profile_cache_.begin(), profile_cache_.end(),
108 raw_profiles_for_filtering.begin(),
109 [](const std::unique_ptr<autofill::AutofillProfile>& p) {
110 return p.get();
111 });
112
113 contact_profiles_ = profile_util::FilterProfilesForContact(
114 raw_profiles_for_filtering, GetApplicationLocale(), spec_);
115
105 // Create the list of available instruments. 116 // Create the list of available instruments.
106 const std::vector<autofill::CreditCard*>& cards = 117 const std::vector<autofill::CreditCard*>& cards =
107 personal_data_manager_->GetCreditCardsToSuggest(); 118 personal_data_manager_->GetCreditCardsToSuggest();
108 const std::set<std::string>& supported_card_networks = 119 const std::set<std::string>& supported_card_networks =
109 spec_->supported_card_networks_set(); 120 spec_->supported_card_networks_set();
110 for (autofill::CreditCard* card : cards) { 121 for (autofill::CreditCard* card : cards) {
111 std::string basic_card_network = 122 std::string basic_card_network =
112 autofill::data_util::GetPaymentRequestData(card->type()) 123 autofill::data_util::GetPaymentRequestData(card->type())
113 .basic_card_payment_type; 124 .basic_card_payment_type;
114 if (!supported_card_networks.count(basic_card_network)) 125 if (!supported_card_networks.count(basic_card_network))
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 // instruments are listed/created in the flow. 178 // instruments are listed/created in the flow.
168 // TODO(crbug.com/702063): A masked card may not satisfy IsValid(). 179 // TODO(crbug.com/702063): A masked card may not satisfy IsValid().
169 return selected_instrument_ != nullptr && selected_instrument_->IsValid(); 180 return selected_instrument_ != nullptr && selected_instrument_->IsValid();
170 } 181 }
171 182
172 bool PaymentRequestState::ArePaymentOptionsSatisfied() { 183 bool PaymentRequestState::ArePaymentOptionsSatisfied() {
173 // TODO(mathp): Have a measure of shipping address completeness. 184 // TODO(mathp): Have a measure of shipping address completeness.
174 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) 185 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr)
175 return false; 186 return false;
176 187
177 // TODO(mathp): Make an encompassing class to validate contact info. 188 profile_util::PaymentsProfileComparator comparator(app_locale_, spec_);
178 if (spec_->request_payer_name() && 189 return comparator.IsContactInfoComplete(selected_contact_profile_);
179 (selected_contact_profile_ == nullptr ||
180 selected_contact_profile_
181 ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale_)
182 .empty())) {
183 return false;
184 }
185 if (spec_->request_payer_email() &&
186 (selected_contact_profile_ == nullptr ||
187 selected_contact_profile_
188 ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS),
189 app_locale_)
190 .empty())) {
191 return false;
192 }
193 if (spec_->request_payer_phone() &&
194 (selected_contact_profile_ == nullptr ||
195 selected_contact_profile_
196 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER),
197 app_locale_)
198 .empty())) {
199 return false;
200 }
201
202 return true;
203 } 190 }
204 191
205 void PaymentRequestState::UpdateSelectedShippingOption() { 192 void PaymentRequestState::UpdateSelectedShippingOption() {
206 selected_shipping_option_ = nullptr; 193 selected_shipping_option_ = nullptr;
207 194
208 // As per the spec, the selected shipping option should initially be the last 195 // As per the spec, the selected shipping option should initially be the last
209 // one in the array that has its selected field set to true. 196 // one in the array that has its selected field set to true.
210 auto selected_shipping_option_it = std::find_if( 197 auto selected_shipping_option_it = std::find_if(
211 spec_->details().shipping_options.rbegin(), 198 spec_->details().shipping_options.rbegin(),
212 spec_->details().shipping_options.rend(), 199 spec_->details().shipping_options.rend(),
213 [](const payments::mojom::PaymentShippingOptionPtr& element) { 200 [](const payments::mojom::PaymentShippingOptionPtr& element) {
214 return element->selected; 201 return element->selected;
215 }); 202 });
216 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { 203 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) {
217 selected_shipping_option_ = selected_shipping_option_it->get(); 204 selected_shipping_option_ = selected_shipping_option_it->get();
218 } 205 }
219 } 206 }
220 207
221 } // namespace payments 208 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698