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