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 "core/inspector/WorkerDebuggerAgent.h"
34 #include "bindings/v8/ScriptDebugServer.h"
35 #include "core/workers/WorkerGlobalScope.h"
36 #include "core/workers/WorkerThread.h"
37 #include "wtf/MessageQueue.h"
43 Mutex& workerDebuggerAgentsMutex()
45 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
49 typedef HashMap<WorkerThread*, WorkerDebuggerAgent*> WorkerDebuggerAgents;
51 WorkerDebuggerAgents& workerDebuggerAgents()
53 DEFINE_STATIC_LOCAL(WorkerDebuggerAgents, agents, ());
58 class RunInspectorCommandsTask FINAL : public ScriptDebugServer::Task {
60 explicit RunInspectorCommandsTask(WorkerThread* thread)
61 : m_thread(thread) { }
62 virtual ~RunInspectorCommandsTask() { }
63 virtual void run() OVERRIDE
65 // Process all queued debugger commands. WorkerThread is certainly
66 // alive if this task is being executed.
67 while (MessageQueueMessageReceived == m_thread->runLoop().runDebuggerTask(WorkerRunLoop::DontWaitForMessage)) { }
71 WorkerThread* m_thread;
76 PassOwnPtr<WorkerDebuggerAgent> WorkerDebuggerAgent::create(WorkerScriptDebugServer* scriptDebugServer, WorkerGlobalScope* inspectedWorkerGlobalScope, InjectedScriptManager* injectedScriptManager)
78 return adoptPtr(new WorkerDebuggerAgent(scriptDebugServer, inspectedWorkerGlobalScope, injectedScriptManager));
81 WorkerDebuggerAgent::WorkerDebuggerAgent(WorkerScriptDebugServer* scriptDebugServer, WorkerGlobalScope* inspectedWorkerGlobalScope, InjectedScriptManager* injectedScriptManager)
82 : InspectorDebuggerAgent(injectedScriptManager)
83 , m_scriptDebugServer(scriptDebugServer)
84 , m_inspectedWorkerGlobalScope(inspectedWorkerGlobalScope)
86 MutexLocker lock(workerDebuggerAgentsMutex());
87 workerDebuggerAgents().set(inspectedWorkerGlobalScope->thread(), this);
90 WorkerDebuggerAgent::~WorkerDebuggerAgent()
92 MutexLocker lock(workerDebuggerAgentsMutex());
93 ASSERT(workerDebuggerAgents().contains(m_inspectedWorkerGlobalScope->thread()));
94 workerDebuggerAgents().remove(m_inspectedWorkerGlobalScope->thread());
97 void WorkerDebuggerAgent::interruptAndDispatchInspectorCommands(WorkerThread* thread)
99 MutexLocker lock(workerDebuggerAgentsMutex());
100 WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread);
102 agent->m_scriptDebugServer->interruptAndRunTask(adoptPtr(new RunInspectorCommandsTask(thread)));
105 void WorkerDebuggerAgent::startListeningScriptDebugServer()
107 scriptDebugServer().addListener(this);
110 void WorkerDebuggerAgent::stopListeningScriptDebugServer()
112 scriptDebugServer().removeListener(this);
115 WorkerScriptDebugServer& WorkerDebuggerAgent::scriptDebugServer()
117 return *m_scriptDebugServer;
120 InjectedScript WorkerDebuggerAgent::injectedScriptForEval(ErrorString* error, const int* executionContextId)
122 if (executionContextId) {
123 *error = "Execution context id is not supported for workers as there is only one execution context.";
124 return InjectedScript();
126 return injectedScriptManager()->injectedScriptFor(m_inspectedWorkerGlobalScope->script()->scriptState());
129 void WorkerDebuggerAgent::muteConsole()
131 // We don't need to mute console for workers.
134 void WorkerDebuggerAgent::unmuteConsole()
136 // We don't need to mute console for workers.
139 } // namespace WebCore