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

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

Issue 2643643006: [WebPayments] Adding something resembling Shipping Address list (Closed)
Patch Set: Cleanup 2 Created 3 years, 11 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
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 binding_.set_connection_error_handler( 28 binding_.set_connection_error_handler(
27 base::Bind(&PaymentRequest::OnError, base::Unretained(this))); 29 base::Bind(&PaymentRequest::OnError, base::Unretained(this)));
28 } 30 }
29 31
30 PaymentRequest::~PaymentRequest() {} 32 PaymentRequest::~PaymentRequest() {}
31 33
32 void PaymentRequest::Init( 34 void PaymentRequest::Init(
33 payments::mojom::PaymentRequestClientPtr client, 35 payments::mojom::PaymentRequestClientPtr client,
34 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, 36 std::vector<payments::mojom::PaymentMethodDataPtr> methodData,
35 payments::mojom::PaymentDetailsPtr details, 37 payments::mojom::PaymentDetailsPtr details,
36 payments::mojom::PaymentOptionsPtr options) { 38 payments::mojom::PaymentOptionsPtr options) {
37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
38 std::string error; 40 std::string error;
39 if (!payments::validatePaymentDetails(details, &error)) { 41 if (!payments::validatePaymentDetails(details, &error)) {
40 LOG(ERROR) << error; 42 LOG(ERROR) << error;
41 OnError(); 43 OnError();
42 client_.reset(); 44 client_.reset();
43 return; 45 return;
44 } 46 }
45 client_ = std::move(client); 47 client_ = std::move(client);
46 details_ = std::move(details); 48 details_ = std::move(details);
49 SetDefaultProfileSelections();
47 } 50 }
48 51
49 void PaymentRequest::Show() { 52 void PaymentRequest::Show() {
50 if (!client_.is_bound() || !binding_.is_bound()) { 53 if (!client_.is_bound() || !binding_.is_bound()) {
51 OnError(); 54 OnError();
52 return; 55 return;
53 } 56 }
54 delegate_->ShowPaymentRequestDialog(this); 57 delegate_->ShowPaymentRequestDialog(this);
55 } 58 }
56 59
(...skipping 11 matching lines...) Expand all
68 const base::Optional<std::string> currency_system, 71 const base::Optional<std::string> currency_system,
69 const std::string& locale_name) { 72 const std::string& locale_name) {
70 if (!currency_formatter_) { 73 if (!currency_formatter_) {
71 currency_formatter_.reset( 74 currency_formatter_.reset(
72 new CurrencyFormatter(currency_code, currency_system, locale_name)); 75 new CurrencyFormatter(currency_code, currency_system, locale_name));
73 } 76 }
74 77
75 return currency_formatter_.get(); 78 return currency_formatter_.get();
76 } 79 }
77 80
78 autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { 81 std::vector<autofill::AutofillProfile*> PaymentRequest::GetProfiles() {
79 // TODO(tmartino): Implement more sophisticated algorithm for populating 82 if (profile_cache_.empty()) {
80 // this when it starts empty.
81 if (!profile_) {
82 autofill::PersonalDataManager* data_manager = 83 autofill::PersonalDataManager* data_manager =
83 delegate_->GetPersonalDataManager(); 84 delegate_->GetPersonalDataManager();
84 auto profiles = data_manager->GetProfiles(); 85 std::vector<autofill::AutofillProfile*> profiles =
85 if (!profiles.empty()) 86 data_manager->GetProfilesToSuggest();
86 profile_ = base::MakeUnique<autofill::AutofillProfile>(*profiles[0]); 87
88 // PaymentRequest may outlive the Profiles returned by the Data Manager.
89 // Thus, we store copies, and return a vector of pointers to these copies.
90 for (autofill::AutofillProfile* profile : profiles) {
91 profile_cache_.push_back(
92 base::MakeUnique<autofill::AutofillProfile>(*profile));
93 }
87 } 94 }
88 return profile_ ? profile_.get() : nullptr; 95
96 std::vector<autofill::AutofillProfile*> return_profiles(
97 profile_cache_.size());
98 std::transform(profile_cache_.begin(), profile_cache_.end(),
99 return_profiles.begin(),
100 [](const std::unique_ptr<autofill::AutofillProfile>& profile) {
101 return profile.get();
102 });
103
104 return return_profiles;
105 }
106
107 autofill::AutofillProfile* PaymentRequest::GetSelectedShippingProfile() {
please use gerrit instead 2017/01/20 19:32:53 Here and below: Simple getters and settings shoul
tmartino 2017/01/25 00:08:41 Done
108 return selected_shipping_profile_;
109 }
110
111 void PaymentRequest::SetShippingProfile(autofill::AutofillProfile* profile) {
112 selected_shipping_profile_ = profile;
113 }
114
115 autofill::AutofillProfile* PaymentRequest::GetSelectedContactProfile() {
116 return selected_contact_profile_;
117 }
118
119 void PaymentRequest::SetContactProfile(autofill::AutofillProfile* profile) {
120 selected_contact_profile_ = profile;
89 } 121 }
90 122
91 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { 123 autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() {
92 // TODO(anthonyvd): Change this code to prioritize server cards and implement 124 // TODO(anthonyvd): Change this code to prioritize server cards and implement
93 // a way to modify this function's return value. 125 // a way to modify this function's return value.
94 autofill::PersonalDataManager* data_manager = 126 autofill::PersonalDataManager* data_manager =
95 delegate_->GetPersonalDataManager(); 127 delegate_->GetPersonalDataManager();
96 128
97 const std::vector<autofill::CreditCard*> cards = 129 const std::vector<autofill::CreditCard*> cards =
98 data_manager->GetCreditCardsToSuggest(); 130 data_manager->GetCreditCardsToSuggest();
99 131
100 auto first_complete_card = std::find_if( 132 auto first_complete_card = std::find_if(
101 cards.begin(), 133 cards.begin(),
102 cards.end(), 134 cards.end(),
103 [] (autofill::CreditCard* card) { 135 [] (autofill::CreditCard* card) {
104 return card->IsValid(); 136 return card->IsValid();
105 }); 137 });
106 138
107 return first_complete_card == cards.end() ? nullptr : *first_complete_card; 139 return first_complete_card == cards.end() ? nullptr : *first_complete_card;
108 } 140 }
109 141
142 void PaymentRequest::SetDefaultProfileSelections() {
143 auto profiles = GetProfiles();
144 if (!profiles.empty()) {
145 // TODO(tmartino): Examine the details to determine whether the shipping
146 // address should be prefilled, and what constraints apply to profiles
147 // for prefilling the contact info.
148 SetShippingProfile(profiles[0]);
149 SetContactProfile(profiles[0]);
150 }
151 }
152
110 } // namespace payments 153 } // namespace payments
OLDNEW
« components/payments/payment_request.h ('K') | « components/payments/payment_request.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698