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.
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "libcef/renderer/blink_glue.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
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"
|
|
|
|
|
2018-07-02 19:11:49 +02:00
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/referrer_script_info.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/script_controller.h"
|
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/script_source_code.h"
|
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
|
2018-06-08 18:53:10 +02:00
|
|
|
#include "third_party/blink/renderer/bindings/core/v8/v8_code_cache.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#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"
|
|
|
|
#include "third_party/blink/renderer/core/exported/web_view_impl.h"
|
|
|
|
#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"
|
|
|
|
#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"
|
|
|
|
#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
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
const int64_t kInvalidFrameId = -1;
|
2014-04-04 18:50:38 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
bool CanGoBack(blink::WebView* view) {
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!view)
|
|
|
|
return false;
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebViewImpl* impl = reinterpret_cast<blink::WebViewImpl*>(view);
|
2017-04-20 21:28:17 +02:00
|
|
|
return (impl->Client()->HistoryBackListCount() > 0);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
bool CanGoForward(blink::WebView* view) {
|
2013-07-24 22:15:18 +02:00
|
|
|
if (!view)
|
|
|
|
return false;
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebViewImpl* impl = reinterpret_cast<blink::WebViewImpl*>(view);
|
2017-04-20 21:28:17 +02:00
|
|
|
return (impl->Client()->HistoryForwardListCount() > 0);
|
2013-07-24 22:15:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
void GoBack(blink::WebView* view) {
|
2013-07-24 22:15:18 +02:00
|
|
|
if (!view)
|
|
|
|
return;
|
2019-04-16 16:38:48 +02:00
|
|
|
|
|
|
|
blink::WebFrame* main_frame = view->MainFrame();
|
|
|
|
if (main_frame && main_frame->IsWebLocalFrame()) {
|
|
|
|
blink::WebViewImpl* view_impl = reinterpret_cast<blink::WebViewImpl*>(view);
|
|
|
|
if (view_impl->Client()->HistoryBackListCount() > 0) {
|
|
|
|
main_frame->ToWebLocalFrame()->Client()->NavigateBackForwardSoon(
|
2019-11-12 17:11:44 +01:00
|
|
|
-1, true /* has_user_gesture */);
|
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) {
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!view)
|
|
|
|
return;
|
2019-04-16 16:38:48 +02:00
|
|
|
|
|
|
|
blink::WebFrame* main_frame = view->MainFrame();
|
|
|
|
if (main_frame && main_frame->IsWebLocalFrame()) {
|
|
|
|
blink::WebViewImpl* view_impl = reinterpret_cast<blink::WebViewImpl*>(view);
|
|
|
|
if (view_impl->Client()->HistoryForwardListCount() > 0) {
|
|
|
|
main_frame->ToWebLocalFrame()->Client()->NavigateBackForwardSoon(
|
2019-11-12 17:11:44 +01:00
|
|
|
1, true /* has_user_gesture */);
|
2019-04-16 16:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
std::string 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();
|
|
|
|
if (document_element.IsNull())
|
2013-10-29 18:53:18 +01:00
|
|
|
return std::string();
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::Element* web_element = document_element.Unwrap<blink::Element>();
|
|
|
|
return blink::WebString(web_element->innerText()).Utf8();
|
2013-10-29 18:53:18 +01:00
|
|
|
}
|
2013-06-04 19:41:37 +02:00
|
|
|
|
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.
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::LocalFrame* frame = blink::ToLocalFrameIfNotDetached(context);
|
2016-08-31 13:25:56 +02:00
|
|
|
DCHECK(frame);
|
|
|
|
if (frame &&
|
2017-04-20 21:28:17 +02:00
|
|
|
frame->GetDocument()->CanExecuteScripts(blink::kAboutToExecuteScript)) {
|
|
|
|
func_rv = blink::V8ScriptRunner::CallFunction(
|
|
|
|
function, frame->GetDocument(), 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
|
|
|
}
|
|
|
|
|
2016-10-27 18:34:19 +02:00
|
|
|
v8::MaybeLocal<v8::Value> ExecuteV8ScriptAndReturnValue(
|
|
|
|
const blink::WebString& source,
|
|
|
|
const blink::WebString& source_url,
|
|
|
|
int start_line,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::TryCatch& tryCatch,
|
2018-11-30 23:21:07 +01:00
|
|
|
blink::SanitizeScriptErrors sanitizeScriptErrors) {
|
2016-10-27 18:34:19 +02:00
|
|
|
// Based on ScriptController::executeScriptAndReturnValue
|
|
|
|
DCHECK(isolate);
|
|
|
|
|
|
|
|
if (start_line < 1)
|
|
|
|
start_line = 1;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
const blink::ScriptSourceCode ssc = blink::ScriptSourceCode(
|
2017-12-07 22:44:24 +01:00
|
|
|
source, blink::ScriptSourceLocationType::kInternal,
|
2018-02-15 01:12:09 +01:00
|
|
|
nullptr, /* cache_handler */
|
2017-12-07 22:44:24 +01:00
|
|
|
blink::KURL(source_url),
|
2017-04-20 21:28:17 +02:00
|
|
|
WTF::TextPosition(WTF::OrdinalNumber::FromOneBasedInt(start_line),
|
2017-05-17 11:29:28 +02:00
|
|
|
WTF::OrdinalNumber::FromZeroBasedInt(0)));
|
2016-10-27 18:34:19 +02:00
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Value> result;
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::LocalFrame* frame = blink::ToLocalFrameIfNotDetached(context);
|
2017-05-31 17:33:30 +02:00
|
|
|
if (!frame)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
blink::V8CacheOptions v8CacheOptions(blink::kV8CacheOptionsDefault);
|
|
|
|
if (frame && frame->GetSettings())
|
|
|
|
v8CacheOptions = frame->GetSettings()->GetV8CacheOptions();
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// Based on V8ScriptRunner::CompileAndRunInternalScript:
|
|
|
|
v8::ScriptCompiler::CompileOptions compile_options;
|
2018-06-08 18:53:10 +02:00
|
|
|
blink::V8CodeCache::ProduceCacheOptions produce_cache_options;
|
2018-03-20 21:15:08 +01:00
|
|
|
v8::ScriptCompiler::NoCacheReason no_cache_reason;
|
|
|
|
std::tie(compile_options, produce_cache_options, no_cache_reason) =
|
2018-06-08 18:53:10 +02:00
|
|
|
blink::V8CodeCache::GetCompileOptions(v8CacheOptions, ssc);
|
2018-03-20 21:15:08 +01:00
|
|
|
|
|
|
|
// Currently internal scripts don't have cache handlers, so we should not
|
|
|
|
// produce cache for them.
|
|
|
|
DCHECK_EQ(produce_cache_options,
|
2018-06-08 18:53:10 +02:00
|
|
|
blink::V8CodeCache::ProduceCacheOptions::kNoProduceCache);
|
2018-03-20 21:15:08 +01:00
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
v8::Local<v8::Script> script;
|
2018-03-20 21:15:08 +01:00
|
|
|
// Use default ReferrerScriptInfo here:
|
|
|
|
// - nonce: empty for internal script, and
|
|
|
|
// - parser_state: always "not parser inserted" for internal scripts.
|
2018-02-15 01:12:09 +01:00
|
|
|
if (!blink::V8ScriptRunner::CompileScript(
|
2018-11-30 23:21:07 +01:00
|
|
|
blink::ScriptState::From(context), ssc, sanitizeScriptErrors,
|
2018-03-20 21:15:08 +01:00
|
|
|
compile_options, no_cache_reason, blink::ReferrerScriptInfo())
|
2017-05-31 17:33:30 +02:00
|
|
|
.ToLocal(&script)) {
|
|
|
|
DCHECK(tryCatch.HasCaught());
|
|
|
|
return result;
|
2016-10-27 18:34:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
return blink::V8ScriptRunner::RunCompiledScript(
|
|
|
|
isolate, script, blink::ToExecutionContext(context));
|
2016-10-27 18:34:19 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void RegisterURLSchemeAsLocal(const blink::WebString& scheme) {
|
|
|
|
blink::SchemeRegistry::RegisterURLSchemeAsLocal(scheme);
|
2017-01-23 18:36:54 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void RegisterURLSchemeAsSecure(const blink::WebString& scheme) {
|
|
|
|
blink::SchemeRegistry::RegisterURLSchemeAsSecure(scheme);
|
2017-01-23 18:36:54 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
blink::ScriptForbiddenScope scope_;
|
|
|
|
};
|
|
|
|
|
|
|
|
CefScriptForbiddenScope::CefScriptForbiddenScope() : impl_(new Impl()) {}
|
|
|
|
|
|
|
|
CefScriptForbiddenScope::~CefScriptForbiddenScope() {}
|
|
|
|
|
2018-01-03 22:14:29 +01:00
|
|
|
bool ResponseWasCached(const blink::WebURLResponse& response) {
|
|
|
|
return response.ToResourceResponse().WasCached();
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
} // namespace blink_glue
|