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

Side by Side Diff: components/payments/content/payment_request_state.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
OLDNEW
(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/autofill/core/browser/personal_data_manager.h"
11 #include "components/payments/content/payment_request.mojom.h"
12 #include "components/payments/core/payment_instrument.h"
13
14 namespace autofill {
15 class AutofillProfile;
16 class CreditCard;
17 } // namespace autofill
18
19 namespace payments {
20
21 class PaymentRequestSpec;
22
23 // Keeps track of the information currently selected by the user and whether the
24 // user is ready to pay. Uses information from the PaymentRequestSpec, which is
25 // what the merchant has specified, as input into the "is ready to pay"
26 // computation.
27 class PaymentRequestState : public PaymentInstrument::Delegate {
28 public:
29 // Any class call add itself as Observer via AddObserver() and receive
30 // notification about the state changing.
31 class Observer {
32 public:
33 // Called when the information (payment method, address/contact info,
34 // shipping option) changes.
35 virtual void OnSelectedInformationChanged() = 0;
36
37 protected:
38 virtual ~Observer() {}
39 };
40
41 class Delegate {
42 public:
43 virtual const std::string& GetApplicationLocale() = 0;
44 // Used to get the user's data.
45 virtual autofill::PersonalDataManager* GetPersonalDataManager() = 0;
46 // Called when the PaymentResponse is available.
47 virtual void OnPaymentResponseAvailable(
48 mojom::PaymentResponsePtr response) = 0;
49
50 protected:
51 virtual ~Delegate() {}
52 };
53
54 PaymentRequestState(PaymentRequestSpec* spec, Delegate* delegate);
55 ~PaymentRequestState() override;
56
57 void AddObserver(Observer* observer);
58 void RemoveObserver(Observer* observer);
59
60 // PaymentInstrument::Delegate:
61 void OnInstrumentDetailsReady(
62 const std::string& method_name,
63 const std::string& stringified_details) override;
64 void OnInstrumentDetailsError() override {}
65
66 // Initiates the generation of the PaymentResponse. Callers should check
67 // |is_ready_to_pay|, which is inexpensive.
68 void GeneratePaymentResponse();
69
70 // Gets the Autofill Profile representing the shipping address or contact
71 // information currently selected for this PaymentRequest flow. Can return
72 // null.
73 autofill::AutofillProfile* selected_shipping_profile() const {
74 return selected_shipping_profile_;
75 }
76 autofill::AutofillProfile* selected_contact_profile() const {
77 return selected_contact_profile_;
78 }
79 // Returns the currently selected credit card for this PaymentRequest flow.
80 // It's not guaranteed to be complete. Returns nullptr if there is no selected
81 // card.
82 autofill::CreditCard* selected_credit_card() const {
83 return selected_credit_card_;
84 }
85 mojom::PaymentShippingOption* selected_shipping_option() {
86 return selected_shipping_option_;
87 }
88
89 // Returns the appropriate Autofill Profiles for this user. The profiles
90 // returned are owned by the PaymentRequestState.
91 const std::vector<autofill::AutofillProfile*>& shipping_profiles() {
92 return shipping_profiles_;
93 }
94 const std::vector<autofill::AutofillProfile*>& contact_profiles() {
95 return contact_profiles_;
96 }
97 const std::vector<autofill::CreditCard*>& credit_cards() {
98 return credit_cards_;
99 }
100
101 // Setters to change the selected information. Will have the side effect of
102 // recomputing "is ready to pay" and notify observers.
103 void SetSelectedShippingProfile(autofill::AutofillProfile* profile);
104 void SetSelectedContactProfile(autofill::AutofillProfile* profile);
105 void SetSelectedCreditCard(autofill::CreditCard* card);
106
107 bool is_ready_to_pay() { return is_ready_to_pay_; }
108
109 private:
110 // Fetches the Autofill Profiles for this user from the PersonalDataManager,
111 // and stores copies of them, owned by this PaymentRequestState, in
112 // profile_cache_.
113 void PopulateProfileCache();
114
115 // Sets the initial selections for credit card and profiles, and notifies
116 // observers.
117 void SetDefaultProfileSelections();
118
119 // Uses the user-selected information as well as the merchant spec to update
120 // |is_ready_to_pay_| with the current state, by validating that all the
121 // required information is available. Will notify observers.
122 void UpdateIsReadyToPayAndNotifyObservers();
123
124 // Notifies all observers that selected information has changed.
125 void NotifyOnSelectedInformationChanged();
126
127 // Returns whether the selected data satisfies the PaymentDetails requirements
128 // (payment methods).
129 bool ArePaymentDetailsSatisfied();
130 // Returns whether the selected data satisfies the PaymentOptions requirements
131 // (contact info, shipping address).
132 bool ArePaymentOptionsSatisfied();
133
134 // Updates the selected_shipping_option based on the data passed to this
135 // payment request by the website. This will set selected_shipping_option_ to
136 // the last option marked selected in the options array.
137 void UpdateSelectedShippingOption();
138
139 bool is_ready_to_pay_;
140
141 // Not owned. Never null. Both outlive this object.
142 PaymentRequestSpec* spec_;
143 Delegate* delegate_;
144
145 autofill::AutofillProfile* selected_shipping_profile_;
146 autofill::AutofillProfile* selected_contact_profile_;
147 autofill::CreditCard* selected_credit_card_;
148 // The shipping options (and thus this pointer) are owned by |spec_| which
149 // outlives this object.
150 mojom::PaymentShippingOption* selected_shipping_option_;
151
152 std::unique_ptr<PaymentInstrument> selected_payment_instrument_;
153
154 // Profiles may change due to (e.g.) sync events, so profiles are cached after
155 // loading and owned here. They are populated once only, and ordered by
156 // frecency.
157 std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_;
158 std::vector<autofill::AutofillProfile*> shipping_profiles_;
159 std::vector<autofill::AutofillProfile*> contact_profiles_;
160 std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_;
161 std::vector<autofill::CreditCard*> credit_cards_;
162
163 base::ObserverList<Observer> observers_;
164
165 DISALLOW_COPY_AND_ASSIGN(PaymentRequestState);
166 };
167
168 } // namespace payments
169
170 #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_
OLDNEW
« no previous file with comments | « components/payments/content/payment_request_spec_unittest.cc ('k') | components/payments/content/payment_request_state.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698