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

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

Issue 2775553004: [WebPayments] Implementing Profile filter and dedupe (Closed)
Patch Set: include algo Created 3 years, 8 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 <set> 7 #include <set>
8 8
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_data_util.h" 10 #include "components/autofill/core/browser/autofill_data_util.h"
11 #include "components/autofill/core/browser/autofill_profile.h" 11 #include "components/autofill/core/browser/autofill_profile.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 #include "components/payments/content/payment_request_spec.h" 14 #include "components/payments/content/payment_request_spec.h"
15 #include "components/payments/content/payment_response_helper.h" 15 #include "components/payments/content/payment_response_helper.h"
16 #include "components/payments/core/autofill_payment_instrument.h" 16 #include "components/payments/core/autofill_payment_instrument.h"
17 #include "components/payments/core/profile_util.h"
17 18
18 namespace payments { 19 namespace payments {
19 20
20 PaymentRequestState::PaymentRequestState( 21 PaymentRequestState::PaymentRequestState(
21 PaymentRequestSpec* spec, 22 PaymentRequestSpec* spec,
22 Delegate* delegate, 23 Delegate* delegate,
23 const std::string& app_locale, 24 const std::string& app_locale,
24 autofill::PersonalDataManager* personal_data_manager) 25 autofill::PersonalDataManager* personal_data_manager)
25 : is_ready_to_pay_(false), 26 : is_ready_to_pay_(false),
26 app_locale_(app_locale), 27 app_locale_(app_locale),
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 std::vector<autofill::AutofillProfile*> profiles = 150 std::vector<autofill::AutofillProfile*> profiles =
150 personal_data_manager_->GetProfilesToSuggest(); 151 personal_data_manager_->GetProfilesToSuggest();
151 152
152 // PaymentRequest may outlive the Profiles returned by the Data Manager. 153 // PaymentRequest may outlive the Profiles returned by the Data Manager.
153 // Thus, we store copies, and return a vector of pointers to these copies 154 // Thus, we store copies, and return a vector of pointers to these copies
154 // whenever Profiles are requested. The same is true for credit cards. 155 // whenever Profiles are requested. The same is true for credit cards.
155 for (size_t i = 0; i < profiles.size(); i++) { 156 for (size_t i = 0; i < profiles.size(); i++) {
156 profile_cache_.push_back( 157 profile_cache_.push_back(
157 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); 158 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
158 159
159 // TODO(tmartino): Implement deduplication rules specific to shipping and 160 // TODO(tmartino): Implement deduplication rules specific to shipping
160 // contact profiles. 161 // profiles.
161 shipping_profiles_.push_back(profile_cache_[i].get()); 162 shipping_profiles_.push_back(profile_cache_[i].get());
162 contact_profiles_.push_back(profile_cache_[i].get());
163 } 163 }
164 164
165 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering(
166 profile_cache_.size());
167 std::transform(profile_cache_.begin(), profile_cache_.end(),
168 raw_profiles_for_filtering.begin(),
169 [](const std::unique_ptr<autofill::AutofillProfile>& p) {
170 return p.get();
171 });
172
173 contact_profiles_ = profile_util::FilterProfilesForContact(
174 raw_profiles_for_filtering, GetApplicationLocale(), spec_);
175
165 // Create the list of available instruments. 176 // Create the list of available instruments.
166 const std::vector<autofill::CreditCard*>& cards = 177 const std::vector<autofill::CreditCard*>& cards =
167 personal_data_manager_->GetCreditCardsToSuggest(); 178 personal_data_manager_->GetCreditCardsToSuggest();
168 const std::set<std::string>& supported_card_networks = 179 const std::set<std::string>& supported_card_networks =
169 spec_->supported_card_networks_set(); 180 spec_->supported_card_networks_set();
170 for (autofill::CreditCard* card : cards) { 181 for (autofill::CreditCard* card : cards) {
171 std::string basic_card_network = 182 std::string basic_card_network =
172 autofill::data_util::GetPaymentRequestData(card->type()) 183 autofill::data_util::GetPaymentRequestData(card->type())
173 .basic_card_payment_type; 184 .basic_card_payment_type;
174 if (!supported_card_networks.count(basic_card_network)) 185 if (!supported_card_networks.count(basic_card_network))
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 // instruments are listed/created in the flow. 238 // instruments are listed/created in the flow.
228 // TODO(crbug.com/702063): A masked card may not satisfy IsValid(). 239 // TODO(crbug.com/702063): A masked card may not satisfy IsValid().
229 return selected_instrument_ != nullptr && selected_instrument_->IsValid(); 240 return selected_instrument_ != nullptr && selected_instrument_->IsValid();
230 } 241 }
231 242
232 bool PaymentRequestState::ArePaymentOptionsSatisfied() { 243 bool PaymentRequestState::ArePaymentOptionsSatisfied() {
233 // TODO(mathp): Have a measure of shipping address completeness. 244 // TODO(mathp): Have a measure of shipping address completeness.
234 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) 245 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr)
235 return false; 246 return false;
236 247
237 // TODO(mathp): Make an encompassing class to validate contact info. 248 profile_util::PaymentsProfileComparator comparator(app_locale_, spec_);
238 if (spec_->request_payer_name() && 249 return comparator.IsContactInfoComplete(selected_contact_profile_);
239 (selected_contact_profile_ == nullptr ||
240 selected_contact_profile_
241 ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale_)
242 .empty())) {
243 return false;
244 }
245 if (spec_->request_payer_email() &&
246 (selected_contact_profile_ == nullptr ||
247 selected_contact_profile_
248 ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS),
249 app_locale_)
250 .empty())) {
251 return false;
252 }
253 if (spec_->request_payer_phone() &&
254 (selected_contact_profile_ == nullptr ||
255 selected_contact_profile_
256 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER),
257 app_locale_)
258 .empty())) {
259 return false;
260 }
261
262 return true;
263 } 250 }
264 251
265 void PaymentRequestState::UpdateSelectedShippingOption() { 252 void PaymentRequestState::UpdateSelectedShippingOption() {
266 selected_shipping_option_ = nullptr; 253 selected_shipping_option_ = nullptr;
267 254
268 // As per the spec, the selected shipping option should initially be the last 255 // As per the spec, the selected shipping option should initially be the last
269 // one in the array that has its selected field set to true. 256 // one in the array that has its selected field set to true.
270 auto selected_shipping_option_it = std::find_if( 257 auto selected_shipping_option_it = std::find_if(
271 spec_->details().shipping_options.rbegin(), 258 spec_->details().shipping_options.rbegin(),
272 spec_->details().shipping_options.rend(), 259 spec_->details().shipping_options.rend(),
273 [](const payments::mojom::PaymentShippingOptionPtr& element) { 260 [](const payments::mojom::PaymentShippingOptionPtr& element) {
274 return element->selected; 261 return element->selected;
275 }); 262 });
276 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { 263 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) {
277 selected_shipping_option_ = selected_shipping_option_it->get(); 264 selected_shipping_option_ = selected_shipping_option_it->get();
278 } 265 }
279 } 266 }
280 267
281 } // namespace payments 268 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698