Chromium Code Reviews| 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> | |
| 8 | |
| 9 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" | 8 #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 10 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" | 11 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 #include "components/payments/core/currency_formatter.h" | 12 #include "components/payments/core/currency_formatter.h" |
| 15 #include "ios/chrome/browser/application_context.h" | 13 #include "ios/chrome/browser/application_context.h" |
| 16 #include "ios/web/public/payments/payment_request.h" | 14 #include "ios/web/public/payments/payment_request.h" |
| 17 | 15 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 46 if (!currency_formatter_) { | 44 if (!currency_formatter_) { |
| 47 currency_formatter_.reset(new payments::CurrencyFormatter( | 45 currency_formatter_.reset(new payments::CurrencyFormatter( |
| 48 base::UTF16ToASCII(web_payment_request_->details.total.amount.currency), | 46 base::UTF16ToASCII(web_payment_request_->details.total.amount.currency), |
| 49 base::UTF16ToASCII( | 47 base::UTF16ToASCII( |
| 50 web_payment_request_->details.total.amount.currency_system), | 48 web_payment_request_->details.total.amount.currency_system), |
| 51 GetApplicationContext()->GetApplicationLocale())); | 49 GetApplicationContext()->GetApplicationLocale())); |
| 52 } | 50 } |
| 53 return currency_formatter_.get(); | 51 return currency_formatter_.get(); |
| 54 } | 52 } |
| 55 | 53 |
| 54 void PaymentRequest::AddCreditCard( | |
| 55 std::unique_ptr<autofill::CreditCard> credit_card) { | |
| 56 credit_card_cache_.insert(credit_card_cache_.begin(), std::move(credit_card)); | |
| 57 credit_cards_.insert(credit_cards_.begin(), credit_card_cache_.front().get()); | |
| 58 } | |
| 59 | |
| 56 void PaymentRequest::PopulateProfileCache() { | 60 void PaymentRequest::PopulateProfileCache() { |
| 57 for (const auto* profile : personal_data_manager_->GetProfilesToSuggest()) { | 61 for (const auto* profile : personal_data_manager_->GetProfilesToSuggest()) { |
| 58 profile_cache_.push_back( | 62 profile_cache_.push_back( |
| 59 base::MakeUnique<autofill::AutofillProfile>(*profile)); | 63 base::MakeUnique<autofill::AutofillProfile>(*profile)); |
| 60 shipping_profiles_.push_back(profile_cache_.back().get()); | 64 shipping_profiles_.push_back(profile_cache_.back().get()); |
| 61 // TODO(crbug.com/602666): Implement deduplication rules for profiles. | 65 // TODO(crbug.com/602666): Implement deduplication rules for profiles. |
| 62 contact_profiles_.push_back(profile_cache_.back().get()); | 66 contact_profiles_.push_back(profile_cache_.back().get()); |
| 63 } | 67 } |
| 64 | 68 |
| 65 // TODO(crbug.com/602666): Implement prioritization rules for shipping and | 69 // TODO(crbug.com/602666): Implement prioritization rules for shipping and |
| 66 // contact profiles. | 70 // contact profiles. |
| 67 | 71 |
| 68 if (!shipping_profiles_.empty()) | 72 if (!shipping_profiles_.empty()) |
| 69 selected_shipping_profile_ = shipping_profiles_[0]; | 73 selected_shipping_profile_ = shipping_profiles_[0]; |
| 70 if (!contact_profiles_.empty()) | 74 if (!contact_profiles_.empty()) |
| 71 selected_contact_profile_ = contact_profiles_[0]; | 75 selected_contact_profile_ = contact_profiles_[0]; |
| 72 } | 76 } |
| 73 | 77 |
| 74 void PaymentRequest::PopulateCreditCardCache() { | 78 void PaymentRequest::PopulateCreditCardCache() { |
| 75 DCHECK(web_payment_request_); | 79 DCHECK(web_payment_request_); |
| 76 std::unordered_set<base::string16> supported_method_types; | |
| 77 for (const auto& method_data : web_payment_request_->method_data) { | 80 for (const auto& method_data : web_payment_request_->method_data) { |
| 78 for (const auto& supported_method : method_data.supported_methods) | 81 for (const auto& supported_method : method_data.supported_methods) |
| 79 supported_method_types.insert(supported_method); | 82 supported_card_networks_.push_back(base::UTF16ToASCII(supported_method)); |
|
please use gerrit instead
2017/03/16 14:36:40
Reject non-ASCII supported_methods.
Moe
2017/03/16 15:59:46
Done.
| |
| 80 } | 83 } |
| 81 | 84 |
| 82 std::vector<autofill::CreditCard*> credit_cards = | 85 std::vector<autofill::CreditCard*> credit_cards = |
| 83 personal_data_manager_->GetCreditCardsToSuggest(); | 86 personal_data_manager_->GetCreditCardsToSuggest(); |
| 84 | 87 |
| 85 // TODO(crbug.com/602666): Update the following logic to allow basic card | 88 // TODO(crbug.com/602666): Update the following logic to allow basic card |
| 86 // payment. https://w3c.github.io/webpayments-methods-card/ | 89 // payment. https://w3c.github.io/webpayments-methods-card/ |
| 87 // new PaymentRequest([{supportedMethods: ['basic-card'], | 90 // new PaymentRequest([{supportedMethods: ['basic-card'], |
| 88 // data: {supportedNetworks:['visa']}]}], ...); | 91 // data: {supportedNetworks:['visa']}]}], ...); |
| 89 | 92 |
| 90 for (const auto* credit_card : credit_cards) { | 93 for (const auto* credit_card : credit_cards) { |
| 91 std::string spec_card_type = | 94 std::string spec_card_type = |
| 92 autofill::data_util::GetPaymentRequestData(credit_card->type()) | 95 autofill::data_util::GetPaymentRequestData(credit_card->type()) |
| 93 .basic_card_payment_type; | 96 .basic_card_payment_type; |
| 94 if (supported_method_types.find(base::ASCIIToUTF16(spec_card_type)) != | 97 if (std::find(supported_card_networks_.begin(), |
| 95 supported_method_types.end()) { | 98 supported_card_networks_.end(), |
| 99 spec_card_type) != supported_card_networks_.end()) { | |
| 96 credit_card_cache_.push_back( | 100 credit_card_cache_.push_back( |
| 97 base::MakeUnique<autofill::CreditCard>(*credit_card)); | 101 base::MakeUnique<autofill::CreditCard>(*credit_card)); |
| 98 credit_cards_.push_back(credit_card_cache_.back().get()); | 102 credit_cards_.push_back(credit_card_cache_.back().get()); |
| 99 } | 103 } |
| 100 } | 104 } |
| 101 | 105 |
| 102 // TODO(crbug.com/602666): Implement prioritization rules for credit cards. | 106 // TODO(crbug.com/602666): Implement prioritization rules for credit cards. |
| 103 | 107 |
| 104 if (!credit_cards_.empty()) | 108 if (!credit_cards_.empty()) |
| 105 selected_credit_card_ = credit_cards_[0]; | 109 selected_credit_card_ = credit_cards_[0]; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 117 | 121 |
| 118 selected_shipping_option_ = nullptr; | 122 selected_shipping_option_ = nullptr; |
| 119 for (auto* shipping_option : shipping_options_) { | 123 for (auto* shipping_option : shipping_options_) { |
| 120 if (shipping_option->selected) { | 124 if (shipping_option->selected) { |
| 121 // If more than one option has |selected| set, the last one in the | 125 // If more than one option has |selected| set, the last one in the |
| 122 // sequence should be treated as the selected item. | 126 // sequence should be treated as the selected item. |
| 123 selected_shipping_option_ = shipping_option; | 127 selected_shipping_option_ = shipping_option; |
| 124 } | 128 } |
| 125 } | 129 } |
| 126 } | 130 } |
| OLD | NEW |