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

Side by Side 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 Google Inc. All rights reserved. 2 * Copyright (c) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 15 matching lines...) Expand all
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "core/inspector/MainThreadDebugger.h" 31 #include "core/inspector/MainThreadDebugger.h"
32 32
33 #include "bindings/core/v8/BindingSecurity.h" 33 #include "bindings/core/v8/BindingSecurity.h"
34 #include "bindings/core/v8/DOMWrapperWorld.h" 34 #include "bindings/core/v8/DOMWrapperWorld.h"
35 #include "bindings/core/v8/ScriptController.h" 35 #include "bindings/core/v8/ScriptController.h"
36 #include "bindings/core/v8/V8Node.h"
36 #include "bindings/core/v8/V8Window.h" 37 #include "bindings/core/v8/V8Window.h"
38 #include "core/dom/ContainerNode.h"
39 #include "core/dom/Document.h"
40 #include "core/dom/Element.h"
37 #include "core/dom/ExecutionContext.h" 41 #include "core/dom/ExecutionContext.h"
42 #include "core/dom/StaticNodeList.h"
38 #include "core/frame/FrameConsole.h" 43 #include "core/frame/FrameConsole.h"
39 #include "core/frame/LocalDOMWindow.h" 44 #include "core/frame/LocalDOMWindow.h"
40 #include "core/frame/LocalFrame.h" 45 #include "core/frame/LocalFrame.h"
41 #include "core/frame/UseCounter.h" 46 #include "core/frame/UseCounter.h"
42 #include "core/inspector/IdentifiersFactory.h" 47 #include "core/inspector/IdentifiersFactory.h"
43 #include "core/inspector/InspectedFrames.h" 48 #include "core/inspector/InspectedFrames.h"
44 #include "core/inspector/InspectorTaskRunner.h" 49 #include "core/inspector/InspectorTaskRunner.h"
45 #include "core/timing/MemoryInfo.h" 50 #include "core/timing/MemoryInfo.h"
46 #include "core/workers/MainThreadWorkletGlobalScope.h" 51 #include "core/workers/MainThreadWorkletGlobalScope.h"
52 #include "core/xml/XPathEvaluator.h"
53 #include "core/xml/XPathResult.h"
47 #include "platform/UserGestureIndicator.h" 54 #include "platform/UserGestureIndicator.h"
48 #include "platform/v8_inspector/public/V8Debugger.h" 55 #include "platform/v8_inspector/public/V8Debugger.h"
49 #include "public/platform/Platform.h" 56 #include "public/platform/Platform.h"
50 #include "wtf/OwnPtr.h" 57 #include "wtf/OwnPtr.h"
51 #include "wtf/PassOwnPtr.h" 58 #include "wtf/PassOwnPtr.h"
52 #include "wtf/ThreadingPrimitives.h" 59 #include "wtf/ThreadingPrimitives.h"
53 60
54 namespace blink { 61 namespace blink {
55 62
56 namespace { 63 namespace {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 234 }
228 235
229 v8::MaybeLocal<v8::Value> MainThreadDebugger::memoryInfo(v8::Isolate* isolate, v 8::Local<v8::Context> context, v8::Local<v8::Object> creationContext) 236 v8::MaybeLocal<v8::Value> MainThreadDebugger::memoryInfo(v8::Isolate* isolate, v 8::Local<v8::Context> context, v8::Local<v8::Object> creationContext)
230 { 237 {
231 ExecutionContext* executionContext = toExecutionContext(context); 238 ExecutionContext* executionContext = toExecutionContext(context);
232 ASSERT_UNUSED(executionContext, executionContext); 239 ASSERT_UNUSED(executionContext, executionContext);
233 ASSERT(executionContext->isDocument()); 240 ASSERT(executionContext->isDocument());
234 return toV8(MemoryInfo::create(), creationContext, isolate); 241 return toV8(MemoryInfo::create(), creationContext, isolate);
235 } 242 }
236 243
244 static void createFunctionProperty(v8::Local<v8::Context> context, v8::Local<v8: :Object> object, const char* name, v8::FunctionCallback callback)
245 {
246 v8::Local<v8::String> funcName = v8String(context->GetIsolate(), name);
247 v8::Local<v8::Function> func;
248 if (!v8::Function::New(context, callback).ToLocal(&func))
249 return;
250 func->SetName(funcName);
251 if (!object->Set(context, funcName, func).FromMaybe(false))
252 return;
253 }
254
255 void MainThreadDebugger::installAdditionalCommandLineAPI(v8::Local<v8::Context> context, v8::Local<v8::Object> object)
256 {
257 createFunctionProperty(context, object, "$", MainThreadDebugger::querySelect orCallback);
258 createFunctionProperty(context, object, "$$", MainThreadDebugger::querySelec torAllCallback);
259 createFunctionProperty(context, object, "$x", MainThreadDebugger::xpathSelec torCallback);
260 }
261
262 static Node* secondArgumentAsNode(const v8::FunctionCallbackInfo<v8::Value>& inf o)
263 {
264 if (info.Length() > 1) {
265 if (Node* node = V8Node::toImplWithTypeCheck(info.GetIsolate(), info[1]) )
266 return node;
267 }
268 ExecutionContext* executionContext = toExecutionContext(info.GetIsolate()->G etCurrentContext());
269 if (executionContext->isDocument())
270 return toDocument(executionContext);
271 return nullptr;
272 }
273
274 void MainThreadDebugger::querySelectorCallback(const v8::FunctionCallbackInfo<v8 ::Value>& info)
275 {
276 if (info.Length() < 1)
277 return;
278 String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
279 if (selector.isEmpty())
280 return;
281 Node* node = secondArgumentAsNode(info);
282 if (!node || !node->isContainerNode())
283 return;
284 ExceptionState exceptionState(ExceptionState::ExecutionContext, "$", "Comman dLineAPI", info.Holder(), info.GetIsolate());
285 Element* element = toContainerNode(node)->querySelector(AtomicString(selecto r), exceptionState);
286 if (exceptionState.hadException())
287 return;
288 if (element)
289 info.GetReturnValue().Set(toV8(element, info.Holder(), info.GetIsolate() ));
290 else
291 info.GetReturnValue().Set(v8::Null(info.GetIsolate()));
292 }
293
294 void MainThreadDebugger::querySelectorAllCallback(const v8::FunctionCallbackInfo <v8::Value>& info)
295 {
296 if (info.Length() < 1)
297 return;
298 String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
299 if (selector.isEmpty())
300 return;
301 Node* node = secondArgumentAsNode(info);
302 if (!node || !node->isContainerNode())
303 return;
304 ExceptionState exceptionState(ExceptionState::ExecutionContext, "$$", "Comma ndLineAPI", info.Holder(), info.GetIsolate());
305 // toV8(elementList) doesn't work here, since we need a proper Array instanc e, not NodeList.
306 StaticElementList* elementList = toContainerNode(node)->querySelectorAll(Ato micString(selector), exceptionState);
307 if (!elementList || exceptionState.hadException())
308 return;
309 v8::Isolate* isolate = info.GetIsolate();
310 v8::Local<v8::Context> context = isolate->GetCurrentContext();
311 v8::Local<v8::Array> nodes = v8::Array::New(isolate, elementList->length());
312 for (size_t i = 0; i < elementList->length(); ++i) {
313 Element* element = elementList->item(i);
314 if (!nodes->Set(context, i, toV8(element, info.Holder(), info.GetIsolate ())).FromMaybe(false))
315 return;
316 }
317 info.GetReturnValue().Set(nodes);
318 }
319
320 void MainThreadDebugger::xpathSelectorCallback(const v8::FunctionCallbackInfo<v8 ::Value>& info)
321 {
322 if (info.Length() < 1)
323 return;
324 String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
325 if (selector.isEmpty())
326 return;
327 Node* node = secondArgumentAsNode(info);
328 if (!node || !node->isContainerNode())
329 return;
330
331 XPathEvaluator* evaluator = XPathEvaluator::create();
332 if (!evaluator)
333 return;
334 ExceptionState exceptionState(ExceptionState::ExecutionContext, "$x", "Comma ndLineAPI", 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.
335 XPathResult* result = evaluator->evaluate(selector, node, nullptr, XPathResu lt::ANY_TYPE, ScriptValue(), exceptionState);
336 if (!result || exceptionState.hadException())
337 return;
338 if (result->resultType() == XPathResult::NUMBER_TYPE) {
339 info.GetReturnValue().Set(toV8(result->numberValue(exceptionState), info .Holder(), info.GetIsolate()));
340 } else if (result->resultType() == XPathResult::STRING_TYPE) {
341 info.GetReturnValue().Set(toV8(result->stringValue(exceptionState), info .Holder(), info.GetIsolate()));
342 } else if (result->resultType() == XPathResult::BOOLEAN_TYPE) {
343 info.GetReturnValue().Set(toV8(result->booleanValue(exceptionState), inf o.Holder(), info.GetIsolate()));
344 } else {
345 v8::Isolate* isolate = info.GetIsolate();
346 v8::Local<v8::Context> context = isolate->GetCurrentContext();
347 v8::Local<v8::Array> nodes = v8::Array::New(isolate);
348 size_t index = 0;
349 while (Node* node = result->iterateNext(exceptionState)) {
350 if (exceptionState.hadException())
351 return;
352 if (!nodes->Set(context, index++, toV8(node, info.Holder(), info.Get Isolate())).FromMaybe(false))
353 return;
354 }
355 info.GetReturnValue().Set(nodes);
356 }
357 }
358
237 } // namespace blink 359 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698