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

Unified Diff: components/payments/content/payment_request_state.h

Issue 2742813004: [Payments] Refactor into PaymentRequestState and Spec (Closed)
Patch Set: another set of tests (state) 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 side-by-side diff with in-line comments
Download patch
Index: components/payments/content/payment_request_state.h
diff --git a/components/payments/content/payment_request_state.h b/components/payments/content/payment_request_state.h
new file mode 100644
index 0000000000000000000000000000000000000000..5c737c52538c79de58bfc3e0da00309775cb2060
--- /dev/null
+++ b/components/payments/content/payment_request_state.h
@@ -0,0 +1,141 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_
+#define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_
+
+#include "base/macros.h"
+#include "base/observer_list.h"
+#include "components/payments/content/payment_request.mojom.h"
+#include "components/payments/core/payment_instrument.h"
+
+namespace autofill {
+class AutofillProfile;
+class CreditCard;
+} // namespace autofill
+
+namespace payments {
+
+class PaymentRequestSpec;
+
+// Keeps track of the information currently selected by the user and whether the
+// user is ready to pay. Uses information from the PaymentRequestSpec, which is
+// what the merchant has specified, as input into the "is ready to pay"
+// computation.
+class PaymentRequestState : public PaymentInstrument::Delegate {
+ public:
+ class Observer {
+ public:
+ // Called when the information (payment method, address/contact info,
+ // shipping option) changes.
+ virtual void OnSelectedInformationChanged() = 0;
+
+ protected:
+ virtual ~Observer() {}
+ };
+
+ class Delegate {
+ public:
+ 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
+ virtual const std::vector<autofill::AutofillProfile*>&
+ GetBillingProfiles() = 0;
+
+ virtual void OnPaymentResponseAvailable(
+ mojom::PaymentResponsePtr response) = 0;
+
+ protected:
+ virtual ~Delegate() {}
+ };
+
+ PaymentRequestState(PaymentRequestSpec* spec, Delegate* delegate);
+ ~PaymentRequestState() override;
+
+ void AddObserver(Observer* observer);
+ void RemoveObserver(Observer* observer);
+
+ // PaymentInstrument::Delegate:
+ void OnInstrumentDetailsReady(
+ const std::string& method_name,
+ const std::string& stringified_details) override;
+ void OnInstrumentDetailsError() override {}
+
+ // Initiates the generation of the PaymentResponse. Callers should check
+ // |is_ready_to_pay|, which is inexpensive.
+ void GeneratePaymentResponse();
+
+ // Gets the Autofill Profile representing the shipping address or contact
+ // information currently selected for this PaymentRequest flow. Can return
+ // null.
+ autofill::AutofillProfile* selected_shipping_profile() const {
+ return selected_shipping_profile_;
+ }
+ autofill::AutofillProfile* selected_contact_profile() const {
+ return selected_contact_profile_;
+ }
+ // Returns the currently selected credit card for this PaymentRequest flow.
+ // It's not guaranteed to be complete. Returns nullptr if there is no selected
+ // card.
+ autofill::CreditCard* selected_credit_card() const {
+ return selected_credit_card_;
+ }
+ mojom::PaymentShippingOption* selected_shipping_option() {
+ return selected_shipping_option_;
+ }
+
+ // Setters to change the selected information. Will have the side effect of
+ // recomputing "is ready to pay" and notify observers.
+ void SetSelectedShippingProfile(autofill::AutofillProfile* profile);
+ void SetSelectedContactProfile(autofill::AutofillProfile* profile);
+ void SetSelectedCreditCard(autofill::CreditCard* card);
+ // Sets the initial selections and computes is ready to pay state, then
+ // notifies observers. Multiple arguments can be null.
+ void SetInitialSelections(autofill::AutofillProfile* shipping,
+ autofill::AutofillProfile* contact,
+ autofill::CreditCard* card);
+
+ bool is_ready_to_pay() { return is_ready_to_pay_; }
+
+ private:
+ // Uses the user-selected information as well as the merchant spec to update
+ // |is_ready_to_pay_| with the current state, by validating that all the
+ // required information is available. Will notify observers.
+ void UpdateIsReadyToPayAndNotifyObservers();
+
+ // Notifies all observers that selected information has changed.
+ void NotifyOnSelectedInformationChanged();
+
+ // Returns whether the selected data satisfies the PaymentDetails requirements
+ // (payment methods).
+ bool ArePaymentDetailsSatisfied();
+ // Returns whether the selected data satisfies the PaymentOptions requirements
+ // (contact info, shipping address).
+ bool ArePaymentOptionsSatisfied();
+
+ // Updates the selected_shipping_option based on the data passed to this
+ // payment request by the website. This will set selected_shipping_option_ to
+ // the last option marked selected in the options array.
+ void UpdateSelectedShippingOption();
+
+ bool is_ready_to_pay_;
+
+ PaymentRequestSpec* spec_;
+ Delegate* delegate_;
+
+ autofill::AutofillProfile* selected_shipping_profile_;
+ autofill::AutofillProfile* selected_contact_profile_;
+ autofill::CreditCard* selected_credit_card_;
+ // This is owned by |details_|, which is owned by this object and lives until
+ // |this| is destructed so it's safe to keep this raw pointer.
+ mojom::PaymentShippingOption* selected_shipping_option_;
+
+ std::unique_ptr<PaymentInstrument> selected_payment_instrument_;
+
+ base::ObserverList<Observer> observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(PaymentRequestState);
+};
+
+} // namespace payments
+
+#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_

Powered by Google App Engine
This is Rietveld 408576698