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.
|
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
// MSVC++ requires this to be set before any other includes to get M_PI.
|
|
|
|
// Otherwise there will be compile errors in wtf/MathExtras.h.
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
2013-10-29 18:53:18 +01:00
|
|
|
// Defines required to access Blink internals (unwrap WebNode).
|
|
|
|
#undef BLINK_IMPLEMENTATION
|
|
|
|
#define BLINK_IMPLEMENTATION 1
|
|
|
|
#undef INSIDE_BLINK
|
|
|
|
#define INSIDE_BLINK 1
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/renderer/webkit_glue.h"
|
|
|
|
|
|
|
|
#include "base/compiler_specific.h"
|
|
|
|
|
|
|
|
MSVC_PUSH_WARNING_LEVEL(0);
|
2013-10-29 18:53:18 +01:00
|
|
|
#include "third_party/WebKit/public/platform/WebString.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebDocument.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebElement.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebNode.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebViewClient.h"
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
#include "third_party/WebKit/Source/bindings/core/v8/ScriptController.h"
|
2016-10-27 18:34:19 +02:00
|
|
|
#include "third_party/WebKit/Source/bindings/core/v8/ScriptSourceCode.h"
|
2016-08-31 13:25:56 +02:00
|
|
|
#include "third_party/WebKit/Source/bindings/core/v8/V8Binding.h"
|
|
|
|
#include "third_party/WebKit/Source/core/dom/Document.h"
|
2015-10-15 19:16:45 +02:00
|
|
|
#include "third_party/WebKit/Source/core/dom/Element.h"
|
2013-10-29 18:53:18 +01:00
|
|
|
#include "third_party/WebKit/Source/core/dom/Node.h"
|
2015-10-09 17:23:12 +02:00
|
|
|
#include "third_party/WebKit/Source/core/editing/serializers/Serialization.h"
|
2016-08-31 13:25:56 +02:00
|
|
|
#include "third_party/WebKit/Source/core/frame/LocalFrame.h"
|
2016-10-27 18:34:19 +02:00
|
|
|
#include "third_party/WebKit/Source/core/frame/Settings.h"
|
2017-01-23 18:36:54 +01:00
|
|
|
#include "third_party/WebKit/Source/platform/weborigin/SchemeRegistry.h"
|
2014-09-24 17:38:11 +02:00
|
|
|
#include "third_party/WebKit/Source/web/WebLocalFrameImpl.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "third_party/WebKit/Source/web/WebViewImpl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
MSVC_POP_WARNING();
|
|
|
|
#undef LOG
|
|
|
|
|
2014-04-04 18:50:38 +02:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "content/public/renderer/render_frame.h"
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
namespace webkit_glue {
|
|
|
|
|
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);
|
2013-10-29 18:53:18 +01: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);
|
2013-10-29 18:53:18 +01: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;
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebViewImpl* impl = reinterpret_cast<blink::WebViewImpl*>(view);
|
2013-10-29 18:53:18 +01:00
|
|
|
if (impl->client()->historyBackListCount() > 0)
|
|
|
|
impl->client()->navigateBackForwardSoon(-1);
|
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;
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebViewImpl* impl = reinterpret_cast<blink::WebViewImpl*>(view);
|
2015-10-15 19:16:45 +02:00
|
|
|
if (impl->client()->historyForwardListCount() > 0)
|
2013-10-29 18:53:18 +01:00
|
|
|
impl->client()->navigateBackForwardSoon(1);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
std::string DumpDocumentText(blink::WebFrame* 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.
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebElement document_element = frame->document().documentElement();
|
2013-06-04 19:41:37 +02:00
|
|
|
if (document_element.isNull())
|
2013-10-29 18:53:18 +01:00
|
|
|
return std::string();
|
|
|
|
|
2015-10-15 19:16:45 +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) {
|
|
|
|
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) {
|
|
|
|
const blink::Node* web_node = node.constUnwrap<blink::Node>();
|
|
|
|
return web_node->nodeName();
|
|
|
|
}
|
|
|
|
|
|
|
|
blink::WebString CreateNodeMarkup(const blink::WebNode& node) {
|
|
|
|
const blink::Node* web_node = node.constUnwrap<blink::Node>();
|
|
|
|
return blink::createMarkup(web_node);
|
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
bool SetNodeValue(blink::WebNode& node, const blink::WebString& value) {
|
2014-09-04 19:53:40 +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-01-06 20:20:54 +01:00
|
|
|
int64_t GetIdentifier(blink::WebFrame* frame) {
|
2014-04-04 18:50:38 +02:00
|
|
|
// Each WebFrame will have an associated RenderFrame. The RenderFrame
|
|
|
|
// routing IDs are unique within a given renderer process.
|
|
|
|
content::RenderFrame* render_frame =
|
|
|
|
content::RenderFrame::FromWebFrame(frame);
|
|
|
|
DCHECK(render_frame);
|
|
|
|
if (render_frame)
|
|
|
|
return render_frame->GetRoutingID();
|
|
|
|
return kInvalidFrameId;
|
|
|
|
}
|
|
|
|
|
2014-09-24 17:38:11 +02:00
|
|
|
// Based on WebViewImpl::findFrameByName and FrameTree::find.
|
|
|
|
blink::WebFrame* FindFrameByUniqueName(const blink::WebString& unique_name,
|
|
|
|
blink::WebFrame* relative_to_frame) {
|
|
|
|
blink::Frame* start_frame = toWebLocalFrameImpl(relative_to_frame)->frame();
|
|
|
|
if (!start_frame)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
const AtomicString& atomic_name = unique_name;
|
|
|
|
blink::Frame* found_frame = NULL;
|
|
|
|
|
|
|
|
// Search the subtree starting with |start_frame|.
|
|
|
|
for (blink::Frame* frame = start_frame;
|
|
|
|
frame;
|
|
|
|
frame = frame->tree().traverseNext(start_frame)) {
|
|
|
|
if (frame->tree().uniqueName() == atomic_name) {
|
|
|
|
found_frame = frame;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_frame && found_frame->isLocalFrame())
|
|
|
|
return blink::WebLocalFrameImpl::fromFrame(toLocalFrame(found_frame));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
blink::LocalFrame* frame =
|
|
|
|
toLocalFrame(blink::toFrameIfNotDetached(context));
|
|
|
|
DCHECK(frame);
|
|
|
|
if (frame &&
|
2017-03-03 23:37:23 +01:00
|
|
|
frame->document()->canExecuteScripts(blink::AboutToExecuteScript)) {
|
2016-08-31 13:25:56 +02:00
|
|
|
func_rv = blink::V8ScriptRunner::callFunction(
|
|
|
|
function, frame->document(), receiver, argc, args, isolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return func_rv;
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
bool IsTextControlElement(const blink::WebElement& element) {
|
|
|
|
const blink::Element* web_element = element.constUnwrap<blink::Element>();
|
|
|
|
return web_element->isTextControl();
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
blink::AccessControlStatus accessControlStatus) {
|
|
|
|
// Based on ScriptController::executeScriptAndReturnValue
|
|
|
|
DCHECK(isolate);
|
|
|
|
|
|
|
|
if (start_line < 1)
|
|
|
|
start_line = 1;
|
|
|
|
|
|
|
|
const blink::KURL kurl = source_url.isEmpty() ?
|
|
|
|
blink::KURL() : blink::KURL(blink::ParsedURLString, source_url);
|
|
|
|
|
|
|
|
const blink::ScriptSourceCode ssc = blink::ScriptSourceCode(source, kurl,
|
|
|
|
WTF::TextPosition(WTF::OrdinalNumber::fromOneBasedInt(start_line),
|
|
|
|
WTF::OrdinalNumber::fromZeroBasedInt(0)));
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Value> result;
|
|
|
|
|
|
|
|
blink::LocalFrame* frame =
|
|
|
|
toLocalFrame(blink::toFrameIfNotDetached(context));
|
|
|
|
DCHECK(frame);
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
blink::V8CacheOptions v8CacheOptions(blink::V8CacheOptionsDefault);
|
|
|
|
if (frame && frame->settings())
|
2017-01-23 18:36:54 +01:00
|
|
|
v8CacheOptions = frame->settings()->getV8CacheOptions();
|
2016-10-27 18:34:19 +02:00
|
|
|
|
|
|
|
v8::Local<v8::Script> script;
|
|
|
|
if (!blink::v8Call(blink::V8ScriptRunner::compileScript(ssc, isolate,
|
|
|
|
accessControlStatus, v8CacheOptions), script, tryCatch)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = blink::V8ScriptRunner::runCompiledScript(isolate, script,
|
|
|
|
blink::toExecutionContext(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
bool IsScriptForbidden() {
|
|
|
|
return blink::ScriptForbiddenScope::isScriptForbidden();
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:36:54 +01:00
|
|
|
void registerURLSchemeAsLocal(const blink::WebString& scheme) {
|
|
|
|
blink::SchemeRegistry::registerURLSchemeAsLocal(scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerURLSchemeAsSecure(const blink::WebString& scheme) {
|
|
|
|
blink::SchemeRegistry::registerURLSchemeAsSecure(scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerURLSchemeAsCORSEnabled(const blink::WebString& scheme) {
|
|
|
|
blink::SchemeRegistry::registerURLSchemeAsCORSEnabled(scheme);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
} // webkit_glue
|