File: content_setting_image_view.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (372 lines) | stat: -rw-r--r-- 13,385 bytes parent folder | download | duplicates (7)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2012 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/location_bar/content_setting_image_view.h"

#include <cstddef>
#include <optional>
#include <string>
#include <utility>

#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/strings/utf_string_conversions.h"
#include "base/token.h"
#include "build/build_config.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
#include "chrome/browser/ui/content_settings/content_setting_image_model.h"
#include "chrome/browser/ui/user_education/browser_user_education_interface.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/content_setting_bubble_contents.h"
#include "chrome/browser/user_education/user_education_service.h"
#include "chrome/browser/user_education/user_education_service_factory.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/grit/generated_resources.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/strings/grit/components_strings.h"
#include "components/user_education/common/feature_promo/feature_promo_controller.h"
#include "components/user_education/common/feature_promo/feature_promo_specification.h"
#include "components/user_education/common/help_bubble/help_bubble_params.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/theme_provider.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"

namespace {

std::optional<ViewID> GetViewID(
    ContentSettingImageModel::ImageType image_type) {
  using ImageType = ContentSettingImageModel::ImageType;
  switch (image_type) {
    case ImageType::JAVASCRIPT:
      return ViewID::VIEW_ID_CONTENT_SETTING_JAVASCRIPT;

    case ImageType::POPUPS:
      return ViewID::VIEW_ID_CONTENT_SETTING_POPUP;

    case ImageType::COOKIES:
    case ImageType::IMAGES:
    case ImageType::GEOLOCATION:
    case ImageType::MIXEDSCRIPT:
    case ImageType::PROTOCOL_HANDLERS:
    case ImageType::MEDIASTREAM:
    case ImageType::ADS:
    case ImageType::AUTOMATIC_DOWNLOADS:
    case ImageType::MIDI_SYSEX:
    case ImageType::SOUND:
    case ImageType::FRAMEBUST:
    case ImageType::CLIPBOARD_READ_WRITE:
    case ImageType::SENSORS:
    case ImageType::NOTIFICATIONS:
    case ImageType::STORAGE_ACCESS:
#if BUILDFLAG(IS_CHROMEOS)
    case ImageType::SMART_CARD:
#endif
      return std::nullopt;

    case ImageType::NUM_IMAGE_TYPES:
      break;
  }
  NOTREACHED();
}

}  // namespace

DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ContentSettingImageView,
                                      kMediaActivityIndicatorElementId);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ContentSettingImageView,
                                      kMidiSysexActivityIndicatorElementId);

ContentSettingImageView::ContentSettingImageView(
    std::unique_ptr<ContentSettingImageModel> image_model,
    IconLabelBubbleView::Delegate* parent_delegate,
    Delegate* delegate,
    Browser* browser,
    const gfx::FontList& font_list)
    : IconLabelBubbleView(font_list, parent_delegate),
      delegate_(delegate),
      content_setting_image_model_(std::move(image_model)),
      bubble_view_(nullptr),
      browser_(browser) {
  DCHECK(delegate_);
  SetUpForInOutAnimation();
  image_container_view()->SetFlipCanvasOnPaintForRTLUI(true);

  UpdateElementIdentifier();

  std::optional<ViewID> view_id =
      GetViewID(content_setting_image_model_->image_type());
  if (view_id) {
    SetID(*view_id);
  }

  // Because this view is focusable, it should always have an accessible name,
  // even if an announcement is not to be made.
  // TODO(crbug.com/40890218): `IconLabelBubbleView::GetAccessibleNodeData`
  // would set the name to explicitly empty when the name was missing.
  // That function no longer exists. As a result we need to handle that here.
  // There appear to be cases in which `Update` is never called and we lack
  // an announcement string ID during construction. As a result, this view
  // will lack an accessible name and the paint checks will fail. Shouldn't
  // this view always have an accessible name? If not, should it be pruned
  // from the accessibility tree when it lacks one?
  const std::u16string& accessible_name =
      content_setting_image_model_->AccessibilityAnnouncementStringId()
          ? l10n_util::GetStringUTF16(content_setting_image_model_
                                          ->AccessibilityAnnouncementStringId())
          : std::u16string();

  GetViewAccessibility().SetName(
      accessible_name, accessible_name.empty()
                           ? ax::mojom::NameFrom::kAttributeExplicitlyEmpty
                           : ax::mojom::NameFrom::kAttribute);

  // The chrome refresh version of this view has a ripple effect which is
  // configured by the background.
  UpdateBackground();
}

