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 "bindings/v8/WorkerScriptDebugServer.h"
34 #include "bindings/v8/V8ScriptRunner.h"
35 #include "core/inspector/ScriptDebugListener.h"
36 #include "core/inspector/WorkerDebuggerAgent.h"
37 #include "core/workers/WorkerGlobalScope.h"
38 #include "core/workers/WorkerThread.h"
40 #include "wtf/MessageQueue.h"
45 WorkerScriptDebugServer::WorkerScriptDebugServer(WorkerGlobalScope* workerGlobalScope, const String& mode)
46 : ScriptDebugServer(v8::Isolate::GetCurrent())
48 , m_workerGlobalScope(workerGlobalScope)
49 , m_debuggerTaskMode(mode)
54 void WorkerScriptDebugServer::addListener(ScriptDebugListener* listener)
56 v8::HandleScope scope(m_isolate);
57 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
58 v8::Context::Scope contextScope(debuggerContext);
61 m_listener = listener;
63 ensureDebuggerScriptCompiled();
64 v8::Local<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate);
65 ASSERT(!debuggerScript->IsUndefined());
66 v8::Debug::SetDebugEventListener2(&WorkerScriptDebugServer::v8DebugEventCallback, v8::External::New(m_isolate, this));
68 v8::Handle<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getWorkerScripts")));
69 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(getScriptsFunction, debuggerScript, 0, 0, m_isolate);
72 ASSERT(!value->IsUndefined() && value->IsArray());
73 v8::Handle<v8::Array> scriptsArray = v8::Handle<v8::Array>::Cast(value);
74 for (unsigned i = 0; i < scriptsArray->Length(); ++i)
75 dispatchDidParseSource(listener, v8::Handle<v8::Object>::Cast(scriptsArray->Get(v8::Integer::New(m_isolate, i))));
78 void WorkerScriptDebugServer::removeListener(ScriptDebugListener* listener)
80 ASSERT(m_listener == listener);
83 v8::Debug::SetDebugEventListener2(0);
86 void WorkerScriptDebugServer::interruptAndRunTask(PassOwnPtr<Task> task)
88 interruptAndRun(task, m_isolate);
91 ScriptDebugListener* WorkerScriptDebugServer::getDebugListenerForContext(v8::Handle<v8::Context>)
93 // There is only one worker context in isolate.
97 void WorkerScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context>)
99 MessageQueueWaitResult result;
101 result = m_workerGlobalScope->thread()->runLoop().runInMode(m_workerGlobalScope, m_debuggerTaskMode);
102 // Keep waiting until execution is resumed.
103 } while (result == MessageQueueMessageReceived && isPaused());
105 // The listener may have been removed in the nested loop.
107 m_listener->didContinue();
110 void WorkerScriptDebugServer::quitMessageLoopOnPause()
112 // Nothing to do here in case of workers since runMessageLoopOnPause will check for paused state after each debugger command.
115 } // namespace WebCore