| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2011 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #include "sky/engine/config.h" | |
| 32 #include "sky/engine/bindings/core/v8/PageScriptDebugServer.h" | |
| 33 | |
| 34 #include "gen/sky/bindings/core/v8/V8Window.h" | |
| 35 #include "sky/engine/bindings/core/v8/DOMWrapperWorld.h" | |
| 36 #include "sky/engine/bindings/core/v8/ScriptController.h" | |
| 37 #include "sky/engine/bindings/core/v8/ScriptSourceCode.h" | |
| 38 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 39 #include "sky/engine/bindings/core/v8/V8ScriptRunner.h" | |
| 40 #include "sky/engine/bindings/core/v8/WindowProxy.h" | |
| 41 #include "sky/engine/core/dom/ExecutionContext.h" | |
| 42 #include "sky/engine/core/frame/FrameConsole.h" | |
| 43 #include "sky/engine/core/frame/LocalFrame.h" | |
| 44 #include "sky/engine/core/frame/UseCounter.h" | |
| 45 #include "sky/engine/core/inspector/InspectorTraceEvents.h" | |
| 46 #include "sky/engine/core/inspector/ScriptDebugListener.h" | |
| 47 #include "sky/engine/core/page/Page.h" | |
| 48 #include "sky/engine/v8_inspector/inspector_host.h" | |
| 49 #include "sky/engine/wtf/OwnPtr.h" | |
| 50 #include "sky/engine/wtf/PassOwnPtr.h" | |
| 51 #include "sky/engine/wtf/StdLibExtras.h" | |
| 52 #include "sky/engine/wtf/TemporaryChange.h" | |
| 53 #include "sky/engine/wtf/text/StringBuilder.h" | |
| 54 | |
| 55 namespace blink { | |
| 56 | |
| 57 static LocalFrame* retrieveFrameWithGlobalObjectCheck(v8::Handle<v8::Context> co
ntext) | |
| 58 { | |
| 59 if (context.IsEmpty()) | |
| 60 return 0; | |
| 61 | |
| 62 // FIXME: This is a temporary hack for crbug.com/345014. | |
| 63 // Currently it's possible that V8 can trigger Debugger::ProcessDebugEvent f
or a context | |
| 64 // that is being initialized (i.e., inside Context::New() of the context). | |
| 65 // We should fix the V8 side so that it won't trigger the event for a half-b
aked context | |
| 66 // because there is no way in the embedder side to check if the context is h
alf-baked or not. | |
| 67 if (isMainThread() && DOMWrapperWorld::windowIsBeingInitialized()) | |
| 68 return 0; | |
| 69 | |
| 70 v8::Handle<v8::Value> global = V8Window::findInstanceInPrototypeChain(contex
t->Global(), context->GetIsolate()); | |
| 71 if (global.IsEmpty()) | |
| 72 return 0; | |
| 73 | |
| 74 return toFrameIfNotDetached(context); | |
| 75 } | |
| 76 | |
| 77 void PageScriptDebugServer::setPreprocessorSource(const String& preprocessorSour
ce) | |
| 78 { | |
| 79 if (preprocessorSource.isEmpty()) | |
| 80 m_preprocessorSourceCode.clear(); | |
| 81 else | |
| 82 m_preprocessorSourceCode = adoptPtr(new ScriptSourceCode(preprocessorSou
rce)); | |
| 83 m_scriptPreprocessor.clear(); | |
| 84 } | |
| 85 | |
| 86 PageScriptDebugServer& PageScriptDebugServer::shared() | |
| 87 { | |
| 88 DEFINE_STATIC_LOCAL(PageScriptDebugServer, server, ()); | |
| 89 return server; | |
| 90 } | |
| 91 | |
| 92 v8::Isolate* PageScriptDebugServer::s_mainThreadIsolate = 0; | |
| 93 | |
| 94 void PageScriptDebugServer::setMainThreadIsolate(v8::Isolate* isolate) | |
| 95 { | |
| 96 s_mainThreadIsolate = isolate; | |
| 97 } | |
| 98 | |
| 99 PageScriptDebugServer::PageScriptDebugServer() | |
| 100 : ScriptDebugServer(s_mainThreadIsolate) | |
| 101 , m_pausedHost(0) | |
| 102 { | |
| 103 } | |
| 104 | |
| 105 PageScriptDebugServer::~PageScriptDebugServer() | |
| 106 { | |
| 107 } | |
| 108 | |
| 109 void PageScriptDebugServer::addListener(ScriptDebugListener* listener, inspector
::InspectorHost* host) | |
| 110 { | |
| 111 v8::HandleScope scope(m_isolate); | |
| 112 | |
| 113 if (!m_listenersMap.size()) { | |
| 114 v8::Debug::SetDebugEventListener(&PageScriptDebugServer::v8DebugEventCal
lback, v8::External::New(m_isolate, this)); | |
| 115 ensureDebuggerScriptCompiled(); | |
| 116 } | |
| 117 | |
| 118 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 119 v8::Context::Scope contextScope(debuggerContext); | |
| 120 | |
| 121 v8::Local<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate); | |
| 122 ASSERT(!debuggerScript->IsUndefined()); | |
| 123 m_listenersMap.set(host, listener); | |
| 124 | |
| 125 v8::Local<v8::Context> context = host->GetContext(); | |
| 126 v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(
debuggerScript->Get(v8AtomicString(m_isolate, "getScripts"))); | |
| 127 v8::Handle<v8::Value> argv[] = { context->GetEmbedderData(0) }; | |
| 128 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(getScript
sFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate); | |
| 129 if (value.IsEmpty()) | |
| 130 return; | |
| 131 ASSERT(!value->IsUndefined() && value->IsArray()); | |
| 132 v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value); | |
| 133 for (unsigned i = 0; i < scriptsArray->Length(); ++i) | |
| 134 dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArr
ay->Get(v8::Integer::New(m_isolate, i))), CompileSuccess); | |
| 135 } | |
| 136 | |
| 137 void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, inspec
tor::InspectorHost* host) | |
| 138 { | |
| 139 if (!m_listenersMap.contains(host)) | |
| 140 return; | |
| 141 | |
| 142 if (m_pausedHost == host) | |
| 143 continueProgram(); | |
| 144 | |
| 145 m_listenersMap.remove(host); | |
| 146 | |
| 147 if (m_listenersMap.isEmpty()) { | |
| 148 discardDebuggerScript(); | |
| 149 v8::Debug::SetDebugEventListener(0); | |
| 150 // FIXME: Remove all breakpoints set by the agent. | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 void PageScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task) | |
| 155 { | |
| 156 ScriptDebugServer::interruptAndRun(task, s_mainThreadIsolate); | |
| 157 } | |
| 158 | |
| 159 void PageScriptDebugServer::setClientMessageLoop(PassOwnPtr<ClientMessageLoop> c
lientMessageLoop) | |
| 160 { | |
| 161 m_clientMessageLoop = clientMessageLoop; | |
| 162 } | |
| 163 | |
| 164 void PageScriptDebugServer::compileScript(ScriptState* scriptState, const String
& expression, const String& sourceURL, String* scriptId, String* exceptionDetail
sText, int* lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace) | |
| 165 { | |
| 166 ExecutionContext* executionContext = scriptState->executionContext(); | |
| 167 RefPtr<LocalFrame> protect = executionContext->executingWindow()->frame(); | |
| 168 ScriptDebugServer::compileScript(scriptState, expression, sourceURL, scriptI
d, exceptionDetailsText, lineNumber, columnNumber, stackTrace); | |
| 169 if (!scriptId->isNull()) | |
| 170 m_compiledScriptURLs.set(*scriptId, sourceURL); | |
| 171 } | |
| 172 | |
| 173 void PageScriptDebugServer::clearCompiledScripts() | |
| 174 { | |
| 175 ScriptDebugServer::clearCompiledScripts(); | |
| 176 m_compiledScriptURLs.clear(); | |
| 177 } | |
| 178 | |
| 179 void PageScriptDebugServer::runScript(ScriptState* scriptState, const String& sc
riptId, ScriptValue* result, bool* wasThrown, String* exceptionDetailsText, int*
lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace) | |
| 180 { | |
| 181 String sourceURL = m_compiledScriptURLs.take(scriptId); | |
| 182 | |
| 183 ExecutionContext* executionContext = scriptState->executionContext(); | |
| 184 LocalFrame* frame = executionContext->executingWindow()->frame(); | |
| 185 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "EvaluateScript
", "data", InspectorEvaluateScriptEvent::data(frame, sourceURL, TextPosition::mi
nimumPosition().m_line.oneBasedInt())); | |
| 186 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "
CallStack", TRACE_EVENT_SCOPE_PROCESS, "stack", InspectorCallStackEvent::current
CallStack()); | |
| 187 | |
| 188 RefPtr<LocalFrame> protect = frame; | |
| 189 ScriptDebugServer::runScript(scriptState, scriptId, result, wasThrown, excep
tionDetailsText, lineNumber, columnNumber, stackTrace); | |
| 190 | |
| 191 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update
Counters", TRACE_EVENT_SCOPE_PROCESS, "data", InspectorUpdateCountersEvent::data
()); | |
| 192 } | |
| 193 | |
| 194 ScriptDebugListener* PageScriptDebugServer::getDebugListenerForContext(v8::Handl
e<v8::Context> context) | |
| 195 { | |
| 196 v8::HandleScope scope(m_isolate); | |
| 197 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(context); | |
| 198 if (!frame) | |
| 199 return 0; | |
| 200 return m_listenersMap.get(frame->page()->inspectorHost()); | |
| 201 } | |
| 202 | |
| 203 void PageScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> contex
t) | |
| 204 { | |
| 205 v8::HandleScope scope(m_isolate); | |
| 206 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(context); | |
| 207 m_pausedHost = frame->page()->inspectorHost(); | |
| 208 | |
| 209 // Wait for continue or step command. | |
| 210 m_clientMessageLoop->run(m_pausedHost); | |
| 211 | |
| 212 // The listener may have been removed in the nested loop. | |
| 213 if (ScriptDebugListener* listener = m_listenersMap.get(m_pausedHost)) | |
| 214 listener->didContinue(); | |
| 215 | |
| 216 m_pausedHost = 0; | |
| 217 } | |
| 218 | |
| 219 void PageScriptDebugServer::quitMessageLoopOnPause() | |
| 220 { | |
| 221 m_clientMessageLoop->quitNow(); | |
| 222 } | |
| 223 | |
| 224 void PageScriptDebugServer::preprocessBeforeCompile(const v8::Debug::EventDetail
s& eventDetails) | |
| 225 { | |
| 226 v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext(); | |
| 227 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(eventContext); | |
| 228 if (!frame) | |
| 229 return; | |
| 230 | |
| 231 if (!canPreprocess(frame)) | |
| 232 return; | |
| 233 | |
| 234 v8::Handle<v8::Object> eventData = eventDetails.GetEventData(); | |
| 235 v8::Local<v8::Context> debugContext = v8::Debug::GetDebugContext(); | |
| 236 v8::Context::Scope contextScope(debugContext); | |
| 237 v8::TryCatch tryCatch; | |
| 238 // <script> tag source and attribute value source are preprocessed before we
enter V8. | |
| 239 // Avoid preprocessing any internal scripts by processing only eval source i
n this V8 event handler. | |
| 240 v8::Handle<v8::Value> argvEventData[] = { eventData }; | |
| 241 v8::Handle<v8::Value> v8Value = callDebuggerMethod("isEvalCompilation", WTF_
ARRAY_LENGTH(argvEventData), argvEventData); | |
| 242 if (v8Value.IsEmpty() || !v8Value->ToBoolean()->Value()) | |
| 243 return; | |
| 244 | |
| 245 // The name and source are in the JS event data. | |
| 246 String scriptName = toCoreStringWithUndefinedOrNullCheck(callDebuggerMethod(
"getScriptName", WTF_ARRAY_LENGTH(argvEventData), argvEventData)); | |
| 247 String script = toCoreStringWithUndefinedOrNullCheck(callDebuggerMethod("get
ScriptSource", WTF_ARRAY_LENGTH(argvEventData), argvEventData)); | |
| 248 | |
| 249 String preprocessedSource = m_scriptPreprocessor->preprocessSourceCode(scri
pt, scriptName); | |
| 250 | |
| 251 v8::Handle<v8::Value> argvPreprocessedScript[] = { eventData, v8String(debug
Context->GetIsolate(), preprocessedSource) }; | |
| 252 callDebuggerMethod("setScriptSource", WTF_ARRAY_LENGTH(argvPreprocessedScrip
t), argvPreprocessedScript); | |
| 253 } | |
| 254 | |
| 255 static bool isCreatingPreprocessor = false; | |
| 256 | |
| 257 bool PageScriptDebugServer::canPreprocess(LocalFrame* frame) | |
| 258 { | |
| 259 ASSERT(frame); | |
| 260 | |
| 261 if (!m_preprocessorSourceCode || !frame->page() || isCreatingPreprocessor) | |
| 262 return false; | |
| 263 | |
| 264 // We delay the creation of the preprocessor until just before the first JS
from the | |
| 265 // Web page to ensure that the debugger's console initialization code has co
mpleted. | |
| 266 if (!m_scriptPreprocessor) { | |
| 267 TemporaryChange<bool> isPreprocessing(isCreatingPreprocessor, true); | |
| 268 m_scriptPreprocessor = adoptPtr(new ScriptPreprocessor(*m_preprocessorSo
urceCode.get(), frame)); | |
| 269 } | |
| 270 | |
| 271 if (m_scriptPreprocessor->isValid()) | |
| 272 return true; | |
| 273 | |
| 274 m_scriptPreprocessor.clear(); | |
| 275 // Don't retry the compile if we fail one time. | |
| 276 m_preprocessorSourceCode.clear(); | |
| 277 return false; | |
| 278 } | |
| 279 | |
| 280 // Source to Source processing iff debugger enabled and it has loaded a preproce
ssor. | |
| 281 PassOwnPtr<ScriptSourceCode> PageScriptDebugServer::preprocess(LocalFrame* frame
, const ScriptSourceCode& sourceCode) | |
| 282 { | |
| 283 if (!canPreprocess(frame)) | |
| 284 return PassOwnPtr<ScriptSourceCode>(); | |
| 285 | |
| 286 String preprocessedSource = m_scriptPreprocessor->preprocessSourceCode(sourc
eCode.source(), sourceCode.url()); | |
| 287 return adoptPtr(new ScriptSourceCode(preprocessedSource, sourceCode.url())); | |
| 288 } | |
| 289 | |
| 290 String PageScriptDebugServer::preprocessEventListener(LocalFrame* frame, const S
tring& source, const String& url, const String& functionName) | |
| 291 { | |
| 292 if (!canPreprocess(frame)) | |
| 293 return source; | |
| 294 | |
| 295 return m_scriptPreprocessor->preprocessSourceCode(source, url, functionName)
; | |
| 296 } | |
| 297 | |
| 298 void PageScriptDebugServer::clearPreprocessor() | |
| 299 { | |
| 300 m_scriptPreprocessor.clear(); | |
| 301 } | |
| 302 | |
| 303 void PageScriptDebugServer::muteWarningsAndDeprecations() | |
| 304 { | |
| 305 FrameConsole::mute(); | |
| 306 UseCounter::muteForInspector(); | |
| 307 } | |
| 308 | |
| 309 void PageScriptDebugServer::unmuteWarningsAndDeprecations() | |
| 310 { | |
| 311 FrameConsole::unmute(); | |
| 312 UseCounter::unmuteForInspector(); | |
| 313 } | |
| 314 | |
| 315 } // namespace blink | |
| OLD | NEW |