File: autofill_progress_dialog_views.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (253 lines) | stat: -rw-r--r-- 10,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2021 The Chromium Authors
// 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/autofill/payments/autofill_progress_dialog_views.h"

#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/ui/autofill/payments/payments_ui_constants.h"
#include "chrome/browser/ui/autofill/payments/payments_view_factory.h"
#include "chrome/browser/ui/tabs/public/tab_dialog_manager.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/autofill/payments/payments_view_util.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/ui/payments/autofill_error_dialog_view.h"
#include "components/autofill/core/browser/ui/payments/autofill_progress_dialog_controller.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/tabs/public/tab_interface.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/style/typography.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"

namespace autofill {

namespace {

// This class owns the Widget which owns the view AutofillProgressDialogViews.
class AutofillProgressDialogViewImpl
    : public AutofillProgressDialogViewDesktop {
 public:
  AutofillProgressDialogViewImpl(
      base::WeakPtr<AutofillProgressDialogController> controller,
      content::WebContents* web_contents);
  AutofillProgressDialogViewImpl(const AutofillProgressDialogViewImpl&) =
      delete;
  AutofillProgressDialogViewImpl& operator=(
      const AutofillProgressDialogViewImpl&) = delete;
  ~AutofillProgressDialogViewImpl() override = default;

  // AutofillProgressDialogViewDesktop:
  views::Widget* GetWidgetForTesting() override;
  void CancelDialogForTesting() override;
  void Dismiss(bool show_confirmation_before_closing,
               bool is_canceled_by_user) override;
  void InvalidateControllerForCallbacks() override;
  base::WeakPtr<AutofillProgressDialogView> GetWeakPtr() override;

 private:
  void CloseWidget(views::Widget::ClosedReason closed_reason);

  std::unique_ptr<views::Widget> dialog_;
};

AutofillProgressDialogViewImpl::AutofillProgressDialogViewImpl(
    base::WeakPtr<AutofillProgressDialogController> controller,
    content::WebContents* web_contents) {
  auto autofill_progress_dialog_view =
      std::make_unique<AutofillProgressDialogViews>(controller);
  auto* tab_interface = tabs::TabInterface::GetFromContents(web_contents);
  CHECK(tab_interface);
  dialog_ = tab_interface->GetTabFeatures()
                ->tab_dialog_manager()
                ->CreateShowDialogAndBlockTabInteraction(
                    autofill_progress_dialog_view.release());
  dialog_->MakeCloseSynchronous(base::BindOnce(
      &AutofillProgressDialogViewImpl::CloseWidget, base::Unretained(this)));
}

void AutofillProgressDialogViewImpl::Dismiss(
    bool show_confirmation_before_closing,
    bool is_canceled_by_user) {
  auto* autofill_progress_dialog_view =
      AsViewClass<AutofillProgressDialogViews>(
          dialog_->GetClientContentsView());
  CHECK(autofill_progress_dialog_view);
  autofill_progress_dialog_view->Dismiss(show_confirmation_before_closing,
                                         is_canceled_by_user);
}

void AutofillProgressDialogViewImpl::InvalidateControllerForCallbacks() {}

base::WeakPtr<AutofillProgressDialogView>
AutofillProgressDialogViewImpl::GetWeakPtr() {
  return nullptr;
}

views::Widget* AutofillProgressDialogViewImpl::GetWidgetForTesting() {
  return dialog_.get();
}

void AutofillProgressDialogViewImpl::CancelDialogForTesting() {
  AsViewClass<AutofillProgressDialogViews>(dialog_->GetClientContentsView())
      ->CancelDialog();
}

void AutofillProgressDialogViewImpl::CloseWidget(
    views::Widget::ClosedReason closed_reason) {
  auto* autofill_progress_dialog_view =
      AsViewClass<AutofillProgressDialogViews>(
          dialog_->GetClientContentsView());
  CHECK(autofill_progress_dialog_view);
  // The following call will result in the destruction of this and, indirectly,
  // the dialog_. Do not access this after the following call.
  autofill_progress_dialog_view->controller()->OnDismissed(
      autofill_progress_dialog_view->is_canceled_by_user() ||
      closed_reason == views::Widget::ClosedReason::kUnspecified ||
      closed_reason == views::Widget::ClosedReason::kCancelButtonClicked);
}

}  // namespace

AutofillProgressDialogViews::AutofillProgressDialogViews(
    base::WeakPtr<AutofillProgressDialogController> controller)
    : controller_(controller) {
  // Set the ownership of the delegate, not the View. The View is owned by the
  // Widget as a child view.
  // TODO(crbug.com/338254375): Remove the following line once this is the
  // default state for widgets.
  SetOwnershipOfNewWidget(views::Widget::InitParams::CLIENT_OWNS_WIDGET);

  SetButtons(static_cast<int>(ui::mojom::DialogButton::kCancel));
  SetButtonLabel(ui::mojom::DialogButton::kCancel,
                 controller_->GetCancelButtonLabel());
  SetCancelCallback(base::BindOnce(
      &AutofillProgressDialogViews::OnDialogCanceled, base::Unretained(this)));
  SetModalType(ui::mojom::ModalType::kChild);
  set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH));
  SetShowCloseButton(false);
  set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
      views::DialogContentType::kControl, views::DialogContentType::kControl));

  auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
      ChromeLayoutProvider::Get()->GetDistanceMetric(
          views::DISTANCE_RELATED_CONTROL_HORIZONTAL)));
  layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kStart);
  layout->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kCenter);

  auto* throbber_container =
      AddChildView(std::make_unique<views::BoxLayoutView>());
  progress_throbber_ =
      throbber_container->AddChildView(std::make_unique<views::Throbber>());

  label_ = AddChildView(std::make_unique<views::Label>(
      controller_->GetLoadingMessage(), views::style::CONTEXT_DIALOG_BODY_TEXT,
      views::style::STYLE_SECONDARY));
  label_->SetMultiLine(true);
  label_->SetEnabledColor(ui::kColorThrobber);
  // Set the maximum width of the label view so it will not compress the
  // throbber view.
  label_->SetMaximumWidth(views::LayoutProvider::Get()->GetDistanceMetric(
                              views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH) -
                          progress_throbber_->GetPreferredSize().width() -
                          ChromeLayoutProvider::Get()->GetDistanceMetric(
                              views::DISTANCE_RELATED_CONTROL_HORIZONTAL) -
                          margins().width());

  // Center-align the throbber vertically with the first line of the label.
  progress_throbber_->SetProperty(
      views::kMarginsKey,
      gfx::Insets().set_top((label_->GetLineHeight() -
                             progress_throbber_->GetPreferredSize().height()) /
                            2));
}

