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 "core/inspector/WorkerDebuggerAgent.h" 33 34 #include "bindings/v8/ScriptDebugServer.h" 35 #include "core/workers/WorkerGlobalScope.h" 36 #include "core/workers/WorkerThread.h" 37 #include "wtf/MessageQueue.h" 38 39 namespace WebCore { 40 41 namespace { 42 43 Mutex& workerDebuggerAgentsMutex() 44 { 45 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); 46 return mutex; 47 } 48 49 typedef HashMap<WorkerThread*, WorkerDebuggerAgent*> WorkerDebuggerAgents; 50 51 WorkerDebuggerAgents& workerDebuggerAgents() 52 { 53 DEFINE_STATIC_LOCAL(WorkerDebuggerAgents, agents, ()); 54 return agents; 55 } 56 57 58 class RunInspectorCommandsTask : public ScriptDebugServer::Task { 59 public: 60 RunInspectorCommandsTask(WorkerThread* thread, WorkerGlobalScope* workerGlobalScope) 61 : m_thread(thread) 62 , m_workerGlobalScope(workerGlobalScope) { } 63 virtual ~RunInspectorCommandsTask() { } 64 virtual void run() 65 { 66 // Process all queued debugger commands. It is safe to use m_workerGlobalScope here 67 // because it is alive if RunWorkerLoop is not terminated, otherwise it will 68 // just be ignored. WorkerThread is certainly alive if this task is being executed. 69 while (MessageQueueMessageReceived == m_thread->runLoop().runInMode(m_workerGlobalScope, WorkerDebuggerAgent::debuggerTaskMode, WorkerRunLoop::DontWaitForMessage)) { } 70 } 71 72 private: 73 WorkerThread* m_thread; 74 WorkerGlobalScope* m_workerGlobalScope; 75 }; 76 77 } // namespace 78 79 const char WorkerDebuggerAgent::debuggerTaskMode[] = "debugger"; 80 81 PassOwnPtr<WorkerDebuggerAgent> WorkerDebuggerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, WorkerScriptDebugServer* scriptDebugServer, WorkerGlobalScope* inspectedWorkerGlobalScope, InjectedScriptManager* injectedScriptManager) 82 { 83 return adoptPtr(new WorkerDebuggerAgent(instrumentingAgents, inspectorState, scriptDebugServer, inspectedWorkerGlobalScope, injectedScriptManager)); 84 } 85 86 WorkerDebuggerAgent::WorkerDebuggerAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, WorkerScriptDebugServer* scriptDebugServer, WorkerGlobalScope* inspectedWorkerGlobalScope, InjectedScriptManager* injectedScriptManager) 87 : InspectorDebuggerAgent(instrumentingAgents, inspectorState, injectedScriptManager) 88 , m_scriptDebugServer(scriptDebugServer) 89 , m_inspectedWorkerGlobalScope(inspectedWorkerGlobalScope) 90 { 91 MutexLocker lock(workerDebuggerAgentsMutex()); 92 workerDebuggerAgents().set(inspectedWorkerGlobalScope->thread(), this); 93 } 94 95 WorkerDebuggerAgent::~WorkerDebuggerAgent() 96 { 97 MutexLocker lock(workerDebuggerAgentsMutex()); 98 ASSERT(workerDebuggerAgents().contains(m_inspectedWorkerGlobalScope->thread())); 99 workerDebuggerAgents().remove(m_inspectedWorkerGlobalScope->thread()); 100 } 101 102 void WorkerDebuggerAgent::interruptAndDispatchInspectorCommands(WorkerThread* thread) 103 { 104 MutexLocker lock(workerDebuggerAgentsMutex()); 105 WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread); 106 if (agent) 107 agent->m_scriptDebugServer->interruptAndRunTask(adoptPtr(new RunInspectorCommandsTask(thread, agent->m_inspectedWorkerGlobalScope))); 108 } 109 110 void WorkerDebuggerAgent::startListeningScriptDebugServer() 111 { 112 scriptDebugServer().addListener(this); 113 } 114 115 void WorkerDebuggerAgent::stopListeningScriptDebugServer() 116 { 117 scriptDebugServer().removeListener(this); 118 } 119 120 WorkerScriptDebugServer& WorkerDebuggerAgent::scriptDebugServer() 121 { 122 return *m_scriptDebugServer; 123 } 124 125 InjectedScript WorkerDebuggerAgent::injectedScriptForEval(ErrorString* error, const int* executionContextId) 126 { 127 if (executionContextId) { 128 *error = "Execution context id is not supported for workers as there is only one execution context."; 129 return InjectedScript(); 130 } 131 ScriptState* scriptState = scriptStateFromWorkerGlobalScope(m_inspectedWorkerGlobalScope); 132 return injectedScriptManager()->injectedScriptFor(scriptState); 133 } 134 135 void WorkerDebuggerAgent::muteConsole() 136 { 137 // We don't need to mute console for workers. 138 } 139 140 void WorkerDebuggerAgent::unmuteConsole() 141 { 142 // We don't need to mute console for workers. 143 } 144 145 } // namespace WebCore 146