OLD | NEW |
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" | 13 #include "base/observer_list.h" |
14 #include "components/payments/content/payment_request.mojom.h" | 14 #include "components/payments/content/payment_request.mojom.h" |
15 #include "components/payments/content/payment_request_delegate.h" | 15 #include "components/payments/content/payment_request_delegate.h" |
16 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "mojo/public/cpp/bindings/interface_request.h" |
17 | 18 |
18 namespace autofill { | 19 namespace autofill { |
19 class AutofillProfile; | 20 class AutofillProfile; |
20 class CreditCard; | 21 class CreditCard; |
21 class PersonalDataManager; | 22 class PersonalDataManager; |
22 } | 23 } |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 class WebContents; | 26 class WebContents; |
26 } | 27 } |
27 | 28 |
28 namespace payments { | 29 namespace payments { |
29 | 30 |
30 class CurrencyFormatter; | 31 class CurrencyFormatter; |
31 class PaymentRequestWebContentsManager; | 32 class PaymentRequestWebContentsManager; |
32 | 33 |
33 class PaymentRequest : payments::mojom::PaymentRequest { | 34 class PaymentRequest : public mojom::PaymentRequest { |
34 public: | 35 public: |
35 class Observer { | 36 class Observer { |
36 public: | 37 public: |
37 // Called when the information (payment method, address/contact info, | 38 // Called when the information (payment method, address/contact info, |
38 // shipping option) changes. | 39 // shipping option) changes. |
39 virtual void OnSelectedInformationChanged() = 0; | 40 virtual void OnSelectedInformationChanged() = 0; |
40 | 41 |
41 protected: | 42 protected: |
42 virtual ~Observer() {} | 43 virtual ~Observer() {} |
43 }; | 44 }; |
44 | 45 |
45 PaymentRequest( | 46 PaymentRequest(content::WebContents* web_contents, |
46 content::WebContents* web_contents, | 47 std::unique_ptr<PaymentRequestDelegate> delegate, |
47 std::unique_ptr<PaymentRequestDelegate> delegate, | 48 PaymentRequestWebContentsManager* manager, |
48 PaymentRequestWebContentsManager* manager, | 49 mojo::InterfaceRequest<mojom::PaymentRequest> request); |
49 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request); | |
50 ~PaymentRequest() override; | 50 ~PaymentRequest() override; |
51 | 51 |
52 // payments::mojom::PaymentRequest "stub" | 52 // mojom::PaymentRequest |
53 void Init(payments::mojom::PaymentRequestClientPtr client, | 53 void Init(mojom::PaymentRequestClientPtr client, |
54 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, | 54 std::vector<mojom::PaymentMethodDataPtr> method_data, |
55 payments::mojom::PaymentDetailsPtr details, | 55 mojom::PaymentDetailsPtr details, |
56 payments::mojom::PaymentOptionsPtr options) override; | 56 mojom::PaymentOptionsPtr options) override; |
57 void Show() override; | 57 void Show() override; |
58 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {} | 58 void UpdateWith(mojom::PaymentDetailsPtr details) override {} |
59 void Abort() override; | 59 void Abort() override; |
60 void Complete(payments::mojom::PaymentComplete result) override {} | 60 void Complete(mojom::PaymentComplete result) override {} |
61 void CanMakePayment() override {} | 61 void CanMakePayment() override {} |
62 | 62 |
63 // Called when the user explicitely cancelled the flow. Will send a message | 63 // Called when the user explicitely cancelled the flow. Will send a message |
64 // to the renderer which will indirectly destroy this object (through | 64 // to the renderer which will indirectly destroy this object (through |
65 // OnConnectionTerminated). | 65 // OnConnectionTerminated). |
66 void UserCancelled(); | 66 void UserCancelled(); |
67 | 67 |
68 // As a result of a browser-side error or renderer-initiated mojo channel | 68 // As a result of a browser-side error or renderer-initiated mojo channel |
69 // closure (e.g. there was an error on the renderer side, or payment was | 69 // closure (e.g. there was an error on the renderer side, or payment was |
70 // successful), this method is called. It is responsible for cleaning up, | 70 // successful), this method is called. It is responsible for cleaning up, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Sets the |profile| to be the selected one and will update state and notify | 117 // Sets the |profile| to be the selected one and will update state and notify |
118 // observers. | 118 // observers. |
119 void SetSelectedShippingProfile(autofill::AutofillProfile* profile); | 119 void SetSelectedShippingProfile(autofill::AutofillProfile* profile); |
120 void SetSelectedContactProfile(autofill::AutofillProfile* profile); | 120 void SetSelectedContactProfile(autofill::AutofillProfile* profile); |
121 void SetSelectedCreditCard(autofill::CreditCard* card); | 121 void SetSelectedCreditCard(autofill::CreditCard* card); |
122 | 122 |
123 autofill::PersonalDataManager* personal_data_manager() { | 123 autofill::PersonalDataManager* personal_data_manager() { |
124 return delegate_->GetPersonalDataManager(); | 124 return delegate_->GetPersonalDataManager(); |
125 } | 125 } |
126 | 126 |
127 payments::mojom::PaymentDetails* details() { return details_.get(); } | 127 mojom::PaymentDetails* details() { return details_.get(); } |
128 const std::vector<std::string>& supported_card_networks() { | 128 const std::vector<std::string>& supported_card_networks() { |
129 return supported_card_networks_; | 129 return supported_card_networks_; |
130 } | 130 } |
131 content::WebContents* web_contents() { return web_contents_; } | 131 content::WebContents* web_contents() { return web_contents_; } |
132 | 132 |
133 bool is_ready_to_pay() { return is_ready_to_pay_; } | 133 bool is_ready_to_pay() { return is_ready_to_pay_; } |
134 | 134 |
135 private: | 135 private: |
136 // Fetches the Autofill Profiles for this user from the PersonalDataManager, | 136 // Fetches the Autofill Profiles for this user from the PersonalDataManager, |
137 // and stores copies of them, owned by this Request, in profile_cache_. | 137 // and stores copies of them, owned by this Request, in profile_cache_. |
138 void PopulateProfileCache(); | 138 void PopulateProfileCache(); |
139 | 139 |
140 // Sets the default values for the selected Shipping and Contact profiles, as | 140 // Sets the default values for the selected Shipping and Contact profiles, as |
141 // well as the selected Credit Card. | 141 // well as the selected Credit Card. |
142 void SetDefaultProfileSelections(); | 142 void SetDefaultProfileSelections(); |
143 | 143 |
144 // Validates the |method_data| and fills |supported_card_networks_|. | 144 // Validates the |method_data| and fills |supported_card_networks_|. |
145 void PopulateValidatedMethodData( | 145 void PopulateValidatedMethodData( |
146 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data); | 146 const std::vector<mojom::PaymentMethodDataPtr>& method_data); |
147 | 147 |
148 // Updates |is_ready_to_pay_| with the current state, by validating that all | 148 // Updates |is_ready_to_pay_| with the current state, by validating that all |
149 // the required information is available and notify observers. | 149 // the required information is available and notify observers. |
150 void UpdateIsReadyToPayAndNotifyObservers(); | 150 void UpdateIsReadyToPayAndNotifyObservers(); |
151 | 151 |
152 // Notifies all observers that selected information has changed. | 152 // Notifies all observers that selected information has changed. |
153 void NotifyOnSelectedInformationChanged(); | 153 void NotifyOnSelectedInformationChanged(); |
154 | 154 |
155 // Returns whether the selected data satisfies the PaymentDetails requirements | 155 // Returns whether the selected data satisfies the PaymentDetails requirements |
156 // (payment methods). | 156 // (payment methods). |
157 bool ArePaymentDetailsSatisfied(); | 157 bool ArePaymentDetailsSatisfied(); |
158 // Returns whether the selected data satisfies the PaymentOptions requirements | 158 // Returns whether the selected data satisfies the PaymentOptions requirements |
159 // (contact info, shipping address). | 159 // (contact info, shipping address). |
160 bool ArePaymentOptionsSatisfied(); | 160 bool ArePaymentOptionsSatisfied(); |
161 | 161 |
162 content::WebContents* web_contents_; | 162 content::WebContents* web_contents_; |
163 std::unique_ptr<PaymentRequestDelegate> delegate_; | 163 std::unique_ptr<PaymentRequestDelegate> delegate_; |
164 // |manager_| owns this PaymentRequest. | 164 // |manager_| owns this PaymentRequest. |
165 PaymentRequestWebContentsManager* manager_; | 165 PaymentRequestWebContentsManager* manager_; |
166 mojo::Binding<payments::mojom::PaymentRequest> binding_; | 166 mojo::Binding<mojom::PaymentRequest> binding_; |
167 payments::mojom::PaymentRequestClientPtr client_; | 167 mojom::PaymentRequestClientPtr client_; |
168 payments::mojom::PaymentDetailsPtr details_; | 168 mojom::PaymentDetailsPtr details_; |
169 payments::mojom::PaymentOptionsPtr options_; | 169 mojom::PaymentOptionsPtr options_; |
170 std::unique_ptr<CurrencyFormatter> currency_formatter_; | 170 std::unique_ptr<CurrencyFormatter> currency_formatter_; |
171 // A set of supported basic card networks. | 171 // A set of supported basic card networks. |
172 std::vector<std::string> supported_card_networks_; | 172 std::vector<std::string> supported_card_networks_; |
173 bool is_ready_to_pay_; | 173 bool is_ready_to_pay_; |
174 | 174 |
175 base::ObserverList<Observer> observers_; | 175 base::ObserverList<Observer> observers_; |
176 | 176 |
177 // Profiles may change due to (e.g.) sync events, so profiles are cached after | 177 // Profiles may change due to (e.g.) sync events, so profiles are cached after |
178 // loading and owned here. They are populated once only, and ordered by | 178 // loading and owned here. They are populated once only, and ordered by |
179 // frecency. | 179 // frecency. |
180 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_; | 180 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_; |
181 std::vector<autofill::AutofillProfile*> shipping_profiles_; | 181 std::vector<autofill::AutofillProfile*> shipping_profiles_; |
182 std::vector<autofill::AutofillProfile*> contact_profiles_; | 182 std::vector<autofill::AutofillProfile*> contact_profiles_; |
183 autofill::AutofillProfile* selected_shipping_profile_; | 183 autofill::AutofillProfile* selected_shipping_profile_; |
184 autofill::AutofillProfile* selected_contact_profile_; | 184 autofill::AutofillProfile* selected_contact_profile_; |
185 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_; | 185 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_; |
186 std::vector<autofill::CreditCard*> credit_cards_; | 186 std::vector<autofill::CreditCard*> credit_cards_; |
187 autofill::CreditCard* selected_credit_card_; | 187 autofill::CreditCard* selected_credit_card_; |
188 | 188 |
189 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); | 189 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); |
190 }; | 190 }; |
191 | 191 |
192 } // namespace payments | 192 } // namespace payments |
193 | 193 |
194 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ | 194 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
OLD | NEW |