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 "InstrumentingAgents.h"
46#include "WorkerDebuggerAgent.h"
47#include <wtf/PassOwnPtr.h>
48
49namespace WebCore {
50
51namespace {
52
53class WorkerRuntimeAgent : public InspectorRuntimeAgent {
54public:
55    WorkerRuntimeAgent(InjectedScriptManager* injectedScriptManager, WorkerContext* workerContext)
56        : InspectorRuntimeAgent(injectedScriptManager)
57        , m_workerContext(workerContext) { }
58    virtual ~WorkerRuntimeAgent() { }
59
60private:
61    virtual ScriptState* getDefaultInspectedState()
62    {
63        return scriptStateFromWorkerContext(m_workerContext);
64    }
65
66    WorkerContext* m_workerContext;
67};
68
69}
70
71WorkerInspectorController::WorkerInspectorController(WorkerContext* workerContext)
72    : m_workerContext(workerContext)
73    , m_state(new InspectorState(0))
74    , m_instrumentingAgents(new InstrumentingAgents())
75    , m_injectedScriptManager(InjectedScriptManager::createForWorker())
76#if ENABLE(JAVASCRIPT_DEBUGGER)
77    , m_debuggerAgent(WorkerDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), workerContext, m_injectedScriptManager.get()))
78#endif
79    , m_runtimeAgent(adoptPtr(new WorkerRuntimeAgent(m_injectedScriptManager.get(), workerContext)))
80{
81    m_injectedScriptManager->injectedScriptHost()->init(0
82        , 0
83#if ENABLE(DATABASE)
84        , 0
85#endif
86#if ENABLE(DOM_STORAGE)
87        , 0
88#endif
89#if ENABLE(JAVASCRIPT_DEBUGGER)
90        , m_debuggerAgent.get()
91#endif
92    );
93}
94
95WorkerInspectorController::~WorkerInspectorController()
96{
97}
98
99void WorkerInspectorController::connectFrontend(InspectorFrontendChannel* channel)
100{
101    ASSERT(!m_frontend);
102    m_state->unmute();
103    m_frontend = new InspectorFrontend(channel);
104    m_backendDispatcher = new InspectorBackendDispatcher(
105        channel,
106#if ENABLE(OFFLINE_WEB_APPLICATIONS)
107        0, // InspectorApplicationCacheAgent
108#endif
109#if ENABLE(JAVASCRIPT_DEBUGGER)
110        0, // InspectorBrowserDebuggerAgent
111#endif
112        0, // InspectorCSSAgent
113        0, // InspectorConsoleAgent
114        0, // InspectorDOMAgent
115#if ENABLE(DOM_STORAGE)
116        0, // InspectorDOMStorageAgent
117#endif
118#if ENABLE(DATABASE)
119        0, // InspectorDatabaseAgent
120#endif
121#if ENABLE(JAVASCRIPT_DEBUGGER)
122        m_debuggerAgent.get(),
123#endif
124        0, // InspectorResourceAgent
125        0, // InspectorPageAgent
126#if ENABLE(JAVASCRIPT_DEBUGGER)
127        0, // InspectorProfilerAgent
128#endif
129        m_runtimeAgent.get(),
130        0 // InspectorTimelineAgent
131    );
132
133    m_injectedScriptManager->injectedScriptHost()->setFrontend(m_frontend.get());
134#if ENABLE(JAVASCRIPT_DEBUGGER)
135    m_debuggerAgent->setFrontend(m_frontend.get());
136#endif
137}
138
139void WorkerInspectorController::disconnectFrontend()
140{
141    if (!m_frontend)
142        return;
143    m_backendDispatcher.clear();
144    // Destroying agents would change the state, but we don't want that.
145    // Pre-disconnect state will be used to restore inspector agents.
146    m_state->mute();
147#if ENABLE(JAVASCRIPT_DEBUGGER)
148    m_debuggerAgent->clearFrontend();
149#endif
150    m_injectedScriptManager->injectedScriptHost()->clearFrontend();
151
152    m_frontend.clear();
153}
154
155void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
156{
157    if (m_backendDispatcher)
158        m_backendDispatcher->dispatch(message);
159}
160
161}
162
163#endif
164