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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
// 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/location_icon_view.h"
#include "base/functional/bind.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_ui_util.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/page_info/page_info_dialog.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_util.h"
#include "chrome/browser/ui/views/page_info/page_info_bubble_view.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/dom_distiller/core/url_constants.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/security_state/core/security_state.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider_utils.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/flood_fill_ink_drop_ripple.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/animation/ink_drop_host.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/ink_drop_ripple.h"
#include "ui/views/controls/label.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/view_class_properties.h"
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "components/vector_icons/vector_icons.h" // nogncheck
#endif
using content::WebContents;
using security_state::SecurityLevel;
std::optional<ui::ColorId>
LocationIconView::Delegate::GetLocationIconBackgroundColorOverride() const {
return std::nullopt;
}
LocationIconView::LocationIconView(
const gfx::FontList& font_list,
IconLabelBubbleView::Delegate* parent_delegate,
Delegate* delegate)
: IconLabelBubbleView(font_list, parent_delegate), delegate_(delegate) {
DCHECK(delegate_);
SetID(VIEW_ID_LOCATION_ICON);
SetUpForAnimation();
SetProperty(views::kElementIdentifierKey, kLocationIconElementId);
// Readability is guaranteed by the omnibox theme.
label()->SetAutoColorReadabilityEnabled(false);
SetAccessibleProperties(/*is_initialization*/ true);
ConfigureInkDropForRefresh2023(this, kColorOmniboxIconHover,
kColorOmniboxIconPressed);
UpdateBorder();
}
LocationIconView::~LocationIconView() = default;
gfx::Size LocationIconView::GetMinimumSize() const {
return GetMinimumSizeForPreferredSize(GetPreferredSize());
}
bool LocationIconView::OnMouseDragged(const ui::MouseEvent& event) {
delegate_->OnLocationIconDragged(event);
return IconLabelBubbleView::OnMouseDragged(event);
}
SkColor LocationIconView::GetForegroundColor() const {
const std::u16string& display_text = GetText();
const bool is_text_dangerous =
display_text == l10n_util::GetStringUTF16(IDS_DANGEROUS_VERBOSE_STATE);
if (is_text_dangerous) {
return GetColorProvider()->GetColor(kColorOmniboxSecurityChipText);
}
SecurityLevel security_level = SecurityLevel::NONE;
if (!delegate_->IsEditingOrEmpty()) {
security_level = GetSecurityLevel();
}
return delegate_->GetSecurityChipColor(security_level);
}
bool LocationIconView::ShouldShowSeparator() const {
return false;
}
bool LocationIconView::ShouldShowLabelAfterAnimation() const {
return ShouldShowLabel();
}
bool LocationIconView::ShowBubble(const ui::Event& event) {
return delegate_->ShowPageInfoDialog();
}
bool LocationIconView::IsBubbleShowing() const {
return PageInfoBubbleView::GetShownBubbleType() !=
PageInfoBubbleView::BUBBLE_NONE;
}
bool LocationIconView::OnMousePressed(const ui::MouseEvent& event) {
delegate_->OnLocationIconPressed(event);
IconLabelBubbleView::OnMousePressed(event);
return true;
}
void LocationIconView::AddedToWidget() {
Update(true);
}
void LocationIconView::OnThemeChanged() {
IconLabelBubbleView::OnThemeChanged();
// Update the background before the icon since the vector icon
// depends on the container background color in certain cases.
// E.g. different icons corresponding to the SuperGIcon are set
// depending on the background color of the container.
UpdateBackground();
UpdateIcon();
}
security_state::SecurityLevel LocationIconView::GetSecurityLevel() const {
if (security_level_for_testing_.has_value()) {
return security_level_for_testing_.value();
}
return delegate_->GetLocationBarModel()->GetSecurityLevel();
}
bool LocationIconView::HasSecurityStateChanged() const {
return last_update_security_level_ != GetSecurityLevel();
}
void LocationIconView::SetSecurityLevelForTesting(
security_state::SecurityLevel security_level) {
security_level_for_testing_ = security_level;
}
int LocationIconView::GetMinimumLabelTextWidth() const {
int width = 0;
std::u16string text = GetText();
if (text == label()->GetText()) {
// Optimize this common case by not creating a new label.
// GetPreferredSize is not dependent on the label's current
// width, so this returns the same value as the branch below.
width = label()
->GetPreferredSize(views::SizeBounds(label()->width(), {}))
.width();
} else {
views::Label label(text, {font_list()});
width = label.GetPreferredSize().width();
}
return GetMinimumSizeForPreferredSize(GetSizeForLabelWidth(width)).width();
}
bool LocationIconView::GetShowText() const {
if (delegate_->IsEditingOrEmpty()) {
return false;
}
const auto* location_bar_model = delegate_->GetLocationBarModel();
const GURL& url = location_bar_model->GetURL();
if (url.SchemeIs(content::kChromeUIScheme) ||
url.SchemeIs(extensions::kExtensionScheme) ||
url.SchemeIs(url::kFileScheme) ||
url.SchemeIs(dom_distiller::kDomDistillerScheme)) {
return true;
}
return !location_bar_model->GetSecureDisplayText().empty();
}
const views::InkDrop* LocationIconView::get_ink_drop_for_testing() {
return views::InkDrop::Get(this)->GetInkDrop();
}
std::u16string LocationIconView::GetText() const {
if (delegate_->IsEditingOrEmpty()) {
return std::u16string();
}
if (delegate_->GetLocationBarModel()->GetURL().SchemeIs(
content::kChromeUIScheme)) {
return l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
}
if (delegate_->GetLocationBarModel()->GetURL().SchemeIs(url::kFileScheme)) {
return l10n_util::GetStringUTF16(IDS_OMNIBOX_FILE);
}
if (delegate_->GetLocationBarModel()->GetURL().SchemeIs(
dom_distiller::kDomDistillerScheme)) {
return l10n_util::GetStringUTF16(IDS_OMNIBOX_READER_MODE);
}
if (delegate_->GetWebContents()) {
// On ChromeOS, this can be called using web_contents from
// SimpleWebViewDialog::GetWebContents() which always returns null.
// TODO(crbug.com/40501128) Remove the null check and make
// SimpleWebViewDialog::GetWebContents return the proper web contents
// instead.
const std::u16string extension_name =
extensions::ui_util::GetEnabledExtensionNameForUrl(
delegate_->GetLocationBarModel()->GetURL(),
delegate_->GetWebContents()->GetBrowserContext());
if (!extension_name.empty()) {
return extension_name;
}
}
return delegate_->GetLocationBarModel()->GetSecureDisplayText();
}
bool LocationIconView::GetAnimateTextVisibilityChange() const {
if (delegate_->IsEditingOrEmpty()) {
return false;
}
SecurityLevel level = GetSecurityLevel();
// Do not animate transitions from WARNING to DANGEROUS, since
// the transition can look confusing/messy.
if (level == SecurityLevel::DANGEROUS &&
last_update_security_level_ == SecurityLevel::WARNING) {
return false;
}
return (level == SecurityLevel::DANGEROUS || level == SecurityLevel::WARNING);
}
void LocationIconView::UpdateTextVisibility(bool suppress_animations) {
SetLabel(GetText());
bool should_show = GetShowText();
if (!GetAnimateTextVisibilityChange() || suppress_animations) {
ResetSlideAnimation(should_show);
} else if (should_show) {
AnimateIn(std::nullopt);
} else {
AnimateOut();
}
}
void LocationIconView::SetAccessibleProperties(bool is_initialization) {
ax::mojom::Role role = delegate_->IsEditingOrEmpty()
? ax::mojom::Role::kImage
: ax::mojom::Role::kPopUpButton;
const std::u16string name =
delegate_->IsEditingOrEmpty()
? l10n_util::GetStringUTF16(IDS_ACC_SEARCH_ICON)
: GetViewAccessibility().GetCachedName();
// If no display text exists, ensure that the accessibility label is added.
const std::u16string description =
delegate_->IsEditingOrEmpty()
? GetViewAccessibility().GetCachedDescription()
: label()->GetText().empty()
? delegate_->GetLocationBarModel()->GetSecureAccessibilityText()
: std::u16string();
GetViewAccessibility().SetRole(role);
GetViewAccessibility().SetName(name);
GetViewAccessibility().SetDescription(description);
}
void LocationIconView::UpdateIcon() {
// Cancel any previous outstanding icon requests, as they are now outdated.
icon_fetch_weak_ptr_factory_.InvalidateWeakPtrs();
ui::ImageModel icon = delegate_->GetLocationIcon(
base::BindOnce(&LocationIconView::OnIconFetched,
icon_fetch_weak_ptr_factory_.GetWeakPtr()));
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const bool is_vector_icon = !icon.IsEmpty() && icon.IsVectorIcon() &&
icon.GetVectorIcon().vector_icon();
if (is_vector_icon) {
const char* const icon_name = icon.GetVectorIcon().vector_icon()->name;
if (icon_name == vector_icons::kGoogleSuperGIcon.name ||
icon_name == vector_icons::kGoogleGLogoMonochromeIcon.name) {
// Remove the inkdrop around the Google G logo since we cannot interact
// with it.
views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::OFF);
} else {
views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON);
}
bool has_custom_theme =
this->GetWidget() && this->GetWidget()->GetCustomTheme();
if (has_custom_theme && icon_name == vector_icons::kGoogleSuperGIcon.name) {
SetBackgroundColor(SK_ColorWHITE);
}
}
#endif
if (!icon.IsEmpty()) {
SetImageModel(icon);
}
}
void LocationIconView::UpdateBackground() {
CHECK(GetColorProvider());
const std::u16string& display_text = GetText();
const bool is_text_dangerous =
display_text == l10n_util::GetStringUTF16(IDS_DANGEROUS_VERBOSE_STATE);
const ui::ColorId id =
delegate_->GetLocationIconBackgroundColorOverride().value_or(
is_text_dangerous ? kColorOmniboxSecurityChipDangerousBackground
: kColorOmniboxIconBackground);
SetBackgroundColor(GetColorProvider()->GetColor(id));
if (is_text_dangerous) {
ConfigureInkDropForRefresh2023(this, kColorOmniboxSecurityChipInkDropHover,
kColorOmniboxSecurityChipInkDropRipple);
} else {
ConfigureInkDropForRefresh2023(this, kColorOmniboxIconHover,
kColorOmniboxIconPressed);
}
}
void LocationIconView::OnIconFetched(const gfx::Image& image) {
DCHECK(!image.IsEmpty());
SetImageModel(ui::ImageModel::FromImage(image));
}
void LocationIconView::Update(bool suppress_animations,
bool force_hide_background) {
UpdateTextVisibility(suppress_animations);
UpdateBorder();
// Update the background before the icon, since the vector icon
// can depend on the container background.
UpdateBackground();
UpdateIcon();
SetAccessibleProperties(/*is_initialization*/ false);
// The label text color may have changed in response to changes in security
// level.
UpdateLabelColors();
if (force_hide_background) {
SetBackgroundColor(SK_ColorTRANSPARENT);
}
bool is_editing_or_empty = delegate_->IsEditingOrEmpty();
// The tooltip should be shown if we are not editing or empty.
SetTooltipText(is_editing_or_empty
? std::u16string()
: l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON));
// We should only enable/disable the InkDrop if the editing state has changed,
// as the drop gets recreated when views::InkDrop::Get(this)->SetMode() is
// called. This can result in strange behaviour, like the the InkDrop
// disappearing mid animation.
if (is_editing_or_empty != was_editing_or_empty_) {
// If the omnibox is empty or editing, the user should not be able to left
// click on the icon. As such, the icon should not show a highlight or be
// focusable. Note: using the middle mouse to copy-and-paste should still
// work on the icon.
if (is_editing_or_empty) {
views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::OFF);
SetFocusBehavior(FocusBehavior::NEVER);
} else {
views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON);
SetFocusBehavior(views::PlatformStyle::kDefaultFocusBehavior);
}
}
last_update_security_level_ = SecurityLevel::NONE;
if (!is_editing_or_empty) {
last_update_security_level_ = GetSecurityLevel();
}
was_editing_or_empty_ = is_editing_or_empty;
}
bool LocationIconView::IsTriggerableEvent(const ui::Event& event) {
if (delegate_->IsEditingOrEmpty()) {
return false;
}
if (event.IsMouseEvent()) {
if (event.AsMouseEvent()->IsOnlyMiddleMouseButton()) {
return false;
}
} else if (event.IsGestureEvent() &&
event.type() != ui::EventType::kGestureTap) {
return false;
}
return IconLabelBubbleView::IsTriggerableEvent(event);
}
void LocationIconView::UpdateBorder() {
// Bubbles are given the full internal height of the location bar so that all
// child views in the location bar have the same height. The visible height of
// the bubble should be smaller, so use an empty border to shrink down the
// content bounds so the background gets painted correctly.
gfx::Insets insets = GetLayoutInsets(LOCATION_BAR_PAGE_INFO_ICON_PADDING);
if (ShouldShowLabel()) {
SecurityLevel level = GetSecurityLevel();
if (level == security_state::DANGEROUS) {
// Extra space between the left edge and label.
const int kLeftHorizontalPadding = 6;
// Extra space between the label and right edge.
const int kRightHorizontalPadding = 10;
insets.set_left(kLeftHorizontalPadding);
insets.set_right(kRightHorizontalPadding);
} else {
// An extra space between chip's label and right edge.
const int kExtraRightPadding = 4;
insets.set_right(insets.right() + kExtraRightPadding);
}
}
SetBorder(views::CreateEmptyBorder(insets));
}
gfx::Size LocationIconView::GetMinimumSizeForPreferredSize(
gfx::Size size) const {
const int kMinCharacters = 10;
size.SetToMin(
GetSizeForLabelWidth(font_list().GetExpectedTextWidth(kMinCharacters)));
return size;
}
void LocationIconView::SetBackgroundColor(SkColor color) {
SetBackground(views::CreateRoundedRectBackground(color, GetCornerRadii()));
}
BEGIN_METADATA(LocationIconView)
ADD_READONLY_PROPERTY_METADATA(int, MinimumLabelTextWidth)
ADD_READONLY_PROPERTY_METADATA(std::u16string, Text)
ADD_READONLY_PROPERTY_METADATA(bool, ShowText)
ADD_READONLY_PROPERTY_METADATA(bool, AnimateTextVisibilityChange)
END_METADATA
|