2014-04-04 18:50:38 +02:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2022-02-21 23:23:40 +01:00
|
|
|
#include "build/build_config.h"
|
2015-04-20 13:11:11 +02:00
|
|
|
|
2018-10-02 14:14:11 +02:00
|
|
|
// Enable deprecation warnings on Windows. See http://crbug.com/585142.
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-10-02 14:14:11 +02:00
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic error "-Wdeprecated-declarations"
|
|
|
|
#else
|
2016-03-16 03:55:59 +01:00
|
|
|
#pragma warning(push)
|
2017-05-17 11:29:28 +02:00
|
|
|
#pragma warning(default : 4996)
|
2016-03-16 03:55:59 +01:00
|
|
|
#endif
|
2018-10-02 14:14:11 +02:00
|
|
|
#endif
|
2015-04-20 13:11:11 +02:00
|
|
|
|
2014-04-04 18:50:38 +02:00
|
|
|
#include "libcef/renderer/render_frame_observer.h"
|
|
|
|
|
2020-06-28 23:05:36 +02:00
|
|
|
#include "libcef/common/app_manager.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "libcef/renderer/blink_glue.h"
|
2019-05-11 00:14:48 +02:00
|
|
|
#include "libcef/renderer/browser_impl.h"
|
2019-05-24 22:23:43 +02:00
|
|
|
#include "libcef/renderer/dom_document_impl.h"
|
2015-04-20 13:11:11 +02:00
|
|
|
#include "libcef/renderer/v8_impl.h"
|
2014-04-04 18:50:38 +02:00
|
|
|
|
|
|
|
#include "content/public/renderer/render_frame.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/web/blink.h"
|
2019-05-24 22:23:43 +02:00
|
|
|
#include "third_party/blink/public/web/web_document.h"
|
2022-04-21 20:58:48 +02:00
|
|
|
#include "third_party/blink/public/web/web_element.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
2019-05-24 22:23:43 +02:00
|
|
|
#include "third_party/blink/public/web/web_node.h"
|
2014-04-04 18:50:38 +02:00
|
|
|
|
|
|
|
CefRenderFrameObserver::CefRenderFrameObserver(
|
|
|
|
content::RenderFrame* render_frame)
|
2017-05-17 11:29:28 +02:00
|
|
|
: content::RenderFrameObserver(render_frame) {}
|
2014-04-04 18:50:38 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
CefRenderFrameObserver::~CefRenderFrameObserver() = default;
|
2020-01-23 22:58:01 +01:00
|
|
|
|
2019-07-16 19:59:21 +02:00
|
|
|
void CefRenderFrameObserver::DidCommitProvisionalLoad(
|
|
|
|
ui::PageTransition transition) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_) {
|
2019-07-16 19:59:21 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-16 19:59:21 +02:00
|
|
|
|
2022-06-17 15:28:55 +02:00
|
|
|
frame_->OnDidCommitProvisionalLoad();
|
|
|
|
|
2019-07-16 19:59:21 +02:00
|
|
|
if (frame_->GetParent() == nullptr) {
|
|
|
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
|
|
|
CefRefPtr<CefBrowserImpl> browserPtr =
|
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(frame->Top());
|
|
|
|
browserPtr->OnLoadingStateChange(true);
|
|
|
|
}
|
|
|
|
OnLoadStart();
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:52:36 +01:00
|
|
|
void CefRenderFrameObserver::WasShown() {
|
|
|
|
if (frame_) {
|
|
|
|
frame_->OnWasShown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:11:44 +01:00
|
|
|
void CefRenderFrameObserver::DidFailProvisionalLoad() {
|
2019-07-16 19:59:21 +02:00
|
|
|
if (frame_) {
|
2019-11-12 17:11:44 +01:00
|
|
|
OnLoadError();
|
2019-07-16 19:59:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
void CefRenderFrameObserver::DidFinishLoad() {
|
2019-05-24 22:23:43 +02:00
|
|
|
if (frame_) {
|
|
|
|
frame_->OnDidFinishLoad();
|
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
2023-12-06 21:16:15 +01:00
|
|
|
void CefRenderFrameObserver::WillDetach(blink::DetachReason detach_reason) {
|
2019-05-24 22:23:43 +02:00
|
|
|
if (frame_) {
|
|
|
|
frame_->OnDetached();
|
|
|
|
frame_ = nullptr;
|
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 19:59:21 +02:00
|
|
|
void CefRenderFrameObserver::FocusedElementChanged(
|
|
|
|
const blink::WebElement& element) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_) {
|
2019-07-16 19:59:21 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-16 19:59:21 +02:00
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
|
|
|
CefRefPtr<CefBrowserImpl> browserPtr =
|
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(frame->Top());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browserPtr) {
|
2017-05-31 17:33:30 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefRenderProcessHandler> handler;
|
2020-06-28 23:05:36 +02:00
|
|
|
CefRefPtr<CefApp> application = CefAppManager::Get()->GetApplication();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (application) {
|
2019-05-24 22:23:43 +02:00
|
|
|
handler = application->GetRenderProcessHandler();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!handler) {
|
2019-05-24 22:23:43 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefFrameImpl> framePtr = browserPtr->GetWebFrameImpl(frame);
|
|
|
|
|
2019-07-16 19:59:21 +02:00
|
|
|
if (element.IsNull()) {
|
2019-05-24 22:23:43 +02:00
|
|
|
handler->OnFocusedNodeChanged(browserPtr.get(), framePtr.get(), nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (element.GetDocument().IsNull()) {
|
2017-07-27 01:19:27 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-07-27 01:19:27 +02:00
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
CefRefPtr<CefDOMDocumentImpl> documentImpl =
|
|
|
|
new CefDOMDocumentImpl(browserPtr.get(), frame);
|
|
|
|
handler->OnFocusedNodeChanged(browserPtr.get(), framePtr.get(),
|
2019-07-16 19:59:21 +02:00
|
|
|
documentImpl->GetOrCreateNode(element));
|
2019-05-24 22:23:43 +02:00
|
|
|
documentImpl->Detach();
|
|
|
|
}
|
|
|
|
|
2015-04-20 13:11:11 +02:00
|
|
|
void CefRenderFrameObserver::DidCreateScriptContext(
|
|
|
|
v8::Handle<v8::Context> context,
|
|
|
|
int world_id) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_) {
|
2019-07-16 19:59:21 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-16 19:59:21 +02:00
|
|
|
|
2015-04-20 13:11:11 +02:00
|
|
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
|
|
|
CefRefPtr<CefBrowserImpl> browserPtr =
|
2017-04-20 21:28:17 +02:00
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(frame->Top());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browserPtr) {
|
2015-04-20 13:11:11 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-20 13:11:11 +02:00
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
CefRefPtr<CefRenderProcessHandler> handler;
|
2020-06-28 23:05:36 +02:00
|
|
|
CefRefPtr<CefApp> application = CefAppManager::Get()->GetApplication();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (application) {
|
2015-07-16 23:40:01 +02:00
|
|
|
handler = application->GetRenderProcessHandler();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2015-04-20 13:11:11 +02:00
|
|
|
CefRefPtr<CefFrameImpl> framePtr = browserPtr->GetWebFrameImpl(frame);
|
|
|
|
|
2021-08-18 23:55:03 +02:00
|
|
|
if (handler) {
|
2024-02-22 19:36:15 +01:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2021-08-18 23:55:03 +02:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Context::Scope scope(context);
|
|
|
|
v8::MicrotasksScope microtasks_scope(isolate,
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2015-04-20 13:11:11 +02:00
|
|
|
|
2021-08-18 23:55:03 +02:00
|
|
|
CefRefPtr<CefV8Context> contextPtr(new CefV8ContextImpl(isolate, context));
|
2015-04-20 13:11:11 +02:00
|
|
|
|
2021-08-18 23:55:03 +02:00
|
|
|
handler->OnContextCreated(browserPtr.get(), framePtr.get(), contextPtr);
|
|
|
|
}
|
2021-05-21 03:42:58 +02:00
|
|
|
|
|
|
|
// Do this last, in case the client callback modified the window object.
|
2022-04-05 19:22:32 +02:00
|
|
|
framePtr->OnContextCreated(context);
|
2015-04-20 13:11:11 +02:00
|
|
|
}
|
|
|
|
|
2014-04-04 18:50:38 +02:00
|
|
|
void CefRenderFrameObserver::WillReleaseScriptContext(
|
|
|
|
v8::Handle<v8::Context> context,
|
|
|
|
int world_id) {
|
2015-04-20 13:11:11 +02:00
|
|
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
2015-07-16 23:40:01 +02:00
|
|
|
CefRefPtr<CefBrowserImpl> browserPtr =
|
2017-04-20 21:28:17 +02:00
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(frame->Top());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!browserPtr) {
|
2022-04-05 19:22:32 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-04-05 19:22:32 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefRenderProcessHandler> handler;
|
|
|
|
CefRefPtr<CefApp> application = CefAppManager::Get()->GetApplication();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (application) {
|
2022-04-05 19:22:32 +02:00
|
|
|
handler = application->GetRenderProcessHandler();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-04-05 19:22:32 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefFrameImpl> framePtr = browserPtr->GetWebFrameImpl(frame);
|
|
|
|
|
|
|
|
if (handler) {
|
2024-02-22 19:36:15 +01:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2022-04-05 19:22:32 +02:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
// The released context should not be used for script execution.
|
|
|
|
// Depending on how the context is released this may or may not already
|
|
|
|
// be set.
|
|
|
|
blink_glue::CefScriptForbiddenScope forbidScript;
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Context> contextPtr(new CefV8ContextImpl(isolate, context));
|
|
|
|
|
|
|
|
handler->OnContextReleased(browserPtr.get(), framePtr.get(), contextPtr);
|
2015-04-20 13:11:11 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 19:22:32 +02:00
|
|
|
framePtr->OnContextReleased();
|
|
|
|
|
2015-04-20 13:11:11 +02:00
|
|
|
CefV8ReleaseContext(context);
|
2014-04-04 18:50:38 +02:00
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2016-06-21 00:59:23 +02:00
|
|
|
void CefRenderFrameObserver::OnDestruct() {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:23:43 +02:00
|
|
|
void CefRenderFrameObserver::AttachFrame(CefFrameImpl* frame) {
|
|
|
|
DCHECK(frame);
|
|
|
|
DCHECK(!frame_);
|
|
|
|
frame_ = frame;
|
2022-02-10 22:52:36 +01:00
|
|
|
frame_->OnAttached();
|
2019-05-24 22:23:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 19:59:21 +02:00
|
|
|
void CefRenderFrameObserver::OnLoadStart() {
|
2020-06-28 23:05:36 +02:00
|
|
|
CefRefPtr<CefApp> app = CefAppManager::Get()->GetApplication();
|
2019-07-16 19:59:21 +02:00
|
|
|
if (app.get()) {
|
|
|
|
CefRefPtr<CefRenderProcessHandler> handler = app->GetRenderProcessHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefLoadHandler> load_handler = handler->GetLoadHandler();
|
|
|
|
if (load_handler.get()) {
|
|
|
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
|
|
|
CefRefPtr<CefBrowserImpl> browserPtr =
|
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(frame->Top());
|
|
|
|
load_handler->OnLoadStart(browserPtr.get(), frame_, TT_EXPLICIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:11:44 +01:00
|
|
|
void CefRenderFrameObserver::OnLoadError() {
|
2020-06-28 23:05:36 +02:00
|
|
|
CefRefPtr<CefApp> app = CefAppManager::Get()->GetApplication();
|
2019-07-16 19:59:21 +02:00
|
|
|
if (app.get()) {
|
|
|
|
CefRefPtr<CefRenderProcessHandler> handler = app->GetRenderProcessHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefLoadHandler> load_handler = handler->GetLoadHandler();
|
2019-11-12 17:11:44 +01:00
|
|
|
// Error codes were removed from DidFailProvisionalLoad() so we now always
|
|
|
|
// pass the same value.
|
2019-07-16 19:59:21 +02:00
|
|
|
if (load_handler.get()) {
|
|
|
|
const cef_errorcode_t errorCode =
|
2019-11-12 17:11:44 +01:00
|
|
|
static_cast<cef_errorcode_t>(net::ERR_ABORTED);
|
|
|
|
const std::string& errorText = net::ErrorToString(errorCode);
|
2019-07-16 19:59:21 +02:00
|
|
|
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
|
|
|
|
CefRefPtr<CefBrowserImpl> browserPtr =
|
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(frame->Top());
|
|
|
|
load_handler->OnLoadError(browserPtr.get(), frame_, errorCode,
|
2021-01-28 00:13:12 +01:00
|
|
|
errorText, frame_->GetURL());
|
2019-07-16 19:59:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 14:14:11 +02:00
|
|
|
// Enable deprecation warnings on Windows. See http://crbug.com/585142.
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-10-02 14:14:11 +02:00
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#else
|
2016-03-16 03:55:59 +01:00
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2018-10-02 14:14:11 +02:00
|
|
|
#endif
|