ContentSettingImageView::~ContentSettingImageView() = default;

void ContentSettingImageView::Update() {
  content::WebContents* web_contents =
      delegate_->GetContentSettingWebContents();

  bool force_hide = delegate_->ShouldHideContentSettingImage();
  content_setting_image_model_->Update(force_hide ? nullptr : web_contents);
  SetTooltipText(content_setting_image_model_->get_tooltip());

  if (!content_setting_image_model_->is_visible()) {
    SetVisible(false);
    GetViewAccessibility().SetIsIgnored(true);
    return;
  }
  DCHECK(web_contents);
  UpdateImage();
  SetVisible(true);
  GetViewAccessibility().SetIsIgnored(false);
  GetViewAccessibility().SetRole(ax::mojom::Role::kButton);

  if (content_setting_image_model_->ShouldNotifyAccessibility(web_contents)) {
    auto name = l10n_util::GetStringUTF16(
        content_setting_image_model_->AccessibilityAnnouncementStringId());
    GetViewAccessibility().SetName(name);

    GetViewAccessibility().AnnounceAlert(l10n_util::GetStringFUTF16(
        IDS_A11Y_INDICATORS_ANNOUNCEMENT, name,
        l10n_util::GetStringUTF16(IDS_A11Y_OMNIBOX_CHIP_HINT)));

    content_setting_image_model_->AccessibilityWasNotified(web_contents);
  }

  if (content_setting_image_model_->ShouldAutoOpenBubble(web_contents)) {
    ShowBubbleImpl();
    content_setting_image_model_->SetBubbleWasAutoOpened(web_contents);
  }

  // If the content usage or blockage should be indicated to the user, start the
  // animation and record that the icon has been shown.
  if (!can_animate_ ||
      !content_setting_image_model_->ShouldRunAnimation(web_contents)) {
    return;
  }

  // We just ignore this blockage if we're already showing some other string to
  // the user.  If this becomes a problem, we could design some sort of queueing
  // mechanism to show one after the other, but it doesn't seem important now.
  int string_id = content_setting_image_model_->explanatory_string_id();
  if (string_id) {
    // Reset the slide animation so that the label's show/hide animation runs.
    ResetSlideAnimation(false);
    AnimateIn(string_id);
  }

  content_setting_image_model_->SetAnimationHasRun(web_contents);

  UpdateElementIdentifier();
}

void ContentSettingImageView::UpdateElementIdentifier() {
  std::optional<ui::ElementIdentifier> element_identifier;
  switch (content_setting_image_model_->image_type()) {
    case ContentSettingImageModel::ImageType::NOTIFICATIONS:
      element_identifier = kNotificationContentSettingImageView;
      break;
    case ContentSettingImageModel::ImageType::MEDIASTREAM:
      element_identifier = kMediaActivityIndicatorElementId;
      break;
    case ContentSettingImageModel::ImageType::MIDI_SYSEX:
      element_identifier = kMidiSysexActivityIndicatorElementId;
      break;
    default:
      break;
  }
  if (element_identifier) {
    SetProperty(views::kElementIdentifierKey, *element_identifier);
  } else {
    ClearProperty(views::kElementIdentifierKey);
  }
}

void ContentSettingImageView::SetIconColor(std::optional<SkColor> color) {
  if (icon_color_ == color) {
    return;
  }
  icon_color_ = color;
  if (content_setting_image_model_->is_visible()) {
    UpdateImage();
  }
  OnPropertyChanged(&icon_color_, views::kPropertyEffectsNone);
}

std::optional<SkColor> ContentSettingImageView::GetIconColor() const {
  return icon_color_;
}

bool ContentSettingImageView::OnMousePressed(const ui::MouseEvent& event) {
  // Pause animation so that the icon does not shrink and deselect while the
  // user is attempting to press it.
  PauseAnimation();
  return IconLabelBubbleView::OnMousePressed(event);
}

