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.
|
|
|
|
|
|
|
|
#include "libcef/browser/browser_contents_delegate.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/browser_host_base.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "libcef/browser/browser_platform_delegate.h"
|
|
|
|
#include "libcef/browser/browser_util.h"
|
2022-04-08 22:48:56 +02:00
|
|
|
#include "libcef/browser/native/cursor_util.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "libcef/common/frame_util.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
|
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"
|
|
|
|
#include "content/public/browser/native_web_keyboard_event.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/browser/navigation_handle.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "content/public/browser/notification_details.h"
|
|
|
|
#include "content/public/browser/notification_source.h"
|
|
|
|
#include "content/public/browser/notification_types.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#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"
|
2021-04-21 00:52:34 +02:00
|
|
|
#include "third_party/blink/public/mojom/favicon/favicon_url.mojom.h"
|
2021-06-04 03:34:56 +02:00
|
|
|
#include "third_party/blink/public/mojom/input/focus_type.mojom-blink.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
|
|
|
|
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_;
|
2022-04-14 01:35:46 +02:00
|
|
|
content::RenderWidgetHost* const render_widget_host_;
|
2022-04-08 22:48:56 +02:00
|
|
|
blink::mojom::WidgetHost* const impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefBrowserContentsDelegate::CefBrowserContentsDelegate(
|
|
|
|
scoped_refptr<CefBrowserInfo> browser_info)
|
|
|
|
: browser_info_(browser_info) {
|
|
|
|
DCHECK(browser_info_->browser());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-09-25 03:40:47 +02:00
|
|
|
registrar_.reset(new content::NotificationRegistrar);
|
|
|
|
|
|
|
|
// When navigating through the history, the restored NavigationEntry's title
|
|
|
|
// will be used. If the entry ends up having the same title after we return
|
|
|
|
// to it, as will usually be the case, the
|
|
|
|
// NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED will then be suppressed, since
|
|
|
|
// the NavigationEntry's title hasn't changed.
|
|
|
|
registrar_->Add(this, content::NOTIFICATION_LOAD_STOP,
|
|
|
|
content::Source<content::NavigationController>(
|
|
|
|
&new_contents->GetController()));
|
|
|
|
|
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.
|
2022-07-21 19:26:10 +02:00
|
|
|
browser_info_->MaybeCreateFrame(new_contents->GetPrimaryMainFrame(),
|
2021-10-04 11:58:22 +02:00
|
|
|
false /* is_guest_view */);
|
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-25 03:40:47 +02:00
|
|
|
} else {
|
|
|
|
registrar_.reset();
|
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.
|
|
|
|
content::WebContents* CefBrowserContentsDelegate::OpenURLFromTab(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::OpenURLParams& params) {
|
|
|
|
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.
|
|
|
|
auto frame = browser()->GetFrame(params.frame_tree_node_id);
|
|
|
|
if (!frame)
|
|
|
|
frame = browser()->GetMainFrame();
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returning nullptr will cancel the navigation.
|
|
|
|
return cancel ? nullptr : web_contents();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
void CefBrowserContentsDelegate::DidNavigatePrimaryMainFramePostCommit(
|
2020-09-18 00:24:08 +02:00
|
|
|
content::WebContents* web_contents) {
|
|
|
|
has_document_ = false;
|
|
|
|
OnStateChanged(State::kDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (auto delegate = platform_delegate()) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetDownloadHandler()) {
|
|
|
|
allow = handler->CanDownload(browser(), url.spec(), request_method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(callback).Run(allow);
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
KeyboardEventProcessingResult
|
|
|
|
CefBrowserContentsDelegate::PreHandleKeyboardEvent(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
if (auto delegate = platform_delegate()) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetKeyboardHandler()) {
|
|
|
|
CefKeyEvent cef_event;
|
|
|
|
if (browser_util::GetCefKeyEvent(event, cef_event)) {
|
|
|
|
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,
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
// Check to see if event should be ignored.
|
|
|
|
if (event.skip_in_browser)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (auto delegate = platform_delegate()) {
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetKeyboardHandler()) {
|
|
|
|
CefKeyEvent cef_event;
|
|
|
|
if (browser_util::GetCefKeyEvent(event, cef_event)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void CefBrowserContentsDelegate::RenderFrameCreated(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
browser_info_->MaybeCreateFrame(render_frame_host, false /* is_guest_view */);
|
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);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
// Just in case RenderFrameCreated wasn't called for some reason.
|
|
|
|
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) {
|
2021-08-19 23:07:44 +02:00
|
|
|
const auto frame_id =
|
|
|
|
frame_util::MakeFrameId(render_frame_host->GetGlobalId());
|
2020-09-18 00:24:08 +02:00
|
|
|
browser_info_->RemoveFrame(render_frame_host);
|
|
|
|
|
|
|
|
if (focused_frame_ && focused_frame_->GetIdentifier() == frame_id) {
|
|
|
|
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) {
|
|
|
|
cef_termination_status_t ts = TS_ABNORMAL_TERMINATION;
|
|
|
|
if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED)
|
|
|
|
ts = TS_PROCESS_WAS_KILLED;
|
|
|
|
else if (status == base::TERMINATION_STATUS_PROCESS_CRASHED)
|
|
|
|
ts = TS_PROCESS_CRASHED;
|
|
|
|
else if (status == base::TERMINATION_STATUS_OOM)
|
|
|
|
ts = TS_PROCESS_OOM;
|
|
|
|
else if (status != base::TERMINATION_STATUS_ABNORMAL_TERMINATION)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto c = client()) {
|
|
|
|
if (auto handler = c->GetRequestHandler()) {
|
|
|
|
auto navigation_lock = browser_info_->CreateNavigationLock();
|
|
|
|
handler->OnRenderProcessTerminated(browser(), ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::OnFrameFocused(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame = static_cast<CefFrameHostImpl*>(
|
|
|
|
browser_info_->GetFrameForHost(render_frame_host).get());
|
|
|
|
if (!frame || frame->IsFocused())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CefRefPtr<CefFrameHostImpl> previous_frame = focused_frame_;
|
|
|
|
if (frame->IsMain())
|
|
|
|
focused_frame_ = nullptr;
|
|
|
|
else
|
|
|
|
focused_frame_ = frame;
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (!navigation_handle->HasCommitted() && error_code == net::OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
auto browser_info = browser_info_;
|
2021-09-15 20:40:53 +02:00
|
|
|
if (!browser_info->browser()) {
|
|
|
|
// Ignore notifications when the browser is closing.
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// 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 =
|
|
|
|
browser_info->GetFrameForGlobalId(global_id);
|
2020-09-18 00:24:08 +02:00
|
|
|
if (!frame) {
|
|
|
|
if (is_main_frame) {
|
|
|
|
frame = browser_info->GetMainFrame();
|
|
|
|
} else {
|
2021-08-19 23:07:44 +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);
|
|
|
|
frame->RefreshAttributes();
|
|
|
|
|
|
|
|
int http_status_code = 0;
|
|
|
|
if (auto response_headers = render_frame_host->GetLastResponseHeaders()) {
|
|
|
|
http_status_code = response_headers->response_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (entry)
|
|
|
|
OnTitleChange(entry->GetTitle());
|
|
|
|
else if (web_contents())
|
|
|
|
OnTitleChange(web_contents()->GetTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
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_ =
|
|
|
|
details->focus_type != blink::mojom::blink::FocusType::kNone &&
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserContentsDelegate::Observe(
|
|
|
|
int type,
|
|
|
|
const content::NotificationSource& source,
|
|
|
|
const content::NotificationDetails& details) {
|
2021-06-04 03:34:56 +02:00
|
|
|
DCHECK_EQ(type, content::NOTIFICATION_LOAD_STOP);
|
2020-09-25 03:40:47 +02:00
|
|
|
|
|
|
|
if (type == content::NOTIFICATION_LOAD_STOP) {
|
2021-07-23 18:40:13 +02:00
|
|
|
OnTitleChange(web_contents()->GetTitle());
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (is_in_onsetfocus_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (browser)
|
|
|
|
return browser->platform_delegate();
|
|
|
|
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) {
|
|
|
|
if (fullscreen == is_fullscreen_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|