OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "ios/chrome/browser/payments/payment_request.h" | 5 #include "ios/chrome/browser/payments/payment_request.h" |
6 | 6 |
7 #include <unordered_set> | 7 #include <unordered_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/core/currency_formatter.h" | 14 #include "components/payments/core/currency_formatter.h" |
15 #include "ios/chrome/browser/application_context.h" | 15 #include "ios/chrome/browser/application_context.h" |
16 #include "ios/web/public/payments/payment_request.h" | 16 #include "ios/web/public/payments/payment_request.h" |
17 | 17 |
18 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
19 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
20 #endif | 20 #endif |
21 | 21 |
22 PaymentRequest::PaymentRequest( | 22 PaymentRequest::PaymentRequest( |
23 std::unique_ptr<web::PaymentRequest> web_payment_request, | 23 std::unique_ptr<web::PaymentRequest> web_payment_request, |
24 autofill::PersonalDataManager* personal_data_manager) | 24 autofill::PersonalDataManager* personal_data_manager) |
25 : web_payment_request_(std::move(web_payment_request)), | 25 : web_payment_request_(std::move(web_payment_request)), |
26 personal_data_manager_(personal_data_manager), | 26 personal_data_manager_(personal_data_manager), |
27 selected_shipping_profile_(nullptr), | 27 selected_shipping_profile_(nullptr), |
| 28 selected_contact_profile_(nullptr), |
28 selected_credit_card_(nullptr), | 29 selected_credit_card_(nullptr), |
29 selected_shipping_option_(nullptr) { | 30 selected_shipping_option_(nullptr) { |
30 PopulateProfileCache(); | 31 PopulateProfileCache(); |
31 PopulateCreditCardCache(); | 32 PopulateCreditCardCache(); |
32 PopulateShippingOptionCache(); | 33 PopulateShippingOptionCache(); |
33 } | 34 } |
34 | 35 |
35 PaymentRequest::~PaymentRequest() {} | 36 PaymentRequest::~PaymentRequest() {} |
36 | 37 |
37 void PaymentRequest::set_payment_details(const web::PaymentDetails& details) { | 38 void PaymentRequest::set_payment_details(const web::PaymentDetails& details) { |
(...skipping 11 matching lines...) Expand all Loading... |
49 web_payment_request_->details.total.amount.currency_system), | 50 web_payment_request_->details.total.amount.currency_system), |
50 GetApplicationContext()->GetApplicationLocale())); | 51 GetApplicationContext()->GetApplicationLocale())); |
51 } | 52 } |
52 return currency_formatter_.get(); | 53 return currency_formatter_.get(); |
53 } | 54 } |
54 | 55 |
55 void PaymentRequest::PopulateProfileCache() { | 56 void PaymentRequest::PopulateProfileCache() { |
56 for (const auto& profile : personal_data_manager_->GetProfilesToSuggest()) { | 57 for (const auto& profile : personal_data_manager_->GetProfilesToSuggest()) { |
57 profile_cache_.push_back( | 58 profile_cache_.push_back( |
58 base::MakeUnique<autofill::AutofillProfile>(*profile)); | 59 base::MakeUnique<autofill::AutofillProfile>(*profile)); |
59 profiles_.push_back(profile_cache_.back().get()); | 60 shipping_profiles_.push_back(profile_cache_.back().get()); |
| 61 // TODO(crbug.com/602666): Implement deduplication rules for profiles. |
| 62 contact_profiles_.push_back(profile_cache_.back().get()); |
60 } | 63 } |
61 | 64 |
62 if (!profiles_.empty()) | 65 // TODO(crbug.com/602666): Implement prioritization rules for shipping and |
63 selected_shipping_profile_ = profiles_[0]; | 66 // contact profiles. |
| 67 |
| 68 if (!shipping_profiles_.empty()) |
| 69 selected_shipping_profile_ = shipping_profiles_[0]; |
| 70 if (!contact_profiles_.empty()) |
| 71 selected_contact_profile_ = contact_profiles_[0]; |
64 } | 72 } |
65 | 73 |
66 void PaymentRequest::PopulateCreditCardCache() { | 74 void PaymentRequest::PopulateCreditCardCache() { |
67 DCHECK(web_payment_request_); | 75 DCHECK(web_payment_request_); |
68 std::unordered_set<base::string16> supported_method_types; | 76 std::unordered_set<base::string16> supported_method_types; |
69 for (const auto& method_data : web_payment_request_->method_data) { | 77 for (const auto& method_data : web_payment_request_->method_data) { |
70 for (const auto& supported_method : method_data.supported_methods) | 78 for (const auto& supported_method : method_data.supported_methods) |
71 supported_method_types.insert(supported_method); | 79 supported_method_types.insert(supported_method); |
72 } | 80 } |
73 | 81 |
74 std::vector<autofill::CreditCard*> credit_cards = | 82 std::vector<autofill::CreditCard*> credit_cards = |
75 personal_data_manager_->GetCreditCardsToSuggest(); | 83 personal_data_manager_->GetCreditCardsToSuggest(); |
76 | 84 |
77 for (const auto& credit_card : credit_cards) { | 85 for (const auto& credit_card : credit_cards) { |
78 std::string spec_card_type = | 86 std::string spec_card_type = |
79 autofill::data_util::GetPaymentRequestData(credit_card->type()) | 87 autofill::data_util::GetPaymentRequestData(credit_card->type()) |
80 .basic_card_payment_type; | 88 .basic_card_payment_type; |
81 if (supported_method_types.find(base::ASCIIToUTF16(spec_card_type)) != | 89 if (supported_method_types.find(base::ASCIIToUTF16(spec_card_type)) != |
82 supported_method_types.end()) { | 90 supported_method_types.end()) { |
83 credit_card_cache_.push_back( | 91 credit_card_cache_.push_back( |
84 base::MakeUnique<autofill::CreditCard>(*credit_card)); | 92 base::MakeUnique<autofill::CreditCard>(*credit_card)); |
85 credit_cards_.push_back(credit_card_cache_.back().get()); | 93 credit_cards_.push_back(credit_card_cache_.back().get()); |
86 } | 94 } |
87 } | 95 } |
88 | 96 |
| 97 // TODO(crbug.com/602666): Implement prioritization rules for credit cards. |
| 98 |
89 if (!credit_cards_.empty()) | 99 if (!credit_cards_.empty()) |
90 selected_credit_card_ = credit_cards_[0]; | 100 selected_credit_card_ = credit_cards_[0]; |
91 } | 101 } |
92 | 102 |
93 void PaymentRequest::PopulateShippingOptionCache() { | 103 void PaymentRequest::PopulateShippingOptionCache() { |
94 DCHECK(web_payment_request_); | 104 DCHECK(web_payment_request_); |
95 shipping_options_.clear(); | 105 shipping_options_.clear(); |
96 shipping_options_.reserve( | 106 shipping_options_.reserve( |
97 web_payment_request_->details.shipping_options.size()); | 107 web_payment_request_->details.shipping_options.size()); |
98 std::transform(std::begin(web_payment_request_->details.shipping_options), | 108 std::transform(std::begin(web_payment_request_->details.shipping_options), |
99 std::end(web_payment_request_->details.shipping_options), | 109 std::end(web_payment_request_->details.shipping_options), |
100 std::back_inserter(shipping_options_), | 110 std::back_inserter(shipping_options_), |
101 [](web::PaymentShippingOption& option) { return &option; }); | 111 [](web::PaymentShippingOption& option) { return &option; }); |
102 | 112 |
103 selected_shipping_option_ = nullptr; | 113 selected_shipping_option_ = nullptr; |
104 for (const auto& shipping_option : shipping_options_) { | 114 for (const auto& shipping_option : shipping_options_) { |
105 if (shipping_option->selected) { | 115 if (shipping_option->selected) { |
106 // If more than one option has |selected| set, the last one in the | 116 // If more than one option has |selected| set, the last one in the |
107 // sequence should be treated as the selected item. | 117 // sequence should be treated as the selected item. |
108 selected_shipping_option_ = shipping_option; | 118 selected_shipping_option_ = shipping_option; |
109 } | 119 } |
110 } | 120 } |
111 } | 121 } |
OLD | NEW |