Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: components/payments/payment_request.cc

Issue 2643643006: [WebPayments] Adding something resembling Shipping Address list (Closed)
Patch Set: Further rebasing Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/payments/payment_request.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/payment_request.h" 5 #include "components/payments/payment_request.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "components/autofill/core/browser/personal_data_manager.h" 8 #include "components/autofill/core/browser/personal_data_manager.h"
9 #include "components/payments/payment_details_validation.h" 9 #include "components/payments/payment_details_validation.h"
10 #include "components/payments/payment_request_delegate.h" 10 #include "components/payments/payment_request_delegate.h"
11 #include "components/payments/payment_request_web_contents_manager.h" 11 #include "components/payments/payment_request_web_contents_manager.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
14 14
15 namespace payments { 15 namespace payments {
16 16
17 PaymentRequest::PaymentRequest( 17 PaymentRequest::PaymentRequest(
18 content::WebContents* web_contents, 18 content::WebContents* web_contents,
19 std::unique_ptr<PaymentRequestDelegate> delegate, 19 std::unique_ptr<PaymentRequestDelegate> delegate,
20 PaymentRequestWebContentsManager* manager, 20 PaymentRequestWebContentsManager* manager,
21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) 21 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request)
22 : web_contents_(web_contents), 22 : web_contents_(web_contents),
23 delegate_(std::move(delegate)), 23 delegate_(std::move(delegate)),
24 manager_(manager), 24 manager_(manager),
25 binding_(this, std::move(request)) { 25 binding_(this, std::move(request)),
26 selected_shipping_profile_(nullptr),
27 selected_contact_profile_(nullptr) {
26 // OnConnectionTerminated will be called when the Mojo pipe is closed. This 28 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
27 // will happen as a result of many renderer-side events (both successful and 29 // will happen as a result of many renderer-side events (both successful and
28 // erroneous in nature). 30 // erroneous in nature).
29 // TODO(crbug.com/683636): Investigate using 31 // TODO(crbug.com/683636): Investigate using
30 // set_connection_error_with_reason_handler with Binding::CloseWithReason. 32 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
31 binding_.set_connection_error_handler(base::Bind( 33 binding_.set_connection_error_handler(base::Bind(
32 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); 34 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
35
33 } 36 }
34 37
35 PaymentRequest::~PaymentRequest() {} 38 PaymentRequest::~PaymentRequest() {}
36 39
37 void PaymentRequest::Init( 40 void PaymentRequest::Init(
38 payments::mojom::PaymentRequestClientPtr client, 41 payments::mojom::PaymentRequestClientPtr client,
39 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, 42 std::vector<payments::mojom::PaymentMethodDataPtr> methodData,
40 payments::mojom::PaymentDetailsPtr details, 43 payments::mojom::PaymentDetailsPtr details,
41 payments::mojom::PaymentOptionsPtr options) { 44 payments::mojom::PaymentOptionsPtr options) {
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
43 std::string error; 46 std::string error;
44 if (!payments::validatePaymentDetails(details, &error)) { 47 if (!payments::validatePaymentDetails(details, &error)) {
45 LOG(ERROR) << error; 48 LOG(ERROR) << error;
46 OnConnectionTerminated(); 49 OnConnectionTerminated();
47 return; 50 return;
48 } 51 }
49 client_ = std::move(client); 52 client_ = std::move(client);
50 details_ = std::move(details); 53 details_ = std::move(details);
54 PopulateProfileCache();
55 SetDefaultProfileSelections();
51 } 56 }
52 57
53 void PaymentRequest::Show() { 58 void PaymentRequest::Show() {
54 if (!client_.is_bound() || !binding_.is_bound()) { 59 if (!client_.is_bound() || !binding_.is_bound()) {
55 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; 60 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
56 OnConnectionTerminated(); 61 OnConnectionTerminated();
57 return; 62 return;
58 } 63 }
59 delegate_->ShowDialog(this); 64 delegate_->ShowDialog(this);
60 } 65 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 const std::string& currency_code, 103 const std::string& currency_code,
99 const std::string& currency_system, 104 const std::string& currency_system,
100 const std::string& locale_name) { 105 const std::string& locale_name) {
101 if (!currency_formatter_) { 106 if (!currency_formatter_) {
102 currency_formatter_.reset( 107 currency_formatter_.reset(
103 new CurrencyFormatter(currency_code, currency_system, locale_name)); 108 new CurrencyFormatter(currency_code, currency_system, locale_name));
104 } 109 }
105 return currency_formatter_.get(); 110 return currency_formatter_.get();
106 } 111 }
107 112
108 autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { 113 const std::vector<autofill::AutofillProfile*>&
109 // TODO(tmartino): Implement more sophisticated algorithm for populating 114 PaymentRequest::shipping_profiles() {
110 // this when it starts empty. 115 return shipping_profiles_;
111 if (!profile_) { 116 }
112 autofill::PersonalDataManager* data_manager = 117
113 delegate_->GetPersonalDataManager(); 118 const std::vector<autofill::AutofillProfile*>&
114 auto profiles = data_manager->GetProfiles(); 119 PaymentRequest::contact_profiles() {
115 if (!profiles.empty()) 120 return contact_profiles_;
116 profile_ = base::MakeUnique<autofill::AutofillProfile>(*profiles[0]);
117 }
118 return profile_ ? profile_.get() : nullptr;
119 } 121 }
120 122
121 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { 123 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() {
122 // TODO(anthonyvd): Change this code to prioritize server cards and implement 124 // TODO(anthonyvd): Change this code to prioritize server cards and implement
123 // a way to modify this function's return value. 125 // a way to modify this function's return value.
124 autofill::PersonalDataManager* data_manager = 126 autofill::PersonalDataManager* data_manager =
125 delegate_->GetPersonalDataManager(); 127 delegate_->GetPersonalDataManager();
126 128
127 const std::vector<autofill::CreditCard*> cards = 129 const std::vector<autofill::CreditCard*> cards =
128 data_manager->GetCreditCardsToSuggest(); 130 data_manager->GetCreditCardsToSuggest();
129 131
130 auto first_complete_card = std::find_if( 132 auto first_complete_card = std::find_if(
131 cards.begin(), 133 cards.begin(),
132 cards.end(), 134 cards.end(),
133 [] (autofill::CreditCard* card) { 135 [] (autofill::CreditCard* card) {
134 return card->IsValid(); 136 return card->IsValid();
135 }); 137 });
136 138
137 return first_complete_card == cards.end() ? nullptr : *first_complete_card; 139 return first_complete_card == cards.end() ? nullptr : *first_complete_card;
138 } 140 }
139 141
142 void PaymentRequest::PopulateProfileCache() {
143 autofill::PersonalDataManager* data_manager =
144 delegate_->GetPersonalDataManager();
145 std::vector<autofill::AutofillProfile*> profiles =
146 data_manager->GetProfilesToSuggest();
147
148 // PaymentRequest may outlive the Profiles returned by the Data Manager.
149 // Thus, we store copies, and return a vector of pointers to these copies
150 // whenever Profiles are requested.
151 for (size_t i = 0; i < profiles.size(); i++) {
152 profile_cache_.push_back(
153 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
154
155 // TODO(tmartino): Implement deduplication rules specific to shipping and
156 // contact profiles.
157 shipping_profiles_.push_back(profile_cache_[i].get());
158 contact_profiles_.push_back(profile_cache_[i].get());
159 }
160 }
161
162 void PaymentRequest::SetDefaultProfileSelections() {
163 if (!shipping_profiles().empty())
164 set_selected_shipping_profile(shipping_profiles()[0]);
165
166 if (!contact_profiles().empty())
167 set_selected_contact_profile(contact_profiles()[0]);
168 }
169
140 } // namespace payments 170 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/payment_request.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698