initial import
[vuplus_webkit] / Source / WebCore / bindings / v8 / WorkerScriptDebugServer.cpp
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 "WorkerScriptDebugServer.h"
33
34 #if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(WORKERS)
35
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"
43 #include <v8.h>
44 #include <wtf/MessageQueue.h>
45
46 namespace WebCore {
47
48 static WorkerContext* retrieveWorkerContext(v8::Handle<v8::Context> context)
49 {
50     v8::Handle<v8::Object> global = context->Global();
51     ASSERT(!global.IsEmpty());
52
53     v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(global->GetPrototype());
54     ASSERT(!prototype.IsEmpty());
55
56     prototype = v8::Handle<v8::Object>::Cast(prototype->GetPrototype());
57     ASSERT(!prototype.IsEmpty());
58
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);
64     ASSERT_NOT_REACHED();
65     return 0;
66 }
67
68 const char* WorkerScriptDebugServer::debuggerTaskMode = "debugger";
69
70 WorkerScriptDebugServer::WorkerScriptDebugServer()
71     : ScriptDebugServer()
72     , m_pausedWorkerContext(0)
73 {
74 }
75
76 void WorkerScriptDebugServer::addListener(ScriptDebugListener* listener, WorkerContext* workerContext)
77 {
78     v8::HandleScope scope;
79     v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
80     v8::Context::Scope contextScope(debuggerContext);
81
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));
87     }
88     m_listenersMap.set(workerContext, listener);
89     
90     WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
91     if (!proxy)
92         return;
93     v8::Handle<v8::Context> context = proxy->context();
94
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);
98     if (value.IsEmpty())
99         return;
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))));
104 }
105
106 void WorkerScriptDebugServer::removeListener(ScriptDebugListener* listener, WorkerContext* workerContext)
107 {
108     if (!m_listenersMap.contains(workerContext))
109         return;
110
111     if (m_pausedWorkerContext == workerContext)
112         continueProgram();
113
114     m_listenersMap.remove(workerContext);
115
116     if (m_listenersMap.isEmpty())
117         v8::Debug::SetDebugEventListener2(0);
118 }
119
120 ScriptDebugListener* WorkerScriptDebugServer::getDebugListenerForContext(v8::Handle<v8::Context> context)
121 {
122     WorkerContext* workerContext = retrieveWorkerContext(context);
123     if (!workerContext)
124         return 0;
125     return m_listenersMap.get(workerContext);
126 }
127
128 void WorkerScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context> context)
129 {
130     WorkerContext* workerContext = retrieveWorkerContext(context);
131     WorkerThread* workerThread = workerContext->thread();
132
133     m_pausedWorkerContext = workerContext;
134
135     MessageQueueWaitResult result;
136     do {
137         result = workerThread->runLoop().runInMode(workerContext, debuggerTaskMode);
138     // Keep waiting until execution is resumed.
139     } while (result == MessageQueueMessageReceived && isPaused());
140     m_pausedWorkerContext = 0;
141     
142     // The listener may have been removed in the nested loop.
143     if (ScriptDebugListener* listener = m_listenersMap.get(workerContext))
144         listener->didContinue();
145 }
146
147 void WorkerScriptDebugServer::quitMessageLoopOnPause()
148 {
149     // Nothing to do here in case of workers since runMessageLoopOnPause will check for paused state after each debugger command.
150 }
151
152 } // namespace WebCore
153
154 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(WORKERS)