| 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 "components/payments/content/payment_request.mojom.h" | 14 #include "components/payments/content/payment_request.mojom.h" |
| 14 #include "components/payments/content/payment_request_delegate.h" | 15 #include "components/payments/content/payment_request_delegate.h" |
| 15 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
| 16 | 17 |
| 17 namespace autofill { | 18 namespace autofill { |
| 18 class AutofillProfile; | 19 class AutofillProfile; |
| 19 class CreditCard; | 20 class CreditCard; |
| 20 class PersonalDataManager; | 21 class PersonalDataManager; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 class WebContents; | 25 class WebContents; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace payments { | 28 namespace payments { |
| 28 | 29 |
| 29 class CurrencyFormatter; | 30 class CurrencyFormatter; |
| 30 class PaymentRequestWebContentsManager; | 31 class PaymentRequestWebContentsManager; |
| 31 | 32 |
| 32 class PaymentRequest : payments::mojom::PaymentRequest { | 33 class PaymentRequest : payments::mojom::PaymentRequest { |
| 33 public: | 34 public: |
| 35 class Observer { |
| 36 public: |
| 37 // Called when the information (payment method, address/contact info, |
| 38 // shipping option) changes. |
| 39 virtual void OnSelectedInformationChanged() = 0; |
| 40 |
| 41 protected: |
| 42 virtual ~Observer() {} |
| 43 }; |
| 44 |
| 34 PaymentRequest( | 45 PaymentRequest( |
| 35 content::WebContents* web_contents, | 46 content::WebContents* web_contents, |
| 36 std::unique_ptr<PaymentRequestDelegate> delegate, | 47 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 37 PaymentRequestWebContentsManager* manager, | 48 PaymentRequestWebContentsManager* manager, |
| 38 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request); | 49 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request); |
| 39 ~PaymentRequest() override; | 50 ~PaymentRequest() override; |
| 40 | 51 |
| 41 // payments::mojom::PaymentRequest "stub" | 52 // payments::mojom::PaymentRequest "stub" |
| 42 void Init(payments::mojom::PaymentRequestClientPtr client, | 53 void Init(payments::mojom::PaymentRequestClientPtr client, |
| 43 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, | 54 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, |
| 44 payments::mojom::PaymentDetailsPtr details, | 55 payments::mojom::PaymentDetailsPtr details, |
| 45 payments::mojom::PaymentOptionsPtr options) override; | 56 payments::mojom::PaymentOptionsPtr options) override; |
| 46 void Show() override; | 57 void Show() override; |
| 47 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {} | 58 void UpdateWith(payments::mojom::PaymentDetailsPtr details) override {} |
| 48 void Abort() override; | 59 void Abort() override; |
| 49 void Complete(payments::mojom::PaymentComplete result) override {} | 60 void Complete(payments::mojom::PaymentComplete result) override {} |
| 50 void CanMakePayment() override {} | 61 void CanMakePayment() override {} |
| 51 | 62 |
| 52 // 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 |
| 53 // to the renderer which will indirectly destroy this object (through | 64 // to the renderer which will indirectly destroy this object (through |
| 54 // OnConnectionTerminated). | 65 // OnConnectionTerminated). |
| 55 void UserCancelled(); | 66 void UserCancelled(); |
| 56 | 67 |
| 57 // 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 |
| 58 // 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 |
| 59 // successful), this method is called. It is responsible for cleaning up, | 70 // successful), this method is called. It is responsible for cleaning up, |
| 60 // such as possibly closing the dialog. | 71 // such as possibly closing the dialog. |
| 61 void OnConnectionTerminated(); | 72 void OnConnectionTerminated(); |
| 62 | 73 |
| 74 // Called when the user clicks on the "Pay" button. |
| 75 void Pay(); |
| 76 |
| 77 void AddObserver(Observer* observer); |
| 78 void RemoveObserver(Observer* observer); |
| 79 |
| 63 // Returns the CurrencyFormatter instance for this PaymentRequest. | 80 // Returns the CurrencyFormatter instance for this PaymentRequest. |
| 64 // |locale_name| should be the result of the browser's GetApplicationLocale(). | 81 // |locale_name| should be the result of the browser's GetApplicationLocale(). |
| 65 // Note: Having multiple currencies per PaymentRequest is not supported; hence | 82 // Note: Having multiple currencies per PaymentRequest is not supported; hence |
| 66 // the CurrencyFormatter is cached here. | 83 // the CurrencyFormatter is cached here. |
| 67 CurrencyFormatter* GetOrCreateCurrencyFormatter( | 84 CurrencyFormatter* GetOrCreateCurrencyFormatter( |
| 68 const std::string& currency_code, | 85 const std::string& currency_code, |
| 69 const std::string& currency_system, | 86 const std::string& currency_system, |
| 70 const std::string& locale_name); | 87 const std::string& locale_name); |
| 71 | 88 |
| 72 // Returns the appropriate Autofill Profiles for this user. On the first | 89 // Returns the appropriate Autofill Profiles for this user. On the first |
| 73 // invocation of either getter, the profiles are fetched from the | 90 // invocation of either getter, the profiles are fetched from the |
| 74 // PersonalDataManager; on subsequent invocations, a cached version is | 91 // PersonalDataManager; on subsequent invocations, a cached version is |
| 75 // returned. The profiles returned are owned by the request object. | 92 // returned. The profiles returned are owned by the request object. |
| 76 const std::vector<autofill::AutofillProfile*>& shipping_profiles(); | 93 const std::vector<autofill::AutofillProfile*>& shipping_profiles() { |
| 77 const std::vector<autofill::AutofillProfile*>& contact_profiles(); | 94 return shipping_profiles_; |
| 95 } |
| 96 const std::vector<autofill::AutofillProfile*>& contact_profiles() { |
| 97 return contact_profiles_; |
| 98 } |
| 99 const std::vector<autofill::CreditCard*>& credit_cards() { |
| 100 return credit_cards_; |
| 101 } |
| 78 | 102 |
| 79 // Gets/sets the Autofill Profile representing the shipping address or contact | 103 // Gets the Autofill Profile representing the shipping address or contact |
| 80 // information currently selected for this PaymentRequest flow. Can return | 104 // information currently selected for this PaymentRequest flow. Can return |
| 81 // null. | 105 // null. |
| 82 autofill::AutofillProfile* selected_shipping_profile() const { | 106 autofill::AutofillProfile* selected_shipping_profile() const { |
| 83 return selected_shipping_profile_; | 107 return selected_shipping_profile_; |
| 84 } | 108 } |
| 85 void set_selected_shipping_profile(autofill::AutofillProfile* profile) { | |
| 86 selected_shipping_profile_ = profile; | |
| 87 } | |
| 88 autofill::AutofillProfile* selected_contact_profile() const { | 109 autofill::AutofillProfile* selected_contact_profile() const { |
| 89 return selected_contact_profile_; | 110 return selected_contact_profile_; |
| 90 } | 111 } |
| 91 void set_selected_contact_profile(autofill::AutofillProfile* profile) { | |
| 92 selected_contact_profile_ = profile; | |
| 93 } | |
| 94 | |
| 95 const std::vector<autofill::CreditCard*>& credit_cards() { | |
| 96 return credit_cards_; | |
| 97 } | |
| 98 | |
| 99 // Returns the currently selected credit card for this PaymentRequest flow. | 112 // Returns the currently selected credit card for this PaymentRequest flow. |
| 100 // It's not guaranteed to be complete. Returns nullptr if there is no selected | 113 // It's not guaranteed to be complete. Returns nullptr if there is no selected |
| 101 // card. | 114 // card. |
| 102 autofill::CreditCard* selected_credit_card() { return selected_credit_card_; } | 115 autofill::CreditCard* selected_credit_card() { return selected_credit_card_; } |
| 103 | 116 |
| 104 void set_selected_credit_card(autofill::CreditCard* credit_card) { | 117 // Sets the |profile| to be the selected one and will update state and notify |
| 105 selected_credit_card_ = credit_card; | 118 // observers. |
| 106 } | 119 void SetSelectedShippingProfile(autofill::AutofillProfile* profile); |
| 120 void SetSelectedContactProfile(autofill::AutofillProfile* profile); |
| 121 void SetSelectedCreditCard(autofill::CreditCard* card); |
| 107 | 122 |
| 108 autofill::PersonalDataManager* personal_data_manager() { | 123 autofill::PersonalDataManager* personal_data_manager() { |
| 109 return delegate_->GetPersonalDataManager(); | 124 return delegate_->GetPersonalDataManager(); |
| 110 } | 125 } |
| 111 | 126 |
| 112 payments::mojom::PaymentDetails* details() { return details_.get(); } | 127 payments::mojom::PaymentDetails* details() { return details_.get(); } |
| 113 const std::vector<std::string>& supported_card_networks() { | 128 const std::vector<std::string>& supported_card_networks() { |
| 114 return supported_card_networks_; | 129 return supported_card_networks_; |
| 115 } | 130 } |
| 116 content::WebContents* web_contents() { return web_contents_; } | 131 content::WebContents* web_contents() { return web_contents_; } |
| 117 | 132 |
| 133 bool is_ready_to_pay() { return is_ready_to_pay_; } |
| 134 |
| 118 private: | 135 private: |
| 119 // Fetches the Autofill Profiles for this user from the PersonalDataManager, | 136 // Fetches the Autofill Profiles for this user from the PersonalDataManager, |
| 120 // 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_. |
| 121 void PopulateProfileCache(); | 138 void PopulateProfileCache(); |
| 122 | 139 |
| 123 // 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 |
| 124 // well as the selected Credit Card. | 141 // well as the selected Credit Card. |
| 125 void SetDefaultProfileSelections(); | 142 void SetDefaultProfileSelections(); |
| 126 | 143 |
| 127 // Validates the |method_data| and fills |supported_card_networks_|. | 144 // Validates the |method_data| and fills |supported_card_networks_|. |
| 128 void PopulateValidatedMethodData( | 145 void PopulateValidatedMethodData( |
| 129 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data); | 146 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data); |
| 130 | 147 |
| 148 // Updates |is_ready_to_pay_| with the current state, by validating that all |
| 149 // the required information is available and notify observers. |
| 150 void UpdateIsReadyToPayAndNotifyObservers(); |
| 151 |
| 152 // Notifies all observers that selected information has changed. |
| 153 void NotifyOnSelectedInformationChanged(); |
| 154 |
| 155 // Returns whether the selected data satisfies the PaymentDetails requirements |
| 156 // (payment methods). |
| 157 bool ArePaymentDetailsSatisfied(); |
| 158 // Returns whether the selected data satisfies the PaymentOptions requirements |
| 159 // (contact info, shipping address). |
| 160 bool ArePaymentOptionsSatisfied(); |
| 161 |
| 131 content::WebContents* web_contents_; | 162 content::WebContents* web_contents_; |
| 132 std::unique_ptr<PaymentRequestDelegate> delegate_; | 163 std::unique_ptr<PaymentRequestDelegate> delegate_; |
| 133 // |manager_| owns this PaymentRequest. | 164 // |manager_| owns this PaymentRequest. |
| 134 PaymentRequestWebContentsManager* manager_; | 165 PaymentRequestWebContentsManager* manager_; |
| 135 mojo::Binding<payments::mojom::PaymentRequest> binding_; | 166 mojo::Binding<payments::mojom::PaymentRequest> binding_; |
| 136 payments::mojom::PaymentRequestClientPtr client_; | 167 payments::mojom::PaymentRequestClientPtr client_; |
| 137 payments::mojom::PaymentDetailsPtr details_; | 168 payments::mojom::PaymentDetailsPtr details_; |
| 169 payments::mojom::PaymentOptionsPtr options_; |
| 138 std::unique_ptr<CurrencyFormatter> currency_formatter_; | 170 std::unique_ptr<CurrencyFormatter> currency_formatter_; |
| 139 // A set of supported basic card networks. | 171 // A set of supported basic card networks. |
| 140 std::vector<std::string> supported_card_networks_; | 172 std::vector<std::string> supported_card_networks_; |
| 173 bool is_ready_to_pay_; |
| 174 |
| 175 base::ObserverList<Observer> observers_; |
| 141 | 176 |
| 142 // 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 |
| 143 // 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 |
| 144 // frecency. | 179 // frecency. |
| 145 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_; | 180 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_; |
| 146 std::vector<autofill::AutofillProfile*> shipping_profiles_; | 181 std::vector<autofill::AutofillProfile*> shipping_profiles_; |
| 147 std::vector<autofill::AutofillProfile*> contact_profiles_; | 182 std::vector<autofill::AutofillProfile*> contact_profiles_; |
| 148 autofill::AutofillProfile* selected_shipping_profile_; | 183 autofill::AutofillProfile* selected_shipping_profile_; |
| 149 autofill::AutofillProfile* selected_contact_profile_; | 184 autofill::AutofillProfile* selected_contact_profile_; |
| 150 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_; | 185 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_; |
| 151 std::vector<autofill::CreditCard*> credit_cards_; | 186 std::vector<autofill::CreditCard*> credit_cards_; |
| 152 autofill::CreditCard* selected_credit_card_; | 187 autofill::CreditCard* selected_credit_card_; |
| 153 | 188 |
| 154 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); | 189 DISALLOW_COPY_AND_ASSIGN(PaymentRequest); |
| 155 }; | 190 }; |
| 156 | 191 |
| 157 } // namespace payments | 192 } // namespace payments |
| 158 | 193 |
| 159 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ | 194 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
| OLD | NEW |