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

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: 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
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_pausedPage(0)
102 {
103 }
104
105 PageScriptDebugServer::~PageScriptDebugServer()
106 {
107 }
108
109 void PageScriptDebugServer::addListener(ScriptDebugListener* listener, Page* pag e)
110 {
111 ScriptController& scriptController = page->mainFrame()->script();
112
113 v8::HandleScope scope(m_isolate);
114
115 if (!m_listenersMap.size()) {
116 v8::Debug::SetDebugEventListener(&PageScriptDebugServer::v8DebugEventCal lback, v8::External::New(m_isolate, this));
117 ensureDebuggerScriptCompiled();
118 }
119
120 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
121 v8::Context::Scope contextScope(debuggerContext);
122
123 v8::Local<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate);
124 ASSERT(!debuggerScript->IsUndefined());
125 m_listenersMap.set(page, listener);
126
127 WindowProxy* windowProxy = scriptController.existingWindowProxy(DOMWrapperWo rld::mainWorld());
128 if (!windowProxy || !windowProxy->isContextInitialized())
129 return;
130 v8::Local<v8::Context> context = windowProxy->context();
131 v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast( debuggerScript->Get(v8AtomicString(m_isolate, "getScripts")));
132 v8::Handle<v8::Value> argv[] = { context->GetEmbedderData(0) };
133 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(getScript sFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate);
134 if (value.IsEmpty())
135 return;
136 ASSERT(!value->IsUndefined() && value->IsArray());
137 v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value);
138 for (unsigned i = 0; i < scriptsArray->Length(); ++i)
139 dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArr ay->Get(v8::Integer::New(m_isolate, i))), CompileSuccess);
140 }
141
142 void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, Page* page)
143 {
144 if (!m_listenersMap.contains(page))
145 return;
146
147 if (m_pausedPage == page)
148 continueProgram();
149
150 m_listenersMap.remove(page);
151
152 if (m_listenersMap.isEmpty()) {
153 discardDebuggerScript();
154 v8::Debug::SetDebugEventListener(0);
155 // FIXME: Remove all breakpoints set by the agent.
156 }
157 }
158
159 void PageScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task)
160 {
161 ScriptDebugServer::interruptAndRun(task, s_mainThreadIsolate);
162 }
163
164 void PageScriptDebugServer::setClientMessageLoop(PassOwnPtr<ClientMessageLoop> c lientMessageLoop)
165 {
166 m_clientMessageLoop = clientMessageLoop;
167 }
168
169 void PageScriptDebugServer::compileScript(ScriptState* scriptState, const String & expression, const String& sourceURL, String* scriptId, String* exceptionDetail sText, int* lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace)
170 {
171 ExecutionContext* executionContext = scriptState->executionContext();
172 RefPtr<LocalFrame> protect = executionContext->executingWindow()->frame();
173 ScriptDebugServer::compileScript(scriptState, expression, sourceURL, scriptI d, exceptionDetailsText, lineNumber, columnNumber, stackTrace);
174 if (!scriptId->isNull())
175 m_compiledScriptURLs.set(*scriptId, sourceURL);
176 }
177
178 void PageScriptDebugServer::clearCompiledScripts()
179 {
180 ScriptDebugServer::clearCompiledScripts();
181 m_compiledScriptURLs.clear();
182 }
183
184 void PageScriptDebugServer::runScript(ScriptState* scriptState, const String& sc riptId, ScriptValue* result, bool* wasThrown, String* exceptionDetailsText, int* lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace)
185 {
186 String sourceURL = m_compiledScriptURLs.take(scriptId);
187
188 ExecutionContext* executionContext = scriptState->executionContext();
189 LocalFrame* frame = executionContext->executingWindow()->frame();
190 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "EvaluateScript ", "data", InspectorEvaluateScriptEvent::data(frame, sourceURL, TextPosition::mi nimumPosition().m_line.oneBasedInt()));
191 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), " CallStack", TRACE_EVENT_SCOPE_PROCESS, "stack", InspectorCallStackEvent::current CallStack());
192
193 RefPtr<LocalFrame> protect = frame;
194 ScriptDebugServer::runScript(scriptState, scriptId, result, wasThrown, excep tionDetailsText, lineNumber, columnNumber, stackTrace);
195
196 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update Counters", TRACE_EVENT_SCOPE_PROCESS, "data", InspectorUpdateCountersEvent::data ());
197 }
198
199 ScriptDebugListener* PageScriptDebugServer::getDebugListenerForContext(v8::Handl e<v8::Context> context)
200 {
201 v8::HandleScope scope(m_isolate);
202 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(context);
203 if (!frame)
204 return 0;
205 return m_listenersMap.get(frame->page());
206 }
207
208 void PageScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> contex t)
209 {
210 v8::HandleScope scope(m_isolate);
211 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(context);
212 m_pausedPage = frame->page();
213
214 // Wait for continue or step command.
215 m_clientMessageLoop->run(m_pausedPage);
216
217 // The listener may have been removed in the nested loop.
218 if (ScriptDebugListener* listener = m_listenersMap.get(m_pausedPage))
219 listener->didContinue();
220
221 m_pausedPage = 0;
222 }
223
224 void PageScriptDebugServer::quitMessageLoopOnPause()
225 {
226 m_clientMessageLoop->quitNow();
227 }
228
229 void PageScriptDebugServer::preprocessBeforeCompile(const v8::Debug::EventDetail s& eventDetails)
230 {
231 v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext();
232 LocalFrame* frame = retrieveFrameWithGlobalObjectCheck(eventContext);
233 if (!frame)
234 return;
235
236 if (!canPreprocess(frame))
237 return;
238
239 v8::Handle<v8::Object> eventData = eventDetails.GetEventData();
240 v8::Local<v8::Context> debugContext = v8::Debug::GetDebugContext();
241 v8::Context::Scope contextScope(debugContext);
242 v8::TryCatch tryCatch;
243 // <script> tag source and attribute value source are preprocessed before we enter V8.
244 // Avoid preprocessing any internal scripts by processing only eval source i n this V8 event handler.
245 v8::Handle<v8::Value> argvEventData[] = { eventData };
246 v8::Handle<v8::Value> v8Value = callDebuggerMethod("isEvalCompilation", WTF_ ARRAY_LENGTH(argvEventData), argvEventData);
247 if (v8Value.IsEmpty() || !v8Value->ToBoolean()->Value())
248 return;
249
250 // The name and source are in the JS event data.
251 String scriptName = toCoreStringWithUndefinedOrNullCheck(callDebuggerMethod( "getScriptName", WTF_ARRAY_LENGTH(argvEventData), argvEventData));
252 String script = toCoreStringWithUndefinedOrNullCheck(callDebuggerMethod("get ScriptSource", WTF_ARRAY_LENGTH(argvEventData), argvEventData));
253
254 String preprocessedSource = m_scriptPreprocessor->preprocessSourceCode(scri pt, scriptName);
255
256 v8::Handle<v8::Value> argvPreprocessedScript[] = { eventData, v8String(debug Context->GetIsolate(), preprocessedSource) };
257 callDebuggerMethod("setScriptSource", WTF_ARRAY_LENGTH(argvPreprocessedScrip t), argvPreprocessedScript);
258 }
259
260 static bool isCreatingPreprocessor = false;
261
262 bool PageScriptDebugServer::canPreprocess(LocalFrame* frame)
263 {
264 ASSERT(frame);
265
266 if (!m_preprocessorSourceCode || !frame->page() || isCreatingPreprocessor)
267 return false;
268
269 // We delay the creation of the preprocessor until just before the first JS from the
270 // Web page to ensure that the debugger's console initialization code has co mpleted.
271 if (!m_scriptPreprocessor) {
272 TemporaryChange<bool> isPreprocessing(isCreatingPreprocessor, true);
273 m_scriptPreprocessor = adoptPtr(new ScriptPreprocessor(*m_preprocessorSo urceCode.get(), frame));
274 }
275
276 if (m_scriptPreprocessor->isValid())
277 return true;
278
279 m_scriptPreprocessor.clear();
280 // Don't retry the compile if we fail one time.
281 m_preprocessorSourceCode.clear();
282 return false;
283 }
284
285 // Source to Source processing iff debugger enabled and it has loaded a preproce ssor.
286 PassOwnPtr<ScriptSourceCode> PageScriptDebugServer::preprocess(LocalFrame* frame , const ScriptSourceCode& sourceCode)
287 {
288 if (!canPreprocess(frame))
289 return PassOwnPtr<ScriptSourceCode>();
290
291 String preprocessedSource = m_scriptPreprocessor->preprocessSourceCode(sourc eCode.source(), sourceCode.url());
292 return adoptPtr(new ScriptSourceCode(preprocessedSource, sourceCode.url()));
293 }
294
295 String PageScriptDebugServer::preprocessEventListener(LocalFrame* frame, const S tring& source, const String& url, const String& functionName)
296 {
297 if (!canPreprocess(frame))
298 return source;
299
300 return m_scriptPreprocessor->preprocessSourceCode(source, url, functionName) ;
301 }
302
303 void PageScriptDebugServer::clearPreprocessor()
304 {
305 m_scriptPreprocessor.clear();
306 }
307
308 void PageScriptDebugServer::muteWarningsAndDeprecations()
309 {
310 FrameConsole::mute();
311 UseCounter::muteForInspector();
312 }
313
314 void PageScriptDebugServer::unmuteWarningsAndDeprecations()
315 {
316 FrameConsole::unmute();
317 UseCounter::unmuteForInspector();
318 }
319
320 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698