electron/shell/browser/ui/autofill_popup.cc
Charles Kerr 30e992dec4
chore: bump chromium to 115.0.5786.0 (main) (#38301)
* chore: bump chromium in DEPS to 115.0.5772.0

* chore: update disable_color_correct_rendering.patch

no manual changes; patch succeeded with fuzz 2.

* chore: update chromium/build_libc_as_static_library.patch

no manual changes; patch succeeded with fuzz 2 (offset 1 line).

* chore: update chromium/feat_configure_launch_options_for_service_process.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4518747

patch manually reapplied due to upstream code shear

* chore: update chromium/chore_patch_out_profile_methods_in_profile_selections_cc.patch

https://chromium-review.googlesource.com/c/chromium/src/+/4502965

patch manually reapplied due to upstream code shear

* chore: update bundle_locations #include location

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4525355

* chore: Remove `extension_name` from DesktopStreamsRegistry

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4514081

* chore: bump chromium in DEPS to 115.0.5778.0

* chore: add chromium/chore_patch_out_profile_methods_in_chrome_browser_pdf.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4482679

Another instance of patching out upstream references to Profile code

* refactor: add WebViewGuestDelegate::GetGuestDelegateWeakPtr()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4515455

This approach copied from GuestViewBase::GetGuestDelegateWeakPtr() approach in that same commit.

* fixup! chore: update bundle_locations #include location

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4525355

Sync namespace use to upstream base::mac -> base::apple changes

* fixup! chore: update bundle_locations #include location

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4525355

Sync namespace use to upstream base::mac -> base::apple changes

* chore: update chromium/mas_disable_remote_accessibility.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4530546

patch manually reapplied due to upstream code shear

* chore: update chromium/printing.patch

no manual changes; patch succeeded with fuzz

* chore: update chromium/build_add_electron_tracing_category.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4517872

no manual changes; patch succeeded with fuzz

* chore: update chromium/feat_ensure_mas_builds_of_the_same_application_can_use_safestorage.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4529098

patch manually reapplied due to upstream code shear

* chromium/build_only_use_the_mas_build_config_in_the_required_components.patch

https://chromium-review.googlesource.com/c/chromium/src/+/4529098

patch manually reapplied due to upstream code shear

* chore: update chromium/mas-cgdisplayusesforcetogray.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4529475

no manual changes; patch succeeded with fuzz

* chore: update chromium/build_only_use_the_mas_build_config_in_the_required_components.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4429011

no manual changes; patch succeeded with fuzz

* chore: remove chromium/cherry-pick-48a136e77e6d.patch

already present upstream

* chore: remove chromium/cherry-pick-e6e23ba00379.patch

already present upstream

* [Code Health] Remove value based RegisterPref for Dict and List

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4533537

* Remove `cxx17_backports.h` and adjust unittest file

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4534916

Notes: mostly just removing #include and s/base::clamp/std::clamp/

* refactor: add FakeBrowserProcess helper class

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4424072

Used in ElectronBrowserMainParts' fake_browser_process_ field. Previously
this was an BrowserProcessImpl instance, but upstream 4424072 makes that
an abstract base class. `FakeBrowserProcess` is a thin subclass with the
minimum code needed to make it concrete.

`ElectronBrowserMainParts::fake_browser_process_` is now a `shared_ptr`
instead of a `unique_ptr` so we can avoid exposing `FakeBrowserProcess`
in the header.

* fixup! refactor: add FakeBrowserProcess helper class

remove unnecessary unique_ptr

* fix: Browser::SetDockIcon() when !Browser.is_ready()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4410741

An alternative fix for https://github.com/electron/electron/pull/36279 .
The previous fix was to call `SetSupportedScales()` ourselves (1b1609a),
but upstream has removed this API.

CC @codebytere

* fixup! chore: remove chromium/cherry-pick-e6e23ba00379.patch

* chore: update chromium/export_gin_v8platform_pageallocator_for_usage_outside_of_the_gin.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4517404

patch manually reapplied due to upstream code shear

* chore: update patches

* chore: add dep: resource_coordinator:mojo_bindings

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4521464

* fixup! chore: add dep: resource_coordinator:mojo_bindings

chore: make lint happy

* chore: patch out Profile methods in titlebar_config

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4508143 (primary)

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4534461 (followup)

* chore: bump chromium in DEPS to 115.0.5780.0

* chore: update chromium/disable_hidden.patch

no manual changes; patch succeeded with fuzz

* chore: update chromium/sysroot.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4535715

no manual changes; patch succeeded with fuzz

* chore: update chromium/feat_enable_offscreen_rendering_with_viz_compositor.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4403623

patch manually reapplied due to upstream code shear

* chore: update chromium/refactor_expose_cursor_changes_to_the_webcontentsobserver.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4547380

patch manually reapplied due to upstream code shear

* chore: update patches

* fixup! fix: Browser::SetDockIcon() when !Browser.is_ready()

chore: iwyu image.h

* chore: bump chromium in DEPS to 115.0.5782.0

* chore: bump chromium in DEPS to 115.0.5784.0

* 4514181: CR2023: Update filled cr-input styles.

https://chromium-review.googlesource.com/c/chromium/src/+/4514181

* 4521894: Remove SiteInstanceDeleting usage outside of tests.

https://chromium-review.googlesource.com/c/chromium/src/+/4521894

* 4525355: Move backup_util and bundle_locations to base/apple

https://chromium-review.googlesource.com/c/chromium/src/+/4525355

* 4484743: webauthn: add underlying support for calling iCloud Keychain on macOS.

https://chromium-review.googlesource.com/c/chromium/src/+/4484743

* chore: fixup patch indices

* 4546398: Convert /chrome/browser to use ARC

https://chromium-review.googlesource.com/c/chromium/src/+/4546398

* chore: bump chromium in DEPS to 115.0.5786.0

* chore: update patches

* 4540682: Initialize PDF SDK with policy in chrome_pdf::RenderPDFPageToDC()

https://chromium-review.googlesource.com/c/chromium/src/+/4540682

* Don't import ObjC++ fn headers into electron_api_app

* 4535715: Refactor sysroot-creator.sh

https://chromium-review.googlesource.com/c/chromium/src/+/4535715

* fixup! refactor: add FakeBrowserProcess helper class

* 4531903: [string][test] Create/Copy strings to old space before externalization

https://chromium-review.googlesource.com/c/v8/v8/+/4531903

* 59645: Add APIs to support RSA keys with large e.

https://boringssl-review.googlesource.com/c/boringssl/+/59645

---------

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-05-23 21:58:58 +02:00

340 lines
11 KiB
C++

// Copyright (c) 2012 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 <algorithm>
#include <memory>
#include <vector>
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "components/autofill/core/common/autofill_features.h"
#include "electron/buildflags/buildflags.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/autofill_popup.h"
#include "shell/common/api/api.mojom.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/text_utils.h"
#if BUILDFLAG(ENABLE_OSR)
#include "shell/browser/osr/osr_render_widget_host_view.h"
#include "shell/browser/osr/osr_view_proxy.h"
#endif
namespace electron {
namespace {
void CalculatePopupXAndWidthHorizontallyCentered(
int popup_preferred_width,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
bool is_rtl,
gfx::Rect* popup_bounds) {
// The preferred horizontal starting point for the pop-up is at the horizontal
// center of the field.
int preferred_starting_point =
std::clamp(element_bounds.x() + (element_bounds.size().width() / 2),
content_area_bounds.x(), content_area_bounds.right());
// The space available to the left and to the right.
int space_to_right = content_area_bounds.right() - preferred_starting_point;
int space_to_left = preferred_starting_point - content_area_bounds.x();
// Calculate the pop-up width. This is either the preferred pop-up width, or
// alternatively the maximum space available if there is not sufficient space
// for the preferred width.
int popup_width =
std::min(popup_preferred_width, space_to_left + space_to_right);
// Calculates the space that is available to grow into the preferred
// direction. In RTL, this is the space to the right side of the content
// area, in LTR this is the space to the left side of the content area.
int space_to_grow_in_preferred_direction =
is_rtl ? space_to_left : space_to_right;
// Calculate how much the pop-up needs to grow into the non-preferred
// direction.
int amount_to_grow_in_unpreferred_direction =
std::max(0, popup_width - space_to_grow_in_preferred_direction);
popup_bounds->set_width(popup_width);
if (is_rtl) {
// Note, in RTL the |pop_up_width| must be subtracted to achieve
// right-alignment of the pop-up with the element.
popup_bounds->set_x(preferred_starting_point - popup_width +
amount_to_grow_in_unpreferred_direction);
} else {
popup_bounds->set_x(preferred_starting_point -
amount_to_grow_in_unpreferred_direction);
}
}
void CalculatePopupXAndWidth(int popup_preferred_width,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
bool is_rtl,
gfx::Rect* popup_bounds) {
int right_growth_start = std::clamp(
element_bounds.x(), content_area_bounds.x(), content_area_bounds.right());
int left_growth_end =
std::clamp(element_bounds.right(), content_area_bounds.x(),
content_area_bounds.right());
int right_available = content_area_bounds.right() - right_growth_start;
int left_available = left_growth_end - content_area_bounds.x();
int popup_width = std::min(popup_preferred_width,
std::max(left_available, right_available));
// Prefer to grow towards the end (right for LTR, left for RTL). But if there
// is not enough space available in the desired direction and more space in
// the other direction, reverse it.
bool grow_left = false;
if (is_rtl) {
grow_left =
left_available >= popup_width || left_available >= right_available;
} else {
grow_left =
right_available < popup_width && right_available < left_available;
}
popup_bounds->set_width(popup_width);
popup_bounds->set_x(grow_left ? left_growth_end - popup_width
: right_growth_start);
}
void CalculatePopupYAndHeight(int popup_preferred_height,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
gfx::Rect* popup_bounds) {
int top_growth_end = std::clamp(element_bounds.y(), content_area_bounds.y(),
content_area_bounds.bottom());
int bottom_growth_start =
std::clamp(element_bounds.bottom(), content_area_bounds.y(),
content_area_bounds.bottom());
int top_available = top_growth_end - content_area_bounds.y();
int bottom_available = content_area_bounds.bottom() - bottom_growth_start;
popup_bounds->set_height(popup_preferred_height);
popup_bounds->set_y(top_growth_end);
if (bottom_available >= popup_preferred_height ||
bottom_available >= top_available) {
popup_bounds->AdjustToFit(
gfx::Rect(popup_bounds->x(), element_bounds.bottom(),
popup_bounds->width(), bottom_available));
} else {
popup_bounds->AdjustToFit(gfx::Rect(popup_bounds->x(),
content_area_bounds.y(),
popup_bounds->width(), top_available));
}
}
gfx::Rect CalculatePopupBounds(const gfx::Size& desired_size,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
bool is_rtl,
bool horizontally_centered) {
gfx::Rect popup_bounds;
if (horizontally_centered) {
CalculatePopupXAndWidthHorizontallyCentered(
desired_size.width(), content_area_bounds, element_bounds, is_rtl,
&popup_bounds);
} else {
CalculatePopupXAndWidth(desired_size.width(), content_area_bounds,
element_bounds, is_rtl, &popup_bounds);
}
CalculatePopupYAndHeight(desired_size.height(), content_area_bounds,
element_bounds, &popup_bounds);
return popup_bounds;
}
} // namespace
AutofillPopup::AutofillPopup() {
bold_font_list_ = gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
smaller_font_list_ =
gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
}
AutofillPopup::~AutofillPopup() {
Hide();
}
void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
content::RenderFrameHost* embedder_frame_host,
bool offscreen,
views::View* parent,
const gfx::RectF& r) {
Hide();
frame_host_ = frame_host;
element_bounds_ = gfx::ToEnclosedRect(r);
gfx::Vector2d height_offset(0, element_bounds_.height());
gfx::Point menu_position(element_bounds_.origin() + height_offset);
views::View::ConvertPointToScreen(parent, &menu_position);
popup_bounds_ = gfx::Rect(menu_position, element_bounds_.size());
parent_ = parent;
parent_->AddObserver(this);
view_ = new AutofillPopupView(this, parent->GetWidget());
#if BUILDFLAG(ENABLE_OSR)
if (offscreen) {
auto* rwhv = frame_host->GetView();
if (embedder_frame_host != nullptr) {
rwhv = embedder_frame_host->GetView();
}
auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(rwhv);
view_->view_proxy_ = std::make_unique<OffscreenViewProxy>(view_);
osr_rwhv->AddViewProxy(view_->view_proxy_.get());
}
#endif
// Do this after OSR setup, we check for view_proxy_ when showing
view_->Show();
}
void AutofillPopup::Hide() {
if (parent_) {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
if (view_) {
view_->Hide();
view_ = nullptr;
}
}
void AutofillPopup::SetItems(const std::vector<std::u16string>& values,
const std::vector<std::u16string>& labels) {
DCHECK(view_);
values_ = values;
labels_ = labels;
UpdatePopupBounds();
view_->OnSuggestionsChanged();
if (view_) // could be hidden after the change
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::AcceptSuggestion(int index) {
mojo::AssociatedRemote<mojom::ElectronAutofillAgent> autofill_agent;
frame_host_->GetRemoteAssociatedInterfaces()->GetInterface(&autofill_agent);
autofill_agent->AcceptDataListSuggestion(GetValueAt(index));
}
void AutofillPopup::UpdatePopupBounds() {
DCHECK(parent_);
gfx::Point origin(element_bounds_.origin());
views::View::ConvertPointToScreen(parent_, &origin);
gfx::Rect bounds(origin, element_bounds_.size());
gfx::Size preferred_size =
gfx::Size(GetDesiredPopupWidth(), GetDesiredPopupHeight());
popup_bounds_ =
CalculatePopupBounds(preferred_size, parent_->GetBoundsInScreen(), bounds,
base::i18n::IsRTL(), false);
}
gfx::Rect AutofillPopup::popup_bounds_in_view() {
gfx::Point origin(popup_bounds_.origin());
views::View::ConvertPointFromScreen(parent_, &origin);
return gfx::Rect(origin, popup_bounds_.size());
}
void AutofillPopup::OnViewBoundsChanged(views::View* view) {
UpdatePopupBounds();
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::OnViewIsDeleting(views::View* view) {
Hide();
}
int AutofillPopup::GetDesiredPopupHeight() {
return 2 * kPopupBorderThickness + values_.size() * kRowHeight;
}
int AutofillPopup::GetDesiredPopupWidth() {
int popup_width = element_bounds_.width();
for (size_t i = 0; i < values_.size(); ++i) {
int row_size =
kEndPadding + 2 * kPopupBorderThickness +
gfx::GetStringWidth(GetValueAt(i), GetValueFontListForRow(i)) +
gfx::GetStringWidth(GetLabelAt(i), GetLabelFontListForRow(i));
if (!GetLabelAt(i).empty())
row_size += kNamePadding + kEndPadding;
popup_width = std::max(popup_width, row_size);
}
return popup_width;
}
gfx::Rect AutofillPopup::GetRowBounds(int index) {
int top = kPopupBorderThickness + index * kRowHeight;
return gfx::Rect(kPopupBorderThickness, top,
popup_bounds_.width() - 2 * kPopupBorderThickness,
kRowHeight);
}
const gfx::FontList& AutofillPopup::GetValueFontListForRow(int index) const {
return bold_font_list_;
}
const gfx::FontList& AutofillPopup::GetLabelFontListForRow(int index) const {
return smaller_font_list_;
}
ui::ColorId AutofillPopup::GetBackgroundColorIDForRow(int index) const {
return (view_ && index == view_->GetSelectedLine())
? ui::kColorResultsTableHoveredBackground
: ui::kColorResultsTableNormalBackground;
}
int AutofillPopup::GetLineCount() {
return values_.size();
}
std::u16string AutofillPopup::GetValueAt(int i) {
return values_.at(i);
}
std::u16string AutofillPopup::GetLabelAt(int i) {
return labels_.at(i);
}
int AutofillPopup::LineFromY(int y) const {
int current_height = kPopupBorderThickness;
for (size_t i = 0; i < values_.size(); ++i) {
current_height += kRowHeight;
if (y <= current_height)
return i;
}
return values_.size() - 1;
}
} // namespace electron