2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/renderer/blink_glue.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/platform/web_string.h"
|
|
|
|
#include "third_party/blink/public/platform/web_url_response.h"
|
|
|
|
#include "third_party/blink/public/web/web_document.h"
|
|
|
|
#include "third_party/blink/public/web/web_element.h"
|
2019-04-16 16:38:48 +02:00
|
|
|
#include "third_party/blink/public/web/web_local_frame_client.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/web/web_node.h"
|
|
|
|
#include "third_party/blink/public/web/web_view_client.h"
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/sanitize_script_errors.h"
|
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/script_evaluation_result.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
|
|
|
|
#include "third_party/blink/renderer/core/dom/document.h"
|
|
|
|
#include "third_party/blink/renderer/core/dom/element.h"
|
|
|
|
#include "third_party/blink/renderer/core/dom/node.h"
|
|
|
|
#include "third_party/blink/renderer/core/editing/serializers/serialization.h"
|
2022-04-05 19:22:32 +02:00
|
|
|
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
|
|
|
|
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_state_observer.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/renderer/core/exported/web_view_impl.h"
|
2020-01-23 22:58:01 +01:00
|
|
|
#include "third_party/blink/renderer/core/frame/frame_owner.h"
|
2020-07-08 19:23:29 +02:00
|
|
|
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/renderer/core/frame/local_frame.h"
|
|
|
|
#include "third_party/blink/renderer/core/frame/settings.h"
|
|
|
|
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
|
2021-01-28 00:13:12 +01:00
|
|
|
#include "third_party/blink/renderer/core/loader/frame_load_request.h"
|
2022-02-10 22:52:36 +01:00
|
|
|
#include "third_party/blink/renderer/core/page/page.h"
|
2021-03-04 23:36:57 +01:00
|
|
|
#include "third_party/blink/renderer/core/script/classic_script.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
|
|
|
|
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
|
|
|
|
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
|
2020-08-29 00:39:23 +02:00
|
|
|
#include "third_party/blink/renderer/platform/loader/fetch/script_fetch_options.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#undef LOG
|
|
|
|
|
2014-04-04 18:50:38 +02:00
|
|
|
#include "base/logging.h"
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
namespace blink_glue {
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-01-03 00:34:43 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
blink::ExecutionContext* GetExecutionContext(v8::Local<v8::Context> context) {
|
|
|
|
blink::LocalFrame* frame = blink::ToLocalFrameIfNotDetached(context);
|
|
|
|
if (frame &&
|
|
|
|
frame->DomWindow()->CanExecuteScripts(blink::kAboutToExecuteScript)) {
|
|
|
|
return frame->GetDocument()->GetExecutionContext();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
bool CanGoBack(blink::WebView* view) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-21 00:52:34 +02:00
|
|
|
return view->HistoryBackListCount() > 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
bool CanGoForward(blink::WebView* view) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2013-07-24 22:15:18 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-21 00:52:34 +02:00
|
|
|
return view->HistoryForwardListCount() > 0;
|
2013-07-24 22:15:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
void GoBack(blink::WebView* view) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2013-07-24 22:15:18 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-16 16:38:48 +02:00
|
|
|
|
|
|
|
blink::WebFrame* main_frame = view->MainFrame();
|
|
|
|
if (main_frame && main_frame->IsWebLocalFrame()) {
|
2021-04-21 00:52:34 +02:00
|
|
|
if (view->HistoryBackListCount() > 0) {
|
2020-03-04 01:29:39 +01:00
|
|
|
blink::Frame* core_frame = blink::WebFrame::ToCoreFrame(*main_frame);
|
|
|
|
blink::To<blink::LocalFrame>(core_frame)
|
|
|
|
->GetLocalFrameHostRemote()
|
2024-03-19 22:11:42 +01:00
|
|
|
.GoToEntryAtOffset(-1, /*has_user_gesture=*/true, std::nullopt);
|
2019-04-16 16:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 22:15:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
void GoForward(blink::WebView* view) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-16 16:38:48 +02:00
|
|
|
|
|
|
|
blink::WebFrame* main_frame = view->MainFrame();
|
|
|
|
if (main_frame && main_frame->IsWebLocalFrame()) {
|
2021-04-21 00:52:34 +02:00
|
|
|
if (view->HistoryForwardListCount() > 0) {
|
2020-03-04 01:29:39 +01:00
|
|
|
blink::Frame* core_frame = blink::WebFrame::ToCoreFrame(*main_frame);
|
|
|
|
blink::To<blink::LocalFrame>(core_frame)
|
|
|
|
->GetLocalFrameHostRemote()
|
2024-03-19 22:11:42 +01:00
|
|
|
.GoToEntryAtOffset(1, /*has_user_gesture=*/true, std::nullopt);
|
2019-04-16 16:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:52:36 +01:00
|
|
|
bool IsInBackForwardCache(blink::WebLocalFrame* frame) {
|
|
|
|
blink::Frame* core_frame = blink::WebFrame::ToCoreFrame(*frame);
|
|
|
|
return blink::To<blink::LocalFrame>(core_frame)
|
|
|
|
->GetPage()
|
|
|
|
->GetPageScheduler()
|
|
|
|
->IsInBackForwardCache();
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
blink::WebString DumpDocumentText(blink::WebLocalFrame* frame) {
|
2013-06-04 19:41:37 +02:00
|
|
|
// We use the document element's text instead of the body text here because
|
|
|
|
// not all documents have a body, such as XML documents.
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::WebElement document_element = frame->GetDocument().DocumentElement();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (document_element.IsNull()) {
|
2021-05-14 18:58:55 +02:00
|
|
|
return blink::WebString();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-10-29 18:53:18 +01:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::Element* web_element = document_element.Unwrap<blink::Element>();
|
2021-05-14 18:58:55 +02:00
|
|
|
return blink::WebString(web_element->innerText());
|
2013-10-29 18:53:18 +01:00
|
|
|
}
|
2013-06-04 19:41:37 +02:00
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
blink::WebString DumpDocumentMarkup(blink::WebLocalFrame* frame) {
|
|
|
|
return blink::CreateMarkup(
|
2021-03-04 23:36:57 +01:00
|
|
|
blink::To<blink::WebLocalFrameImpl>(frame)->GetFrame()->GetDocument());
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
cef_dom_node_type_t GetNodeType(const blink::WebNode& node) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const blink::Node* web_node = node.ConstUnwrap<blink::Node>();
|
2016-03-16 03:55:59 +01:00
|
|
|
switch (web_node->getNodeType()) {
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kElementNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_ELEMENT;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kAttributeNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_ATTRIBUTE;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kTextNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_TEXT;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kCdataSectionNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_CDATA_SECTION;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kProcessingInstructionNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_PROCESSING_INSTRUCTIONS;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kCommentNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_COMMENT;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kDocumentNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_DOCUMENT;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kDocumentTypeNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_DOCUMENT_TYPE;
|
2016-08-31 13:25:56 +02:00
|
|
|
case blink::Node::kDocumentFragmentNode:
|
2015-10-09 17:23:12 +02:00
|
|
|
return DOM_NODE_TYPE_DOCUMENT_FRAGMENT;
|
|
|
|
}
|
|
|
|
return DOM_NODE_TYPE_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebString GetNodeName(const blink::WebNode& node) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const blink::Node* web_node = node.ConstUnwrap<blink::Node>();
|
2015-10-09 17:23:12 +02:00
|
|
|
return web_node->nodeName();
|
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebString CreateNodeMarkup(const blink::WebNode& node) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const blink::Node* web_node = node.ConstUnwrap<blink::Node>();
|
|
|
|
return blink::CreateMarkup(web_node);
|
2015-10-09 17:23:12 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
bool SetNodeValue(blink::WebNode& node, const blink::WebString& value) {
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::Node* web_node = node.Unwrap<blink::Node>();
|
2013-10-29 18:53:18 +01:00
|
|
|
web_node->setNodeValue(value);
|
|
|
|
return true;
|
2013-06-04 19:41:37 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
v8::MaybeLocal<v8::Value> CallV8Function(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Function> function,
|
|
|
|
v8::Local<v8::Object> receiver,
|
|
|
|
int argc,
|
|
|
|
v8::Local<v8::Value> args[],
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
v8::MaybeLocal<v8::Value> func_rv;
|
|
|
|
|
|
|
|
// Execute the function call using the V8ScriptRunner so that inspector
|
|
|
|
// instrumentation works.
|
2023-01-03 00:34:43 +01:00
|
|
|
if (auto execution_context = GetExecutionContext(context)) {
|
2017-04-20 21:28:17 +02:00
|
|
|
func_rv = blink::V8ScriptRunner::CallFunction(
|
2023-01-03 00:34:43 +01:00
|
|
|
function, execution_context, receiver, argc, args, isolate);
|
2016-08-31 13:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return func_rv;
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
bool IsTextControlElement(const blink::WebElement& element) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const blink::Element* web_element = element.ConstUnwrap<blink::Element>();
|
|
|
|
return web_element->IsTextControl();
|
2016-11-23 21:54:29 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 21:54:42 +02:00
|
|
|
v8::Local<v8::Value> ExecuteV8ScriptAndReturnValue(
|
2016-10-27 18:34:19 +02:00
|
|
|
const blink::WebString& source,
|
|
|
|
const blink::WebString& source_url,
|
|
|
|
int start_line,
|
|
|
|
v8::Local<v8::Context> context,
|
2020-10-08 21:54:42 +02:00
|
|
|
v8::TryCatch& tryCatch) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (start_line < 1) {
|
2016-10-27 18:34:19 +02:00
|
|
|
start_line = 1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-10-27 18:34:19 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::LocalFrame* frame = blink::ToLocalFrameIfNotDetached(context);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame) {
|
2020-10-08 21:54:42 +02:00
|
|
|
return v8::Local<v8::Value>();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-31 17:33:30 +02:00
|
|
|
|
2021-12-16 23:35:54 +01:00
|
|
|
auto* script = blink::ClassicScript::Create(
|
|
|
|
source, blink::KURL(source_url), blink::KURL(source_url),
|
|
|
|
blink::ScriptFetchOptions(), blink::ScriptSourceLocationType::kInternal,
|
|
|
|
blink::SanitizeScriptErrors::kDoNotSanitize, /*cache_handler=*/nullptr,
|
2020-08-29 00:39:23 +02:00
|
|
|
WTF::TextPosition(WTF::OrdinalNumber::FromOneBasedInt(start_line),
|
|
|
|
WTF::OrdinalNumber::FromZeroBasedInt(0)));
|
|
|
|
|
2020-10-08 21:54:42 +02:00
|
|
|
// The Rethrow() message is unused due to kDoNotSanitize but it still needs
|
|
|
|
// to be non-nullopt for exceptions to be re-thrown as expected.
|
|
|
|
auto result = blink::V8ScriptRunner::CompileAndRunScript(
|
2024-05-23 03:52:35 +02:00
|
|
|
blink::ScriptState::From(context->GetIsolate(), context), script,
|
2021-01-28 00:13:12 +01:00
|
|
|
blink::ExecuteScriptPolicy::kExecuteScriptWhenScriptsDisabled,
|
2020-10-08 21:54:42 +02:00
|
|
|
blink::V8ScriptRunner::RethrowErrorsOption::Rethrow(""));
|
|
|
|
|
|
|
|
if (result.GetResultType() ==
|
|
|
|
blink::ScriptEvaluationResult::ResultType::kSuccess) {
|
|
|
|
return result.GetSuccessValue();
|
2016-10-27 18:34:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 21:54:42 +02:00
|
|
|
DCHECK(tryCatch.HasCaught());
|
|
|
|
return v8::Local<v8::Value>();
|
2016-10-27 18:34:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-03 00:34:43 +01:00
|
|
|
v8::MicrotaskQueue* GetMicrotaskQueue(v8::Local<v8::Context> context) {
|
|
|
|
if (auto execution_context = GetExecutionContext(context)) {
|
|
|
|
return execution_context->GetMicrotaskQueue();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
bool IsScriptForbidden() {
|
2017-04-20 21:28:17 +02:00
|
|
|
return blink::ScriptForbiddenScope::IsScriptForbidden();
|
2016-08-31 13:25:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 19:22:32 +02:00
|
|
|
std::unique_ptr<CefObserverRegistration>
|
|
|
|
RegisterExecutionContextLifecycleStateObserver(
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
CefExecutionContextLifecycleStateObserver* observer) {
|
|
|
|
class Observer : public blink::GarbageCollected<Observer>,
|
|
|
|
public blink::ExecutionContextLifecycleStateObserver {
|
|
|
|
public:
|
|
|
|
Observer(blink::ExecutionContext* execution_context,
|
|
|
|
CefExecutionContextLifecycleStateObserver* observer)
|
|
|
|
: blink::ExecutionContextLifecycleStateObserver(execution_context),
|
|
|
|
observer_(observer) {
|
|
|
|
UpdateStateIfNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextLifecycleStateChanged(
|
|
|
|
blink::mojom::blink::FrameLifecycleState state) override {
|
|
|
|
observer_->ContextLifecycleStateChanged(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextDestroyed() override {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefExecutionContextLifecycleStateObserver* observer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Registration : public CefObserverRegistration {
|
|
|
|
public:
|
2024-01-20 23:48:57 +01:00
|
|
|
explicit Registration(blink::Persistent<Observer> observer)
|
|
|
|
: observer_(observer) {}
|
2022-04-05 19:22:32 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
blink::Persistent<Observer> observer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::make_unique<Registration>(blink::MakeGarbageCollected<Observer>(
|
|
|
|
blink::ExecutionContext::From(context), observer));
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:31:31 +01:00
|
|
|
void RegisterURLSchemeAsSupportingFetchAPI(const blink::WebString& scheme) {
|
|
|
|
blink::SchemeRegistry::RegisterURLSchemeAsSupportingFetchAPI(scheme);
|
|
|
|
}
|
|
|
|
|
2017-07-06 22:39:37 +02:00
|
|
|
struct CefScriptForbiddenScope::Impl {
|
2024-06-21 17:26:59 +02:00
|
|
|
STACK_ALLOCATED_IGNORE()
|
2017-07-06 22:39:37 +02:00
|
|
|
blink::ScriptForbiddenScope scope_;
|
|
|
|
};
|
|
|
|
|
|
|
|
CefScriptForbiddenScope::CefScriptForbiddenScope() : impl_(new Impl()) {}
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefScriptForbiddenScope::~CefScriptForbiddenScope() = default;
|
2017-07-06 22:39:37 +02:00
|
|
|
|
2018-01-03 22:14:29 +01:00
|
|
|
bool ResponseWasCached(const blink::WebURLResponse& response) {
|
|
|
|
return response.ToResourceResponse().WasCached();
|
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
bool HasPluginFrameOwner(blink::WebLocalFrame* frame) {
|
|
|
|
blink::Frame* core_frame = blink::WebFrame::ToCoreFrame(*frame);
|
|
|
|
return core_frame->Owner() && core_frame->Owner()->IsPlugin();
|
|
|
|
}
|
|
|
|
|
2021-01-28 00:13:12 +01:00
|
|
|
// Based on WebLocalFrameImpl::StartNavigation which was removed in
|
|
|
|
// https://crrev.com/de4fc2a5fe.
|
|
|
|
void StartNavigation(blink::WebLocalFrame* frame,
|
|
|
|
const blink::WebURLRequest& request) {
|
|
|
|
DCHECK(!request.IsNull());
|
|
|
|
DCHECK(!request.Url().ProtocolIs("javascript"));
|
|
|
|
|
|
|
|
blink::FrameLoadRequest frame_load_request(nullptr,
|
|
|
|
request.ToResourceRequest());
|
|
|
|
blink::Frame* core_frame = blink::WebFrame::ToCoreFrame(*frame);
|
|
|
|
blink::To<blink::LocalFrame>(core_frame)
|
|
|
|
->Loader()
|
|
|
|
.StartNavigation(frame_load_request, blink::WebFrameLoadType::kStandard);
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
} // namespace blink_glue
|