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