File: authenticator_request_sheet_view.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (323 lines) | stat: -rw-r--r-- 13,118 bytes parent folder | download | duplicates (5)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2018 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/webauthn/authenticator_request_sheet_view.h"

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/memory/scoped_refptr.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "cc/paint/skottie_wrapper.h"
#include "chrome/browser/ui/views/accessibility/non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/webauthn/authenticator_request_sheet_model.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/platform/ax_platform.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/text_constants.h"
#include "ui/lottie/animation.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/animated_image_view.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/progress_bar.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/typography.h"
#include "ui/views/view_class_properties.h"
namespace {

// Margins around any illustration.
constexpr int kImageMarginTop = 22;
constexpr int kImageMarginBottom = 2;

template <typename T>
void ConfigureHeaderIllustration(T* illustration, gfx::Size header_size) {
  illustration->SetBorder(views::CreateEmptyBorder(
      gfx::Insets::TLBR(kImageMarginTop, 0, kImageMarginBottom, 0)));
  illustration->SetSize(header_size);
  illustration->SetVerticalAlignment(views::ImageView::Alignment::kLeading);
}

}  // namespace

using views::BoxLayout;

AuthenticatorRequestSheetView::AuthenticatorRequestSheetView(
    std::unique_ptr<AuthenticatorRequestSheetModel> model)
    : model_(std::move(model)) {}

AuthenticatorRequestSheetView::~AuthenticatorRequestSheetView() = default;

void AuthenticatorRequestSheetView::ReInitChildViews() {
  child_views_ = ChildViews();
  RemoveAllChildViews();

  if (model()->IsActivityIndicatorVisible()) {
    constexpr int kActivityIndicatorHeight = 4;
    auto activity_indicator = std::make_unique<views::ProgressBar>();
    activity_indicator->SetPreferredHeight(kActivityIndicatorHeight);
    activity_indicator->SetPreferredCornerRadii(std::nullopt);
    activity_indicator->SetValue(-1 /* infinite animation */);
    activity_indicator->SetBackgroundColor(SK_ColorTRANSPARENT);
    activity_indicator->SetPreferredSize(
        gfx::Size(ChromeLayoutProvider::Get()->GetDistanceMetric(
                      views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH),
                  kActivityIndicatorHeight));
    activity_indicator->SizeToPreferredSize();
    // The indicator is positioned absolutely at the top of the dialog.
    activity_indicator->SetProperty(views::kViewIgnoredByLayoutKey, true);
    AddChildView(std::move(activity_indicator));
  }

  // No need to add further spacing between the upper and lower half. The image
  // is designed to fill the dialog's top half without any border/margins, and
  // the |lower_half| will already contain the standard dialog borders.
  SetLayoutManager(std::make_unique<BoxLayout>(
      BoxLayout::Orientation::kVertical, gfx::Insets(),
      0 /* between_child_spacing */));

  std::unique_ptr<views::View> upper_half = CreateIllustrationWithOverlays();
  std::unique_ptr<views::View> lower_half = CreateContentsBelowIllustration();
  AddChildView(std::move(upper_half));
  AddChildView(std::move(lower_half));
  InvalidateLayout();
}

views::View* AuthenticatorRequestSheetView::GetInitiallyFocusedView() {
  if (should_focus_step_specific_content_ == AutoFocus::kYes) {
    return child_views_.step_specific_content_;
  }
  if (ui::AXPlatform::GetInstance().IsScreenReaderActive()) {
    // Focus the title label if a screen reader is detected to nudge it to
    // announce the title when the sheet changes.
    return child_views_.title_label_;
  }
  return nullptr;
}

std::unique_ptr<views::View>
AuthenticatorRequestSheetView::BuildStepSpecificHeader() {
  return nullptr;
}

std::pair<std::unique_ptr<views::View>,
          AuthenticatorRequestSheetView::AutoFocus>
AuthenticatorRequestSheetView::BuildStepSpecificContent() {
  return std::make_pair(nullptr, AutoFocus::kNo);
}

int AuthenticatorRequestSheetView::GetSpacingBetweenTitleAndDescription() {
  return views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_RELATED_CONTROL_VERTICAL);
}

