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" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 currency_formatter_.reset(new payments::CurrencyFormatter( | 47 currency_formatter_.reset(new payments::CurrencyFormatter( |
48 base::UTF16ToASCII(web_payment_request_->details.total.amount.currency), | 48 base::UTF16ToASCII(web_payment_request_->details.total.amount.currency), |
49 base::UTF16ToASCII( | 49 base::UTF16ToASCII( |
50 web_payment_request_->details.total.amount.currency_system), | 50 web_payment_request_->details.total.amount.currency_system), |
51 GetApplicationContext()->GetApplicationLocale())); | 51 GetApplicationContext()->GetApplicationLocale())); |
52 } | 52 } |
53 return currency_formatter_.get(); | 53 return currency_formatter_.get(); |
54 } | 54 } |
55 | 55 |
56 void PaymentRequest::PopulateProfileCache() { | 56 void PaymentRequest::PopulateProfileCache() { |
57 for (const auto& profile : personal_data_manager_->GetProfilesToSuggest()) { | 57 for (const auto* profile : personal_data_manager_->GetProfilesToSuggest()) { |
58 profile_cache_.push_back( | 58 profile_cache_.push_back( |
59 base::MakeUnique<autofill::AutofillProfile>(*profile)); | 59 base::MakeUnique<autofill::AutofillProfile>(*profile)); |
60 shipping_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. | 61 // TODO(crbug.com/602666): Implement deduplication rules for profiles. |
62 contact_profiles_.push_back(profile_cache_.back().get()); | 62 contact_profiles_.push_back(profile_cache_.back().get()); |
63 } | 63 } |
64 | 64 |
65 // TODO(crbug.com/602666): Implement prioritization rules for shipping and | 65 // TODO(crbug.com/602666): Implement prioritization rules for shipping and |
66 // contact profiles. | 66 // contact profiles. |
67 | 67 |
68 if (!shipping_profiles_.empty()) | 68 if (!shipping_profiles_.empty()) |
69 selected_shipping_profile_ = shipping_profiles_[0]; | 69 selected_shipping_profile_ = shipping_profiles_[0]; |
70 if (!contact_profiles_.empty()) | 70 if (!contact_profiles_.empty()) |
71 selected_contact_profile_ = contact_profiles_[0]; | 71 selected_contact_profile_ = contact_profiles_[0]; |
72 } | 72 } |
73 | 73 |
74 void PaymentRequest::PopulateCreditCardCache() { | 74 void PaymentRequest::PopulateCreditCardCache() { |
75 DCHECK(web_payment_request_); | 75 DCHECK(web_payment_request_); |
76 std::unordered_set<base::string16> supported_method_types; | 76 std::unordered_set<base::string16> supported_method_types; |
77 for (const auto& method_data : web_payment_request_->method_data) { | 77 for (const auto& method_data : web_payment_request_->method_data) { |
78 for (const auto& supported_method : method_data.supported_methods) | 78 for (const auto& supported_method : method_data.supported_methods) |
79 supported_method_types.insert(supported_method); | 79 supported_method_types.insert(supported_method); |
80 } | 80 } |
81 | 81 |
82 std::vector<autofill::CreditCard*> credit_cards = | 82 std::vector<autofill::CreditCard*> credit_cards = |
83 personal_data_manager_->GetCreditCardsToSuggest(); | 83 personal_data_manager_->GetCreditCardsToSuggest(); |
84 | 84 |
85 for (const auto& credit_card : credit_cards) { | 85 for (const auto* credit_card : credit_cards) { |
86 std::string spec_card_type = | 86 std::string spec_card_type = |
87 autofill::data_util::GetPaymentRequestData(credit_card->type()) | 87 autofill::data_util::GetPaymentRequestData(credit_card->type()) |
88 .basic_card_payment_type; | 88 .basic_card_payment_type; |
89 if (supported_method_types.find(base::ASCIIToUTF16(spec_card_type)) != | 89 if (supported_method_types.find(base::ASCIIToUTF16(spec_card_type)) != |
90 supported_method_types.end()) { | 90 supported_method_types.end()) { |
91 credit_card_cache_.push_back( | 91 credit_card_cache_.push_back( |
92 base::MakeUnique<autofill::CreditCard>(*credit_card)); | 92 base::MakeUnique<autofill::CreditCard>(*credit_card)); |
93 credit_cards_.push_back(credit_card_cache_.back().get()); | 93 credit_cards_.push_back(credit_card_cache_.back().get()); |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 // TODO(crbug.com/602666): Implement prioritization rules for credit cards. | 97 // TODO(crbug.com/602666): Implement prioritization rules for credit cards. |
98 | 98 |
99 if (!credit_cards_.empty()) | 99 if (!credit_cards_.empty()) |
100 selected_credit_card_ = credit_cards_[0]; | 100 selected_credit_card_ = credit_cards_[0]; |
101 } | 101 } |
102 | 102 |
103 void PaymentRequest::PopulateShippingOptionCache() { | 103 void PaymentRequest::PopulateShippingOptionCache() { |
104 DCHECK(web_payment_request_); | 104 DCHECK(web_payment_request_); |
105 shipping_options_.clear(); | 105 shipping_options_.clear(); |
106 shipping_options_.reserve( | 106 shipping_options_.reserve( |
107 web_payment_request_->details.shipping_options.size()); | 107 web_payment_request_->details.shipping_options.size()); |
108 std::transform(std::begin(web_payment_request_->details.shipping_options), | 108 std::transform(std::begin(web_payment_request_->details.shipping_options), |
109 std::end(web_payment_request_->details.shipping_options), | 109 std::end(web_payment_request_->details.shipping_options), |
110 std::back_inserter(shipping_options_), | 110 std::back_inserter(shipping_options_), |
111 [](web::PaymentShippingOption& option) { return &option; }); | 111 [](web::PaymentShippingOption& option) { return &option; }); |
112 | 112 |
113 selected_shipping_option_ = nullptr; | 113 selected_shipping_option_ = nullptr; |
114 for (const auto& shipping_option : shipping_options_) { | 114 for (auto* shipping_option : shipping_options_) { |
115 if (shipping_option->selected) { | 115 if (shipping_option->selected) { |
116 // 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 |
117 // sequence should be treated as the selected item. | 117 // sequence should be treated as the selected item. |
118 selected_shipping_option_ = shipping_option; | 118 selected_shipping_option_ = shipping_option; |
119 } | 119 } |
120 } | 120 } |
121 } | 121 } |
OLD | NEW |