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

Side by Side Diff: sky/engine/bindings/core/v8/PageScriptDebugServer.cpp

Issue 727593004: Wire up the Inspector V8 Debugger (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Actually works Created 6 years, 1 month 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
(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 "config.h"
32 #include "bindings/core/v8/PageScriptDebugServer.h"
33
34 #include "bindings/core/v8/DOMWrapperWorld.h"
35 #include "bindings/core/v8/ScriptController.h"
36 #include "bindings/core/v8/ScriptSourceCode.h"
37 #include "bindings/core/v8/V8Binding.h"
38 #include "bindings/core/v8/V8ScriptRunner.h"
39 #include "bindings/core/v8/V8Window.h"
40 #include "bindings/core/v8/WindowProxy.h"
41 #include "core/dom/ExecutionContext.h"
42 #include "core/frame/FrameConsole.h"
43 #include "core/frame/FrameHost.h"
44 #include "core/frame/LocalFrame.h"
45 #include "core/frame/UseCounter.h"
46 #include "core/inspector/InspectorTraceEvents.h"
47 #include "core/inspector/ScriptDebugListener.h"
48 #include "core/page/Page.h"
49 #include "wtf/OwnPtr.h"
50 #include "wtf/PassOwnPtr.h"
51 #include "wtf/StdLibExtras.h"
52 #include "wtf/TemporaryChange.h"
53 #include "wtf/text/StringBuilder.h"
54 #include "gin/modules/console.h"
55 #include "gin/converter.h"
56
57 namespace blink {
58
59 static LocalFrame* retrieveFrameWithGlobalObjectCheck(v8::Handle<v8::Context> co ntext)
60 {
61 if (context.IsEmpty())
62 return 0;
63
64 // FIXME: This is a temporary hack for crbug.com/345014.
65 // Currently it's possible that V8 can trigger Debugger::ProcessDebugEvent f or a context
66 // that is being initialized (i.e., inside Context::New() of the context).
67 // We should fix the V8 side so that it won't trigger the event for a half-b aked context
68 // because there is no way in the embedder side to check if the context is h alf-baked or not.
69 if (isMainThread() && DOMWrapperWorld::windowIsBeingInitialized())
70 return 0;
71
72 v8::Handle<v8::Value> global = V8Window::findInstanceInPrototypeChain(contex t->Global(), context->GetIsolate());
73 if (global.IsEmpty())
74 return 0;
75
76 return toFrameIfNotDetached(context);
77 }
78
79 void PageScriptDebugServer::setPreprocessorSource(const String& preprocessorSour ce)
80 {
81 if (preprocessorSource.isEmpty())
82 m_preprocessorSourceCode.clear();
83 else
84 m_preprocessorSourceCode = adoptPtr(new ScriptSourceCode(preprocessorSou rce));
85 m_scriptPreprocessor.clear();
86 }
87
88 PageScriptDebugServer& PageScriptDebugServer::shared()
89 {
90 DEFINE_STATIC_LOCAL(PageScriptDebugServer, server, ());
91 return server;
92 }
93
94 v8::Isolate* PageScriptDebugServer::s_mainThreadIsolate = 0;
95
96 void PageScriptDebugServer::setMainThreadIsolate(v8::Isolate* isolate)
97 {
98 s_mainThreadIsolate = isolate;
99 }
100
101 PageScriptDebugServer::PageScriptDebugServer()
102 : ScriptDebugServer(s_mainThreadIsolate)
103 , m_pausedPage(0)
104 {
105 }
106
107 PageScriptDebugServer::~PageScriptDebugServer()
108 {
109 }
110
111 void PageScriptDebugServer::addListener(ScriptDebugListener* listener, Page* pag e)
112 {
113 ScriptController& scriptController = page->mainFrame()->script();
114
115 v8::HandleScope scope(m_isolate);
116
117 if (!m_listenersMap.size()) {
118 v8::Debug::SetDebugEventListener(&PageScriptDebugServer::v8DebugEventCal lback, v8::External::New(m_isolate, this));
119 ensureDebuggerScriptCompiled();
120 }
121
122 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
123 v8::Context::Scope contextScope(debuggerContext);
124
125 v8::Local<v8::Value> console = gin::Console::GetModule(m_isolate);
126 debuggerContext->Global()->Set(gin::StringToV8(m_isolate, "console"), consol e);
127
128 v8::Local<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate);
129 ASSERT(!debuggerScript->IsUndefined());
130 m_listenersMap.set(page, listener);
131
132 WindowProxy* windowProxy = scriptController.existingWindowProxy(DOMWrapperWo rld::mainWorld());
133 if (!windowProxy || !windowProxy->isContextInitialized())
134 return;
135 v8::Local<v8::Context> context = windowProxy->context();
136 v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast( debuggerScript->Get(v8AtomicString(m_isolate, "getScripts")));
137 v8::Handle<v8::Value> argv[] = { context->GetEmbedderData(0) };
138 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(getScript sFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate);
139 if (value.IsEmpty())
140 return;
141 ASSERT(!value->IsUndefined() && value->IsArray());
142 v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value);
143 for (unsigned i = 0; i < scriptsArray->Length(); ++i)
144 dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArr ay->Get(v8::Integer::New(m_isolate, i))), CompileSuccess);
145 }
146
147 void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, Page* page)
148 {
149 if (!m_listenersMap.contains(page))
150 return;
151
152 if (m_pausedPage == page)
153 continueProgram();
154
155 m_listenersMap.remove(page);
156
157 if (m_listenersMap.isEmpty()) {
158 discardDebuggerScript();
159 v8::Debug::SetDebugEventListener(0);
160 // FIXME: Remove all breakpoints set by the agent.
161 }
162 }
163
164 void PageScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task)
165 {
166 ScriptDebugServer::interruptAndRun(task, s_mainThreadIsolate);
167 }
168
169 void PageScriptDebugServer::setClientMessageLoop(PassOwnPtr<ClientMessageLoop> c lientMessageLoop)
170 {
171 m_clientMessageLoop = clientMessageLoop;
172 }
173
174 void PageScriptDebugServer::compileScript(ScriptState* scriptState, const String & expression, const String& sourceURL, String* scriptId, String* exceptionDetail sText, int* lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace)
175 {
176 ExecutionContext* executionContext = scriptState->executionContext();
177 RefPtr<LocalFrame> protect = executionContext->executingWindow()->frame();
178 ScriptDebugServer::compileScript(scriptState, expression, sourceURL, scriptI d, exceptionDetailsText, lineNumber, columnNumber, stackTrace);
179 if (!scriptId->isNull())
180 m_compiledScriptURLs.set(*scriptId, sourceURL);
181 }
182
183 void PageScriptDebugServer::clearCompiledScripts()
184 {
185 ScriptDebugServer::clearCompiledScripts();
186 m_compiledScriptURLs.clear();
187 }
188
189 void PageScriptDebugServer::runScript(ScriptState* scriptState, const String& sc riptId, ScriptValue* result, bool* wasThrown, String* exceptionDetailsText, int* lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace)
190 {
191 String sourceURL = m_compiledScriptURLs.take(scriptId);
192
193 ExecutionContext* executionContext = scriptState->executionContext();
194 LocalFrame* frame = executionContext->executingWindow()->frame();
195 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "EvaluateScript ", "data", InspectorEvaluateScriptEvent::data(frame, sourceURL, TextPosition::mi nimumPosition().m_line.oneBasedInt()));
196 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), " CallStack", TRACE_EVENT_SCOPE_PROCESS, "stack", InspectorCallStackEvent::current CallStack());
197
198 RefPtr<LocalFrame> protect = frame;
199 ScriptDebugServer::runScript(scriptState, scriptId, result, wasThrown, excep tionDetailsText, lineNumber, columnNumber, stackTrace);
200
201 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update Counters", TRACE_EVENT_SCOPE_PROCESS, "data", InspectorUpdateCountersEvent::data ());
202 }
203
204 ScriptDebugListener* PageScriptDebugServer::getDebugListenerForContext(v8::Handl e<v8::Context> context)
205 {
206 v8::HandleScope scope(m_isolate);
207 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(context);
208 if (!frame)
209 return 0;
210 return m_listenersMap.get(frame->page());
211 }
212
213 void PageScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> contex t)
214 {
215 v8::HandleScope scope(m_isolate);
216 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(context);
217 m_pausedPage = frame->page();
218
219 // Wait for continue or step command.
220 m_clientMessageLoop->run(m_pausedPage);
221
222 // The listener may have been removed in the nested loop.
223 if (ScriptDebugListener* listener = m_listenersMap.get(m_pausedPage))
224 listener->didContinue();
225
226 m_pausedPage = 0;
227 }
228
229 void PageScriptDebugServer::quitMessageLoopOnPause()
230 {
231 m_clientMessageLoop->quitNow();
232 }
233
234 void PageScriptDebugServer::preprocessBeforeCompile(const v8::Debug::EventDetail s& eventDetails)
235 {
236 v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext();
237 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(eventContext);
238 if (!frame)
239 return;
240
241 if (!canPreprocess(frame))
242 return;
243
244 v8::Handle<v8::Object> eventData = eventDetails.GetEventData();
245 v8::Local<v8::Context> debugContext = v8::Debug::GetDebugContext();
246 v8::Context::Scope contextScope(debugContext);
247 v8::TryCatch tryCatch;
248 // <script> tag source and attribute value source are preprocessed before we enter V8.
249 // Avoid preprocessing any internal scripts by processing only eval source i n this V8 event handler.
250 v8::Handle<v8::Value> argvEventData[] = { eventData };
251 v8::Handle<v8::Value> v8Value = callDebuggerMethod("isEvalCompilation", WTF_ ARRAY_LENGTH(argvEventData), argvEventData);
252 if (v8Value.IsEmpty() || !v8Value->ToBoolean()->Value())
253 return;
254
255 // The name and source are in the JS event data.
256 String scriptName = toCoreStringWithUndefinedOrNullCheck(callDebuggerMethod( "getScriptName", WTF_ARRAY_LENGTH(argvEventData), argvEventData));
257 String script = toCoreStringWithUndefinedOrNullCheck(callDebuggerMethod("get ScriptSource", WTF_ARRAY_LENGTH(argvEventData), argvEventData));
258
259 String preprocessedSource = m_scriptPreprocessor->preprocessSourceCode(scri pt, scriptName);
260
261 v8::Handle<v8::Value> argvPreprocessedScript[] = { eventData, v8String(debug Context->GetIsolate(), preprocessedSource) };
262 callDebuggerMethod("setScriptSource", WTF_ARRAY_LENGTH(argvPreprocessedScrip t), argvPreprocessedScript);
263 }
264
265 static bool isCreatingPreprocessor = false;
266
267 bool PageScriptDebugServer::canPreprocess(LocalFrame* frame)
268 {
269 ASSERT(frame);
270
271 if (!m_preprocessorSourceCode || !frame->page() || isCreatingPreprocessor)
272 return false;
273
274 // We delay the creation of the preprocessor until just before the first JS from the
275 // Web page to ensure that the debugger's console initialization code has co mpleted.
276 if (!m_scriptPreprocessor) {
277 TemporaryChange<bool> isPreprocessing(isCreatingPreprocessor, true);
278 m_scriptPreprocessor = adoptPtr(new ScriptPreprocessor(*m_preprocessorSo urceCode.get(), frame));
279 }
280
281 if (m_scriptPreprocessor->isValid())
282 return true;
283
284 m_scriptPreprocessor.clear();
285 // Don't retry the compile if we fail one time.
286 m_preprocessorSourceCode.clear();
287 return false;
288 }
289
290 // Source to Source processing iff debugger enabled and it has loaded a preproce ssor.
291 PassOwnPtr<ScriptSourceCode> PageScriptDebugServer::preprocess(LocalFrame* frame , const ScriptSourceCode& sourceCode)
292 {
293 if (!canPreprocess(frame))
294 return PassOwnPtr<ScriptSourceCode>();
295
296 String preprocessedSource = m_scriptPreprocessor->preprocessSourceCode(sourc eCode.source(), sourceCode.url());
297 return adoptPtr(new ScriptSourceCode(preprocessedSource, sourceCode.url()));
298 }
299
300 String PageScriptDebugServer::preprocessEventListener(LocalFrame* frame, const S tring& source, const String& url, const String& functionName)
301 {
302 if (!canPreprocess(frame))
303 return source;
304
305 return m_scriptPreprocessor->preprocessSourceCode(source, url, functionName) ;
306 }
307
308 void PageScriptDebugServer::clearPreprocessor()
309 {
310 m_scriptPreprocessor.clear();
311 }
312
313 void PageScriptDebugServer::muteWarningsAndDeprecations()
314 {
315 FrameConsole::mute();
316 UseCounter::muteForInspector();
317 }
318
319 void PageScriptDebugServer::unmuteWarningsAndDeprecations()
320 {
321 FrameConsole::unmute();
322 UseCounter::unmuteForInspector();
323 }
324
325 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698