2020-09-18 00:24:08 +02:00
|
|
|
// Copyright 2020 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/browser_contents_delegate.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2024-07-02 20:16:55 +02:00
|
|
|
#include "cef/libcef/browser/browser_event_util.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_host_base.h"
|
|
|
|
#include "cef/libcef/browser/browser_platform_delegate.h"
|
|
|
|
#include "cef/libcef/browser/native/cursor_util.h"
|
|
|
|
#include "cef/libcef/common/frame_util.h"
|
2024-03-12 20:47:10 +01:00
|
|
|
#include "chrome/browser/ui/views/sad_tab_view.h"
|
|
|
|
#include "chrome/common/chrome_result_codes.h"
|
2024-06-14 19:01:45 +02:00
|
|
|
#include "components/input/native_web_keyboard_event.h"
|
2022-04-08 22:48:56 +02:00
|
|
|
#include "content/browser/renderer_host/render_widget_host_impl.h"
|
2021-06-04 03:34:56 +02:00
|
|
|
#include "content/public/browser/focused_node_details.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "content/public/browser/keyboard_event_processing_result.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/browser/navigation_handle.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
2021-10-04 11:58:22 +02:00
|
|
|
#include "content/public/browser/render_widget_host.h"
|
2022-04-08 22:48:56 +02:00
|
|
|
#include "content/public/browser/render_widget_host_observer.h"
|
2021-10-04 11:58:22 +02:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2023-12-06 21:16:15 +01:00
|
|
|
#include "services/network/public/mojom/url_response_head.mojom.h"
|
2021-04-21 00:52:34 +02:00
|
|
|
#include "third_party/blink/public/mojom/favicon/favicon_url.mojom.h"
|
2024-11-14 23:07:03 +01:00
|
|
|
#include "third_party/blink/public/mojom/input/focus_type.mojom.h"
|
2024-04-23 22:06:00 +02:00
|
|
|
#include "third_party/blink/public/mojom/page/draggable_region.mojom.h"
|
2022-04-08 22:48:56 +02:00
|
|
|
#include "third_party/blink/public/mojom/widget/platform_widget.mojom-test-utils.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2024-03-12 20:47:10 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "sandbox/win/src/sandbox_types.h"
|
|
|
|
#endif
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
using content::KeyboardEventProcessingResult;
|
|
|
|
|
2022-04-08 22:48:56 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefWidgetHostInterceptor
|
|
|
|
: public blink::mojom::WidgetHostInterceptorForTesting,
|
|
|
|
public content::RenderWidgetHostObserver {
|
|
|
|
public:
|
|
|
|
CefWidgetHostInterceptor(CefRefPtr<CefBrowser> browser,
|
2022-04-14 01:35:46 +02:00
|
|
|
content::RenderWidgetHost* render_widget_host)
|
2022-04-08 22:48:56 +02:00
|
|
|
: browser_(browser),
|
2022-04-14 01:35:46 +02:00
|
|
|
render_widget_host_(render_widget_host),
|
|
|
|
impl_(static_cast<content::RenderWidgetHostImpl*>(render_widget_host)
|
|
|
|
->widget_host_receiver_for_testing()
|
2022-04-08 22:48:56 +02:00
|
|
|
.SwapImplForTesting(this)) {
|
|
|
|
render_widget_host_->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefWidgetHostInterceptor(const CefWidgetHostInterceptor&) = delete;
|
|
|
|
CefWidgetHostInterceptor& operator=(const CefWidgetHostInterceptor&) = delete;
|
|
|
|
|
|
|
|
blink::mojom::WidgetHost* GetForwardingInterface() override { return impl_; }
|
|
|
|
|
|
|
|
// WidgetHostInterceptorForTesting method:
|
|
|
|
void SetCursor(const ui::Cursor& cursor) override {
|
|
|
|
if (cursor_util::OnCursorChange(browser_, cursor)) {
|
|
|
|
// Don't change the cursor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetForwardingInterface()->SetCursor(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderWidgetHostObserver method:
|
|
|
|
void RenderWidgetHostDestroyed(
|
|
|
|
content::RenderWidgetHost* widget_host) override {
|
|
|
|
widget_host->RemoveObserver(this);
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefBrowser> const browser_;
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<content::RenderWidgetHost> render_widget_host_;
|
|
|
|
const raw_ptr<blink::mojom::WidgetHost> impl_;
|
2022-04-08 22:48:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefBrowserContentsDelegate::CefBrowserContentsDelegate(
|
|
|
|
scoped_refptr<CefBrowserInfo> browser_info)
|
2024-07-18 19:50:37 +02:00
|
|
|
: browser_info_(browser_info) {}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::ObserveWebContents(
|
|
|
|
content::WebContents* new_contents) {
|
2020-09-25 03:40:47 +02:00
|
|
|
WebContentsObserver::Observe(new_contents);
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
if (new_contents) {
|
2021-10-04 11:58:22 +02:00
|
|
|
// Make sure MaybeCreateFrame is called at least one time.
|
2020-09-18 00:24:08 +02:00
|
|
|
// Create the frame representation before OnAfterCreated is called for a new
|
2021-10-04 11:58:22 +02:00
|
|
|
// browser.
|
2024-04-24 23:23:47 +02:00
|
|
|
browser_info_->MaybeCreateFrame(new_contents->GetPrimaryMainFrame());
|
2022-04-14 01:35:46 +02:00
|
|
|
|
|
|
|
// Make sure RenderWidgetCreated is called at least one time. This Observer
|
|
|
|
// is registered too late to catch the initial creation.
|
|
|
|
RenderWidgetCreated(new_contents->GetRenderViewHost()->GetWidget());
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::AddObserver(Observer* observer) {
|
|
|
|
observers_.AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::RemoveObserver(Observer* observer) {
|
|
|
|
observers_.RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// |source| may be NULL for navigations in the current tab, or if the
|
|
|
|
// navigation originates from a guest view via MaybeAllowNavigation.
|
2024-04-23 22:06:00 +02:00
|
|
|
content::WebContents* CefBrowserContentsDelegate::OpenURLFromTabEx(
|
2020-09-25 03:40:47 +02:00
|
|
|
content::WebContents* source,
|
2024-04-23 22:06:00 +02:00
|
|
|
const content::OpenURLParams& params,
|
|
|
|
base::OnceCallback<void(content::NavigationHandle&)>&
|
|
|
|
navigation_handle_callback) {
|
2020-09-25 03:40:47 +02:00
|
|
|
bool cancel = false;
|
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetRequestHandler()) {
|
2021-04-11 22:10:11 +02:00
|
|
|
// May return nullptr for omnibox navigations.
|
2024-01-26 03:12:43 +01:00
|
|
|
auto frame = browser_info_->browser()->GetFrameForHost(
|
|
|
|
content::RenderFrameHost::FromID(params.source_render_process_id,
|
|
|
|
params.source_render_frame_id));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame) {
|
2021-04-11 22:10:11 +02:00
|
|
|
frame = browser()->GetMainFrame();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
cancel = handler->OnOpenURLFromTab(
|
2021-04-11 22:10:11 +02:00
|
|
|
browser(), frame, params.url.spec(),
|
2020-09-25 03:40:47 +02:00
|
|
|
static_cast<cef_window_open_disposition_t>(params.disposition),
|
|
|
|
params.user_gesture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
if (!cancel) {
|
|
|
|
// TODO: Do something with |navigation_handle_callback|.
|
|
|
|
return web_contents();
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Returning nullptr will cancel the navigation.
|
2024-04-23 22:06:00 +02:00
|
|
|
return nullptr;
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::LoadingStateChanged(
|
|
|
|
content::WebContents* source,
|
2021-12-16 23:35:54 +01:00
|
|
|
bool should_show_loading_ui) {
|
2020-09-18 00:24:08 +02:00
|
|
|
const int current_index =
|
|
|
|
source->GetController().GetLastCommittedEntryIndex();
|
|
|
|
const int max_index = source->GetController().GetEntryCount() - 1;
|
|
|
|
|
|
|
|
const bool is_loading = source->IsLoading();
|
|
|
|
const bool can_go_back = (current_index > 0);
|
|
|
|
const bool can_go_forward = (current_index < max_index);
|
|
|
|
|
|
|
|
// This method may be called multiple times in a row with |is_loading|
|
|
|
|
// true as a result of https://crrev.com/5e750ad0. Ignore the 2nd+ times.
|
|
|
|
if (is_loading_ == is_loading && can_go_back_ == can_go_back &&
|
|
|
|
can_go_forward_ == can_go_forward) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_loading_ = is_loading;
|
|
|
|
can_go_back_ = can_go_back;
|
|
|
|
can_go_forward_ = can_go_forward;
|
|
|
|
OnStateChanged(State::kNavigation);
|
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetLoadHandler()) {
|
2021-04-03 00:07:43 +02:00
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
2020-09-18 00:24:08 +02:00
|
|
|
handler->OnLoadingStateChange(browser(), is_loading, can_go_back,
|
|
|
|
can_go_forward);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::UpdateTargetURL(content::WebContents* source,
|
|
|
|
const GURL& url) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
handler->OnStatusMessage(browser(), url.spec());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserContentsDelegate::DidAddMessageToConsole(
|
|
|
|
content::WebContents* source,
|
|
|
|
blink::mojom::ConsoleMessageLevel log_level,
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& message,
|
2020-09-18 00:24:08 +02:00
|
|
|
int32_t line_no,
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& source_id) {
|
2020-09-18 00:24:08 +02:00
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
// Use LOGSEVERITY_DEBUG for unrecognized |level| values.
|
|
|
|
cef_log_severity_t cef_level = LOGSEVERITY_DEBUG;
|
|
|
|
switch (log_level) {
|
|
|
|
case blink::mojom::ConsoleMessageLevel::kVerbose:
|
|
|
|
cef_level = LOGSEVERITY_DEBUG;
|
|
|
|
break;
|
|
|
|
case blink::mojom::ConsoleMessageLevel::kInfo:
|
|
|
|
cef_level = LOGSEVERITY_INFO;
|
|
|
|
break;
|
|
|
|
case blink::mojom::ConsoleMessageLevel::kWarning:
|
|
|
|
cef_level = LOGSEVERITY_WARNING;
|
|
|
|
break;
|
|
|
|
case blink::mojom::ConsoleMessageLevel::kError:
|
|
|
|
cef_level = LOGSEVERITY_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler->OnConsoleMessage(browser(), cef_level, message, source_id,
|
|
|
|
line_no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::EnterFullscreenModeForTab(
|
|
|
|
content::RenderFrameHost* requesting_frame,
|
|
|
|
const blink::mojom::FullscreenOptions& options) {
|
|
|
|
OnFullscreenModeChange(/*fullscreen=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::ExitFullscreenModeForTab(
|
|
|
|
content::WebContents* web_contents) {
|
|
|
|
OnFullscreenModeChange(/*fullscreen=*/false);
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:40:28 +01:00
|
|
|
void CefBrowserContentsDelegate::CanDownload(
|
|
|
|
const GURL& url,
|
|
|
|
const std::string& request_method,
|
|
|
|
base::OnceCallback<void(bool)> callback) {
|
|
|
|
bool allow = true;
|
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDownloadHandler()) {
|
|
|
|
allow = handler->CanDownload(browser(), url.spec(), request_method);
|
2022-03-22 22:40:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(callback).Run(allow);
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
KeyboardEventProcessingResult
|
|
|
|
CefBrowserContentsDelegate::PreHandleKeyboardEvent(
|
|
|
|
content::WebContents* source,
|
2024-06-14 19:01:45 +02:00
|
|
|
const input::NativeWebKeyboardEvent& event) {
|
2020-09-25 03:40:47 +02:00
|
|
|
if (auto delegate = platform_delegate()) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetKeyboardHandler()) {
|
|
|
|
CefKeyEvent cef_event;
|
2024-07-02 20:16:55 +02:00
|
|
|
if (GetCefKeyEvent(event, cef_event)) {
|
2020-09-25 03:40:47 +02:00
|
|
|
cef_event.focus_on_editable_field = focus_on_editable_field_;
|
|
|
|
|
|
|
|
auto event_handle = delegate->GetEventHandle(event);
|
|
|
|
bool is_keyboard_shortcut = false;
|
|
|
|
bool result = handler->OnPreKeyEvent(
|
|
|
|
browser(), cef_event, event_handle, &is_keyboard_shortcut);
|
|
|
|
if (result) {
|
|
|
|
return KeyboardEventProcessingResult::HANDLED;
|
|
|
|
} else if (is_keyboard_shortcut) {
|
|
|
|
return KeyboardEventProcessingResult::NOT_HANDLED_IS_SHORTCUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyboardEventProcessingResult::NOT_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserContentsDelegate::HandleKeyboardEvent(
|
|
|
|
content::WebContents* source,
|
2024-06-14 19:01:45 +02:00
|
|
|
const input::NativeWebKeyboardEvent& event) {
|
2020-09-25 03:40:47 +02:00
|
|
|
// Check to see if event should be ignored.
|
2023-09-15 21:51:43 +02:00
|
|
|
if (event.skip_if_unhandled) {
|
2020-09-25 03:40:47 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
|
|
|
|
if (auto delegate = platform_delegate()) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetKeyboardHandler()) {
|
|
|
|
CefKeyEvent cef_event;
|
2024-07-02 20:16:55 +02:00
|
|
|
if (GetCefKeyEvent(event, cef_event)) {
|
2020-09-25 03:40:47 +02:00
|
|
|
cef_event.focus_on_editable_field = focus_on_editable_field_;
|
|
|
|
|
|
|
|
auto event_handle = delegate->GetEventHandle(event);
|
|
|
|
if (handler->OnKeyEvent(browser(), cef_event, event_handle)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
void CefBrowserContentsDelegate::DraggableRegionsChanged(
|
|
|
|
const std::vector<blink::mojom::DraggableRegionPtr>& regions,
|
|
|
|
content::WebContents* contents) {
|
|
|
|
// Already converted to window bounds in WebViewImpl::DraggableRegionsChanged.
|
|
|
|
std::vector<cef::mojom::DraggableRegionEntryPtr> cef_regions;
|
|
|
|
if (!regions.empty()) {
|
|
|
|
cef_regions.reserve(regions.size());
|
|
|
|
for (const auto& region : regions) {
|
|
|
|
auto cef_region = cef::mojom::DraggableRegionEntry::New(
|
|
|
|
region->bounds, region->draggable);
|
|
|
|
cef_regions.emplace_back(std::move(cef_region));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
browser_info_->GetMainFrame()->UpdateDraggableRegions(std::move(cef_regions));
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::RenderFrameCreated(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2024-04-24 23:23:47 +02:00
|
|
|
browser_info_->MaybeCreateFrame(render_frame_host);
|
2021-10-04 11:58:22 +02:00
|
|
|
if (render_frame_host->GetParent() == nullptr) {
|
|
|
|
auto render_view_host = render_frame_host->GetRenderViewHost();
|
|
|
|
auto base_background_color = platform_delegate()->GetBackgroundColor();
|
|
|
|
if (browser_info_ && browser_info_->is_popup()) {
|
|
|
|
// force reset page base background color because popup window won't get
|
|
|
|
// the page base background from web_contents at the creation time
|
|
|
|
web_contents()->SetPageBaseBackgroundColor(SkColor());
|
|
|
|
web_contents()->SetPageBaseBackgroundColor(base_background_color);
|
|
|
|
}
|
2023-02-17 20:06:32 +01:00
|
|
|
if (render_view_host->GetWidget() &&
|
|
|
|
render_view_host->GetWidget()->GetView()) {
|
|
|
|
render_view_host->GetWidget()->GetView()->SetBackgroundColor(
|
|
|
|
base_background_color);
|
|
|
|
}
|
2022-04-08 22:48:56 +02:00
|
|
|
|
|
|
|
platform_delegate()->RenderViewCreated(render_view_host);
|
2021-10-04 11:58:22 +02:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::RenderFrameHostChanged(
|
|
|
|
content::RenderFrameHost* old_host,
|
|
|
|
content::RenderFrameHost* new_host) {
|
2024-05-13 23:36:09 +02:00
|
|
|
// Update tracking for the RFH.
|
2020-09-18 00:24:08 +02:00
|
|
|
RenderFrameCreated(new_host);
|
|
|
|
}
|
|
|
|
|
2021-07-25 19:31:39 +02:00
|
|
|
void CefBrowserContentsDelegate::RenderFrameHostStateChanged(
|
|
|
|
content::RenderFrameHost* host,
|
|
|
|
content::RenderFrameHost::LifecycleState old_state,
|
|
|
|
content::RenderFrameHost::LifecycleState new_state) {
|
|
|
|
browser_info_->FrameHostStateChanged(host, old_state, new_state);
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::RenderFrameDeleted(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
browser_info_->RemoveFrame(render_frame_host);
|
|
|
|
|
2024-01-26 03:12:43 +01:00
|
|
|
if (focused_frame_ && focused_frame_->IsSameFrame(render_frame_host)) {
|
2020-09-18 00:24:08 +02:00
|
|
|
focused_frame_ = nullptr;
|
|
|
|
OnStateChanged(State::kFocusedFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 01:35:46 +02:00
|
|
|
void CefBrowserContentsDelegate::RenderWidgetCreated(
|
|
|
|
content::RenderWidgetHost* render_widget_host) {
|
|
|
|
new CefWidgetHostInterceptor(browser(), render_widget_host);
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::RenderViewReady() {
|
2022-04-08 22:48:56 +02:00
|
|
|
platform_delegate()->RenderViewReady();
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetRequestHandler()) {
|
|
|
|
handler->OnRenderViewReady(browser());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:57:31 +01:00
|
|
|
void CefBrowserContentsDelegate::PrimaryMainFrameRenderProcessGone(
|
2020-09-18 00:24:08 +02:00
|
|
|
base::TerminationStatus status) {
|
2024-03-12 20:47:10 +01:00
|
|
|
static_assert(static_cast<int>(CEF_RESULT_CODE_CHROME_FIRST) ==
|
|
|
|
static_cast<int>(chrome::RESULT_CODE_CHROME_START),
|
|
|
|
"enum mismatch");
|
|
|
|
static_assert(static_cast<int>(CEF_RESULT_CODE_CHROME_LAST) ==
|
|
|
|
static_cast<int>(chrome::RESULT_CODE_CHROME_LAST_CODE),
|
|
|
|
"enum mismatch");
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
static_assert(static_cast<int>(CEF_RESULT_CODE_SANDBOX_FATAL_FIRST) ==
|
|
|
|
static_cast<int>(sandbox::SBOX_FATAL_INTEGRITY),
|
|
|
|
"enum mismatch");
|
|
|
|
static_assert(static_cast<int>(CEF_RESULT_CODE_SANDBOX_FATAL_LAST) ==
|
|
|
|
static_cast<int>(sandbox::SBOX_FATAL_LAST),
|
|
|
|
"enum mismatch");
|
|
|
|
#endif
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
cef_termination_status_t ts = TS_ABNORMAL_TERMINATION;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
|
2020-09-18 00:24:08 +02:00
|
|
|
ts = TS_PROCESS_WAS_KILLED;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (status == base::TERMINATION_STATUS_PROCESS_CRASHED) {
|
2020-09-18 00:24:08 +02:00
|
|
|
ts = TS_PROCESS_CRASHED;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (status == base::TERMINATION_STATUS_OOM) {
|
2020-09-18 00:24:08 +02:00
|
|
|
ts = TS_PROCESS_OOM;
|
2024-03-12 20:47:10 +01:00
|
|
|
} else if (status == base::TERMINATION_STATUS_LAUNCH_FAILED) {
|
|
|
|
ts = TS_LAUNCH_FAILED;
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
} else if (status == base::TERMINATION_STATUS_INTEGRITY_FAILURE) {
|
|
|
|
ts = TS_INTEGRITY_FAILURE;
|
|
|
|
#endif
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (status != base::TERMINATION_STATUS_ABNORMAL_TERMINATION) {
|
2020-09-18 00:24:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetRequestHandler()) {
|
2024-03-12 20:47:10 +01:00
|
|
|
int error_code = web_contents()->GetCrashedErrorCode();
|
2020-09-18 00:24:08 +02:00
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
2024-03-12 20:47:10 +01:00
|
|
|
handler->OnRenderProcessTerminated(browser(), ts, error_code,
|
|
|
|
SadTabView::ErrorToString(error_code));
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnFrameFocused(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame = static_cast<CefFrameHostImpl*>(
|
|
|
|
browser_info_->GetFrameForHost(render_frame_host).get());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame || frame->IsFocused()) {
|
2020-09-18 00:24:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefFrameHostImpl> previous_frame = focused_frame_;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (frame->IsMain()) {
|
2020-09-18 00:24:08 +02:00
|
|
|
focused_frame_ = nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2020-09-18 00:24:08 +02:00
|
|
|
focused_frame_ = frame;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
if (!previous_frame) {
|
|
|
|
// The main frame is focused by default.
|
|
|
|
previous_frame = browser_info_->GetMainFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous_frame->GetIdentifier() != frame->GetIdentifier()) {
|
|
|
|
previous_frame->SetFocused(false);
|
|
|
|
frame->SetFocused(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
OnStateChanged(State::kFocusedFrame);
|
|
|
|
}
|
|
|
|
|
2022-02-21 23:23:40 +01:00
|
|
|
void CefBrowserContentsDelegate::PrimaryMainDocumentElementAvailable() {
|
2020-09-18 00:24:08 +02:00
|
|
|
has_document_ = true;
|
|
|
|
OnStateChanged(State::kDocument);
|
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetRequestHandler()) {
|
|
|
|
handler->OnDocumentAvailableInMainFrame(browser());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::LoadProgressChanged(double progress) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
handler->OnLoadingProgressChange(browser(), progress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::DidStopLoading() {
|
|
|
|
// Notify all renderers that loading has stopped. We used to use
|
|
|
|
// RenderFrameObserver::DidStopLoading in the renderer process but that was
|
|
|
|
// removed in https://crrev.com/3e37dd0ead. However, that callback wasn't
|
|
|
|
// necessarily accurate because it wasn't called in all of the cases where
|
|
|
|
// RenderFrameImpl sends the FrameHostMsg_DidStopLoading message. This adds
|
|
|
|
// an additional round trip but should provide the same or improved
|
|
|
|
// functionality.
|
|
|
|
for (const auto& frame : browser_info_->GetAllFrames()) {
|
|
|
|
frame->MaybeSendDidStopLoading();
|
|
|
|
}
|
2023-12-06 21:16:15 +01:00
|
|
|
|
|
|
|
OnTitleChange(web_contents()->GetTitle());
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::DidFinishNavigation(
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
|
|
|
const net::Error error_code = navigation_handle->GetNetErrorCode();
|
|
|
|
|
|
|
|
// Skip calls where the navigation has not yet committed and there is no
|
|
|
|
// error code. For example, when creating a browser without loading a URL.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!navigation_handle->HasCommitted() && error_code == net::OK) {
|
2020-09-18 00:24:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
if (browser_info_->IsClosing()) {
|
|
|
|
// Ignore notifications when the browser is closing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
if (navigation_handle->IsInPrimaryMainFrame() &&
|
|
|
|
navigation_handle->HasCommitted()) {
|
|
|
|
// A primary main frame navigation has occured.
|
|
|
|
has_document_ = false;
|
|
|
|
OnStateChanged(State::kDocument);
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
const bool is_main_frame = navigation_handle->IsInMainFrame();
|
2021-08-19 23:07:44 +02:00
|
|
|
const auto global_id = frame_util::GetGlobalId(navigation_handle);
|
2020-09-18 00:24:08 +02:00
|
|
|
const GURL& url =
|
|
|
|
(error_code == net::OK ? navigation_handle->GetURL() : GURL());
|
|
|
|
|
|
|
|
// May return NULL when starting a new navigation if the previous navigation
|
|
|
|
// caused the renderer process to crash during load.
|
2021-08-19 23:07:44 +02:00
|
|
|
CefRefPtr<CefFrameHostImpl> frame =
|
2024-05-13 23:36:09 +02:00
|
|
|
browser_info_->GetFrameForGlobalId(global_id);
|
2020-09-18 00:24:08 +02:00
|
|
|
if (!frame) {
|
|
|
|
if (is_main_frame) {
|
2024-05-13 23:36:09 +02:00
|
|
|
frame = browser_info_->GetMainFrame();
|
2020-09-18 00:24:08 +02:00
|
|
|
} else {
|
2024-05-13 23:36:09 +02:00
|
|
|
frame = browser_info_->CreateTempSubFrame(frame_util::InvalidGlobalId());
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
frame->RefreshAttributes();
|
|
|
|
|
|
|
|
if (error_code == net::OK) {
|
|
|
|
// The navigation has been committed and there is no error.
|
|
|
|
DCHECK(navigation_handle->HasCommitted());
|
|
|
|
|
|
|
|
// Don't call OnLoadStart for same page navigations (fragments,
|
|
|
|
// history state).
|
|
|
|
if (!navigation_handle->IsSameDocument()) {
|
|
|
|
OnLoadStart(frame.get(), navigation_handle->GetPageTransition());
|
2022-04-05 19:22:32 +02:00
|
|
|
if (navigation_handle->IsServedFromBackForwardCache()) {
|
|
|
|
// We won't get an OnLoadEnd notification from anywhere else.
|
|
|
|
OnLoadEnd(frame.get(), navigation_handle->GetURL(), 0);
|
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_main_frame) {
|
|
|
|
OnAddressChange(url);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The navigation failed with an error. This may happen before commit
|
|
|
|
// (e.g. network error) or after commit (e.g. response filter error).
|
|
|
|
// If the error happened before commit then this call will originate from
|
|
|
|
// RenderFrameHostImpl::OnDidFailProvisionalLoadWithError.
|
|
|
|
// OnLoadStart/OnLoadEnd will not be called.
|
|
|
|
OnLoadError(frame.get(), navigation_handle->GetURL(), error_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::DidFailLoad(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const GURL& validated_url,
|
|
|
|
int error_code) {
|
|
|
|
// The navigation failed after commit. OnLoadStart was called so we also
|
|
|
|
// call OnLoadEnd.
|
|
|
|
auto frame = browser_info_->GetFrameForHost(render_frame_host);
|
|
|
|
frame->RefreshAttributes();
|
|
|
|
OnLoadError(frame, validated_url, error_code);
|
|
|
|
OnLoadEnd(frame, validated_url, error_code);
|
|
|
|
}
|
|
|
|
|
2022-08-26 00:17:51 +02:00
|
|
|
void CefBrowserContentsDelegate::DidFinishLoad(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const GURL& validated_url) {
|
|
|
|
auto frame = browser_info_->GetFrameForHost(render_frame_host);
|
2024-04-24 21:38:49 +02:00
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-26 00:17:51 +02:00
|
|
|
frame->RefreshAttributes();
|
|
|
|
|
|
|
|
int http_status_code = 0;
|
2023-12-06 21:16:15 +01:00
|
|
|
if (auto response_head = render_frame_host->GetLastResponseHead()) {
|
|
|
|
if (response_head->headers) {
|
|
|
|
http_status_code = response_head->headers->response_code();
|
|
|
|
}
|
2022-08-26 00:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
OnLoadEnd(frame, validated_url, http_status_code);
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::TitleWasSet(content::NavigationEntry* entry) {
|
|
|
|
// |entry| may be NULL if a popup is created via window.open and never
|
|
|
|
// navigated.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (entry) {
|
2020-09-18 00:24:08 +02:00
|
|
|
OnTitleChange(entry->GetTitle());
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (web_contents()) {
|
2020-09-18 00:24:08 +02:00
|
|
|
OnTitleChange(web_contents()->GetTitle());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::DidUpdateFaviconURL(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const std::vector<blink::mojom::FaviconURLPtr>& candidates) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
std::vector<CefString> icon_urls;
|
|
|
|
for (const auto& icon : candidates) {
|
|
|
|
if (icon->icon_type == blink::mojom::FaviconIconType::kFavicon) {
|
|
|
|
icon_urls.push_back(icon->icon_url.spec());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!icon_urls.empty()) {
|
|
|
|
handler->OnFaviconURLChange(browser(), icon_urls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnWebContentsFocused(
|
|
|
|
content::RenderWidgetHost* render_widget_host) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetFocusHandler()) {
|
|
|
|
handler->OnGotFocus(browser());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 03:34:56 +02:00
|
|
|
void CefBrowserContentsDelegate::OnFocusChangedInPage(
|
|
|
|
content::FocusedNodeDetails* details) {
|
|
|
|
focus_on_editable_field_ =
|
2024-11-14 23:07:03 +01:00
|
|
|
details->focus_type != blink::mojom::FocusType::kNone &&
|
2021-06-04 03:34:56 +02:00
|
|
|
details->is_editable_node;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::WebContentsDestroyed() {
|
2020-09-25 03:40:47 +02:00
|
|
|
auto wc = web_contents();
|
2020-09-18 00:24:08 +02:00
|
|
|
ObserveWebContents(nullptr);
|
|
|
|
for (auto& observer : observers_) {
|
2020-09-25 03:40:47 +02:00
|
|
|
observer.OnWebContentsDestroyed(wc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserContentsDelegate::OnSetFocus(cef_focus_source_t source) {
|
|
|
|
// SetFocus() might be called while inside the OnSetFocus() callback. If
|
|
|
|
// so, don't re-enter the callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (is_in_onsetfocus_) {
|
2020-09-25 03:40:47 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetFocusHandler()) {
|
|
|
|
is_in_onsetfocus_ = true;
|
|
|
|
bool handled = handler->OnSetFocus(browser(), source);
|
|
|
|
is_in_onsetfocus_ = false;
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefClient> CefBrowserContentsDelegate::client() const {
|
|
|
|
if (auto b = browser()) {
|
|
|
|
return b->GetHost()->GetClient();
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowser> CefBrowserContentsDelegate::browser() const {
|
|
|
|
return browser_info_->browser();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserPlatformDelegate* CefBrowserContentsDelegate::platform_delegate()
|
|
|
|
const {
|
|
|
|
auto browser = browser_info_->browser();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser) {
|
2020-09-25 03:40:47 +02:00
|
|
|
return browser->platform_delegate();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
return nullptr;
|
2020-09-18 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnAddressChange(const GURL& url) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
// On the handler of an address change.
|
|
|
|
handler->OnAddressChange(browser(), browser_info_->GetMainFrame(),
|
|
|
|
url.spec());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnLoadStart(
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
ui::PageTransition transition_type) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetLoadHandler()) {
|
2021-04-03 00:07:43 +02:00
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
2020-09-18 00:24:08 +02:00
|
|
|
// On the handler that loading has started.
|
|
|
|
handler->OnLoadStart(browser(), frame,
|
|
|
|
static_cast<cef_transition_type_t>(transition_type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 00:17:51 +02:00
|
|
|
void CefBrowserContentsDelegate::OnLoadEnd(CefRefPtr<CefFrame> frame,
|
|
|
|
const GURL& url,
|
|
|
|
int http_status_code) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetLoadHandler()) {
|
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
|
|
|
handler->OnLoadEnd(browser(), frame, http_status_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::OnLoadError(CefRefPtr<CefFrame> frame,
|
|
|
|
const GURL& url,
|
|
|
|
int error_code) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetLoadHandler()) {
|
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
|
|
|
// On the handler that loading has failed.
|
|
|
|
handler->OnLoadError(browser(), frame,
|
|
|
|
static_cast<cef_errorcode_t>(error_code),
|
|
|
|
net::ErrorToShortString(error_code), url.spec());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void CefBrowserContentsDelegate::OnTitleChange(const std::u16string& title) {
|
2020-09-18 00:24:08 +02:00
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
handler->OnTitleChange(browser(), title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnFullscreenModeChange(bool fullscreen) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (fullscreen == is_fullscreen_) {
|
2020-09-18 00:24:08 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
is_fullscreen_ = fullscreen;
|
|
|
|
OnStateChanged(State::kFullscreen);
|
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDisplayHandler()) {
|
|
|
|
handler->OnFullscreenModeChange(browser(), fullscreen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnStateChanged(State state_changed) {
|
|
|
|
for (auto& observer : observers_) {
|
|
|
|
observer.OnStateChanged(state_changed);
|
|
|
|
}
|
|
|
|
}
|