Unreviewed build fix for Debug builders.
[WebKit-https.git] / Source / WebCore / inspector / WorkerInspectorController.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
33 #if ENABLE(INSPECTOR) && ENABLE(WORKERS)
34
35 #include "WorkerInspectorController.h"
36
37 #include "InjectedScriptHost.h"
38 #include "InjectedScriptManager.h"
39 #include "InspectorBackendDispatcher.h"
40 #include "InspectorClient.h"
41 #include "InspectorFrontend.h"
42 #include "InspectorFrontendChannel.h"
43 #include "InspectorRuntimeAgent.h"
44 #include "InspectorState.h"
45 #include "InspectorStateClient.h"
46 #include "InstrumentingAgents.h"
47 #include "WorkerContext.h"
48 #include "WorkerDebuggerAgent.h"
49 #include "WorkerReportingProxy.h"
50 #include "WorkerThread.h"
51 #include <wtf/PassOwnPtr.h>
52
53 namespace WebCore {
54
55 namespace {
56
57 class WorkerRuntimeAgent : public InspectorRuntimeAgent {
58 public:
59     WorkerRuntimeAgent(InjectedScriptManager* injectedScriptManager, WorkerContext* workerContext)
60         : InspectorRuntimeAgent(injectedScriptManager)
61         , m_workerContext(workerContext) { }
62     virtual ~WorkerRuntimeAgent() { }
63
64 private:
65     virtual ScriptState* scriptStateForFrameId(const String&)
66     {
67         return 0;
68     }
69
70     virtual ScriptState* getDefaultInspectedState()
71     {
72         return scriptStateFromWorkerContext(m_workerContext);
73     }
74
75     WorkerContext* m_workerContext;
76 };
77
78 class PageInspectorProxy : public InspectorFrontendChannel {
79 public:
80     explicit PageInspectorProxy(WorkerContext* workerContext) : m_workerContext(workerContext) { }
81     virtual ~PageInspectorProxy() { }
82 private:
83     virtual bool sendMessageToFrontend(const String& message)
84     {
85         m_workerContext->thread()->workerReportingProxy().postMessageToPageInspector(message);
86         return true;
87     }
88     WorkerContext* m_workerContext;
89 };
90
91 class WorkerStateClient : public InspectorStateClient {
92 public:
93     WorkerStateClient(WorkerContext* context) : m_workerContext(context) { }
94     virtual ~WorkerStateClient() { }
95
96 private:
97     virtual void updateInspectorStateCookie(const String& cookie)
98     {
99         m_workerContext->thread()->workerReportingProxy().updateInspectorStateCookie(cookie);
100     }
101
102     WorkerContext* m_workerContext;
103 };
104
105 }
106
107 WorkerInspectorController::WorkerInspectorController(WorkerContext* workerContext)
108     : m_workerContext(workerContext)
109     , m_stateClient(adoptPtr(new WorkerStateClient(workerContext)))
110     , m_state(adoptPtr(new InspectorState(m_stateClient.get())))
111     , m_instrumentingAgents(adoptPtr(new InstrumentingAgents()))
112     , m_injectedScriptManager(InjectedScriptManager::createForWorker())
113 #if ENABLE(JAVASCRIPT_DEBUGGER)
114     , m_debuggerAgent(WorkerDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), workerContext, m_injectedScriptManager.get()))
115 #endif
116     , m_runtimeAgent(adoptPtr(new WorkerRuntimeAgent(m_injectedScriptManager.get(), workerContext)))
117 {
118     m_injectedScriptManager->injectedScriptHost()->init(0
119         , 0
120 #if ENABLE(SQL_DATABASE)
121         , 0
122 #endif
123 #if ENABLE(DOM_STORAGE)
124         , 0
125 #endif
126     );
127
128 #if ENABLE(JAVASCRIPT_DEBUGGER)
129     m_runtimeAgent->setScriptDebugServer(&m_debuggerAgent->scriptDebugServer());
130 #endif
131 }
132  
133 WorkerInspectorController::~WorkerInspectorController()
134 {
135 }
136
137 void WorkerInspectorController::connectFrontend()
138 {
139     ASSERT(!m_frontend);
140     m_state->unmute();
141     m_frontendChannel = adoptPtr(new PageInspectorProxy(m_workerContext));
142     m_frontend = adoptPtr(new InspectorFrontend(m_frontendChannel.get()));
143     m_backendDispatcher = adoptRef(new InspectorBackendDispatcher(
144         m_frontendChannel.get(),
145         0, // InspectorApplicationCacheAgent
146 #if ENABLE(JAVASCRIPT_DEBUGGER)
147         0, // InspectorDOMDebuggerAgent
148 #endif
149         0, // InspectorCSSAgent
150         0, // InspectorConsoleAgent
151         0, // InspectorDOMAgent
152 #if ENABLE(DOM_STORAGE)
153         0, // InspectorDOMStorageAgent
154 #endif
155 #if ENABLE(SQL_DATABASE)
156         0, // InspectorDatabaseAgent
157 #endif
158 #if ENABLE(JAVASCRIPT_DEBUGGER)
159         m_debuggerAgent.get(),
160 #endif
161         0, // InspectorResourceAgent
162         0, // InspectorPageAgent
163 #if ENABLE(JAVASCRIPT_DEBUGGER)
164         0, // InspectorProfilerAgent
165 #endif
166         m_runtimeAgent.get(),
167         0, // InspectorTimelineAgent
168         0 // InspectorWorkerAgent
169     ));
170
171     m_injectedScriptManager->injectedScriptHost()->setFrontend(m_frontend.get());
172 #if ENABLE(JAVASCRIPT_DEBUGGER)
173     m_debuggerAgent->setFrontend(m_frontend.get());
174 #endif
175 }
176
177 void WorkerInspectorController::disconnectFrontend()
178 {
179     if (!m_frontend)
180         return;
181     m_backendDispatcher->clearFrontend();
182     m_backendDispatcher.clear();
183     // Destroying agents would change the state, but we don't want that.
184     // Pre-disconnect state will be used to restore inspector agents.
185     m_state->mute();
186 #if ENABLE(JAVASCRIPT_DEBUGGER)
187     m_debuggerAgent->clearFrontend();
188 #endif
189     m_injectedScriptManager->injectedScriptHost()->clearFrontend();
190
191     m_frontend.clear();
192     m_frontendChannel.clear();
193 }
194
195 void WorkerInspectorController::restoreInspectorStateFromCookie(const String& inspectorCookie)
196 {
197     ASSERT(!m_frontend);
198     connectFrontend();
199     m_state->loadFromCookie(inspectorCookie);
200
201 #if ENABLE(JAVASCRIPT_DEBUGGER)
202     m_debuggerAgent->restore();
203 #endif
204 }
205
206 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
207 {
208     if (m_backendDispatcher)
209         m_backendDispatcher->dispatch(message);
210 }
211
212 }
213
214 #endif