2 * Copyright (c) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "WorkerScriptDebugServer.h"
34 #if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(WORKERS)
36 #include "ScriptDebugListener.h"
37 #include "V8DOMWrapper.h"
38 #include "V8DedicatedWorkerContext.h"
39 #include "V8SharedWorkerContext.h"
40 #include "WorkerContext.h"
41 #include "WorkerContextExecutionProxy.h"
42 #include "WorkerThread.h"
44 #include <wtf/MessageQueue.h>
48 static WorkerContext* retrieveWorkerContext(v8::Handle<v8::Context> context)
50 v8::Handle<v8::Object> global = context->Global();
51 ASSERT(!global.IsEmpty());
53 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(global->GetPrototype());
54 ASSERT(!prototype.IsEmpty());
56 prototype = v8::Handle<v8::Object>::Cast(prototype->GetPrototype());
57 ASSERT(!prototype.IsEmpty());
59 WrapperTypeInfo* typeInfo = V8DOMWrapper::domWrapperType(prototype);
60 if (&V8DedicatedWorkerContext::info == typeInfo)
61 return V8DedicatedWorkerContext::toNative(prototype);
62 if (&V8SharedWorkerContext::info == typeInfo)
63 return V8SharedWorkerContext::toNative(prototype);
68 const char* WorkerScriptDebugServer::debuggerTaskMode = "debugger";
70 WorkerScriptDebugServer::WorkerScriptDebugServer()
72 , m_pausedWorkerContext(0)
76 void WorkerScriptDebugServer::addListener(ScriptDebugListener* listener, WorkerContext* workerContext)
78 v8::HandleScope scope;
79 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
80 v8::Context::Scope contextScope(debuggerContext);
82 if (!m_listenersMap.size()) {
83 // FIXME: synchronize access to this code.
84 ensureDebuggerScriptCompiled();
85 ASSERT(!m_debuggerScript.get()->IsUndefined());
86 v8::Debug::SetDebugEventListener2(&WorkerScriptDebugServer::v8DebugEventCallback, v8::External::New(this));
88 m_listenersMap.set(workerContext, listener);
90 WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
93 v8::Handle<v8::Context> context = proxy->context();
95 v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(m_debuggerScript.get()->Get(v8::String::New("getWorkerScripts")));
96 v8::Handle<v8::Value> argv[] = { v8::Handle<v8::Value>() };
97 v8::Handle<v8::Value> value = getScriptsFunction->Call(m_debuggerScript.get(), 0, argv);
100 ASSERT(!value->IsUndefined() && value->IsArray());
101 v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value);
102 for (unsigned i = 0; i < scriptsArray->Length(); ++i)
103 dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArray->Get(v8::Integer::New(i))));
106 void WorkerScriptDebugServer::removeListener(ScriptDebugListener* listener, WorkerContext* workerContext)
108 if (!m_listenersMap.contains(workerContext))
111 if (m_pausedWorkerContext == workerContext)
114 m_listenersMap.remove(workerContext);
116 if (m_listenersMap.isEmpty())
117 v8::Debug::SetDebugEventListener2(0);
120 ScriptDebugListener* WorkerScriptDebugServer::getDebugListenerForContext(v8::Handle<v8::Context> context)
122 WorkerContext* workerContext = retrieveWorkerContext(context);
125 return m_listenersMap.get(workerContext);
128 void WorkerScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> context)
130 WorkerContext* workerContext = retrieveWorkerContext(context);
131 WorkerThread* workerThread = workerContext->thread();
133 m_pausedWorkerContext = workerContext;
135 MessageQueueWaitResult result;
137 result = workerThread->runLoop().runInMode(workerContext, debuggerTaskMode);
138 // Keep waiting until execution is resumed.
139 } while (result == MessageQueueMessageReceived && isPaused());
140 m_pausedWorkerContext = 0;
142 // The listener may have been removed in the nested loop.
143 if (ScriptDebugListener* listener = m_listenersMap.get(workerContext))
144 listener->didContinue();
147 void WorkerScriptDebugServer::quitMessageLoopOnPause()
149 // Nothing to do here in case of workers since runMessageLoopOnPause will check for paused state after each debugger command.
152 } // namespace WebCore
154 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(WORKERS)