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.
33 #include "core/inspector/WorkerInspectorController.h"
35 #include "InspectorBackendDispatcher.h"
36 #include "InspectorFrontend.h"
37 #include "core/inspector/InjectedScriptHost.h"
38 #include "core/inspector/InjectedScriptManager.h"
39 #include "core/inspector/InspectorConsoleAgent.h"
40 #include "core/inspector/InspectorFrontendChannel.h"
41 #include "core/inspector/InspectorHeapProfilerAgent.h"
42 #include "core/inspector/InspectorProfilerAgent.h"
43 #include "core/inspector/InspectorState.h"
44 #include "core/inspector/InspectorStateClient.h"
45 #include "core/inspector/InspectorTimelineAgent.h"
46 #include "core/inspector/InstrumentingAgents.h"
47 #include "core/inspector/WorkerConsoleAgent.h"
48 #include "core/inspector/WorkerDebuggerAgent.h"
49 #include "core/inspector/WorkerRuntimeAgent.h"
50 #include "core/workers/WorkerGlobalScope.h"
51 #include "core/workers/WorkerReportingProxy.h"
52 #include "core/workers/WorkerThread.h"
53 #include "wtf/PassOwnPtr.h"
59 class PageInspectorProxy FINAL : public InspectorFrontendChannel {
60 WTF_MAKE_FAST_ALLOCATED;
62 explicit PageInspectorProxy(WorkerGlobalScope* workerGlobalScope) : m_workerGlobalScope(workerGlobalScope) { }
63 virtual ~PageInspectorProxy() { }
65 virtual void sendMessageToFrontend(PassRefPtr<JSONObject> message) OVERRIDE
67 m_workerGlobalScope->thread()->workerReportingProxy().postMessageToPageInspector(message->toJSONString());
69 virtual void flush() OVERRIDE { }
70 WorkerGlobalScope* m_workerGlobalScope;
73 class WorkerStateClient FINAL : public InspectorStateClient {
74 WTF_MAKE_FAST_ALLOCATED;
76 WorkerStateClient(WorkerGlobalScope* context) : m_workerGlobalScope(context) { }
77 virtual ~WorkerStateClient() { }
80 virtual void updateInspectorStateCookie(const String& cookie) OVERRIDE
82 m_workerGlobalScope->thread()->workerReportingProxy().updateInspectorStateCookie(cookie);
85 WorkerGlobalScope* m_workerGlobalScope;
90 WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope* workerGlobalScope)
91 : m_workerGlobalScope(workerGlobalScope)
92 , m_stateClient(adoptPtr(new WorkerStateClient(workerGlobalScope)))
93 , m_state(adoptPtr(new InspectorCompositeState(m_stateClient.get())))
94 , m_instrumentingAgents(InstrumentingAgents::create())
95 , m_injectedScriptManager(InjectedScriptManager::createForWorker())
96 , m_debugServer(adoptPtr(new WorkerScriptDebugServer(workerGlobalScope)))
97 , m_agents(m_instrumentingAgents.get(), m_state.get())
99 m_agents.append(WorkerRuntimeAgent::create(m_injectedScriptManager.get(), m_debugServer.get(), workerGlobalScope));
101 OwnPtr<InspectorTimelineAgent> timelineAgent = InspectorTimelineAgent::create(0, 0, 0, InspectorTimelineAgent::WorkerInspector, 0);
102 m_agents.append(WorkerDebuggerAgent::create(m_debugServer.get(), workerGlobalScope, m_injectedScriptManager.get()));
104 m_agents.append(InspectorProfilerAgent::create(m_injectedScriptManager.get(), 0));
105 m_agents.append(InspectorHeapProfilerAgent::create(m_injectedScriptManager.get()));
106 m_agents.append(WorkerConsoleAgent::create(timelineAgent.get(), m_injectedScriptManager.get()));
107 m_agents.append(timelineAgent.release());
109 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), m_debugServer.get());
112 WorkerInspectorController::~WorkerInspectorController()
114 m_instrumentingAgents->reset();
115 disconnectFrontend();
118 void WorkerInspectorController::connectFrontend()
122 m_frontendChannel = adoptPtr(new PageInspectorProxy(m_workerGlobalScope));
123 m_frontend = adoptPtr(new InspectorFrontend(m_frontendChannel.get()));
124 m_backendDispatcher = InspectorBackendDispatcher::create(m_frontendChannel.get());
125 m_agents.registerInDispatcher(m_backendDispatcher.get());
126 m_agents.setFrontend(m_frontend.get());
129 void WorkerInspectorController::disconnectFrontend()
133 m_backendDispatcher->clearFrontend();
134 m_backendDispatcher.clear();
135 // Destroying agents would change the state, but we don't want that.
136 // Pre-disconnect state will be used to restore inspector agents.
138 m_agents.clearFrontend();
140 m_frontendChannel.clear();
143 void WorkerInspectorController::restoreInspectorStateFromCookie(const String& inspectorCookie)
147 m_state->loadFromCookie(inspectorCookie);
152 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
154 if (m_backendDispatcher)
155 m_backendDispatcher->dispatch(message);
158 void WorkerInspectorController::resume()
160 if (WorkerRuntimeAgent* runtimeAgent = m_instrumentingAgents->workerRuntimeAgent()) {
162 runtimeAgent->run(&unused);