WorkerDebuggerAgent.cpp 5.7 KB

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