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

Side by Side Diff: components/payments/content/payment_request.h

Issue 2742813004: [Payments] Refactor into PaymentRequestState and Spec (Closed)
Patch Set: another set of tests (state) Created 3 years, 9 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 #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ 5 #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
6 #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ 6 #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/observer_list.h"
14 #include "components/payments/content/payment_request.mojom.h" 13 #include "components/payments/content/payment_request.mojom.h"
15 #include "components/payments/content/payment_request_delegate.h" 14 #include "components/payments/content/payment_request_delegate.h"
16 #include "components/payments/core/payment_instrument.h" 15 #include "components/payments/content/payment_request_spec.h"
16 #include "components/payments/content/payment_request_state.h"
17 #include "mojo/public/cpp/bindings/binding.h" 17 #include "mojo/public/cpp/bindings/binding.h"
18 18
19 namespace autofill { 19 namespace autofill {
20 class AutofillProfile; 20 class AutofillProfile;
21 class CreditCard; 21 class CreditCard;
22 class PersonalDataManager; 22 class PersonalDataManager;
23 } 23 }
24 24
25 namespace content { 25 namespace content {
26 class WebContents; 26 class WebContents;
27 } 27 }
28 28
29 namespace payments { 29 namespace payments {
30 30
31 class CurrencyFormatter; 31 class CurrencyFormatter;
32 class PaymentRequestWebContentsManager; 32 class PaymentRequestWebContentsManager;
33 33
34 class PaymentRequest : public mojom::PaymentRequest, 34 class PaymentRequest : public mojom::PaymentRequest,
35 public PaymentInstrument::Delegate { 35 public PaymentRequestSpec::Observer,
36 public PaymentRequestState::Delegate {
36 public: 37 public:
37 class Observer {
38 public:
39 // Called when the information (payment method, address/contact info,
40 // shipping option) changes.
41 virtual void OnSelectedInformationChanged() = 0;
42
43 protected:
44 virtual ~Observer() {}
45 };
46
47 PaymentRequest( 38 PaymentRequest(
48 content::WebContents* web_contents, 39 content::WebContents* web_contents,
49 std::unique_ptr<PaymentRequestDelegate> delegate, 40 std::unique_ptr<PaymentRequestDelegate> delegate,
50 PaymentRequestWebContentsManager* manager, 41 PaymentRequestWebContentsManager* manager,
51 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request); 42 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request);
52 ~PaymentRequest() override; 43 ~PaymentRequest() override;
53 44
54 // payments::mojom::PaymentRequest "stub" 45 // payments::mojom::PaymentRequest "stub"
55 void Init(payments::mojom::PaymentRequestClientPtr client, 46 void Init(payments::mojom::PaymentRequestClientPtr client,
56 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, 47 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
57 payments::mojom::PaymentDetailsPtr details, 48 payments::mojom::PaymentDetailsPtr details,
58 payments::mojom::PaymentOptionsPtr options) override; 49 payments::mojom::PaymentOptionsPtr options) override;
59 void Show() override; 50 void Show() override;
60 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {} 51 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {}
61 void Abort() override; 52 void Abort() override;
62 void Complete(payments::mojom::PaymentComplete result) override; 53 void Complete(payments::mojom::PaymentComplete result) override;
63 void CanMakePayment() override; 54 void CanMakePayment() override;
64 55
65 // PaymentInstrument::Delegate: 56 // PaymentRequestSpec::Observer:
66 void OnInstrumentDetailsReady( 57 void OnInvalidSpecProvided() override;
67 const std::string& method_name, 58
68 const std::string& stringified_details) override; 59 // PaymentRequestState::Delegate:
69 void OnInstrumentDetailsError() override {} 60 const std::string& GetApplicationLocale() override;
61 const std::vector<autofill::AutofillProfile*>& GetBillingProfiles() override;
62
63 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
70 64
71 // Called when the user explicitely cancelled the flow. Will send a message 65 // Called when the user explicitely cancelled the flow. Will send a message
72 // to the renderer which will indirectly destroy this object (through 66 // to the renderer which will indirectly destroy this object (through
73 // OnConnectionTerminated). 67 // OnConnectionTerminated).
74 void UserCancelled(); 68 void UserCancelled();
75 69
76 // As a result of a browser-side error or renderer-initiated mojo channel 70 // As a result of a browser-side error or renderer-initiated mojo channel
77 // closure (e.g. there was an error on the renderer side, or payment was 71 // closure (e.g. there was an error on the renderer side, or payment was
78 // successful), this method is called. It is responsible for cleaning up, 72 // successful), this method is called. It is responsible for cleaning up,
79 // such as possibly closing the dialog. 73 // such as possibly closing the dialog.
80 void OnConnectionTerminated(); 74 void OnConnectionTerminated();
81 75
82 // Called when the user clicks on the "Pay" button. 76 // Called when the user clicks on the "Pay" button.
83 void Pay(); 77 void Pay();
84 78
85 void AddObserver(Observer* observer);
86 void RemoveObserver(Observer* observer);
87
88 // Returns the CurrencyFormatter instance for this PaymentRequest. 79 // Returns the CurrencyFormatter instance for this PaymentRequest.
89 // |locale_name| should be the result of the browser's GetApplicationLocale(). 80 // |locale_name| should be the result of the browser's GetApplicationLocale().
90 // Note: Having multiple currencies per PaymentRequest is not supported; hence 81 // Note: Having multiple currencies per PaymentRequest is not supported; hence
91 // the CurrencyFormatter is cached here. 82 // the CurrencyFormatter is cached here.
92 CurrencyFormatter* GetOrCreateCurrencyFormatter( 83 CurrencyFormatter* GetOrCreateCurrencyFormatter(
93 const std::string& currency_code, 84 const std::string& currency_code,
94 const std::string& currency_system, 85 const std::string& currency_system,
95 const std::string& locale_name); 86 const std::string& locale_name);
96 87
97 // Uses CurrencyFormatter to format |amount| with the currency symbol for this 88 // Uses CurrencyFormatter to format |amount| with the currency symbol for this
98 // request's currency. 89 // request's currency.
99 base::string16 GetFormattedCurrencyAmount(const std::string& amount); 90 base::string16 GetFormattedCurrencyAmount(const std::string& amount);
100 91
101 // Uses CurrencyFormatter to get the formatted currency code for this 92 // Uses CurrencyFormatter to get the formatted currency code for this
anthonyvd 2017/03/14 16:46:41 This and the following 5 functions will also have
Mathieu 2017/03/14 18:05:39 Moved some to state, the currency formatter I don'
102 // request's currency. 93 // request's currency.
103 std::string GetFormattedCurrencyCode(); 94 std::string GetFormattedCurrencyCode();
104 95
105 // Returns the appropriate Autofill Profiles for this user. On the first 96 // Returns the appropriate Autofill Profiles for this user. On the first
106 // invocation of either getter, the profiles are fetched from the 97 // invocation of either getter, the profiles are fetched from the
107 // PersonalDataManager; on subsequent invocations, a cached version is 98 // PersonalDataManager; on subsequent invocations, a cached version is
108 // returned. The profiles returned are owned by the request object. 99 // returned. The profiles returned are owned by the request object.
109 const std::vector<autofill::AutofillProfile*>& shipping_profiles() { 100 const std::vector<autofill::AutofillProfile*>& shipping_profiles() {
110 return shipping_profiles_; 101 return shipping_profiles_;
111 } 102 }
112 const std::vector<autofill::AutofillProfile*>& contact_profiles() { 103 const std::vector<autofill::AutofillProfile*>& contact_profiles() {
113 return contact_profiles_; 104 return contact_profiles_;
114 } 105 }
115 const std::vector<autofill::CreditCard*>& credit_cards() { 106 const std::vector<autofill::CreditCard*>& credit_cards() {
116 return credit_cards_; 107 return credit_cards_;
117 } 108 }
118 109
119 // Gets the Autofill Profile representing the shipping address or contact
120 // information currently selected for this PaymentRequest flow. Can return
121 // null.
122 autofill::AutofillProfile* selected_shipping_profile() const {
123 return selected_shipping_profile_;
124 }
125 autofill::AutofillProfile* selected_contact_profile() const {
126 return selected_contact_profile_;
127 }
128 // Returns the currently selected credit card for this PaymentRequest flow.
129 // It's not guaranteed to be complete. Returns nullptr if there is no selected
130 // card.
131 autofill::CreditCard* selected_credit_card() { return selected_credit_card_; }
132
133 payments::mojom::PaymentShippingOption* selected_shipping_option() {
134 return selected_shipping_option_;
135 }
136
137 // Sets the |profile| to be the selected one and will update state and notify
138 // observers.
139 void SetSelectedShippingProfile(autofill::AutofillProfile* profile);
140 void SetSelectedContactProfile(autofill::AutofillProfile* profile);
141 void SetSelectedCreditCard(autofill::CreditCard* card);
142
143 autofill::PersonalDataManager* personal_data_manager() { 110 autofill::PersonalDataManager* personal_data_manager() {
144 return delegate_->GetPersonalDataManager(); 111 return delegate_->GetPersonalDataManager();
145 } 112 }
146 const std::string& locale() { return delegate_->GetApplicationLocale(); }
147 113
148 payments::mojom::PaymentDetails* details() { return details_.get(); }
149 payments::mojom::PaymentOptions* options() { return options_.get(); }
150 const std::vector<std::string>& supported_card_networks() {
151 return supported_card_networks_;
152 }
153 content::WebContents* web_contents() { return web_contents_; } 114 content::WebContents* web_contents() { return web_contents_; }
154 115
155 bool request_shipping() const { return options_->request_shipping; } 116 PaymentRequestSpec* spec() { return spec_.get(); }
156 bool request_payer_name() const { return options_->request_payer_name; } 117 PaymentRequestState* state() { return state_.get(); }
157 bool request_payer_phone() const { return options_->request_payer_phone; }
158 bool request_payer_email() const { return options_->request_payer_email; }
159
160 bool is_ready_to_pay() { return is_ready_to_pay_; }
161 118
162 private: 119 private:
163 // Fetches the Autofill Profiles for this user from the PersonalDataManager, 120 // Fetches the Autofill Profiles for this user from the PersonalDataManager,
164 // and stores copies of them, owned by this Request, in profile_cache_. 121 // and stores copies of them, owned by this Request, in profile_cache_.
165 void PopulateProfileCache(); 122 void PopulateProfileCache();
166 123
167 // Sets the default values for the selected Shipping and Contact profiles, as 124 // Sets the default values for the selected Shipping and Contact profiles, as
168 // well as the selected Credit Card. 125 // well as the selected Credit Card.
169 void SetDefaultProfileSelections(); 126 void SetDefaultProfileSelections();
170 127
171 // Validates the |method_data| and fills |supported_card_networks_|.
172 void PopulateValidatedMethodData(
173 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data);
174
175 // Updates |is_ready_to_pay_| with the current state, by validating that all
176 // the required information is available and notify observers.
177 void UpdateIsReadyToPayAndNotifyObservers();
178
179 // Notifies all observers that selected information has changed.
180 void NotifyOnSelectedInformationChanged();
181
182 // Returns whether the selected data satisfies the PaymentDetails requirements
183 // (payment methods).
184 bool ArePaymentDetailsSatisfied();
185 // Returns whether the selected data satisfies the PaymentOptions requirements
186 // (contact info, shipping address).
187 bool ArePaymentOptionsSatisfied();
188
189 // Updates the selected_shipping_option based on the data passed to this
190 // payment request by the website. This will set selected_shipping_option_ to
191 // the last option marked selected in the options array.
192 void UpdateSelectedShippingOptionFromDetails();
193
194 content::WebContents* web_contents_; 128 content::WebContents* web_contents_;
195 std::unique_ptr<PaymentRequestDelegate> delegate_; 129 std::unique_ptr<PaymentRequestDelegate> delegate_;
196 // |manager_| owns this PaymentRequest. 130 // |manager_| owns this PaymentRequest.
197 PaymentRequestWebContentsManager* manager_; 131 PaymentRequestWebContentsManager* manager_;
198 mojo::Binding<payments::mojom::PaymentRequest> binding_; 132 mojo::Binding<payments::mojom::PaymentRequest> binding_;
199 payments::mojom::PaymentRequestClientPtr client_; 133 payments::mojom::PaymentRequestClientPtr client_;
200 payments::mojom::PaymentDetailsPtr details_;
201 payments::mojom::PaymentOptionsPtr options_;
202 std::unique_ptr<CurrencyFormatter> currency_formatter_; 134 std::unique_ptr<CurrencyFormatter> currency_formatter_;
203 // A set of supported basic card networks.
204 std::vector<std::string> supported_card_networks_;
205 bool is_ready_to_pay_;
206 std::unique_ptr<PaymentInstrument> selected_payment_instrument_;
207 mojom::PaymentResponsePtr payment_response_;
208 135
209 base::ObserverList<Observer> observers_; 136 std::unique_ptr<PaymentRequestSpec> spec_;
anthonyvd 2017/03/14 16:46:41 unique_ptr because they have to be initialized in
Mathieu 2017/03/14 18:05:39 Yes
137 std::unique_ptr<PaymentRequestState> state_;
210 138
211 // Profiles may change due to (e.g.) sync events, so profiles are cached after 139 // Profiles may change due to (e.g.) sync events, so profiles are cached after
212 // loading and owned here. They are populated once only, and ordered by 140 // loading and owned here. They are populated once only, and ordered by
213 // frecency. 141 // frecency.
214 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_; 142 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_;
215 std::vector<autofill::AutofillProfile*> shipping_profiles_; 143 std::vector<autofill::AutofillProfile*> shipping_profiles_;
216 std::vector<autofill::AutofillProfile*> contact_profiles_; 144 std::vector<autofill::AutofillProfile*> contact_profiles_;
217 autofill::AutofillProfile* selected_shipping_profile_;
218 autofill::AutofillProfile* selected_contact_profile_;
219 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_; 145 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_;
220 std::vector<autofill::CreditCard*> credit_cards_; 146 std::vector<autofill::CreditCard*> credit_cards_;
221 autofill::CreditCard* selected_credit_card_;
222 // This is owned by |details_|, which is owned by this object and lives until
223 // |this| is destructed so it's safe to keep this raw pointer.
224 payments::mojom::PaymentShippingOption* selected_shipping_option_;
225 147
226 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); 148 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
227 }; 149 };
228 150
229 } // namespace payments 151 } // namespace payments
230 152
231 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ 153 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698