| 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 "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 std::vector<mojom::PaymentMethodDataPtr> method_data, | 52 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 53 Observer* observer, | 53 Observer* observer, |
| 54 const std::string& app_locale) | 54 const std::string& app_locale) |
| 55 : options_(std::move(options)), | 55 : options_(std::move(options)), |
| 56 details_(std::move(details)), | 56 details_(std::move(details)), |
| 57 app_locale_(app_locale), | 57 app_locale_(app_locale), |
| 58 selected_shipping_option_(nullptr), | 58 selected_shipping_option_(nullptr), |
| 59 observer_for_testing_(nullptr) { | 59 observer_for_testing_(nullptr) { |
| 60 if (observer) | 60 if (observer) |
| 61 AddObserver(observer); | 61 AddObserver(observer); |
| 62 UpdateSelectedShippingOption(); | 62 UpdateSelectedShippingOption(/*after_update=*/false); |
| 63 PopulateValidatedMethodData(method_data); | 63 PopulateValidatedMethodData(method_data); |
| 64 } | 64 } |
| 65 PaymentRequestSpec::~PaymentRequestSpec() {} | 65 PaymentRequestSpec::~PaymentRequestSpec() {} |
| 66 | 66 |
| 67 void PaymentRequestSpec::UpdateWith(mojom::PaymentDetailsPtr details) { | 67 void PaymentRequestSpec::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 68 details_ = std::move(details); | 68 details_ = std::move(details); |
| 69 // We reparse the |details_| and update the observers. | 69 // We reparse the |details_| and update the observers. |
| 70 UpdateSelectedShippingOption(); | 70 UpdateSelectedShippingOption(/*after_update=*/true); |
| 71 NotifyOnSpecUpdated(); | 71 NotifyOnSpecUpdated(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void PaymentRequestSpec::AddObserver(Observer* observer) { | 74 void PaymentRequestSpec::AddObserver(Observer* observer) { |
| 75 CHECK(observer); | 75 CHECK(observer); |
| 76 observers_.AddObserver(observer); | 76 observers_.AddObserver(observer); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void PaymentRequestSpec::RemoveObserver(Observer* observer) { | 79 void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
| 80 observers_.RemoveObserver(observer); | 80 observers_.RemoveObserver(observer); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 method_data_vector.push_back(std::move(method_data)); | 163 method_data_vector.push_back(std::move(method_data)); |
| 164 } | 164 } |
| 165 | 165 |
| 166 data_util::ParseBasicCardSupportedNetworks(method_data_vector, | 166 data_util::ParseBasicCardSupportedNetworks(method_data_vector, |
| 167 &supported_card_networks_, | 167 &supported_card_networks_, |
| 168 &basic_card_specified_networks_); | 168 &basic_card_specified_networks_); |
| 169 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 169 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 170 supported_card_networks_.end()); | 170 supported_card_networks_.end()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void PaymentRequestSpec::UpdateSelectedShippingOption() { | 173 void PaymentRequestSpec::UpdateSelectedShippingOption(bool after_update) { |
| 174 if (!request_shipping()) | 174 if (!request_shipping()) |
| 175 return; | 175 return; |
| 176 | 176 |
| 177 selected_shipping_option_ = nullptr; |
| 177 selected_shipping_option_error_.clear(); | 178 selected_shipping_option_error_.clear(); |
| 179 if (details().shipping_options.empty()) { |
| 180 // No options are provided by the merchant. |
| 181 if (after_update) { |
| 182 // This is after an update, which means that the selected address is no |
| 183 // supported. The merchant may have customized the error string, or a |
| 184 // generic one is used. |
| 185 if (!details().error.empty()) { |
| 186 selected_shipping_option_error_ = base::UTF8ToUTF16(details().error); |
| 187 } else { |
| 188 selected_shipping_option_error_ = l10n_util::GetStringUTF16( |
| 189 IDS_PAYMENTS_UNSUPPORTED_SHIPPING_ADDRESS); |
| 190 } |
| 191 } |
| 192 return; |
| 193 } |
| 194 |
| 178 // 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 |
| 179 // 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. If none are |
| 197 // selected by the merchant, |selected_shipping_option_| stays nullptr. |
| 180 auto selected_shipping_option_it = std::find_if( | 198 auto selected_shipping_option_it = std::find_if( |
| 181 details().shipping_options.rbegin(), details().shipping_options.rend(), | 199 details().shipping_options.rbegin(), details().shipping_options.rend(), |
| 182 [](const payments::mojom::PaymentShippingOptionPtr& element) { | 200 [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 183 return element->selected; | 201 return element->selected; |
| 184 }); | 202 }); |
| 185 if (selected_shipping_option_it != details().shipping_options.rend()) { | 203 if (selected_shipping_option_it != details().shipping_options.rend()) { |
| 186 selected_shipping_option_ = selected_shipping_option_it->get(); | 204 selected_shipping_option_ = selected_shipping_option_it->get(); |
| 187 } else { | |
| 188 // It's possible that there is no selected shipping option. | |
| 189 if (!details().error.empty()) { | |
| 190 selected_shipping_option_error_ = base::UTF8ToUTF16(details().error); | |
| 191 } else { | |
| 192 selected_shipping_option_error_ = | |
| 193 l10n_util::GetStringUTF16(IDS_PAYMENTS_UNSUPPORTED_SHIPPING_ADDRESS); | |
| 194 } | |
| 195 selected_shipping_option_ = nullptr; | |
| 196 } | 205 } |
| 197 } | 206 } |
| 198 | 207 |
| 199 void PaymentRequestSpec::NotifyOnSpecUpdated() { | 208 void PaymentRequestSpec::NotifyOnSpecUpdated() { |
| 200 for (auto& observer : observers_) | 209 for (auto& observer : observers_) |
| 201 observer.OnSpecUpdated(); | 210 observer.OnSpecUpdated(); |
| 202 if (observer_for_testing_) | 211 if (observer_for_testing_) |
| 203 observer_for_testing_->OnSpecUpdated(); | 212 observer_for_testing_->OnSpecUpdated(); |
| 204 } | 213 } |
| 205 | 214 |
| 206 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( | 215 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
| 207 const std::string& currency_code, | 216 const std::string& currency_code, |
| 208 const std::string& currency_system, | 217 const std::string& currency_system, |
| 209 const std::string& locale_name) { | 218 const std::string& locale_name) { |
| 210 if (!currency_formatter_) { | 219 if (!currency_formatter_) { |
| 211 currency_formatter_.reset( | 220 currency_formatter_.reset( |
| 212 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 221 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 213 } | 222 } |
| 214 return currency_formatter_.get(); | 223 return currency_formatter_.get(); |
| 215 } | 224 } |
| 216 | 225 |
| 217 } // namespace payments | 226 } // namespace payments |
| OLD | NEW |