std::unique_ptr<views::View>
AuthenticatorRequestSheetView::CreateIllustrationWithOverlays() {
  constexpr int kImageHeight = 112;
  constexpr int kHeaderHeight =
      kImageHeight + kImageMarginTop + kImageMarginBottom;
  const int dialog_width = ChromeLayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH);
  const gfx::Size header_size(dialog_width, kHeaderHeight);

  // The actual illustration image is set in `UpdateIconImageFromModel`, below,
  // because it's not until that point that we know whether the light or dark
  // illustration should be used.
  std::unique_ptr<View> illustration;
  if (model()->lottie_illustrations()) {
    auto animation = std::make_unique<views::AnimatedImageView>();
    // `AnimatedImageView` will horizontally center if the width is larger than
    // the size from the Lottie file, but the height is just used to truncate
    // the image, so that is disabled with a very large value.
    animation->SetPreferredSize(gfx::Size(dialog_width, 9999));
    ConfigureHeaderIllustration(animation.get(), header_size);
    child_views_.step_illustration_animation_ = animation.get();
    illustration = std::move(animation);
  } else if (model()->vector_illustrations()) {
    auto image_view = std::make_unique<NonAccessibleImageView>();
    ConfigureHeaderIllustration(image_view.get(), header_size);
    child_views_.step_illustration_image_ = image_view.get();
    illustration = std::move(image_view);
  } else {
    return std::make_unique<views::View>();
  }

  // The container view has no layout, so its preferred size is hardcoded to
  // match the size of the header, and all overlays are absolutely positioned.
  auto header_view = std::make_unique<views::View>();
  header_view->SetPreferredSize(header_size);
  header_view->AddChildView(std::move(illustration));

  if (GetWidget()) {
    UpdateIconImageFromModel();
  }

  return header_view;
}

