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

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() < 2) {
265 ExecutionContext* executionContext = toExecutionContext(info.GetIsolate( )->GetCurrentContext());
266 if (executionContext->isDocument())
267 return toDocument(executionContext);
268 } else {
269 return V8Node::toImplWithTypeCheck(info.GetIsolate(), info[1]);
270 }
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 if (Element* element = toContainerNode(node)->querySelector(AtomicString(sel ector), IGNORE_EXCEPTION))
285 info.GetReturnValue().Set(toV8(element, info.Holder(), info.GetIsolate() ));
286 }
287
288 void MainThreadDebugger::querySelectorAllCallback(const v8::FunctionCallbackInfo <v8::Value>& info)
289 {
290 if (info.Length() < 1)
291 return;
292 String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
293 if (selector.isEmpty())
294 return;
295 Node* node = secondArgumentAsNode(info);
296 if (!node || !node->isContainerNode())
297 return;
298 // toV8(elementList) doesn't work here, since we need a proper Array instanc e, not NodeList.
299 StaticElementList* elementList = toContainerNode(node)->querySelectorAll(Ato micString(selector), IGNORE_EXCEPTION);
300 if (!elementList)
301 return;
302 v8::Isolate* isolate = info.GetIsolate();
303 v8::Local<v8::Context> context = isolate->GetCurrentContext();
304 v8::Local<v8::Array> nodes = v8::Array::New(isolate, elementList->length());
305 for (size_t i = 0; i < elementList->length(); ++i) {
306 Element* element = elementList->item(i);
307 if (!nodes->Set(context, i, toV8(element, info.Holder(), info.GetIsolate ())).FromMaybe(false))
308 return;
309 }
310 info.GetReturnValue().Set(nodes);
311 }
312
313 void MainThreadDebugger::xpathSelectorCallback(const v8::FunctionCallbackInfo<v8 ::Value>& info)
314 {
315 if (info.Length() < 1)
316 return;
317 String selector = toCoreStringWithUndefinedOrNullCheck(info[0]);
318 if (selector.isEmpty())
319 return;
320 Node* node = secondArgumentAsNode(info);
321 if (info.Length() > 1 && node && node->getNodeType() != Node::DOCUMENT_NODE)
322 node = node->ownerDocument();
dgozman 2016/05/18 03:30:18 I think you lose node here, while it's important i
kozy 2016/05/18 22:46:39 Done.
323 if (!node || !node->isContainerNode())
324 return;
325
326 XPathEvaluator* evaluator = XPathEvaluator::create();
327 XPathResult* result = evaluator->evaluate(selector, node, nullptr, XPathResu lt::ANY_TYPE, ScriptValue(), IGNORE_EXCEPTION);
328 if (!result)
329 return;
330 if (result->resultType() == XPathResult::NUMBER_TYPE) {
331 info.GetReturnValue().Set(toV8(result->numberValue(IGNORE_EXCEPTION), in fo.Holder(), info.GetIsolate()));
332 } else if (result->resultType() == XPathResult::STRING_TYPE) {
333 info.GetReturnValue().Set(toV8(result->stringValue(IGNORE_EXCEPTION), in fo.Holder(), info.GetIsolate()));
334 } else if (result->resultType() == XPathResult::BOOLEAN_TYPE) {
335 info.GetReturnValue().Set(toV8(result->booleanValue(IGNORE_EXCEPTION), i nfo.Holder(), info.GetIsolate()));
336 } else {
337 v8::Isolate* isolate = info.GetIsolate();
338 v8::Local<v8::Context> context = isolate->GetCurrentContext();
339 v8::Local<v8::Array> nodes = v8::Array::New(isolate);
340 size_t index = 0;
341 while (Node* node = result->iterateNext(IGNORE_EXCEPTION)) {
342 if (!nodes->Set(context, index++, toV8(node, info.Holder(), info.Get Isolate())).FromMaybe(false))
343 return;
344 }
345 info.GetReturnValue().Set(nodes);
346 }
347 }
348
237 } // namespace blink 349 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698