tizen beta release
[framework/web/webkit-efl.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 "InspectorConsoleAgent.h"
42 #include "InspectorFrontend.h"
43 #include "InspectorFrontendChannel.h"
44 #include "InspectorState.h"
45 #include "InspectorStateClient.h"
46 #include "InstrumentingAgents.h"
47 #include "WorkerConsoleAgent.h"
48 #include "WorkerContext.h"
49 #include "WorkerDebuggerAgent.h"
50 #include "WorkerReportingProxy.h"
51 #include "WorkerRuntimeAgent.h"
52 #include "WorkerThread.h"
53 #include <wtf/PassOwnPtr.h>
54
55 namespace WebCore {
56
57 namespace {
58
59 class PageInspectorProxy : public InspectorFrontendChannel {
60 public:
61     explicit PageInspectorProxy(WorkerContext* workerContext) : m_workerContext(workerContext) { }
62     virtual ~PageInspectorProxy() { }
63 private:
64     virtual bool sendMessageToFrontend(const String& message)
65     {
66         m_workerContext->thread()->workerReportingProxy().postMessageToPageInspector(message);
67         return true;
68     }
69     WorkerContext* m_workerContext;
70 };
71
72 class WorkerStateClient : public InspectorStateClient {
73 public:
74     WorkerStateClient(WorkerContext* context) : m_workerContext(context) { }
75     virtual ~WorkerStateClient() { }
76
77 private:
78     virtual void updateInspectorStateCookie(const String& cookie)
79     {
80         m_workerContext->thread()->workerReportingProxy().updateInspectorStateCookie(cookie);
81     }
82
83     WorkerContext* m_workerContext;
84 };
85
86 }
87
88 WorkerInspectorController::WorkerInspectorController(WorkerContext* workerContext)
89     : m_workerContext(workerContext)
90     , m_stateClient(adoptPtr(new WorkerStateClient(workerContext)))
91     , m_state(adoptPtr(new InspectorState(m_stateClient.get())))
92     , m_instrumentingAgents(adoptPtr(new InstrumentingAgents()))
93     , m_injectedScriptManager(InjectedScriptManager::createForWorker())
94 {
95
96 #if ENABLE(JAVASCRIPT_DEBUGGER)
97     m_debuggerAgent = WorkerDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), workerContext, m_injectedScriptManager.get());
98 #endif
99     m_runtimeAgent = WorkerRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), workerContext);
100     m_consoleAgent = WorkerConsoleAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get());
101
102     m_injectedScriptManager->injectedScriptHost()->init(0
103         , 0
104 #if ENABLE(SQL_DATABASE)
105         , 0
106 #endif
107         , 0
108     );
109
110 #if ENABLE(JAVASCRIPT_DEBUGGER)
111     m_runtimeAgent->setScriptDebugServer(&m_debuggerAgent->scriptDebugServer());
112 #endif
113 }
114  
115 WorkerInspectorController::~WorkerInspectorController()
116 {
117     disconnectFrontend();
118 }
119
120 void WorkerInspectorController::connectFrontend()
121 {
122     ASSERT(!m_frontend);
123     m_state->unmute();
124     m_frontendChannel = adoptPtr(new PageInspectorProxy(m_workerContext));
125     m_frontend = adoptPtr(new InspectorFrontend(m_frontendChannel.get()));
126     m_backendDispatcher = adoptRef(new InspectorBackendDispatcher(m_frontendChannel.get()));
127     m_consoleAgent->registerInDispatcher(m_backendDispatcher.get());
128 #if ENABLE(JAVASCRIPT_DEBUGGER)
129     m_debuggerAgent->registerInDispatcher(m_backendDispatcher.get());
130 #endif
131     m_runtimeAgent->registerInDispatcher(m_backendDispatcher.get());
132
133 #if ENABLE(JAVASCRIPT_DEBUGGER)
134     m_debuggerAgent->setFrontend(m_frontend.get());
135 #endif
136     m_consoleAgent->setFrontend(m_frontend.get());
137 }
138
139 void WorkerInspectorController::disconnectFrontend()
140 {
141     if (!m_frontend)
142         return;
143     m_backendDispatcher->clearFrontend();
144     m_backendDispatcher.clear();
145     // Destroying agents would change the state, but we don't want that.
146     // Pre-disconnect state will be used to restore inspector agents.
147     m_state->mute();
148 #if ENABLE(JAVASCRIPT_DEBUGGER)
149     m_debuggerAgent->clearFrontend();
150 #endif
151     m_consoleAgent->clearFrontend();
152
153     m_frontend.clear();
154     m_frontendChannel.clear();
155 }
156
157 void WorkerInspectorController::restoreInspectorStateFromCookie(const String& inspectorCookie)
158 {
159     ASSERT(!m_frontend);
160     connectFrontend();
161     m_state->loadFromCookie(inspectorCookie);
162
163 #if ENABLE(JAVASCRIPT_DEBUGGER)
164     m_debuggerAgent->restore();
165 #endif
166     m_consoleAgent->restore();
167 }
168
169 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
170 {
171     if (m_backendDispatcher)
172         m_backendDispatcher->dispatch(message);
173 }
174
175 #if ENABLE(JAVASCRIPT_DEBUGGER)
176 void WorkerInspectorController::resume()
177 {
178     ErrorString unused;
179     m_runtimeAgent->run(&unused);
180 }
181 #endif
182
183 }
184
185 #endif