std::unique_ptr<views::View>
AuthenticatorRequestSheetView::CreateContentsBelowIllustration() {
  auto contents = std::make_unique<views::View>();
  contents->SetLayoutManager(std::make_unique<BoxLayout>(
      BoxLayout::Orientation::kVertical, gfx::Insets(),
      views::LayoutProvider::Get()->GetDistanceMetric(
          views::DISTANCE_UNRELATED_CONTROL_VERTICAL)));

  contents->SetBorder(views::CreateEmptyBorder(
      views::LayoutProvider::Get()->GetDialogInsetsForContentType(
          views::DialogContentType::kControl,
          views::DialogContentType::kControl)));

  std::unique_ptr<views::View> step_specific_header = BuildStepSpecificHeader();
  if (step_specific_header) {
    child_views_.step_specific_header_ =
        contents->AddChildView(std::move(step_specific_header));

    if (model()->lottie_illustrations() || model()->vector_illustrations()) {
      auto insets = contents->GetBorder()->GetInsets();
      insets.set_top(0);
      contents->SetBorder(views::CreateEmptyBorder(insets));
    }
  }

  // GPM PIN dialogs have a different spacing, 4px.
  auto label_container = std::make_unique<views::View>();
  label_container->SetLayoutManager(std::make_unique<BoxLayout>(
      BoxLayout::Orientation::kVertical, gfx::Insets(),
      GetSpacingBetweenTitleAndDescription()));

  std::unique_ptr<views::View> step_specific_content;
  // Compute `should_focus_step_specific_content_` before setting `title_label`
  // so that the focus behavior of the `title_label` is set correctly.
  std::tie(step_specific_content, should_focus_step_specific_content_) =
      BuildStepSpecificContent();
  DCHECK(should_focus_step_specific_content_ == AutoFocus::kNo ||
         step_specific_content);

  const std::u16string title = model()->GetStepTitle();
  if (!title.empty()) {
    auto title_label = std::make_unique<views::Label>(
        title, views::style::CONTEXT_DIALOG_TITLE,
        views::style::STYLE_HEADLINE_4);
    title_label->SetMultiLine(true);
    title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    title_label->GetViewAccessibility().SetRole(ax::mojom::Role::kHeading);
    title_label->SetAllowCharacterBreak(true);
    if (ui::AXPlatform::GetInstance().IsScreenReaderActive() &&
        should_focus_step_specific_content_ == AutoFocus::kNo) {
      title_label->SetFocusBehavior(FocusBehavior::ALWAYS);
    }
    child_views_.title_label_ =
        label_container->AddChildView(std::move(title_label));
  }

  std::u16string description = model()->GetStepDescription();
  if (!description.empty()) {
    auto description_label = std::make_unique<views::Label>(
        std::move(description), views::style::CONTEXT_DIALOG_BODY_TEXT);
    description_label->SetMultiLine(true);
    description_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    description_label->SetAllowCharacterBreak(true);
    label_container->AddChildView(std::move(description_label));
  }

  for (const std::u16string& msg : model()->GetAdditionalDescriptions()) {
    if (msg.empty()) {
      continue;
    }
    auto label = std::make_unique<views::Label>(
        msg, views::style::CONTEXT_DIALOG_BODY_TEXT);
    label->SetMultiLine(true);
    label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    label->SetAllowCharacterBreak(true);
    label_container->AddChildView(std::move(label));
  }

  contents->AddChildView(std::move(label_container));

  auto content_error_and_hint_view = std::make_unique<views::View>();
  BoxLayout* content_error_and_hint_layout =
      content_error_and_hint_view->SetLayoutManager(std::make_unique<BoxLayout>(
          BoxLayout::Orientation::kVertical, gfx::Insets(),
          views::LayoutProvider::Get()->GetDistanceMetric(
              views::DISTANCE_RELATED_CONTROL_VERTICAL)));

  if (step_specific_content) {
    child_views_.step_specific_content_ =
        content_error_and_hint_view->AddChildView(
            std::move(step_specific_content));
    content_error_and_hint_layout->SetFlexForView(
        child_views_.step_specific_content_, 1);
  }

  std::u16string error = model()->GetError();
  if (!error.empty()) {
    auto error_label = std::make_unique<views::Label>(
        std::move(error), views::style::CONTEXT_LABEL, STYLE_RED);
    error_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    error_label->SetMultiLine(true);
    child_views_.error_label_ =
        content_error_and_hint_view->AddChildView(std::move(error_label));
  }

  std::u16string hint = model()->GetHint();
  if (!hint.empty()) {
    auto hint_label = std::make_unique<views::Label>(
        std::move(hint), views::style::CONTEXT_LABEL, views::style::STYLE_HINT);
    hint_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    child_views_.hint_label_ =
        content_error_and_hint_view->AddChildView(std::move(hint_label));
  }

  contents->AddChildView(std::move(content_error_and_hint_view));
  return contents;
}

void AuthenticatorRequestSheetView::OnThemeChanged() {
  views::View::OnThemeChanged();
  UpdateIconImageFromModel();
}

void AuthenticatorRequestSheetView::UpdateIconImageFromModel() {
  const bool is_dark = color_utils::IsDark(
      GetColorProvider()->GetColor(ui::kColorDialogBackground));
  if (child_views_.step_illustration_image_) {
    child_views_.step_illustration_image_->SetImage(
        ui::ImageModel::FromVectorIcon(
            model()->vector_illustrations()->get(is_dark),
            gfx::kPlaceholderColor));
  } else if (child_views_.step_illustration_animation_) {
    const int lottie_id = model()->lottie_illustrations()->get(is_dark);
    std::optional<std::vector<uint8_t>> lottie_bytes =
        ui::ResourceBundle::GetSharedInstance().GetLottieData(lottie_id);
    scoped_refptr<cc::SkottieWrapper> skottie =
        cc::SkottieWrapper::UnsafeCreateSerializable(std::move(*lottie_bytes));
    child_views_.step_illustration_animation_->SetAnimatedImage(
        std::make_unique<lottie::Animation>(skottie));
    child_views_.step_illustration_animation_->SizeToPreferredSize();
    child_views_.step_illustration_animation_->Play();
  }
}

BEGIN_METADATA(AuthenticatorRequestSheetView)
END_METADATA