2015-11-17 19:20:13 +01:00
|
|
|
// Copyright 2015 The Chromium Embedded Framework Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/native/browser_platform_delegate_native_win.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
#include <shellapi.h>
|
|
|
|
#include <wininet.h>
|
|
|
|
#include <winspool.h>
|
|
|
|
|
2021-09-27 13:15:35 +02:00
|
|
|
#include "base/base_paths_win.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "base/files/file_util.h"
|
2021-09-27 13:15:35 +02:00
|
|
|
#include "base/path_service.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2018-02-21 22:52:02 +01:00
|
|
|
#include "base/win/win_util.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/alloy/alloy_browser_host_impl.h"
|
|
|
|
#include "cef/libcef/browser/context.h"
|
|
|
|
#include "cef/libcef/browser/geometry_util.h"
|
|
|
|
#include "cef/libcef/browser/native/window_delegate_view.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
2024-06-14 19:01:45 +02:00
|
|
|
#include "components/input/native_web_keyboard_event.h"
|
2020-03-04 01:29:39 +01:00
|
|
|
#include "third_party/blink/public/common/input/web_mouse_event.h"
|
|
|
|
#include "third_party/blink/public/common/input/web_mouse_wheel_event.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "ui/aura/window.h"
|
|
|
|
#include "ui/base/win/shell.h"
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "ui/display/display.h"
|
|
|
|
#include "ui/display/screen.h"
|
2022-10-25 01:47:04 +02:00
|
|
|
#include "ui/display/win/screen_win.h"
|
2016-07-06 21:34:09 +02:00
|
|
|
#include "ui/events/keycodes/dom/dom_key.h"
|
|
|
|
#include "ui/events/keycodes/dom/keycode_converter.h"
|
|
|
|
#include "ui/events/keycodes/keyboard_code_conversion_win.h"
|
|
|
|
#include "ui/events/keycodes/platform_key_map_win.h"
|
2020-01-23 22:58:01 +01:00
|
|
|
#include "ui/gfx/geometry/vector2d.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "ui/gfx/win/hwnd_util.h"
|
|
|
|
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_win.h"
|
|
|
|
#include "ui/views/widget/widget.h"
|
|
|
|
#include "ui/views/win/hwnd_util.h"
|
|
|
|
|
|
|
|
#pragma comment(lib, "dwmapi.lib")
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-11-15 18:50:53 +01:00
|
|
|
void WriteTempFileAndView(const std::string& data) {
|
2018-03-20 21:15:08 +01:00
|
|
|
CEF_REQUIRE_BLOCKING();
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
base::FilePath tmp_file;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!base::CreateTemporaryFile(&tmp_file)) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// The shell command will look at the file extension to identify the correct
|
|
|
|
// program to open.
|
|
|
|
tmp_file = tmp_file.AddExtension(L"txt");
|
|
|
|
|
2024-08-26 14:44:25 +02:00
|
|
|
const bool write_success = base::WriteFile(tmp_file, data);
|
|
|
|
DCHECK(write_success);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
ui::win::OpenFileViaShell(tmp_file);
|
|
|
|
}
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
gfx::Rect GetDisplayWorkAreaNearestPoint(gfx::Point dip_point) {
|
2022-06-27 12:29:16 +02:00
|
|
|
const auto display =
|
2022-10-25 01:47:04 +02:00
|
|
|
display::Screen::GetScreen()->GetDisplayNearestPoint(dip_point);
|
|
|
|
// Work area in DIP.
|
|
|
|
return display.work_area();
|
2022-06-27 12:29:16 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
CefRect GetScreenFrameRectFromDIPContentRect(HWND window,
|
|
|
|
gfx::Rect dip_rect,
|
|
|
|
DWORD style,
|
|
|
|
DWORD ex_style,
|
|
|
|
bool has_menu) {
|
|
|
|
// Convert from DIP using a method that can handle multiple displays with
|
|
|
|
// different DPI. If |window| is nullptr the closest display will be used.
|
|
|
|
const auto screen_rect =
|
|
|
|
display::win::ScreenWin::DIPToScreenRect(window, dip_rect);
|
2022-06-27 12:29:16 +02:00
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
RECT rect = {screen_rect.x(), screen_rect.y(),
|
|
|
|
screen_rect.x() + screen_rect.width(),
|
|
|
|
screen_rect.y() + screen_rect.height()};
|
2022-06-27 12:29:16 +02:00
|
|
|
|
|
|
|
AdjustWindowRectEx(&rect, style, has_menu, ex_style);
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
// Keep the original origin while potentially increasing the size to include
|
|
|
|
// the frame non-client area.
|
|
|
|
return CefRect(screen_rect.x(), screen_rect.y(), rect.right - rect.left,
|
2022-06-27 12:29:16 +02:00
|
|
|
rect.bottom - rect.top);
|
|
|
|
}
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
CefRect GetAdjustedScreenFrameRect(CefRect screen_rect,
|
|
|
|
DWORD style,
|
|
|
|
DWORD ex_style,
|
|
|
|
bool has_menu) {
|
|
|
|
// If height or width is not provided let the OS determine the position and
|
|
|
|
// size similar to Chromium behavior. Note that |CW_USEDEFAULT| cannot be
|
|
|
|
// stored in a gfx::Rect due to clamping.
|
|
|
|
if (screen_rect.width == CW_USEDEFAULT ||
|
|
|
|
screen_rect.height == CW_USEDEFAULT) {
|
2022-06-27 12:29:16 +02:00
|
|
|
return CefRect(CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT);
|
|
|
|
}
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
if (screen_rect.x == CW_USEDEFAULT) {
|
|
|
|
screen_rect.x = 0;
|
2022-06-27 12:29:16 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
if (screen_rect.y == CW_USEDEFAULT) {
|
|
|
|
screen_rect.y = 0;
|
2022-06-27 12:29:16 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
// Convert to DIP using a method that can handle multiple displays with
|
|
|
|
// different DPI.
|
|
|
|
const auto dip_rect = display::win::ScreenWin::ScreenToDIPRect(
|
|
|
|
nullptr, gfx::Rect(screen_rect.x, screen_rect.y, screen_rect.width,
|
|
|
|
screen_rect.height));
|
|
|
|
const auto visible_dip_rect = MakeVisibleOnScreenRect(
|
|
|
|
dip_rect, GetDisplayWorkAreaNearestPoint(dip_rect.origin()));
|
2022-06-27 12:29:16 +02:00
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
return GetScreenFrameRectFromDIPContentRect(
|
|
|
|
/*window=*/nullptr, visible_dip_rect, style, ex_style, has_menu);
|
2022-06-27 12:29:16 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefBrowserPlatformDelegateNativeWin::CefBrowserPlatformDelegateNativeWin(
|
2017-04-20 21:28:17 +02:00
|
|
|
const CefWindowInfo& window_info,
|
2020-07-04 20:21:34 +02:00
|
|
|
SkColor background_color)
|
2020-09-25 03:40:47 +02:00
|
|
|
: CefBrowserPlatformDelegateNativeAura(window_info, background_color) {}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-04-08 22:48:56 +02:00
|
|
|
void CefBrowserPlatformDelegateNativeWin::set_widget(
|
|
|
|
views::Widget* widget,
|
|
|
|
CefWindowHandle widget_handle) {
|
|
|
|
DCHECK(!window_widget_);
|
|
|
|
window_widget_ = widget;
|
|
|
|
DCHECK(!window_info_.window);
|
|
|
|
window_info_.window = widget_handle;
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefBrowserPlatformDelegateNativeWin::BrowserDestroyed(
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserHostBase* browser) {
|
2022-04-08 22:48:56 +02:00
|
|
|
CefBrowserPlatformDelegateNativeAura::BrowserDestroyed(browser);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
if (host_window_created_) {
|
|
|
|
// Release the reference added in CreateHostWindow().
|
|
|
|
browser->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserPlatformDelegateNativeWin::CreateHostWindow() {
|
|
|
|
RegisterWindowClass();
|
|
|
|
|
2022-04-11 22:54:33 +02:00
|
|
|
if (window_info_.style == 0) {
|
|
|
|
// Client didn't intialize the CefWindowInfo. Provide reasonable defaults.
|
|
|
|
window_info_.SetAsPopup(nullptr, CefString());
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:52:02 +01:00
|
|
|
has_frame_ = !(window_info_.style & WS_CHILD);
|
|
|
|
|
2022-06-27 12:29:16 +02:00
|
|
|
const std::wstring windowName(CefString(&window_info_.window_name));
|
|
|
|
|
|
|
|
CefRect window_rect = window_info_.bounds;
|
|
|
|
|
|
|
|
if (!window_info_.parent_window) {
|
|
|
|
const bool has_menu =
|
2024-01-20 23:48:57 +01:00
|
|
|
!(window_info_.style & WS_CHILD) && (window_info_.menu != nullptr);
|
2022-10-25 01:47:04 +02:00
|
|
|
window_rect = GetAdjustedScreenFrameRect(window_rect, window_info_.style,
|
|
|
|
window_info_.ex_style, has_menu);
|
2022-06-27 12:29:16 +02:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// Create the new browser window.
|
2018-02-21 22:52:02 +01:00
|
|
|
CreateWindowEx(window_info_.ex_style, GetWndClass(), windowName.c_str(),
|
2022-06-27 12:29:16 +02:00
|
|
|
window_info_.style, window_rect.x, window_rect.y,
|
|
|
|
window_rect.width, window_rect.height,
|
|
|
|
window_info_.parent_window, window_info_.menu,
|
2024-01-20 23:48:57 +01:00
|
|
|
::GetModuleHandle(nullptr), this);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// It's possible for CreateWindowEx to fail if the parent window was
|
|
|
|
// destroyed between the call to CreateBrowser and the above one.
|
2018-02-21 22:52:02 +01:00
|
|
|
DCHECK(window_info_.window);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!window_info_.window) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
host_window_created_ = true;
|
|
|
|
|
|
|
|
// Add a reference that will later be released in DestroyBrowser().
|
|
|
|
browser_->AddRef();
|
|
|
|
|
2023-02-27 19:52:38 +01:00
|
|
|
if (!called_enable_non_client_dpi_scaling_ && has_frame_) {
|
2018-02-21 22:52:02 +01:00
|
|
|
// This call gets Windows to scale the non-client area when WM_DPICHANGED
|
|
|
|
// is fired on Windows versions < 10.0.14393.0.
|
|
|
|
// Derived signature; not available in headers.
|
2023-02-27 19:52:38 +01:00
|
|
|
using EnableChildWindowDpiMessagePtr = LRESULT(WINAPI*)(HWND, BOOL);
|
|
|
|
static const auto enable_child_window_dpi_message_func =
|
|
|
|
reinterpret_cast<EnableChildWindowDpiMessagePtr>(
|
|
|
|
base::win::GetUser32FunctionPointer("EnableChildWindowDpiMessage"));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (enable_child_window_dpi_message_func) {
|
2018-02-21 22:52:02 +01:00
|
|
|
enable_child_window_dpi_message_func(window_info_.window, TRUE);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-02-21 22:52:02 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
DCHECK(!window_widget_);
|
|
|
|
|
2018-02-21 22:52:02 +01:00
|
|
|
RECT cr;
|
|
|
|
GetClientRect(window_info_.window, &cr);
|
2022-10-25 01:47:04 +02:00
|
|
|
|
|
|
|
// Convert to DIP using a method that can handle multiple displays with
|
|
|
|
// different DPI. Client coordinates always have origin (0,0).
|
|
|
|
const gfx::Rect dip_rect = display::win::ScreenWin::ScreenToDIPRect(
|
|
|
|
window_info_.window, gfx::Rect(0, 0, cr.right, cr.bottom));
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2018-02-28 05:47:36 +01:00
|
|
|
// Stay on top if top-most window hosting the web view is topmost.
|
|
|
|
HWND top_level_window = GetAncestor(window_info_.window, GA_ROOT);
|
|
|
|
DWORD top_level_window_ex_styles =
|
|
|
|
GetWindowLongPtr(top_level_window, GWL_EXSTYLE);
|
|
|
|
bool always_on_top =
|
|
|
|
(top_level_window_ex_styles & WS_EX_TOPMOST) == WS_EX_TOPMOST;
|
|
|
|
|
2019-07-17 20:47:27 +02:00
|
|
|
CefWindowDelegateView* delegate_view = new CefWindowDelegateView(
|
2023-02-07 00:43:00 +01:00
|
|
|
GetBackgroundColor(), always_on_top, GetBoundsChangedCallback(),
|
|
|
|
GetWidgetDeleteCallback());
|
2020-07-08 19:23:29 +02:00
|
|
|
delegate_view->Init(window_info_.window, web_contents_,
|
2022-10-25 01:47:04 +02:00
|
|
|
gfx::Rect(0, 0, dip_rect.width(), dip_rect.height()));
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
window_widget_ = delegate_view->GetWidget();
|
2019-02-07 22:36:31 +01:00
|
|
|
|
|
|
|
const HWND widget_hwnd = HWNDForWidget(window_widget_);
|
|
|
|
DCHECK(widget_hwnd);
|
|
|
|
const DWORD widget_ex_styles = GetWindowLongPtr(widget_hwnd, GWL_EXSTYLE);
|
|
|
|
|
|
|
|
if (window_info_.ex_style & WS_EX_NOACTIVATE) {
|
|
|
|
// Add the WS_EX_NOACTIVATE style on the DesktopWindowTreeHostWin HWND
|
|
|
|
// so that HWNDMessageHandler::Show() called via Widget::Show() does not
|
|
|
|
// activate the window.
|
|
|
|
SetWindowLongPtr(widget_hwnd, GWL_EXSTYLE,
|
|
|
|
widget_ex_styles | WS_EX_NOACTIVATE);
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
window_widget_->Show();
|
|
|
|
|
2019-02-07 22:36:31 +01:00
|
|
|
if (window_info_.ex_style & WS_EX_NOACTIVATE) {
|
|
|
|
// Remove the WS_EX_NOACTIVATE style so that future mouse clicks inside the
|
|
|
|
// browser correctly activate and focus the window.
|
|
|
|
SetWindowLongPtr(widget_hwnd, GWL_EXSTYLE, widget_ex_styles);
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeWin::CloseHostWindow() {
|
2024-01-20 23:48:57 +01:00
|
|
|
if (window_info_.window != nullptr) {
|
2015-11-17 19:20:13 +01:00
|
|
|
HWND frameWnd = GetAncestor(window_info_.window, GA_ROOT);
|
|
|
|
PostMessage(frameWnd, WM_CLOSE, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefWindowHandle CefBrowserPlatformDelegateNativeWin::GetHostWindowHandle()
|
|
|
|
const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (windowless_handler_) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return windowless_handler_->GetParentWindowHandle();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
return window_info_.window;
|
|
|
|
}
|
|
|
|
|
|
|
|
views::Widget* CefBrowserPlatformDelegateNativeWin::GetWindowWidget() const {
|
|
|
|
return window_widget_;
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:50:07 +02:00
|
|
|
void CefBrowserPlatformDelegateNativeWin::SetFocus(bool setFocus) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!setFocus) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
if (window_widget_) {
|
2022-08-05 20:58:31 +02:00
|
|
|
// Give native focus to the DesktopWindowTreeHostWin ("Chrome_WidgetWin_0")
|
|
|
|
// associated with the root window. The currently focused HWND may be
|
|
|
|
// "CefBrowserWindow" if we're called in response to our WndProc receiving
|
2022-08-10 19:52:39 +02:00
|
|
|
// the WM_SETFOCUS event (possibly due to "CefBrowserWindow" recieving the
|
|
|
|
// top-level WM_ACTIVATE event), or some other HWND if the client calls
|
|
|
|
// CefBrowserHost::SetFocus(true) directly. DesktopWindowTreeHostWin may
|
|
|
|
// also receive focus/blur and mouse click events from the OS directly, in
|
|
|
|
// which case this method will not be called but the below discussion still
|
|
|
|
// applies.
|
2015-11-17 19:20:13 +01:00
|
|
|
//
|
2022-08-10 19:52:39 +02:00
|
|
|
// The DesktopWindowTreeHostWin::HandleNativeFocus/HandleNativeBlur methods
|
2015-11-17 19:20:13 +01:00
|
|
|
// are called in response to WM_SETFOCUS/WM_KILLFOCUS respectively. The
|
2022-08-10 19:52:39 +02:00
|
|
|
// DesktopWindowTreeHostWin::HandleMouseEvent method is called if the user
|
|
|
|
// clicks on the WebContents. These methods have all been patched to call
|
|
|
|
// HandleActivationChanged (indirectly via ::SetFocus in the case of mouse
|
|
|
|
// clicks). HandleActivationChanged will then trigger the following
|
|
|
|
// behaviors:
|
2015-11-17 19:20:13 +01:00
|
|
|
// 1. Update focus/activation state of the aura::Window indirectly via
|
|
|
|
// wm::FocusController. This allows focus-related behaviors (e.g. focus
|
|
|
|
// rings, flashing caret, onFocus/onBlur JS events, etc.) to work as
|
2022-08-10 19:52:39 +02:00
|
|
|
// expected (see issue #1677) and also triggers an initial call to
|
|
|
|
// WebContents::Focus which gives logical focus to the
|
|
|
|
// RenderWidgetHostViewAura in the views hierarchy (see issue #3306).
|
2015-11-17 19:20:13 +01:00
|
|
|
// 2. Update focus state of the ui::InputMethod. If this does not occur
|
2022-08-05 20:58:31 +02:00
|
|
|
// then:
|
|
|
|
// (a) InputMethodBase::GetTextInputClient will return NULL and
|
|
|
|
// InputMethodWin::OnChar will fail to send character events to the
|
|
|
|
// renderer (see issue #1700); and
|
|
|
|
// (b) InputMethodWinBase::IsWindowFocused will return false due to
|
|
|
|
// ::GetFocus() returning the currently focused HWND (e.g.
|
|
|
|
// "CefBrowserWindow") instead of the expected "Chrome_WidgetWin_0" HWND,
|
|
|
|
// causing TSF not to handle IME events (see issue #3306). For this same
|
|
|
|
// reason, ::SetFocus needs to be called before WebContents::Focus which
|
|
|
|
// sends the InputMethod OnWillChangeFocusedClient notification that then
|
2022-08-10 19:52:39 +02:00
|
|
|
// calls IsWindowFocused (e.g. WebContents::Focus is intentionally called
|
|
|
|
// multiple times).
|
2015-11-17 19:20:13 +01:00
|
|
|
//
|
|
|
|
// This differs from activation in Chrome which is handled via
|
|
|
|
// HWNDMessageHandler::PostProcessActivateMessage (Widget::Show indirectly
|
|
|
|
// calls HWNDMessageHandler::Activate which calls ::SetForegroundWindow
|
|
|
|
// resulting in a WM_ACTIVATE message being sent to the window). The Chrome
|
|
|
|
// code path doesn't work for CEF because IsTopLevelWindow in
|
|
|
|
// hwnd_message_handler.cc will return false and consequently
|
|
|
|
// HWNDMessageHandler::PostProcessActivateMessage will not be called.
|
|
|
|
//
|
|
|
|
// Activation events are usually reserved for the top-level window so
|
|
|
|
// triggering activation based on focus events may be incorrect in some
|
|
|
|
// circumstances. Revisit this implementation if additional problems are
|
|
|
|
// discovered.
|
|
|
|
::SetFocus(HWNDForWidget(window_widget_));
|
|
|
|
}
|
2022-08-05 20:58:31 +02:00
|
|
|
|
|
|
|
if (web_contents_) {
|
|
|
|
// Give logical focus to the RenderWidgetHostViewAura in the views
|
2022-08-10 19:52:39 +02:00
|
|
|
// hierarchy. This does not change the native keyboard focus. When
|
|
|
|
// |window_widget_| exists this additional Focus() call is necessary to
|
|
|
|
// correctly assign focus/input state after native focus resulting from
|
|
|
|
// window activation (see the InputMethod discussion above).
|
2022-08-05 20:58:31 +02:00
|
|
|
web_contents_->Focus();
|
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeWin::NotifyMoveOrResizeStarted() {
|
|
|
|
// Call the parent method to dismiss any existing popups.
|
2022-04-08 22:48:56 +02:00
|
|
|
CefBrowserPlatformDelegateNativeAura::NotifyMoveOrResizeStarted();
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!window_widget_) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// Notify DesktopWindowTreeHostWin of move events so that screen rectangle
|
|
|
|
// information is communicated to the renderer process and popups are
|
|
|
|
// displayed in the correct location.
|
|
|
|
views::DesktopWindowTreeHostWin* tree_host =
|
|
|
|
static_cast<views::DesktopWindowTreeHostWin*>(
|
|
|
|
aura::WindowTreeHost::GetForAcceleratedWidget(
|
|
|
|
HWNDForWidget(window_widget_)));
|
|
|
|
DCHECK(tree_host);
|
|
|
|
if (tree_host) {
|
|
|
|
// Cast to HWNDMessageHandlerDelegate so we can access HandleMove().
|
|
|
|
static_cast<views::HWNDMessageHandlerDelegate*>(tree_host)->HandleMove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeWin::SizeTo(int width, int height) {
|
|
|
|
HWND window = window_info_.window;
|
|
|
|
|
2022-06-27 12:29:16 +02:00
|
|
|
const DWORD style = GetWindowLong(window, GWL_STYLE);
|
|
|
|
const DWORD ex_style = GetWindowLong(window, GWL_EXSTYLE);
|
2024-01-20 23:48:57 +01:00
|
|
|
const bool has_menu = !(style & WS_CHILD) && (GetMenu(window) != nullptr);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-10-25 01:47:04 +02:00
|
|
|
const auto frame_rect = GetScreenFrameRectFromDIPContentRect(
|
|
|
|
window, gfx::Rect(0, 0, width, height), style, ex_style, has_menu);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
// Size the window. The left/top values may be negative.
|
2024-01-20 23:48:57 +01:00
|
|
|
SetWindowPos(window, nullptr, 0, 0, frame_rect.width, frame_rect.height,
|
2017-05-17 11:29:28 +02:00
|
|
|
SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeWin::ViewText(const std::string& text) {
|
2022-11-15 18:50:53 +01:00
|
|
|
CEF_POST_USER_VISIBLE_TASK(base::BindOnce(WriteTempFileAndView, text));
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-03 02:15:09 +01:00
|
|
|
bool CefBrowserPlatformDelegateNativeWin::HandleKeyboardEvent(
|
2024-06-14 19:01:45 +02:00
|
|
|
const input::NativeWebKeyboardEvent& event) {
|
2015-11-17 19:20:13 +01:00
|
|
|
// Any unhandled keyboard/character messages are sent to DefWindowProc so that
|
|
|
|
// shortcut keys work correctly.
|
|
|
|
if (event.os_event) {
|
2021-09-20 11:06:23 +02:00
|
|
|
const auto& msg = event.os_event->native_event();
|
2018-11-03 02:15:09 +01:00
|
|
|
return !DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
|
2015-11-17 19:20:13 +01:00
|
|
|
} else {
|
|
|
|
MSG msg = {};
|
|
|
|
|
|
|
|
msg.hwnd = GetHostWindowHandle();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!msg.hwnd) {
|
2018-11-03 02:15:09 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
switch (event.GetType()) {
|
2020-06-09 19:48:00 +02:00
|
|
|
case blink::WebInputEvent::Type::kRawKeyDown:
|
2017-04-20 21:28:17 +02:00
|
|
|
msg.message = event.is_system_key ? WM_SYSKEYDOWN : WM_KEYDOWN;
|
2015-11-17 19:20:13 +01:00
|
|
|
break;
|
2020-06-09 19:48:00 +02:00
|
|
|
case blink::WebInputEvent::Type::kKeyUp:
|
2017-04-20 21:28:17 +02:00
|
|
|
msg.message = event.is_system_key ? WM_SYSKEYUP : WM_KEYUP;
|
2015-11-17 19:20:13 +01:00
|
|
|
break;
|
2020-06-09 19:48:00 +02:00
|
|
|
case blink::WebInputEvent::Type::kChar:
|
2017-05-17 11:29:28 +02:00
|
|
|
msg.message = event.is_system_key ? WM_SYSCHAR : WM_CHAR;
|
2015-11-17 19:20:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2018-11-03 02:15:09 +01:00
|
|
|
return false;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
msg.wParam = event.windows_key_code;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
UINT scan_code = ::MapVirtualKeyW(event.windows_key_code, MAPVK_VK_TO_VSC);
|
2015-11-17 19:20:13 +01:00
|
|
|
msg.lParam = (scan_code << 16) | // key scan code
|
2017-05-17 11:29:28 +02:00
|
|
|
1; // key repeat count
|
2024-06-14 19:01:45 +02:00
|
|
|
if (event.GetModifiers() & input::NativeWebKeyboardEvent::kAltKey) {
|
2015-11-17 19:20:13 +01:00
|
|
|
msg.lParam |= (1 << 29);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2018-11-03 02:15:09 +01:00
|
|
|
return !DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefEventHandle CefBrowserPlatformDelegateNativeWin::GetEventHandle(
|
2024-06-14 19:01:45 +02:00
|
|
|
const input::NativeWebKeyboardEvent& event) const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!event.os_event) {
|
2024-01-20 23:48:57 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-09-20 11:06:23 +02:00
|
|
|
return ChromeToWindowsType(
|
|
|
|
const_cast<CHROME_MSG*>(&event.os_event->native_event()));
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
ui::KeyEvent CefBrowserPlatformDelegateNativeWin::TranslateUiKeyEvent(
|
|
|
|
const CefKeyEvent& key_event) const {
|
|
|
|
int flags = TranslateUiEventModifiers(key_event.modifiers);
|
|
|
|
ui::KeyboardCode key_code =
|
|
|
|
ui::KeyboardCodeForWindowsKeyCode(key_event.windows_key_code);
|
|
|
|
ui::DomCode dom_code =
|
|
|
|
ui::KeycodeConverter::NativeKeycodeToDomCode(key_event.native_key_code);
|
|
|
|
base::TimeTicks time_stamp = GetEventTimeStamp();
|
|
|
|
|
|
|
|
if (key_event.type == KEYEVENT_CHAR) {
|
2023-08-09 23:17:17 +02:00
|
|
|
return ui::KeyEvent::FromCharacter(/*character=*/key_event.windows_key_code,
|
|
|
|
key_code, dom_code, flags, time_stamp);
|
2020-01-23 22:58:01 +01:00
|
|
|
}
|
|
|
|
|
2024-07-29 19:09:20 +02:00
|
|
|
ui::EventType type = ui::EventType::kUnknown;
|
2020-01-23 22:58:01 +01:00
|
|
|
switch (key_event.type) {
|
|
|
|
case KEYEVENT_RAWKEYDOWN:
|
|
|
|
case KEYEVENT_KEYDOWN:
|
2024-07-29 19:09:20 +02:00
|
|
|
type = ui::EventType::kKeyPressed;
|
2020-01-23 22:58:01 +01:00
|
|
|
break;
|
|
|
|
case KEYEVENT_KEYUP:
|
2024-07-29 19:09:20 +02:00
|
|
|
type = ui::EventType::kKeyReleased;
|
2020-01-23 22:58:01 +01:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2020-01-23 22:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ui::DomKey dom_key =
|
|
|
|
ui::PlatformKeyMap::DomKeyFromKeyboardCode(key_code, &flags);
|
|
|
|
return ui::KeyEvent(type, key_code, dom_code, flags, dom_key, time_stamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Vector2d CefBrowserPlatformDelegateNativeWin::GetUiWheelEventOffset(
|
|
|
|
int deltaX,
|
|
|
|
int deltaY) const {
|
|
|
|
static const ULONG defaultScrollCharsPerWheelDelta = 1;
|
|
|
|
static const FLOAT scrollbarPixelsPerLine = 100.0f / 3.0f;
|
|
|
|
static const ULONG defaultScrollLinesPerWheelDelta = 3;
|
|
|
|
|
|
|
|
float wheelDeltaX = float(deltaX) / WHEEL_DELTA;
|
|
|
|
float wheelDeltaY = float(deltaY) / WHEEL_DELTA;
|
|
|
|
float scrollDeltaX = wheelDeltaX;
|
|
|
|
float scrollDeltaY = wheelDeltaY;
|
|
|
|
|
|
|
|
ULONG scrollChars = defaultScrollCharsPerWheelDelta;
|
|
|
|
SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &scrollChars, 0);
|
|
|
|
scrollDeltaX *= static_cast<FLOAT>(scrollChars) * scrollbarPixelsPerLine;
|
|
|
|
|
|
|
|
ULONG scrollLines = defaultScrollLinesPerWheelDelta;
|
|
|
|
SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
|
|
|
|
scrollDeltaY *= static_cast<FLOAT>(scrollLines) * scrollbarPixelsPerLine;
|
|
|
|
|
|
|
|
return gfx::Vector2d(scrollDeltaX, scrollDeltaY);
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
// static
|
|
|
|
void CefBrowserPlatformDelegateNativeWin::RegisterWindowClass() {
|
|
|
|
static bool registered = false;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (registered) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// Register the window class
|
|
|
|
WNDCLASSEX wcex = {
|
2017-05-17 11:29:28 +02:00
|
|
|
/* cbSize = */ sizeof(WNDCLASSEX),
|
|
|
|
/* style = */ CS_HREDRAW | CS_VREDRAW,
|
|
|
|
/* lpfnWndProc = */ CefBrowserPlatformDelegateNativeWin::WndProc,
|
|
|
|
/* cbClsExtra = */ 0,
|
|
|
|
/* cbWndExtra = */ 0,
|
2024-01-20 23:48:57 +01:00
|
|
|
/* hInstance = */ ::GetModuleHandle(nullptr),
|
|
|
|
/* hIcon = */ nullptr,
|
|
|
|
/* hCursor = */ LoadCursor(nullptr, IDC_ARROW),
|
|
|
|
/* hbrBackground = */ nullptr,
|
|
|
|
/* lpszMenuName = */ nullptr,
|
2017-05-17 11:29:28 +02:00
|
|
|
/* lpszClassName = */ CefBrowserPlatformDelegateNativeWin::GetWndClass(),
|
2024-01-20 23:48:57 +01:00
|
|
|
/* hIconSm = */ nullptr,
|
2015-11-17 19:20:13 +01:00
|
|
|
};
|
|
|
|
RegisterClassEx(&wcex);
|
|
|
|
|
|
|
|
registered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
LPCTSTR CefBrowserPlatformDelegateNativeWin::GetWndClass() {
|
|
|
|
return L"CefBrowserWindow";
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-05-17 11:29:28 +02:00
|
|
|
LRESULT CALLBACK CefBrowserPlatformDelegateNativeWin::WndProc(HWND hwnd,
|
|
|
|
UINT message,
|
|
|
|
WPARAM wParam,
|
|
|
|
LPARAM lParam) {
|
2018-02-21 22:52:02 +01:00
|
|
|
CefBrowserPlatformDelegateNativeWin* platform_delegate = nullptr;
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserHostBase* browser = nullptr;
|
2018-02-21 22:52:02 +01:00
|
|
|
|
|
|
|
if (message != WM_NCCREATE) {
|
|
|
|
platform_delegate = static_cast<CefBrowserPlatformDelegateNativeWin*>(
|
|
|
|
gfx::GetWindowUserData(hwnd));
|
2020-09-25 03:40:47 +02:00
|
|
|
if (platform_delegate) {
|
2018-02-21 22:52:02 +01:00
|
|
|
browser = platform_delegate->browser_;
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
2018-02-21 22:52:02 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
switch (message) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case WM_CLOSE:
|
|
|
|
if (browser && !browser->TryCloseBrowser()) {
|
|
|
|
// Cancel the close.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the close.
|
|
|
|
break;
|
|
|
|
|
2018-02-21 22:52:02 +01:00
|
|
|
case WM_NCCREATE: {
|
|
|
|
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
|
|
|
|
platform_delegate =
|
|
|
|
reinterpret_cast<CefBrowserPlatformDelegateNativeWin*>(
|
|
|
|
cs->lpCreateParams);
|
|
|
|
DCHECK(platform_delegate);
|
|
|
|
// Associate |platform_delegate| with the window handle.
|
|
|
|
gfx::SetWindowUserData(hwnd, platform_delegate);
|
|
|
|
platform_delegate->window_info_.window = hwnd;
|
|
|
|
|
2023-02-27 19:52:38 +01:00
|
|
|
if (platform_delegate->has_frame_) {
|
2018-02-21 22:52:02 +01:00
|
|
|
// This call gets Windows to scale the non-client area when
|
|
|
|
// WM_DPICHANGED is fired on Windows versions >= 10.0.14393.0.
|
2023-02-27 19:52:38 +01:00
|
|
|
using EnableNonClientDpiScalingPtr =
|
|
|
|
decltype(::EnableNonClientDpiScaling)*;
|
|
|
|
static const auto enable_non_client_dpi_scaling_func =
|
|
|
|
reinterpret_cast<EnableNonClientDpiScalingPtr>(
|
|
|
|
base::win::GetUser32FunctionPointer(
|
|
|
|
"EnableNonClientDpiScaling"));
|
2018-02-21 22:52:02 +01:00
|
|
|
platform_delegate->called_enable_non_client_dpi_scaling_ =
|
|
|
|
!!(enable_non_client_dpi_scaling_func &&
|
|
|
|
enable_non_client_dpi_scaling_func(hwnd));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2017-09-01 00:10:51 +02:00
|
|
|
case WM_NCDESTROY:
|
2017-05-17 11:29:28 +02:00
|
|
|
if (platform_delegate) {
|
|
|
|
// Clear the user data pointer.
|
2024-01-20 23:48:57 +01:00
|
|
|
gfx::SetWindowUserData(hwnd, nullptr);
|
2017-05-17 11:29:28 +02:00
|
|
|
|
|
|
|
// Force the browser to be destroyed. This will result in a call to
|
|
|
|
// BrowserDestroyed() that will release the reference added in
|
|
|
|
// CreateHostWindow().
|
2024-04-17 18:01:26 +02:00
|
|
|
AlloyBrowserHostImpl::FromBaseChecked(browser)->WindowDestroyed();
|
2017-05-17 11:29:28 +02:00
|
|
|
}
|
2017-09-01 00:10:51 +02:00
|
|
|
break;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
case WM_SIZE:
|
|
|
|
if (platform_delegate && platform_delegate->window_widget_) {
|
|
|
|
// Pass window resize events to the HWND for the DesktopNativeWidgetAura
|
|
|
|
// root window. Passing size 0x0 (wParam == SIZE_MINIMIZED, for example)
|
|
|
|
// will cause the widget to be hidden which reduces resource usage.
|
|
|
|
RECT rc;
|
|
|
|
GetClientRect(hwnd, &rc);
|
2024-01-20 23:48:57 +01:00
|
|
|
SetWindowPos(HWNDForWidget(platform_delegate->window_widget_), nullptr,
|
2017-05-17 11:29:28 +02:00
|
|
|
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
|
|
|
|
SWP_NOZORDER);
|
|
|
|
}
|
|
|
|
return 0;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
case WM_MOVING:
|
|
|
|
case WM_MOVE:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser) {
|
2017-05-17 11:29:28 +02:00
|
|
|
browser->NotifyMoveOrResizeStarted();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
return 0;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
case WM_SETFOCUS:
|
2019-02-11 23:22:42 +01:00
|
|
|
// Selecting "Close window" from the task bar menu may send a focus
|
|
|
|
// notification even though the window is currently disabled (e.g. while
|
|
|
|
// a modal JS dialog is displayed).
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser && ::IsWindowEnabled(hwnd)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
browser->SetFocus(true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
return 0;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
case WM_ERASEBKGND:
|
|
|
|
return 0;
|
2018-02-21 22:52:02 +01:00
|
|
|
|
|
|
|
case WM_DPICHANGED:
|
|
|
|
if (platform_delegate && platform_delegate->has_frame_) {
|
|
|
|
// Suggested size and position of the current window scaled for the
|
|
|
|
// new DPI.
|
|
|
|
const RECT* rect = reinterpret_cast<RECT*>(lParam);
|
2024-01-20 23:48:57 +01:00
|
|
|
SetWindowPos(platform_delegate->GetHostWindowHandle(), nullptr,
|
|
|
|
rect->left, rect->top, rect->right - rect->left,
|
2018-02-21 22:52:02 +01:00
|
|
|
rect->bottom - rect->top, SWP_NOZORDER | SWP_NOACTIVATE);
|
|
|
|
}
|
|
|
|
break;
|
2022-07-15 13:04:21 +02:00
|
|
|
|
|
|
|
case WM_ENABLE:
|
|
|
|
if (wParam == TRUE && browser) {
|
|
|
|
// Give focus to the browser after EnableWindow enables this window
|
|
|
|
// (e.g. after a modal dialog is dismissed).
|
|
|
|
browser->SetFocus(true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProc(hwnd, message, wParam, lParam);
|
2023-02-07 00:43:00 +01:00
|
|
|
}
|