Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(573)

Unified Diff: third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp

Issue 1983423002: [DevTools] Move CommandLineAPI querySelector and querySelectorAll to native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pass-bind-remote-object
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp
diff --git a/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp b/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp
index ff6e8c82292a042be85c2105c36b004b9f06bed3..5919b7ece8f05d826421c2d316e849af2eb5d3d5 100644
--- a/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp
+++ b/third_party/WebKit/Source/core/inspector/MainThreadDebugger.cpp
@@ -33,8 +33,13 @@
#include "bindings/core/v8/BindingSecurity.h"
#include "bindings/core/v8/DOMWrapperWorld.h"
#include "bindings/core/v8/ScriptController.h"
+#include "bindings/core/v8/V8Node.h"
#include "bindings/core/v8/V8Window.h"
+#include "core/dom/ContainerNode.h"
+#include "core/dom/Document.h"
+#include "core/dom/Element.h"
#include "core/dom/ExecutionContext.h"
+#include "core/dom/StaticNodeList.h"
#include "core/frame/FrameConsole.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
@@ -44,6 +49,8 @@
#include "core/inspector/InspectorTaskRunner.h"
#include "core/timing/MemoryInfo.h"
#include "core/workers/MainThreadWorkletGlobalScope.h"
+#include "core/xml/XPathEvaluator.h"
+#include "core/xml/XPathResult.h"
#include "platform/UserGestureIndicator.h"
#include "platform/v8_inspector/public/V8Debugger.h"
#include "public/platform/Platform.h"
@@ -234,4 +241,119 @@ v8::MaybeLocal<v8::Value> MainThreadDebugger::memoryInfo(v8::Isolate* isolate, v
return toV8(MemoryInfo::create(), creationContext, isolate);
}
+static void createFunctionProperty(v8::Local<v8::Context> context, v8::Local<v8::Object> object, const char* name, v8::FunctionCallback callback)
+{
+ v8::Local<v8::String> funcName = v8String(context->GetIsolate(), name);
+ v8::Local<v8::Function> func;
+ if (!v8::Function::New(context, callback).ToLocal(&func))
+ return;
+ func->SetName(funcName);
+ if (!object->Set(context, funcName, func).FromMaybe(false))
+ return;
+}
+
+void MainThreadDebugger::installAdditionalCommandLineAPI(v8::Local<v8::Context> context, v8::Local<v8::Object> object)
+{
+ createFunctionProperty(context, object, "$", MainThreadDebugger::querySelectorCallback);
+ createFunctionProperty(context, object, "$$", MainThreadDebugger::querySelectorAllCallback);
+ createFunctionProperty(context, object, "$x", MainThreadDebugger::xpathSelectorCallback);
+}
+
+static Node* secondArgumentAsNode(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ if (info.Length() > 1) {
+ if (Node* node = V8Node::toImplWithTypeCheck(info.GetIsolate(), info[1]))
+ return node;
+ }
+ ExecutionContext* executionContext = toExecutionContext(info.GetIsolate()->GetCurrentContext());
+ if (executionContext->isDocument())
+ return toDocument(executionContext);
+ return nullptr;
+}
+
+void MainThreadDebugger::querySelectorCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ if (info.Length() < 1)
+ return;
+ String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
+ if (selector.isEmpty())
+ return;
+ Node* node = secondArgumentAsNode(info);
+ if (!node || !node->isContainerNode())
+ return;
+ ExceptionState exceptionState(ExceptionState::ExecutionContext, "$", "CommandLineAPI", info.Holder(), info.GetIsolate());
+ Element* element = toContainerNode(node)->querySelector(AtomicString(selector), exceptionState);
+ if (exceptionState.hadException())
+ return;
+ if (element)
+ info.GetReturnValue().Set(toV8(element, info.Holder(), info.GetIsolate()));
+ else
+ info.GetReturnValue().Set(v8::Null(info.GetIsolate()));
+}
+
+void MainThreadDebugger::querySelectorAllCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ if (info.Length() < 1)
+ return;
+ String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
+ if (selector.isEmpty())
+ return;
+ Node* node = secondArgumentAsNode(info);
+ if (!node || !node->isContainerNode())
+ return;
+ ExceptionState exceptionState(ExceptionState::ExecutionContext, "$$", "CommandLineAPI", info.Holder(), info.GetIsolate());
+ // toV8(elementList) doesn't work here, since we need a proper Array instance, not NodeList.
+ StaticElementList* elementList = toContainerNode(node)->querySelectorAll(AtomicString(selector), exceptionState);
+ if (!elementList || exceptionState.hadException())
+ return;
+ v8::Isolate* isolate = info.GetIsolate();
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+ v8::Local<v8::Array> nodes = v8::Array::New(isolate, elementList->length());
+ for (size_t i = 0; i < elementList->length(); ++i) {
+ Element* element = elementList->item(i);
+ if (!nodes->Set(context, i, toV8(element, info.Holder(), info.GetIsolate())).FromMaybe(false))
+ return;
+ }
+ info.GetReturnValue().Set(nodes);
+}
+
+void MainThreadDebugger::xpathSelectorCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ if (info.Length() < 1)
+ return;
+ String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
+ if (selector.isEmpty())
+ return;
+ Node* node = secondArgumentAsNode(info);
+ if (!node || !node->isContainerNode())
+ return;
+
+ XPathEvaluator* evaluator = XPathEvaluator::create();
+ if (!evaluator)
+ return;
+ ExceptionState exceptionState(ExceptionState::ExecutionContext, "$x", "CommandLineAPI", info.Holder(), info.GetIsolate());
dgozman 2016/05/18 23:15:18 Do we surface this exception in console? Previousl
kozy 2016/05/19 00:45:59 Done.
+ XPathResult* result = evaluator->evaluate(selector, node, nullptr, XPathResult::ANY_TYPE, ScriptValue(), exceptionState);
+ if (!result || exceptionState.hadException())
+ return;
+ if (result->resultType() == XPathResult::NUMBER_TYPE) {
+ info.GetReturnValue().Set(toV8(result->numberValue(exceptionState), info.Holder(), info.GetIsolate()));
+ } else if (result->resultType() == XPathResult::STRING_TYPE) {
+ info.GetReturnValue().Set(toV8(result->stringValue(exceptionState), info.Holder(), info.GetIsolate()));
+ } else if (result->resultType() == XPathResult::BOOLEAN_TYPE) {
+ info.GetReturnValue().Set(toV8(result->booleanValue(exceptionState), info.Holder(), info.GetIsolate()));
+ } else {
+ v8::Isolate* isolate = info.GetIsolate();
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+ v8::Local<v8::Array> nodes = v8::Array::New(isolate);
+ size_t index = 0;
+ while (Node* node = result->iterateNext(exceptionState)) {
+ if (exceptionState.hadException())
+ return;
+ if (!nodes->Set(context, index++, toV8(node, info.Holder(), info.GetIsolate())).FromMaybe(false))
+ return;
+ }
+ info.GetReturnValue().Set(nodes);
+ }
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698