Commit 9380084b authored by anthonyvd's avatar anthonyvd Committed by Commit bot

[WebPayments] Factor out sheet-specific logic in Controllers.

This CL adds a mechanism to isolate sheet-specific code previously in
PaymentRequestDialog to Controller classes. This will allow for clearer
view creation and event handling as the different sheets in the dialog
are implemented.

BUG=667872

Review-Url: https://codereview.chromium.org/2579513002
Cr-Commit-Position: refs/heads/master@{#439141}
parent ea7ca15b
......@@ -1793,8 +1793,15 @@ split_static_library("ui") {
"views/passwords/manage_passwords_bubble_view.h",
"views/passwords/manage_passwords_icon_views.cc",
"views/passwords/manage_passwords_icon_views.h",
"views/payments/order_summary_view_controller.cc",
"views/payments/order_summary_view_controller.h",
"views/payments/payment_request_dialog.cc",
"views/payments/payment_request_dialog.h",
"views/payments/payment_request_sheet_controller.h",
"views/payments/payment_request_views_util.cc",
"views/payments/payment_request_views_util.h",
"views/payments/payment_sheet_view_controller.cc",
"views/payments/payment_sheet_view_controller.h",
"views/payments/view_stack.cc",
"views/payments/view_stack.h",
"views/process_singleton_dialog_linux.cc",
......
// Copyright 2016 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.
#include "chrome/browser/ui/views/payments/order_summary_view_controller.h"
#include <memory>
#include <utility>
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog.h"
#include "chrome/browser/ui/views/payments/payment_request_views_util.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/view.h"
namespace {
// The tag for the button that navigates back to the payment sheet.
constexpr int kBackButtonTag = 0;
} // namespace
namespace payments {
OrderSummaryViewController::OrderSummaryViewController(
PaymentRequestImpl* impl, PaymentRequestDialog* dialog)
: PaymentRequestSheetController(impl, dialog) {}
OrderSummaryViewController::~OrderSummaryViewController() {}
std::unique_ptr<views::View> OrderSummaryViewController::CreateView() {
std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>();
views::GridLayout* layout = new views::GridLayout(content_view.get());
content_view->SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER,
0, views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0);
views::LabelButton* back_button =
views::MdTextButton::CreateSecondaryUiBlueButton(
this, base::ASCIIToUTF16("Back"));
back_button->set_tag(kBackButtonTag);
layout->AddView(back_button);
return payments::CreatePaymentView(
l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_TITLE),
std::move(content_view));
}
void OrderSummaryViewController::ButtonPressed(
views::Button* sender, const ui::Event& event) {
DCHECK_EQ(kBackButtonTag, sender->tag());
dialog()->GoBack();
}
} // namespace payments
// Copyright 2016 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 CHROME_BROWSER_UI_VIEWS_PAYMENTS_ORDER_SUMMARY_VIEW_CONTROLLER_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_ORDER_SUMMARY_VIEW_CONTROLLER_H_
#include "base/macros.h"
#include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h"
#include "ui/views/controls/button/button.h"
namespace payments {
class PaymentRequestImpl;
class PaymentRequestDialog;
// The PaymentRequestSheetController subtype for the Order Summary screen of the
// Payment Request flow.
class OrderSummaryViewController : public PaymentRequestSheetController,
public views::ButtonListener {
public:
// Does not take ownership of the arguments, which should outlive this object.
OrderSummaryViewController(PaymentRequestImpl* impl,
PaymentRequestDialog* dialog);
~OrderSummaryViewController() override;
// PaymentRequestSheetController:
std::unique_ptr<views::View> CreateView() override;
private:
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
DISALLOW_COPY_AND_ASSIGN(OrderSummaryViewController);
};
} // namespace payments
#endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_ORDER_SUMMARY_VIEW_CONTROLLER_H_
......@@ -4,70 +4,34 @@
#include "chrome/browser/ui/views/payments/payment_request_dialog.h"
#include "base/strings/utf_string_conversions.h"
#include <utility>
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/payments/payment_request_impl.h"
#include "chrome/browser/ui/views/payments/order_summary_view_controller.h"
#include "chrome/browser/ui/views/payments/payment_sheet_view_controller.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/grid_layout.h"
namespace {
// The tag for the button that navigates back to the payment sheet.
constexpr int kBackButtonTag = 0;
// The tag for the button that navigates to the Order Summary sheet.
constexpr int kOrderSummaryTag = 1;
std::unique_ptr<views::View> CreateOrderSummaryView(
views::ButtonListener* button_listener) {
std::unique_ptr<views::View> view = base::MakeUnique<views::View>();
views::GridLayout* layout = new views::GridLayout(view.get());
view->SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER,
0, views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0);
layout->AddView(new views::Label(
l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_TITLE)));
layout->StartRow(0, 0);
views::LabelButton* back_button =
views::MdTextButton::CreateSecondaryUiBlueButton(
button_listener, base::ASCIIToUTF16("Back"));
back_button->set_tag(kBackButtonTag);
layout->AddView(back_button);
return view;
}
std::unique_ptr<views::View> CreatePaymentSheetView(
views::ButtonListener* button_listener) {
std::unique_ptr<views::View> view = base::MakeUnique<views::View>();
views::GridLayout* layout = new views::GridLayout(view.get());
view->SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER,
0, views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0);
layout->AddView(new views::Label(
l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_PAYMENT_SHEET_TITLE)));
layout->StartRow(0, 0);
views::LabelButton* order_summary_button =
views::MdTextButton::CreateSecondaryUiBlueButton(
button_listener, base::ASCIIToUTF16("Order Summary"));
order_summary_button->set_tag(kOrderSummaryTag);
layout->AddView(order_summary_button);
// This function creates an instance of a PaymentRequestSheetController
// subclass of concrete type |Controller|, passing it non-owned pointers to
// |dialog| and the |impl| that initiated that dialog. |map| should be owned by
// |dialog|.
template<typename Controller>
std::unique_ptr<views::View> CreateViewAndInstallController(
payments::ControllerMap* map,
payments::PaymentRequestImpl* impl,
payments::PaymentRequestDialog* dialog) {
std::unique_ptr<Controller> controller =
base::MakeUnique<Controller>(impl, dialog);
std::unique_ptr<views::View> view = controller->CreateView();
(*map)[view.get()] = std::move(controller);
return view;
}
......@@ -101,34 +65,42 @@ ui::ModalType PaymentRequestDialog::GetModalType() const {
return ui::MODAL_TYPE_CHILD;
}
gfx::Size PaymentRequestDialog::GetPreferredSize() const {
return gfx::Size(300, 300);
}
bool PaymentRequestDialog::Cancel() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
impl_->Cancel();
return true;
}
void PaymentRequestDialog::ShowInitialPaymentSheet() {
view_stack_.Push(CreatePaymentSheetView(this), false);
void PaymentRequestDialog::GoBack() {
view_stack_.Pop();
}
void PaymentRequestDialog::ShowOrderSummary() {
view_stack_.Push(CreateOrderSummaryView(this), true);
view_stack_.Push(
CreateViewAndInstallController<OrderSummaryViewController>(
&controller_map_, impl_, this),
true);
}
void PaymentRequestDialog::GoBack() {
view_stack_.Pop();
void PaymentRequestDialog::ShowInitialPaymentSheet() {
view_stack_.Push(
CreateViewAndInstallController<PaymentSheetViewController>(
&controller_map_, impl_, this),
false);
}
gfx::Size PaymentRequestDialog::GetPreferredSize() const {
return gfx::Size(450, 450);
}
void PaymentRequestDialog::ButtonPressed(
views::Button* sender, const ui::Event& event) {
if (sender->tag() == kBackButtonTag) {
GoBack();
} else if (sender->tag() == kOrderSummaryTag) {
ShowOrderSummary();
void PaymentRequestDialog::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
// When a view that is associated with a controller is removed from this
// view's descendants, dispose of the controller.
if (!details.is_add &&
controller_map_.find(details.child) != controller_map_.end()) {
DCHECK(!details.move_view);
controller_map_.erase(details.child);
}
}
......
......@@ -5,21 +5,28 @@
#ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_DIALOG_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_DIALOG_H_
#include <map>
#include <memory>
#include "base/macros.h"
#include "chrome/browser/ui/views/payments/view_stack.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/window/dialog_delegate.h"
class ViewStack;
namespace payments {
class PaymentRequestImpl;
class PaymentRequestSheetController;
// Maps views owned by PaymentRequestDialog::view_stack_ to their controller.
// PaymentRequestDialog is responsible for listening for those views being
// removed from the hierarchy and delete the associated controllers.
using ControllerMap =
std::map<views::View*, std::unique_ptr<PaymentRequestSheetController>>;
// The dialog delegate that represents a desktop WebPayments dialog. This class
// is responsible for displaying the view associated with the current state of
// the WebPayments flow and managing the transition between those states.
class PaymentRequestDialog : public views::DialogDelegateView,
public views::ButtonListener {
class PaymentRequestDialog : public views::DialogDelegateView {
public:
explicit PaymentRequestDialog(PaymentRequestImpl* impl);
~PaymentRequestDialog() override;
......@@ -27,25 +34,26 @@ class PaymentRequestDialog : public views::DialogDelegateView,
// views::WidgetDelegate
ui::ModalType GetModalType() const override;
// views::View
gfx::Size GetPreferredSize() const override;
// views::DialogDelegate
bool Cancel() override;
void GoBack();
void ShowOrderSummary();
private:
void ShowInitialPaymentSheet();
void ShowOrderSummary();
void GoBack();
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
// views::View
gfx::Size GetPreferredSize() const override;
void ViewHierarchyChanged(const ViewHierarchyChangedDetails& details)
override;
// Non-owned reference to the PaymentRequestImpl that initiated this dialog.
// Since the PaymentRequestImpl object always outlives this one, the pointer
// should always be valid even though there is no direct ownership
// relationship between the two.
PaymentRequestImpl* impl_;
ControllerMap controller_map_;
ViewStack view_stack_;
DISALLOW_COPY_AND_ASSIGN(PaymentRequestDialog);
......
// Copyright 2016 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 CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_SHEET_CONTROLLER_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_SHEET_CONTROLLER_H_
#include <memory>
#include "base/logging.h"
#include "base/macros.h"
namespace views {
class View;
}
namespace payments {
class PaymentRequestDialog;
class PaymentRequestImpl;
// The base class for objects responsible for the creation and event handling in
// views shown in the PaymentRequestDialog.
class PaymentRequestSheetController {
public:
// Objects of this class are owned by |dialog|, so it's a non-owned pointer
// that should be valid throughout this object's lifetime.
// |impl| is also not owned by this and is guaranteed to outlive dialog.
// Neither |impl| or |dialog| should be null.
PaymentRequestSheetController(PaymentRequestImpl* impl,
PaymentRequestDialog* dialog)
: impl_(impl),
dialog_(dialog) {
DCHECK(impl_);
DCHECK(dialog_);
}
virtual ~PaymentRequestSheetController() {}
virtual std::unique_ptr<views::View> CreateView() = 0;
// The PaymentRequestImpl object associated with this instance of the dialog.
// Caller should not take ownership of the result.
PaymentRequestImpl* impl() { return impl_; }
// The dialog that contains and owns this object.
// Caller should not take ownership of the result.
PaymentRequestDialog* dialog() { return dialog_; }
private:
// Not owned. Will outlive this.
PaymentRequestImpl* impl_;
// Not owned. Will outlive this.
PaymentRequestDialog* dialog_;
DISALLOW_COPY_AND_ASSIGN(PaymentRequestSheetController);
};
} // namespace payments
#endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_SHEET_CONTROLLER_H_
// Copyright 2016 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.
#include "chrome/browser/ui/views/payments/payment_request_views_util.h"
#include "base/memory/ptr_util.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/views/background.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/view.h"
namespace payments {
std::unique_ptr<views::View> CreatePaymentView(
const base::string16& title, std::unique_ptr<views::View> content_view) {
std::unique_ptr<views::View> view = base::MakeUnique<views::View>();
view->set_background(views::Background::CreateSolidBackground(SK_ColorWHITE));
// Paint the sheets to layers, otherwise the MD buttons (which do paint to a
// layer) won't do proper clipping.
view->SetPaintToLayer(true);
views::GridLayout* layout = new views::GridLayout(view.get());
view->SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
1, views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0);
layout->AddView(new views::Label(title));
layout->StartRow(0, 0);
// |content_view| will be deleted when |view| is.
layout->AddView(content_view.release());
return view;
}
} // namespace payments
// Copyright 2016 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 CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_VIEWS_UTIL_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_VIEWS_UTIL_H_
#include <memory>
#include "base/strings/string16.h"
namespace views {
class View;
}
namespace payments {
// Creates a view to be displayed in the PaymentRequestDialog. |title| is the
// text displayed on top of the dialog and |content_view| is displayed between
// the title and the pay/cancel buttons. The returned view takes ownership of
// |content_view|.
std::unique_ptr<views::View> CreatePaymentView(
const base::string16& title, std::unique_ptr<views::View> content_view);
} // namespace payments
#endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_VIEWS_UTIL_H_
// Copyright 2016 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.
#include "chrome/browser/ui/views/payments/payment_sheet_view_controller.h"
#include <memory>
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog.h"
#include "chrome/browser/ui/views/payments/payment_request_views_util.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/view.h"
namespace {
// The tag for the button that navigates to the Order Summary sheet.
constexpr int kOrderSummaryTag = 0;
} // namespace
namespace payments {
PaymentSheetViewController::PaymentSheetViewController(
PaymentRequestImpl* impl, PaymentRequestDialog* dialog)
: PaymentRequestSheetController(impl, dialog) {}
PaymentSheetViewController::~PaymentSheetViewController() {}
std::unique_ptr<views::View> PaymentSheetViewController::CreateView() {
std::unique_ptr<views::View> content_view = base::MakeUnique<views::View>();
views::GridLayout* layout = new views::GridLayout(content_view.get());
content_view->SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER,
0, views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0);
views::LabelButton* order_summary_button =
views::MdTextButton::CreateSecondaryUiBlueButton(
this, base::ASCIIToUTF16("Order Summary"));
order_summary_button->set_tag(kOrderSummaryTag);
layout->AddView(order_summary_button);
return payments::CreatePaymentView(
l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_PAYMENT_SHEET_TITLE),
std::move(content_view));
}
void PaymentSheetViewController::ButtonPressed(
views::Button* sender, const ui::Event& event) {
DCHECK_EQ(kOrderSummaryTag, sender->tag());
dialog()->ShowOrderSummary();
}
} // namespace payments
// Copyright 2016 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 CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_SHEET_VIEW_CONTROLLER_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_SHEET_VIEW_CONTROLLER_H_
#include "base/macros.h"
#include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h"
#include "ui/views/controls/button/button.h"
namespace payments {
class PaymentRequestImpl;
class PaymentRequestDialog;
// The PaymentRequestSheetController subtype for the Payment Sheet screen of the
// Payment Request dialog.
class PaymentSheetViewController : public PaymentRequestSheetController,
public views::ButtonListener {
public:
// Does not take ownership of the arguments, which should outlive this object.
PaymentSheetViewController(PaymentRequestImpl* impl,
PaymentRequestDialog* dialog);
~PaymentSheetViewController() override;
// PaymentRequestSheetController:
std::unique_ptr<views::View> CreateView() override;
private:
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
DISALLOW_COPY_AND_ASSIGN(PaymentSheetViewController);
};
} // namespace payments
#endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_SHEET_VIEW_CONTROLLER_H_
......@@ -12,6 +12,11 @@ ViewStack::ViewStack()
SetLayoutManager(new views::FillLayout());
slide_out_animator_->AddObserver(this);
// Paint to a layer and Mask to Bounds, otherwise descendant views that paint
// to a layer themselves will still paint while they're being animated out and
// are out of bounds of their parent.
SetPaintToLayer(true);
layer()->SetMasksToBounds(true);
}
ViewStack::~ViewStack() {}
......@@ -19,6 +24,7 @@ ViewStack::~ViewStack() {}
void ViewStack::Push(std::unique_ptr<views::View> view, bool animate) {
gfx::Rect destination = bounds();
destination.set_origin(gfx::Point(0, 0));
if (animate) {
// First add the new view out of bounds since it'll slide in from right to
// left.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment