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

Unified 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 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..536ec8d523a450d6ce067a1e03d0e55a7b5b96e1
--- /dev/null
+++ b/components/payments/content/payment_request_state.h
@@ -0,0 +1,170 @@
+// 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/autofill/core/browser/personal_data_manager.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:
+ // Any class call add itself as Observer via AddObserver() and receive
+ // notification about the state changing.
+ 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;
+ // Used to get the user's data.
+ virtual autofill::PersonalDataManager* GetPersonalDataManager() = 0;
+ // Called when the PaymentResponse is available.
+ 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_;
+ }
+
+ // Returns the appropriate Autofill Profiles for this user. The profiles
+ // returned are owned by the PaymentRequestState.
+ const std::vector<autofill::AutofillProfile*>& shipping_profiles() {
+ return shipping_profiles_;
+ }
+ const std::vector<autofill::AutofillProfile*>& contact_profiles() {
+ return contact_profiles_;
+ }
+ const std::vector<autofill::CreditCard*>& credit_cards() {
+ return credit_cards_;
+ }
+
+ // 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);
+
+ bool is_ready_to_pay() { return is_ready_to_pay_; }
+
+ private:
+ // Fetches the Autofill Profiles for this user from the PersonalDataManager,
+ // and stores copies of them, owned by this PaymentRequestState, in
+ // profile_cache_.
+ void PopulateProfileCache();
+
+ // Sets the initial selections for credit card and profiles, and notifies
+ // observers.
+ void SetDefaultProfileSelections();
+
+ // 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_;
+
+ // Not owned. Never null. Both outlive this object.
+ PaymentRequestSpec* spec_;
+ Delegate* delegate_;
+
+ autofill::AutofillProfile* selected_shipping_profile_;
+ autofill::AutofillProfile* selected_contact_profile_;
+ autofill::CreditCard* selected_credit_card_;
+ // The shipping options (and thus this pointer) are owned by |spec_| which
+ // outlives this object.
+ mojom::PaymentShippingOption* selected_shipping_option_;
+
+ std::unique_ptr<PaymentInstrument> selected_payment_instrument_;
+
+ // Profiles may change due to (e.g.) sync events, so profiles are cached after
+ // loading and owned here. They are populated once only, and ordered by
+ // frecency.
+ std::vector<std::unique_ptr<autofill::AutofillProfile>> profile_cache_;
+ std::vector<autofill::AutofillProfile*> shipping_profiles_;
+ std::vector<autofill::AutofillProfile*> contact_profiles_;
+ std::vector<std::unique_ptr<autofill::CreditCard>> card_cache_;
+ std::vector<autofill::CreditCard*> credit_cards_;
+
+ base::ObserverList<Observer> observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(PaymentRequestState);
+};
+
+} // namespace payments
+
+#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_STATE_H_
« 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