Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_ | |
| 6 #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/observer_list.h" | |
| 10 #include "components/payments/content/payment_request.mojom.h" | |
| 11 #include "components/payments/core/payment_instrument.h" | |
| 12 | |
| 13 namespace autofill { | |
| 14 class AutofillProfile; | |
| 15 class CreditCard; | |
| 16 } // namespace autofill | |
| 17 | |
| 18 namespace payments { | |
| 19 | |
| 20 class PaymentRequestSpec; | |
| 21 | |
| 22 // Keeps track of the information currently selected by the user and whether the | |
| 23 // user is ready to pay. Uses information from the PaymentRequestSpec, which is | |
| 24 // what the merchant has specified, as input into the "is ready to pay" | |
| 25 // computation. | |
| 26 class PaymentRequestState : public PaymentInstrument::Delegate { | |
| 27 public: | |
| 28 class Observer { | |
| 29 public: | |
| 30 // Called when the information (payment method, address/contact info, | |
| 31 // shipping option) changes. | |
| 32 virtual void OnSelectedInformationChanged() = 0; | |
| 33 | |
| 34 protected: | |
| 35 virtual ~Observer() {} | |
| 36 }; | |
| 37 | |
| 38 class Delegate { | |
| 39 public: | |
| 40 virtual const std::string& GetApplicationLocale() = 0; | |
|
anthonyvd
2017/03/14 16:46:41
Spec will also probably need the Locale, can we ju
Mathieu
2017/03/14 18:05:39
I don't think spec will need the locale... perhaps
anthonyvd
2017/03/14 18:11:24
I'm thinking of the shipping options provided by t
Mathieu
2017/03/15 02:51:14
You're right, I think I'll probably move the Curre
| |
| 41 virtual const std::vector<autofill::AutofillProfile*>& | |
| 42 GetBillingProfiles() = 0; | |
| 43 | |
| 44 virtual void OnPaymentResponseAvailable( | |
| 45 mojom::PaymentResponsePtr response) = 0; | |
| 46 | |
| 47 protected: | |
| 48 virtual ~Delegate() {} | |
| 49 }; | |
| 50 | |
| 51 PaymentRequestState(PaymentRequestSpec* spec, Delegate* delegate); | |
| 52 ~PaymentRequestState() override; | |
| 53 | |
| 54 void AddObserver(Observer* observer); | |
| 55 void RemoveObserver(Observer* observer); | |
| 56 | |
| 57 // PaymentInstrument::Delegate: | |
| 58 void OnInstrumentDetailsReady( | |
| 59 const std::string& method_name, | |
| 60 const std::string& stringified_details) override; | |
| 61 void OnInstrumentDetailsError() override {} | |
| 62 | |
| 63 // Initiates the generation of the PaymentResponse. Callers should check | |
| 64 // |is_ready_to_pay|, which is inexpensive. | |
| 65 void GeneratePaymentResponse(); | |
| 66 | |
| 67 // Gets the Autofill Profile representing the shipping address or contact | |
| 68 // information currently selected for this PaymentRequest flow. Can return | |
| 69 // null. | |
| 70 autofill::AutofillProfile* selected_shipping_profile() const { | |
| 71 return selected_shipping_profile_; | |
| 72 } | |
| 73 autofill::AutofillProfile* selected_contact_profile() const { | |
| 74 return selected_contact_profile_; | |
| 75 } | |
| 76 // Returns the currently selected credit card for this PaymentRequest flow. | |
| 77 // It's not guaranteed to be complete. Returns nullptr if there is no selected | |
| 78 // card. | |
| 79 autofill::CreditCard* selected_credit_card() const { | |
| 80 return selected_credit_card_; | |
| 81 } | |
| 82 mojom::PaymentShippingOption* selected_shipping_option() { | |
| 83 return selected_shipping_option_; | |
| 84 } | |
| 85 | |
| 86 // Setters to change the selected information. Will have the side effect of | |
| 87 // recomputing "is ready to pay" and notify observers. | |
| 88 void SetSelectedShippingProfile(autofill::AutofillProfile* profile); | |
| 89 void SetSelectedContactProfile(autofill::AutofillProfile* profile); | |
| 90 void SetSelectedCreditCard(autofill::CreditCard* card); | |
| 91 // Sets the initial selections and computes is ready to pay state, then | |
| 92 // notifies observers. Multiple arguments can be null. | |
| 93 void SetInitialSelections(autofill::AutofillProfile* shipping, | |
| 94 autofill::AutofillProfile* contact, | |
| 95 autofill::CreditCard* card); | |
| 96 | |
| 97 bool is_ready_to_pay() { return is_ready_to_pay_; } | |
| 98 | |
| 99 private: | |
| 100 // Uses the user-selected information as well as the merchant spec to update | |
| 101 // |is_ready_to_pay_| with the current state, by validating that all the | |
| 102 // required information is available. Will notify observers. | |
| 103 void UpdateIsReadyToPayAndNotifyObservers(); | |
| 104 | |
| 105 // Notifies all observers that selected information has changed. | |
| 106 void NotifyOnSelectedInformationChanged(); | |
| 107 | |
| 108 // Returns whether the selected data satisfies the PaymentDetails requirements | |
| 109 // (payment methods). | |
| 110 bool ArePaymentDetailsSatisfied(); | |
| 111 // Returns whether the selected data satisfies the PaymentOptions requirements | |
| 112 // (contact info, shipping address). | |
| 113 bool ArePaymentOptionsSatisfied(); | |
| 114 | |
| 115 // Updates the selected_shipping_option based on the data passed to this | |
| 116 // payment request by the website. This will set selected_shipping_option_ to | |
| 117 // the last option marked selected in the options array. | |
| 118 void UpdateSelectedShippingOption(); | |
| 119 | |
| 120 bool is_ready_to_pay_; | |
| 121 | |
| 122 PaymentRequestSpec* spec_; | |
| 123 Delegate* delegate_; | |
| 124 | |
| 125 autofill::AutofillProfile* selected_shipping_profile_; | |
| 126 autofill::AutofillProfile* selected_contact_profile_; | |
| 127 autofill::CreditCard* selected_credit_card_; | |
| 128 // This is owned by |details_|, which is owned by this object and lives until | |
| 129 // |this| is destructed so it's safe to keep this raw pointer. | |
| 130 mojom::PaymentShippingOption* selected_shipping_option_; | |
| 131 | |
| 132 std::unique_ptr<PaymentInstrument> selected_payment_instrument_; | |
| 133 | |
| 134 base::ObserverList<Observer> observers_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(PaymentRequestState); | |
| 137 }; | |
| 138 | |
| 139 } // namespace payments | |
| 140 | |
| 141 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_ | |
| OLD | NEW |