| 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 | 10 |
| 11 namespace payments { | 11 namespace payments { |
| 12 | 12 |
| 13 const char kBasicCardMethodName[] = "basic-card"; | 13 const char kBasicCardMethodName[] = "basic-card"; |
| 14 | 14 |
| 15 PaymentRequestSpec::PaymentRequestSpec( | 15 PaymentRequestSpec::PaymentRequestSpec( |
| 16 mojom::PaymentOptionsPtr options, | 16 mojom::PaymentOptionsPtr options, |
| 17 mojom::PaymentDetailsPtr details, | 17 mojom::PaymentDetailsPtr details, |
| 18 std::vector<mojom::PaymentMethodDataPtr> method_data, | 18 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 19 Observer* observer, | 19 Observer* observer, |
| 20 const std::string& app_locale) | 20 const std::string& app_locale) |
| 21 : options_(std::move(options)), | 21 : options_(std::move(options)), |
| 22 details_(std::move(details)), | 22 details_(std::move(details)), |
| 23 app_locale_(app_locale) { | 23 app_locale_(app_locale), |
| 24 selected_shipping_option_(nullptr), |
| 25 observer_for_testing_(nullptr) { |
| 24 if (observer) | 26 if (observer) |
| 25 AddObserver(observer); | 27 AddObserver(observer); |
| 28 UpdateSelectedShippingOption(); |
| 26 PopulateValidatedMethodData(method_data); | 29 PopulateValidatedMethodData(method_data); |
| 27 } | 30 } |
| 28 PaymentRequestSpec::~PaymentRequestSpec() {} | 31 PaymentRequestSpec::~PaymentRequestSpec() {} |
| 29 | 32 |
| 33 void PaymentRequestSpec::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 34 details_ = std::move(details); |
| 35 // We reparse the |details_| and update the observers. |
| 36 UpdateSelectedShippingOption(); |
| 37 NotifyOnSpecUpdated(); |
| 38 } |
| 39 |
| 30 void PaymentRequestSpec::AddObserver(Observer* observer) { | 40 void PaymentRequestSpec::AddObserver(Observer* observer) { |
| 31 CHECK(observer); | 41 CHECK(observer); |
| 32 observers_.AddObserver(observer); | 42 observers_.AddObserver(observer); |
| 33 } | 43 } |
| 34 | 44 |
| 35 void PaymentRequestSpec::RemoveObserver(Observer* observer) { | 45 void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
| 36 observers_.RemoveObserver(observer); | 46 observers_.RemoveObserver(observer); |
| 37 } | 47 } |
| 38 | 48 |
| 39 bool PaymentRequestSpec::request_shipping() const { | 49 bool PaymentRequestSpec::request_shipping() const { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 167 } |
| 158 } | 168 } |
| 159 } | 169 } |
| 160 } | 170 } |
| 161 } | 171 } |
| 162 | 172 |
| 163 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 173 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 164 supported_card_networks_.end()); | 174 supported_card_networks_.end()); |
| 165 } | 175 } |
| 166 | 176 |
| 177 void PaymentRequestSpec::UpdateSelectedShippingOption() { |
| 178 if (!request_shipping()) |
| 179 return; |
| 180 |
| 181 // As per the spec, the selected shipping option should initially be the last |
| 182 // one in the array that has its selected field set to true. |
| 183 auto selected_shipping_option_it = std::find_if( |
| 184 details().shipping_options.rbegin(), details().shipping_options.rend(), |
| 185 [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 186 return element->selected; |
| 187 }); |
| 188 if (selected_shipping_option_it != details().shipping_options.rend()) { |
| 189 selected_shipping_option_ = selected_shipping_option_it->get(); |
| 190 } |
| 191 } |
| 192 |
| 167 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { | 193 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { |
| 168 for (auto& observer : observers_) | 194 for (auto& observer : observers_) |
| 169 observer.OnInvalidSpecProvided(); | 195 observer.OnInvalidSpecProvided(); |
| 196 if (observer_for_testing_) |
| 197 observer_for_testing_->OnInvalidSpecProvided(); |
| 198 } |
| 199 |
| 200 void PaymentRequestSpec::NotifyOnSpecUpdated() { |
| 201 for (auto& observer : observers_) |
| 202 observer.OnSpecUpdated(); |
| 203 if (observer_for_testing_) |
| 204 observer_for_testing_->OnSpecUpdated(); |
| 170 } | 205 } |
| 171 | 206 |
| 172 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( | 207 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
| 173 const std::string& currency_code, | 208 const std::string& currency_code, |
| 174 const std::string& currency_system, | 209 const std::string& currency_system, |
| 175 const std::string& locale_name) { | 210 const std::string& locale_name) { |
| 176 if (!currency_formatter_) { | 211 if (!currency_formatter_) { |
| 177 currency_formatter_.reset( | 212 currency_formatter_.reset( |
| 178 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 213 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 179 } | 214 } |
| 180 return currency_formatter_.get(); | 215 return currency_formatter_.get(); |
| 181 } | 216 } |
| 182 | 217 |
| 183 } // namespace payments | 218 } // namespace payments |
| OLD | NEW |