AutofillProgressDialogViews::~AutofillProgressDialogViews() = default;

void AutofillProgressDialogViews::Dismiss(bool show_confirmation_before_closing,
                                          bool is_canceled_by_user) {
  is_canceled_by_user_ = is_canceled_by_user;

  // If `show_confirmation_before_closing` is true, show the confirmation and
  // close the widget with a delay. `show_confirmation_before_closing` being
  // true implies that the user did not cancel the dialog, as it is only set to
  // true once this step in the current flow is completed without any user
  // interaction.
  if (show_confirmation_before_closing && controller_) {
    progress_throbber_->Stop();
    label_->SetText(controller_->GetConfirmationMessage());
    // For accessibility consideration, announce the confirmation message when
    // unmasking is finished.
    label_->GetViewAccessibility().AnnouncePolitely(
        controller_->GetConfirmationMessage());
    progress_throbber_->SetChecked(true);
    GetBubbleFrameView()->SetTitleView(
        std::make_unique<TitleWithIconAfterLabelView>(
            controller_->GetConfirmationTitle(),
            TitleWithIconAfterLabelView::Icon::GOOGLE_PAY));
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce(&AutofillProgressDialogViews::CloseWidget,
                       weak_ptr_factory_.GetWeakPtr()),
        kDelayBeforeDismissingProgressDialog);
    return;
  }

  // Otherwise close the widget directly.
  CloseWidget();
}

void AutofillProgressDialogViews::InvalidateControllerForCallbacks() {
  controller_ = nullptr;
}

void AutofillProgressDialogViews::AddedToWidget() {
  DCHECK(progress_throbber_);
  progress_throbber_->Start();

  GetBubbleFrameView()->SetTitleView(
      std::make_unique<TitleWithIconAfterLabelView>(
          GetWindowTitle(), TitleWithIconAfterLabelView::Icon::GOOGLE_PAY));
}

std::u16string AutofillProgressDialogViews::GetWindowTitle() const {
  return controller_ ? controller_->GetLoadingTitle() : u"";
}

void AutofillProgressDialogViews::CloseWidget() {
  GetWidget()->CloseWithReason(
      is_canceled_by_user_ ? views::Widget::ClosedReason::kCancelButtonClicked
                           : views::Widget::ClosedReason::kAcceptButtonClicked);
}

void AutofillProgressDialogViews::OnDialogCanceled() {
  is_canceled_by_user_ = true;
}

std::unique_ptr<AutofillProgressDialogView> CreateAndShowProgressDialog(
    base::WeakPtr<AutofillProgressDialogController> controller,
    content::WebContents* web_contents) {
  return std::make_unique<AutofillProgressDialogViewImpl>(controller,
                                                          web_contents);
}

BEGIN_METADATA(AutofillProgressDialogViews)
END_METADATA

}  // namespace autofill