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

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

Issue 2742813004: [Payments] Refactor into PaymentRequestState and Spec (Closed)
Patch Set: don't stop rebasin' 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
« no previous file with comments | « components/payments/content/BUILD.gn ('k') | components/payments/content/payment_request.cc » ('j') | 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 #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 #include "mojo/public/cpp/bindings/interface_request.h" 18 #include "mojo/public/cpp/bindings/interface_request.h"
19 19
20 namespace autofill { 20 namespace autofill {
21 class AutofillProfile;
22 class CreditCard;
23 class PersonalDataManager; 21 class PersonalDataManager;
24 } 22 }
25 23
26 namespace content { 24 namespace content {
27 class WebContents; 25 class WebContents;
28 } 26 }
29 27
30 namespace payments { 28 namespace payments {
31 29
32 class CurrencyFormatter; 30 class CurrencyFormatter;
33 class PaymentRequestWebContentsManager; 31 class PaymentRequestWebContentsManager;
34 32
33 // This class manages the interaction between the renderer (through the
34 // PaymentRequestClient and Mojo stub implementation) and the UI (through the
35 // PaymentRequestDelegate). The API user (merchant) specification (supported
36 // payment methods, required information, order details) is stored in
37 // PaymentRequestSpec, and the current user selection state (and related data)
38 // is stored in PaymentRequestSpec.
35 class PaymentRequest : public mojom::PaymentRequest, 39 class PaymentRequest : public mojom::PaymentRequest,
36 public PaymentInstrument::Delegate { 40 public PaymentRequestSpec::Observer,
41 public PaymentRequestState::Delegate {
37 public: 42 public:
38 class Observer {
39 public:
40 // Called when the information (payment method, address/contact info,
41 // shipping option) changes.
42 virtual void OnSelectedInformationChanged() = 0;
43
44 protected:
45 virtual ~Observer() {}
46 };
47
48 PaymentRequest(content::WebContents* web_contents, 43 PaymentRequest(content::WebContents* web_contents,
49 std::unique_ptr<PaymentRequestDelegate> delegate, 44 std::unique_ptr<PaymentRequestDelegate> delegate,
50 PaymentRequestWebContentsManager* manager, 45 PaymentRequestWebContentsManager* manager,
51 mojo::InterfaceRequest<mojom::PaymentRequest> request); 46 mojo::InterfaceRequest<mojom::PaymentRequest> request);
52 ~PaymentRequest() override; 47 ~PaymentRequest() override;
53 48
54 // mojom::PaymentRequest 49 // mojom::PaymentRequest
55 void Init(mojom::PaymentRequestClientPtr client, 50 void Init(mojom::PaymentRequestClientPtr client,
56 std::vector<mojom::PaymentMethodDataPtr> method_data, 51 std::vector<mojom::PaymentMethodDataPtr> method_data,
57 mojom::PaymentDetailsPtr details, 52 mojom::PaymentDetailsPtr details,
58 mojom::PaymentOptionsPtr options) override; 53 mojom::PaymentOptionsPtr options) override;
59 void Show() override; 54 void Show() override;
60 void UpdateWith(mojom::PaymentDetailsPtr details) override {} 55 void UpdateWith(mojom::PaymentDetailsPtr details) override {}
61 void Abort() override; 56 void Abort() override;
62 void Complete(mojom::PaymentComplete result) override; 57 void Complete(mojom::PaymentComplete result) override;
63 void CanMakePayment() override; 58 void CanMakePayment() override;
64 59
65 // PaymentInstrument::Delegate: 60 // PaymentRequestSpec::Observer:
66 void OnInstrumentDetailsReady( 61 void OnInvalidSpecProvided() override;
67 const std::string& method_name, 62
68 const std::string& stringified_details) override; 63 // PaymentRequestState::Delegate:
69 void OnInstrumentDetailsError() override {} 64 const std::string& GetApplicationLocale() override;
65 autofill::PersonalDataManager* GetPersonalDataManager() override;
66 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
70 67
71 // Called when the user explicitely cancelled the flow. Will send a message 68 // Called when the user explicitely cancelled the flow. Will send a message
72 // to the renderer which will indirectly destroy this object (through 69 // to the renderer which will indirectly destroy this object (through
73 // OnConnectionTerminated). 70 // OnConnectionTerminated).
74 void UserCancelled(); 71 void UserCancelled();
75 72
76 // As a result of a browser-side error or renderer-initiated mojo channel 73 // 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 74 // 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, 75 // successful), this method is called. It is responsible for cleaning up,
79 // such as possibly closing the dialog. 76 // such as possibly closing the dialog.
80 void OnConnectionTerminated(); 77 void OnConnectionTerminated();
81 78
82 // Called when the user clicks on the "Pay" button. 79 // Called when the user clicks on the "Pay" button.
83 void Pay(); 80 void Pay();
84 81
85 void AddObserver(Observer* observer);
86 void RemoveObserver(Observer* observer);
87
88 // Returns the CurrencyFormatter instance for this PaymentRequest. 82 // Returns the CurrencyFormatter instance for this PaymentRequest.
89 // |locale_name| should be the result of the browser's GetApplicationLocale(). 83 // |locale_name| should be the result of the browser's GetApplicationLocale().
90 // Note: Having multiple currencies per PaymentRequest is not supported; hence 84 // Note: Having multiple currencies per PaymentRequest is not supported; hence
91 // the CurrencyFormatter is cached here. 85 // the CurrencyFormatter is cached here.
92 CurrencyFormatter* GetOrCreateCurrencyFormatter( 86 CurrencyFormatter* GetOrCreateCurrencyFormatter(
93 const std::string& currency_code, 87 const std::string& currency_code,
94 const std::string& currency_system, 88 const std::string& currency_system,
95 const std::string& locale_name); 89 const std::string& locale_name);
96 90
97 // Uses CurrencyFormatter to format |amount| with the currency symbol for this 91 // Uses CurrencyFormatter to format |amount| with the currency symbol for this
98 // request's currency. 92 // request's currency.
99 base::string16 GetFormattedCurrencyAmount(const std::string& amount); 93 base::string16 GetFormattedCurrencyAmount(const std::string& amount);
100 94
101 // Uses CurrencyFormatter to get the formatted currency code for this 95 // Uses CurrencyFormatter to get the formatted currency code for this
102 // request's currency. 96 // request's currency.
103 std::string GetFormattedCurrencyCode(); 97 std::string GetFormattedCurrencyCode();
104 98
105 // Returns the appropriate Autofill Profiles for this user. On the first
106 // invocation of either getter, the profiles are fetched from the
107 // PersonalDataManager; on subsequent invocations, a cached version is
108 // returned. The profiles returned are owned by the request object.
109 const std::vector<autofill::AutofillProfile*>& shipping_profiles() {
110 return shipping_profiles_;
111 }
112 const std::vector<autofill::AutofillProfile*>& contact_profiles() {
113 return contact_profiles_;
114 }
115 const std::vector<autofill::CreditCard*>& credit_cards() {
116 return credit_cards_;
117 }
118
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() {
144 return delegate_->GetPersonalDataManager();
145 }
146 const std::string& locale() { return delegate_->GetApplicationLocale(); }
147
148 mojom::PaymentDetails* details() { return details_.get(); }
149 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_; } 99 content::WebContents* web_contents() { return web_contents_; }
154 100
155 bool request_shipping() const { return options_->request_shipping; } 101 PaymentRequestSpec* spec() { return spec_.get(); }
156 bool request_payer_name() const { return options_->request_payer_name; } 102 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 103
162 private: 104 private:
163 // Fetches the Autofill Profiles for this user from the PersonalDataManager,
164 // and stores copies of them, owned by this Request, in profile_cache_.
165 void PopulateProfileCache();
166
167 // Sets the default values for the selected Shipping and Contact profiles, as
168 // well as the selected Credit Card.
169 void SetDefaultProfileSelections();
170
171 // Validates the |method_data| and fills |supported_card_networks_|.
172 void PopulateValidatedMethodData(
173 const std::vector<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_; 105 content::WebContents* web_contents_;
195 std::unique_ptr<PaymentRequestDelegate> delegate_; 106 std::unique_ptr<PaymentRequestDelegate> delegate_;
196 // |manager_| owns this PaymentRequest. 107 // |manager_| owns this PaymentRequest.
197 PaymentRequestWebContentsManager* manager_; 108 PaymentRequestWebContentsManager* manager_;
198 mojo::Binding<mojom::PaymentRequest> binding_; 109 mojo::Binding<mojom::PaymentRequest> binding_;
199 mojom::PaymentRequestClientPtr client_; 110 mojom::PaymentRequestClientPtr client_;
200 mojom::PaymentDetailsPtr details_;
201 mojom::PaymentOptionsPtr options_;
202 std::unique_ptr<CurrencyFormatter> currency_formatter_; 111 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 112
209 base::ObserverList<Observer> observers_; 113 std::unique_ptr<PaymentRequestSpec> spec_;
210 114 std::unique_ptr<PaymentRequestState> state_;
211 // 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
213 // frecency.
214 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_;
215 std::vector<autofill::AutofillProfile*> shipping_profiles_;
216 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_;
220 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 115
226 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); 116 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
227 }; 117 };
228 118
229 } // namespace payments 119 } // namespace payments
230 120
231 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ 121 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
OLDNEW
« no previous file with comments | « components/payments/content/BUILD.gn ('k') | components/payments/content/payment_request.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698