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/WorkerScriptDebugServer.h" 33 34#include "bindings/core/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" 39#include "wtf/MessageQueue.h" 40#include <v8.h> 41 42 43namespace blink { 44 45WorkerScriptDebugServer::WorkerScriptDebugServer(WorkerGlobalScope* workerGlobalScope) 46 : ScriptDebugServer(v8::Isolate::GetCurrent()) 47 , m_listener(0) 48 , m_workerGlobalScope(workerGlobalScope) 49{ 50 ASSERT(m_isolate); 51} 52 53void WorkerScriptDebugServer::addListener(ScriptDebugListener* listener) 54{ 55 v8::HandleScope scope(m_isolate); 56 ASSERT(!m_listener); 57 58 v8::Debug::SetDebugEventListener(&WorkerScriptDebugServer::v8DebugEventCallback, v8::External::New(m_isolate, this)); 59 ensureDebuggerScriptCompiled(); 60 m_listener = listener; 61 62 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); 63 v8::Context::Scope contextScope(debuggerContext); 64 65 v8::Local<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate); 66 ASSERT(!debuggerScript->IsUndefined()); 67 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); 70 if (value.IsEmpty()) 71 return; 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))), CompileSuccess); 76} 77 78void WorkerScriptDebugServer::removeListener(ScriptDebugListener* listener) 79{ 80 ASSERT(m_listener == listener); 81 continueProgram(); 82 discardDebuggerScript(); 83 m_listener = 0; 84 v8::Debug::SetDebugEventListener(0); 85} 86 87void WorkerScriptDebugServer::interruptAndRunTask(PassOwnPtr<Task> task) 88{ 89 interruptAndRun(task, m_isolate); 90} 91 92ScriptDebugListener* WorkerScriptDebugServer::getDebugListenerForContext(v8::Handle<v8::Context>) 93{ 94 // There is only one worker context in isolate. 95 return m_listener; 96} 97 98void WorkerScriptDebugServer::runMessageLoopOnPause(v8::Handle<v8::Context>) 99{ 100 MessageQueueWaitResult result; 101 m_workerGlobalScope->thread()->willEnterNestedLoop(); 102 do { 103 result = m_workerGlobalScope->thread()->runDebuggerTask(); 104 // Keep waiting until execution is resumed. 105 } while (result == MessageQueueMessageReceived && isPaused()); 106 m_workerGlobalScope->thread()->didLeaveNestedLoop(); 107 108 // The listener may have been removed in the nested loop. 109 if (m_listener) 110 m_listener->didContinue(); 111} 112 113void WorkerScriptDebugServer::quitMessageLoopOnPause() 114{ 115 // Nothing to do here in case of workers since runMessageLoopOnPause will check for paused state after each debugger command. 116} 117 118} // namespace blink 119