bool ContentSettingImageView::OnKeyPressed(const ui::KeyEvent& event) {
  // Pause animation so that the icon does not shrink and deselect while the
  // user is attempting to press it using key commands.
  if (GetKeyClickActionForEvent(event) == KeyClickAction::kOnKeyRelease) {
    PauseAnimation();
  }
  return Button::OnKeyPressed(event);
}

void ContentSettingImageView::OnThemeChanged() {
  UpdateImage();
  IconLabelBubbleView::OnThemeChanged();
}

bool ContentSettingImageView::ShouldShowSeparator() const {
  return false;
}

bool ContentSettingImageView::ShowBubble(const ui::Event& event) {
  return ShowBubbleImpl();
}

bool ContentSettingImageView::ShowBubbleImpl() {
  PauseAnimation();
  content::WebContents* web_contents =
      delegate_->GetContentSettingWebContents();
  if (web_contents && !bubble_view_) {
    views::View* const anchor = parent();
    bubble_view_ = new ContentSettingBubbleContents(
        content_setting_image_model_->CreateBubbleModel(
            delegate_->GetContentSettingBubbleModelDelegate(), web_contents),
        web_contents, anchor, views::BubbleBorder::TOP_RIGHT);
    bubble_view_->SetHighlightedButton(this);
    views::Widget* bubble_widget =
        views::BubbleDialogDelegateView::CreateBubble(bubble_view_);
    observation_.Observe(bubble_widget);

    // Update popup bubble's title style.
    if (views::BubbleFrameView* const frame_view =
            bubble_view_->GetBubbleFrameView()) {
      if (views::Label* title_label = frame_view->default_title()) {
        title_label->SetTextStyle(views::style::STYLE_HEADLINE_4);
        title_label->SetEnabledColor(kColorActivityIndicatorForeground);
      }
    }

    bubble_widget->Show();
    delegate_->OnContentSettingImageBubbleShown(
        content_setting_image_model_->image_type());
  }

  return true;
}

bool ContentSettingImageView::IsBubbleShowing() const {
  return bubble_view_ != nullptr;
}

ContentSettingImageModel::ImageType ContentSettingImageView::GetType() const {
  return content_setting_image_model_->image_type();
}

views::Widget* ContentSettingImageView::GetBubbleWidgetForTesting() const {
  if (!bubble_view_) {
    return nullptr;
  }

  return bubble_view_->GetWidget();
}

void ContentSettingImageView::OnWidgetDestroying(views::Widget* widget) {
  if (!bubble_view_ || bubble_view_->GetWidget() != widget) {
    return;
  }

#if BUILDFLAG(IS_MAC)
  if (content_setting_image_model_->image_type() ==
          ContentSettingImageModel::ImageType::GEOLOCATION &&
      content_setting_image_model_->explanatory_string_id() ==
          IDS_GEOLOCATION_TURNED_OFF) {
    base::RecordAction(
        base::UserMetricsAction("ContentSettings.GeolocationDialog.Closed"));
  }
#endif  // BUILDFLAG(IS_MAC)

  DCHECK(observation_.IsObservingSource(widget));
  observation_.Reset();
  bubble_view_ = nullptr;
  UnpauseAnimation();
}

void ContentSettingImageView::UpdateImage() {
  gfx::Image icon = content_setting_image_model_->GetIcon(icon_color_.value_or(
      color_utils::DeriveDefaultIconColor(GetForegroundColor())));
  if (!icon.IsEmpty()) {
    SetImageModel(ui::ImageModel::FromImage(icon));
  }
}

void ContentSettingImageView::AnimationEnded(const gfx::Animation* animation) {
  IconLabelBubbleView::AnimationEnded(animation);

  content::WebContents* web_contents =
      delegate_->GetContentSettingWebContents();

  // The promo currently is only used for Notifications, and it is only shown
  // directly after the animation is shown.
  if (web_contents) {
    const webapps::AppId* app_id =
        web_app::WebAppTabHelper::GetAppId(web_contents);
    if (app_id) {
      user_education::FeaturePromoParams params(
          feature_engagement::kIPHPwaQuietNotificationFeature, *app_id);
      browser_->window()->MaybeShowFeaturePromo(std::move(params));
    }
  }
}

BEGIN_METADATA(ContentSettingImageView)
ADD_PROPERTY_METADATA(std::optional<SkColor>, IconColor)
END_METADATA