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/content/payment_request_spec.h" | 5 #include "components/payments/content/payment_request_spec.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "components/payments/core/payment_method_data.h" | 10 #include "components/payments/core/payment_method_data.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 129 |
130 void PaymentRequestSpec::StartWaitingForUpdateWith( | 130 void PaymentRequestSpec::StartWaitingForUpdateWith( |
131 PaymentRequestSpec::UpdateReason reason) { | 131 PaymentRequestSpec::UpdateReason reason) { |
132 for (auto& observer : observers_) { | 132 for (auto& observer : observers_) { |
133 observer.OnStartUpdating(reason); | 133 observer.OnStartUpdating(reason); |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 void PaymentRequestSpec::PopulateValidatedMethodData( | 137 void PaymentRequestSpec::PopulateValidatedMethodData( |
138 const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom) { | 138 const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom) { |
139 if (method_data_mojom.empty()) { | |
140 LOG(ERROR) << "Invalid payment methods or data"; | |
141 NotifyOnInvalidSpecProvided(); | |
142 return; | |
143 } | |
144 | |
145 std::vector<PaymentMethodData> method_data_vector; | 139 std::vector<PaymentMethodData> method_data_vector; |
146 method_data_vector.reserve(method_data_mojom.size()); | 140 method_data_vector.reserve(method_data_mojom.size()); |
147 for (const mojom::PaymentMethodDataPtr& method_data_entry : | 141 for (const mojom::PaymentMethodDataPtr& method_data_entry : |
148 method_data_mojom) { | 142 method_data_mojom) { |
149 PaymentMethodData method_data; | 143 PaymentMethodData method_data; |
150 method_data.supported_methods = method_data_entry->supported_methods; | 144 method_data.supported_methods = method_data_entry->supported_methods; |
151 // Transfer the supported basic card networks. | 145 // Transfer the supported basic card networks. |
152 std::vector<std::string> supported_networks; | 146 std::vector<std::string> supported_networks; |
153 for (const mojom::BasicCardNetwork& network : | 147 for (const mojom::BasicCardNetwork& network : |
154 method_data_entry->supported_networks) { | 148 method_data_entry->supported_networks) { |
155 supported_networks.push_back(GetBasicCardNetworkName(network)); | 149 supported_networks.push_back(GetBasicCardNetworkName(network)); |
156 } | 150 } |
157 method_data.supported_networks = std::move(supported_networks); | 151 method_data.supported_networks = std::move(supported_networks); |
158 | 152 |
159 // TODO(crbug.com/708603): Add browser-side support for | 153 // TODO(crbug.com/708603): Add browser-side support for |
160 // |method_data.supported_types|. | 154 // |method_data.supported_types|. |
161 method_data_vector.push_back(std::move(method_data)); | 155 method_data_vector.push_back(std::move(method_data)); |
162 } | 156 } |
163 | 157 |
164 if (!data_util::ParseBasicCardSupportedNetworks( | 158 data_util::ParseBasicCardSupportedNetworks(method_data_vector, |
165 method_data_vector, &supported_card_networks_, | 159 &supported_card_networks_, |
166 &basic_card_specified_networks_)) { | 160 &basic_card_specified_networks_); |
167 LOG(ERROR) << "Invalid payment methods or data"; | |
168 NotifyOnInvalidSpecProvided(); | |
169 return; | |
170 } | |
171 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 161 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
172 supported_card_networks_.end()); | 162 supported_card_networks_.end()); |
173 } | 163 } |
174 | 164 |
175 void PaymentRequestSpec::UpdateSelectedShippingOption() { | 165 void PaymentRequestSpec::UpdateSelectedShippingOption() { |
176 if (!request_shipping()) | 166 if (!request_shipping()) |
177 return; | 167 return; |
178 | 168 |
179 // As per the spec, the selected shipping option should initially be the last | 169 // As per the spec, the selected shipping option should initially be the last |
180 // one in the array that has its selected field set to true. | 170 // one in the array that has its selected field set to true. |
181 auto selected_shipping_option_it = std::find_if( | 171 auto selected_shipping_option_it = std::find_if( |
182 details().shipping_options.rbegin(), details().shipping_options.rend(), | 172 details().shipping_options.rbegin(), details().shipping_options.rend(), |
183 [](const payments::mojom::PaymentShippingOptionPtr& element) { | 173 [](const payments::mojom::PaymentShippingOptionPtr& element) { |
184 return element->selected; | 174 return element->selected; |
185 }); | 175 }); |
186 if (selected_shipping_option_it != details().shipping_options.rend()) { | 176 if (selected_shipping_option_it != details().shipping_options.rend()) { |
187 selected_shipping_option_ = selected_shipping_option_it->get(); | 177 selected_shipping_option_ = selected_shipping_option_it->get(); |
188 } else { | 178 } else { |
189 // It's possible that there is no selected shipping option. | 179 // It's possible that there is no selected shipping option. |
190 // TODO(crbug.com/710004): Show an error in this case. | 180 // TODO(crbug.com/710004): Show an error in this case. |
191 selected_shipping_option_ = nullptr; | 181 selected_shipping_option_ = nullptr; |
192 } | 182 } |
193 } | 183 } |
194 | 184 |
195 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { | |
196 for (auto& observer : observers_) | |
197 observer.OnInvalidSpecProvided(); | |
198 if (observer_for_testing_) | |
199 observer_for_testing_->OnInvalidSpecProvided(); | |
200 } | |
201 | |
202 void PaymentRequestSpec::NotifyOnSpecUpdated() { | 185 void PaymentRequestSpec::NotifyOnSpecUpdated() { |
203 for (auto& observer : observers_) | 186 for (auto& observer : observers_) |
204 observer.OnSpecUpdated(); | 187 observer.OnSpecUpdated(); |
205 if (observer_for_testing_) | 188 if (observer_for_testing_) |
206 observer_for_testing_->OnSpecUpdated(); | 189 observer_for_testing_->OnSpecUpdated(); |
207 } | 190 } |
208 | 191 |
209 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( | 192 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
210 const std::string& currency_code, | 193 const std::string& currency_code, |
211 const std::string& currency_system, | 194 const std::string& currency_system, |
212 const std::string& locale_name) { | 195 const std::string& locale_name) { |
213 if (!currency_formatter_) { | 196 if (!currency_formatter_) { |
214 currency_formatter_.reset( | 197 currency_formatter_.reset( |
215 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 198 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
216 } | 199 } |
217 return currency_formatter_.get(); | 200 return currency_formatter_.get(); |
218 } | 201 } |
219 | 202 |
220 } // namespace payments | 203 } // namespace payments |
OLD | NEW |