| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/inspector/ThreadDebugger.h" | 5 #include "core/inspector/ThreadDebugger.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/SourceLocation.h" | 7 #include "bindings/core/v8/SourceLocation.h" |
| 8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8DOMException.h" | 9 #include "bindings/core/v8/V8DOMException.h" |
| 10 #include "bindings/core/v8/V8DOMTokenList.h" | 10 #include "bindings/core/v8/V8DOMTokenList.h" |
| 11 #include "bindings/core/v8/V8Event.h" | 11 #include "bindings/core/v8/V8Event.h" |
| 12 #include "bindings/core/v8/V8EventListener.h" | 12 #include "bindings/core/v8/V8EventListener.h" |
| 13 #include "bindings/core/v8/V8EventListenerInfo.h" | 13 #include "bindings/core/v8/V8EventListenerInfo.h" |
| 14 #include "bindings/core/v8/V8EventListenerList.h" | 14 #include "bindings/core/v8/V8EventListenerList.h" |
| 15 #include "bindings/core/v8/V8HTMLAllCollection.h" | 15 #include "bindings/core/v8/V8HTMLAllCollection.h" |
| 16 #include "bindings/core/v8/V8HTMLCollection.h" | 16 #include "bindings/core/v8/V8HTMLCollection.h" |
| 17 #include "bindings/core/v8/V8Node.h" | 17 #include "bindings/core/v8/V8Node.h" |
| 18 #include "bindings/core/v8/V8NodeList.h" | 18 #include "bindings/core/v8/V8NodeList.h" |
| 19 #include "bindings/core/v8/V8ScriptRunner.h" |
| 19 #include "core/inspector/ConsoleMessage.h" | 20 #include "core/inspector/ConsoleMessage.h" |
| 20 #include "core/inspector/InspectorDOMDebuggerAgent.h" | 21 #include "core/inspector/InspectorDOMDebuggerAgent.h" |
| 21 #include "core/inspector/InspectorTraceEvents.h" | 22 #include "core/inspector/InspectorTraceEvents.h" |
| 22 #include "platform/ScriptForbiddenScope.h" | 23 #include "platform/ScriptForbiddenScope.h" |
| 23 #include "wtf/CurrentTime.h" | 24 #include "wtf/CurrentTime.h" |
| 24 #include "wtf/PtrUtil.h" | 25 #include "wtf/PtrUtil.h" |
| 25 #include <memory> | 26 #include <memory> |
| 26 | 27 |
| 27 namespace blink { | 28 namespace blink { |
| 28 | 29 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 { | 178 { |
| 178 DCHECK(m_asyncInstrumentationEnabled); | 179 DCHECK(m_asyncInstrumentationEnabled); |
| 179 m_asyncInstrumentationEnabled = false; | 180 m_asyncInstrumentationEnabled = false; |
| 180 } | 181 } |
| 181 | 182 |
| 182 static void returnDataCallback(const v8::FunctionCallbackInfo<v8::Value>& info) | 183 static void returnDataCallback(const v8::FunctionCallbackInfo<v8::Value>& info) |
| 183 { | 184 { |
| 184 info.GetReturnValue().Set(info.Data()); | 185 info.GetReturnValue().Set(info.Data()); |
| 185 } | 186 } |
| 186 | 187 |
| 187 void ThreadDebugger::createFunctionProperty(v8::Local<v8::Context> context, v8::
Local<v8::Object> object, const char* name, v8::FunctionCallback callback, const
char* description) | 188 static void createFunctionPropertyWithData(v8::Local<v8::Context> context, v8::L
ocal<v8::Object> object, const char* name, v8::FunctionCallback callback, v8::Lo
cal<v8::Value> data, const char* description) |
| 188 { | 189 { |
| 189 v8::Local<v8::String> funcName = v8String(context->GetIsolate(), name); | 190 v8::Local<v8::String> funcName = v8String(context->GetIsolate(), name); |
| 190 v8::Local<v8::Function> func; | 191 v8::Local<v8::Function> func; |
| 191 if (!v8::Function::New(context, callback, v8::External::New(context->GetIsol
ate(), this), 0, v8::ConstructorBehavior::kThrow).ToLocal(&func)) | 192 if (!v8::Function::New(context, callback, data, 0, v8::ConstructorBehavior::
kThrow).ToLocal(&func)) |
| 192 return; | 193 return; |
| 193 func->SetName(funcName); | 194 func->SetName(funcName); |
| 194 v8::Local<v8::String> returnValue = v8String(context->GetIsolate(), descript
ion); | 195 v8::Local<v8::String> returnValue = v8String(context->GetIsolate(), descript
ion); |
| 195 v8::Local<v8::Function> toStringFunction; | 196 v8::Local<v8::Function> toStringFunction; |
| 196 if (v8::Function::New(context, returnDataCallback, returnValue, 0, v8::Const
ructorBehavior::kThrow).ToLocal(&toStringFunction)) | 197 if (v8::Function::New(context, returnDataCallback, returnValue, 0, v8::Const
ructorBehavior::kThrow).ToLocal(&toStringFunction)) |
| 197 func->Set(v8String(context->GetIsolate(), "toString"), toStringFunction)
; | 198 func->Set(v8String(context->GetIsolate(), "toString"), toStringFunction)
; |
| 198 if (!object->Set(context, funcName, func).FromMaybe(false)) | 199 if (!object->Set(context, funcName, func).FromMaybe(false)) |
| 199 return; | 200 return; |
| 200 } | 201 } |
| 201 | 202 |
| 203 void ThreadDebugger::createFunctionProperty(v8::Local<v8::Context> context, v8::
Local<v8::Object> object, const char* name, v8::FunctionCallback callback, const
char* description) |
| 204 { |
| 205 createFunctionPropertyWithData(context, object, name, callback, v8::External
::New(context->GetIsolate(), this), description); |
| 206 } |
| 207 |
| 202 void ThreadDebugger::installAdditionalCommandLineAPI(v8::Local<v8::Context> cont
ext, v8::Local<v8::Object> object) | 208 void ThreadDebugger::installAdditionalCommandLineAPI(v8::Local<v8::Context> cont
ext, v8::Local<v8::Object> object) |
| 203 { | 209 { |
| 204 createFunctionProperty(context, object, "monitorEvents", ThreadDebugger::mon
itorEventsCallback, "function monitorEvents(object, [types]) { [Command Line API
] }"); | |
| 205 createFunctionProperty(context, object, "unmonitorEvents", ThreadDebugger::u
nmonitorEventsCallback, "function unmonitorEvents(object, [types]) { [Command Li
ne API] }"); | |
| 206 createFunctionProperty(context, object, "getEventListeners", ThreadDebugger:
:getEventListenersCallback, "function getEventListeners(node) { [Command Line AP
I] }"); | 210 createFunctionProperty(context, object, "getEventListeners", ThreadDebugger:
:getEventListenersCallback, "function getEventListeners(node) { [Command Line AP
I] }"); |
| 211 |
| 212 v8::Local<v8::Value> functionValue; |
| 213 bool success = V8ScriptRunner::compileAndRunInternalScript(v8String(m_isolat
e, "(function(e) { console.log(e.type, e); })"), m_isolate).ToLocal(&functionVal
ue) && functionValue->IsFunction(); |
| 214 DCHECK(success); |
| 215 createFunctionPropertyWithData(context, object, "monitorEvents", ThreadDebug
ger::monitorEventsCallback, functionValue, "function monitorEvents(object, [type
s]) { [Command Line API] }"); |
| 216 createFunctionPropertyWithData(context, object, "unmonitorEvents", ThreadDeb
ugger::unmonitorEventsCallback, functionValue, "function unmonitorEvents(object,
[types]) { [Command Line API] }"); |
| 207 } | 217 } |
| 208 | 218 |
| 209 static Vector<String> normalizeEventTypes(const v8::FunctionCallbackInfo<v8::Val
ue>& info) | 219 static Vector<String> normalizeEventTypes(const v8::FunctionCallbackInfo<v8::Val
ue>& info) |
| 210 { | 220 { |
| 211 Vector<String> types; | 221 Vector<String> types; |
| 212 if (info.Length() > 1 && info[1]->IsString()) | 222 if (info.Length() > 1 && info[1]->IsString()) |
| 213 types.append(toCoreString(info[1].As<v8::String>())); | 223 types.append(toCoreString(info[1].As<v8::String>())); |
| 214 if (info.Length() > 1 && info[1]->IsArray()) { | 224 if (info.Length() > 1 && info[1]->IsArray()) { |
| 215 v8::Local<v8::Array> typesArray = v8::Local<v8::Array>::Cast(info[1]); | 225 v8::Local<v8::Array> typesArray = v8::Local<v8::Array>::Cast(info[1]); |
| 216 for (size_t i = 0; i < typesArray->Length(); ++i) { | 226 for (size_t i = 0; i < typesArray->Length(); ++i) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 234 else if (types[i] == "pointer") | 244 else if (types[i] == "pointer") |
| 235 outputTypes.appendVector(Vector<String>({ "pointerover", "pointerout
", "pointerenter", "pointerleave", "pointerdown", "pointerup", "pointermove", "p
ointercancel", "gotpointercapture", "lostpointercapture" })); | 245 outputTypes.appendVector(Vector<String>({ "pointerover", "pointerout
", "pointerenter", "pointerleave", "pointerdown", "pointerup", "pointermove", "p
ointercancel", "gotpointercapture", "lostpointercapture" })); |
| 236 else if (types[i] == "control") | 246 else if (types[i] == "control") |
| 237 outputTypes.appendVector(Vector<String>({ "resize", "scroll", "zoom"
, "focus", "blur", "select", "input", "change", "submit", "reset" })); | 247 outputTypes.appendVector(Vector<String>({ "resize", "scroll", "zoom"
, "focus", "blur", "select", "input", "change", "submit", "reset" })); |
| 238 else | 248 else |
| 239 outputTypes.append(types[i]); | 249 outputTypes.append(types[i]); |
| 240 } | 250 } |
| 241 return outputTypes; | 251 return outputTypes; |
| 242 } | 252 } |
| 243 | 253 |
| 244 void ThreadDebugger::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info
) | |
| 245 { | |
| 246 if (info.Length() < 1) | |
| 247 return; | |
| 248 ThreadDebugger* debugger = static_cast<ThreadDebugger*>(v8::Local<v8::Extern
al>::Cast(info.Data())->Value()); | |
| 249 DCHECK(debugger); | |
| 250 Event* event = V8Event::toImplWithTypeCheck(info.GetIsolate(), info[0]); | |
| 251 if (!event) | |
| 252 return; | |
| 253 debugger->debugger()->logToConsole(info.GetIsolate()->GetCurrentContext(), v
8String(info.GetIsolate(), event->type()), info[0]); | |
| 254 } | |
| 255 | |
| 256 v8::Local<v8::Function> ThreadDebugger::eventLogFunction() | |
| 257 { | |
| 258 if (m_eventLogFunction.IsEmpty()) | |
| 259 m_eventLogFunction.Reset(m_isolate, v8::Function::New(m_isolate->GetCurr
entContext(), logCallback, v8::External::New(m_isolate, this), 0, v8::Constructo
rBehavior::kThrow).ToLocalChecked()); | |
| 260 return m_eventLogFunction.Get(m_isolate); | |
| 261 } | |
| 262 | |
| 263 static EventTarget* firstArgumentAsEventTarget(const v8::FunctionCallbackInfo<v8
::Value>& info) | 254 static EventTarget* firstArgumentAsEventTarget(const v8::FunctionCallbackInfo<v8
::Value>& info) |
| 264 { | 255 { |
| 265 if (info.Length() < 1) | 256 if (info.Length() < 1) |
| 266 return nullptr; | 257 return nullptr; |
| 267 if (EventTarget* target = V8EventTarget::toImplWithTypeCheck(info.GetIsolate
(), info[0])) | 258 if (EventTarget* target = V8EventTarget::toImplWithTypeCheck(info.GetIsolate
(), info[0])) |
| 268 return target; | 259 return target; |
| 269 return toDOMWindow(info.GetIsolate(), info[0]); | 260 return toDOMWindow(info.GetIsolate(), info[0]); |
| 270 } | 261 } |
| 271 | 262 |
| 272 void ThreadDebugger::setMonitorEventsCallback(const v8::FunctionCallbackInfo<v8:
:Value>& info, bool enabled) | 263 void ThreadDebugger::setMonitorEventsCallback(const v8::FunctionCallbackInfo<v8:
:Value>& info, bool enabled) |
| 273 { | 264 { |
| 274 EventTarget* eventTarget = firstArgumentAsEventTarget(info); | 265 EventTarget* eventTarget = firstArgumentAsEventTarget(info); |
| 275 if (!eventTarget) | 266 if (!eventTarget) |
| 276 return; | 267 return; |
| 277 Vector<String> types = normalizeEventTypes(info); | 268 Vector<String> types = normalizeEventTypes(info); |
| 278 ThreadDebugger* debugger = static_cast<ThreadDebugger*>(v8::Local<v8::Extern
al>::Cast(info.Data())->Value()); | 269 EventListener* eventListener = V8EventListenerList::getEventListener(ScriptS
tate::current(info.GetIsolate()), v8::Local<v8::Function>::Cast(info.Data()), fa
lse, enabled ? ListenerFindOrCreate : ListenerFindOnly); |
| 279 DCHECK(debugger); | |
| 280 EventListener* eventListener = V8EventListenerList::getEventListener(ScriptS
tate::current(info.GetIsolate()), debugger->eventLogFunction(), false, enabled ?
ListenerFindOrCreate : ListenerFindOnly); | |
| 281 if (!eventListener) | 270 if (!eventListener) |
| 282 return; | 271 return; |
| 283 for (size_t i = 0; i < types.size(); ++i) { | 272 for (size_t i = 0; i < types.size(); ++i) { |
| 284 if (enabled) | 273 if (enabled) |
| 285 eventTarget->addEventListener(AtomicString(types[i]), eventListener,
false); | 274 eventTarget->addEventListener(AtomicString(types[i]), eventListener,
false); |
| 286 else | 275 else |
| 287 eventTarget->removeEventListener(AtomicString(types[i]), eventListen
er, false); | 276 eventTarget->removeEventListener(AtomicString(types[i]), eventListen
er, false); |
| 288 } | 277 } |
| 289 } | 278 } |
| 290 | 279 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 { | 377 { |
| 389 for (size_t index = 0; index < m_timers.size(); ++index) { | 378 for (size_t index = 0; index < m_timers.size(); ++index) { |
| 390 if (m_timers[index].get() == timer) { | 379 if (m_timers[index].get() == timer) { |
| 391 m_timerCallbacks[index](m_timerData[index]); | 380 m_timerCallbacks[index](m_timerData[index]); |
| 392 return; | 381 return; |
| 393 } | 382 } |
| 394 } | 383 } |
| 395 } | 384 } |
| 396 | 385 |
| 397 } // namespace blink | 386 } // namespace blink |
| OLD | NEW |