| 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 "components/payments/core/payment_instrument.h" | 16 #include "components/payments/core/payment_instrument.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 | 19 |
| 19 namespace autofill { | 20 namespace autofill { |
| 20 class AutofillProfile; | 21 class AutofillProfile; |
| 21 class CreditCard; | 22 class CreditCard; |
| 22 class PersonalDataManager; | 23 class PersonalDataManager; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace content { | 26 namespace content { |
| 26 class WebContents; | 27 class WebContents; |
| 27 } | 28 } |
| 28 | 29 |
| 29 namespace payments { | 30 namespace payments { |
| 30 | 31 |
| 31 class CurrencyFormatter; | 32 class CurrencyFormatter; |
| 32 class PaymentRequestWebContentsManager; | 33 class PaymentRequestWebContentsManager; |
| 33 | 34 |
| 34 class PaymentRequest : public mojom::PaymentRequest, | 35 class PaymentRequest : public mojom::PaymentRequest, |
| 35 public PaymentInstrument::Delegate { | 36 public PaymentInstrument::Delegate { |
| 36 public: | 37 public: |
| 37 class Observer { | 38 class Observer { |
| 38 public: | 39 public: |
| 39 // Called when the information (payment method, address/contact info, | 40 // Called when the information (payment method, address/contact info, |
| 40 // shipping option) changes. | 41 // shipping option) changes. |
| 41 virtual void OnSelectedInformationChanged() = 0; | 42 virtual void OnSelectedInformationChanged() = 0; |
| 42 | 43 |
| 43 protected: | 44 protected: |
| 44 virtual ~Observer() {} | 45 virtual ~Observer() {} |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 PaymentRequest( | 48 PaymentRequest(content::WebContents* web_contents, |
| 48 content::WebContents* web_contents, | 49 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 49 std::unique_ptr<PaymentRequestDelegate> delegate, | 50 PaymentRequestWebContentsManager* manager, |
| 50 PaymentRequestWebContentsManager* manager, | 51 mojo::InterfaceRequest<mojom::PaymentRequest> request); |
| 51 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request); | |
| 52 ~PaymentRequest() override; | 52 ~PaymentRequest() override; |
| 53 | 53 |
| 54 // payments::mojom::PaymentRequest "stub" | 54 // mojom::PaymentRequest |
| 55 void Init(payments::mojom::PaymentRequestClientPtr client, | 55 void Init(mojom::PaymentRequestClientPtr client, |
| 56 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, | 56 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 57 payments::mojom::PaymentDetailsPtr details, | 57 mojom::PaymentDetailsPtr details, |
| 58 payments::mojom::PaymentOptionsPtr options) override; | 58 mojom::PaymentOptionsPtr options) override; |
| 59 void Show() override; | 59 void Show() override; |
| 60 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {} | 60 void UpdateWith(mojom::PaymentDetailsPtr details) override {} |
| 61 void Abort() override; | 61 void Abort() override; |
| 62 void Complete(payments::mojom::PaymentComplete result) override; | 62 void Complete(mojom::PaymentComplete result) override; |
| 63 void CanMakePayment() override; | 63 void CanMakePayment() override; |
| 64 | 64 |
| 65 // PaymentInstrument::Delegate: | 65 // PaymentInstrument::Delegate: |
| 66 void OnInstrumentDetailsReady( | 66 void OnInstrumentDetailsReady( |
| 67 const std::string& method_name, | 67 const std::string& method_name, |
| 68 const std::string& stringified_details) override; | 68 const std::string& stringified_details) override; |
| 69 void OnInstrumentDetailsError() override {} | 69 void OnInstrumentDetailsError() override {} |
| 70 | 70 |
| 71 // Called when the user explicitely cancelled the flow. Will send a message | 71 // Called when the user explicitely cancelled the flow. Will send a message |
| 72 // to the renderer which will indirectly destroy this object (through | 72 // to the renderer which will indirectly destroy this object (through |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // observers. | 138 // observers. |
| 139 void SetSelectedShippingProfile(autofill::AutofillProfile* profile); | 139 void SetSelectedShippingProfile(autofill::AutofillProfile* profile); |
| 140 void SetSelectedContactProfile(autofill::AutofillProfile* profile); | 140 void SetSelectedContactProfile(autofill::AutofillProfile* profile); |
| 141 void SetSelectedCreditCard(autofill::CreditCard* card); | 141 void SetSelectedCreditCard(autofill::CreditCard* card); |
| 142 | 142 |
| 143 autofill::PersonalDataManager* personal_data_manager() { | 143 autofill::PersonalDataManager* personal_data_manager() { |
| 144 return delegate_->GetPersonalDataManager(); | 144 return delegate_->GetPersonalDataManager(); |
| 145 } | 145 } |
| 146 const std::string& locale() { return delegate_->GetApplicationLocale(); } | 146 const std::string& locale() { return delegate_->GetApplicationLocale(); } |
| 147 | 147 |
| 148 payments::mojom::PaymentDetails* details() { return details_.get(); } | 148 mojom::PaymentDetails* details() { return details_.get(); } |
| 149 payments::mojom::PaymentOptions* options() { return options_.get(); } | 149 mojom::PaymentOptions* options() { return options_.get(); } |
| 150 const std::vector<std::string>& supported_card_networks() { | 150 const std::vector<std::string>& supported_card_networks() { |
| 151 return supported_card_networks_; | 151 return supported_card_networks_; |
| 152 } | 152 } |
| 153 content::WebContents* web_contents() { return web_contents_; } | 153 content::WebContents* web_contents() { return web_contents_; } |
| 154 | 154 |
| 155 bool request_shipping() const { return options_->request_shipping; } | 155 bool request_shipping() const { return options_->request_shipping; } |
| 156 bool request_payer_name() const { return options_->request_payer_name; } | 156 bool request_payer_name() const { return options_->request_payer_name; } |
| 157 bool request_payer_phone() const { return options_->request_payer_phone; } | 157 bool request_payer_phone() const { return options_->request_payer_phone; } |
| 158 bool request_payer_email() const { return options_->request_payer_email; } | 158 bool request_payer_email() const { return options_->request_payer_email; } |
| 159 | 159 |
| 160 bool is_ready_to_pay() { return is_ready_to_pay_; } | 160 bool is_ready_to_pay() { return is_ready_to_pay_; } |
| 161 | 161 |
| 162 private: | 162 private: |
| 163 // Fetches the Autofill Profiles for this user from the PersonalDataManager, | 163 // Fetches the Autofill Profiles for this user from the PersonalDataManager, |
| 164 // and stores copies of them, owned by this Request, in profile_cache_. | 164 // and stores copies of them, owned by this Request, in profile_cache_. |
| 165 void PopulateProfileCache(); | 165 void PopulateProfileCache(); |
| 166 | 166 |
| 167 // Sets the default values for the selected Shipping and Contact profiles, as | 167 // Sets the default values for the selected Shipping and Contact profiles, as |
| 168 // well as the selected Credit Card. | 168 // well as the selected Credit Card. |
| 169 void SetDefaultProfileSelections(); | 169 void SetDefaultProfileSelections(); |
| 170 | 170 |
| 171 // Validates the |method_data| and fills |supported_card_networks_|. | 171 // Validates the |method_data| and fills |supported_card_networks_|. |
| 172 void PopulateValidatedMethodData( | 172 void PopulateValidatedMethodData( |
| 173 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data); | 173 const std::vector<mojom::PaymentMethodDataPtr>& method_data); |
| 174 | 174 |
| 175 // Updates |is_ready_to_pay_| with the current state, by validating that all | 175 // Updates |is_ready_to_pay_| with the current state, by validating that all |
| 176 // the required information is available and notify observers. | 176 // the required information is available and notify observers. |
| 177 void UpdateIsReadyToPayAndNotifyObservers(); | 177 void UpdateIsReadyToPayAndNotifyObservers(); |
| 178 | 178 |
| 179 // Notifies all observers that selected information has changed. | 179 // Notifies all observers that selected information has changed. |
| 180 void NotifyOnSelectedInformationChanged(); | 180 void NotifyOnSelectedInformationChanged(); |
| 181 | 181 |
| 182 // Returns whether the selected data satisfies the PaymentDetails requirements | 182 // Returns whether the selected data satisfies the PaymentDetails requirements |
| 183 // (payment methods). | 183 // (payment methods). |
| 184 bool ArePaymentDetailsSatisfied(); | 184 bool ArePaymentDetailsSatisfied(); |
| 185 // Returns whether the selected data satisfies the PaymentOptions requirements | 185 // Returns whether the selected data satisfies the PaymentOptions requirements |
| 186 // (contact info, shipping address). | 186 // (contact info, shipping address). |
| 187 bool ArePaymentOptionsSatisfied(); | 187 bool ArePaymentOptionsSatisfied(); |
| 188 | 188 |
| 189 // Updates the selected_shipping_option based on the data passed to this | 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 | 190 // payment request by the website. This will set selected_shipping_option_ to |
| 191 // the last option marked selected in the options array. | 191 // the last option marked selected in the options array. |
| 192 void UpdateSelectedShippingOptionFromDetails(); | 192 void UpdateSelectedShippingOptionFromDetails(); |
| 193 | 193 |
| 194 content::WebContents* web_contents_; | 194 content::WebContents* web_contents_; |
| 195 std::unique_ptr<PaymentRequestDelegate> delegate_; | 195 std::unique_ptr<PaymentRequestDelegate> delegate_; |
| 196 // |manager_| owns this PaymentRequest. | 196 // |manager_| owns this PaymentRequest. |
| 197 PaymentRequestWebContentsManager* manager_; | 197 PaymentRequestWebContentsManager* manager_; |
| 198 mojo::Binding<payments::mojom::PaymentRequest> binding_; | 198 mojo::Binding<mojom::PaymentRequest> binding_; |
| 199 payments::mojom::PaymentRequestClientPtr client_; | 199 mojom::PaymentRequestClientPtr client_; |
| 200 payments::mojom::PaymentDetailsPtr details_; | 200 mojom::PaymentDetailsPtr details_; |
| 201 payments::mojom::PaymentOptionsPtr options_; | 201 mojom::PaymentOptionsPtr options_; |
| 202 std::unique_ptr<CurrencyFormatter> currency_formatter_; | 202 std::unique_ptr<CurrencyFormatter> currency_formatter_; |
| 203 // A set of supported basic card networks. | 203 // A set of supported basic card networks. |
| 204 std::vector<std::string> supported_card_networks_; | 204 std::vector<std::string> supported_card_networks_; |
| 205 bool is_ready_to_pay_; | 205 bool is_ready_to_pay_; |
| 206 std::unique_ptr<PaymentInstrument> selected_payment_instrument_; | 206 std::unique_ptr<PaymentInstrument> selected_payment_instrument_; |
| 207 mojom::PaymentResponsePtr payment_response_; | 207 mojom::PaymentResponsePtr payment_response_; |
| 208 | 208 |
| 209 base::ObserverList<Observer> observers_; | 209 base::ObserverList<Observer> observers_; |
| 210 | 210 |
| 211 // Profiles may change due to (e.g.) sync events, so profiles are cached after | 211 // Profiles may change due to (e.g.) sync events, so profiles are cached after |
| (...skipping 10 matching lines...) Expand all Loading... |
| 222 // This is owned by |details_|, which is owned by this object and lives until | 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. | 223 // |this| is destructed so it's safe to keep this raw pointer. |
| 224 payments::mojom::PaymentShippingOption* selected_shipping_option_; | 224 payments::mojom::PaymentShippingOption* selected_shipping_option_; |
| 225 | 225 |
| 226 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); | 226 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 } // namespace payments | 229 } // namespace payments |
| 230 | 230 |
| 231 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ | 231 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
| OLD | NEW |