WorkerInspectorController.cpp 6.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(INSPECTOR) && ENABLE(WORKERS)

#include "WorkerInspectorController.h"

#include "InjectedScriptHost.h"
#include "InjectedScriptManager.h"
#include "InspectorBackendDispatcher.h"
#include "InspectorClient.h"
41
#include "InspectorConsoleAgent.h"
42 43
#include "InspectorFrontend.h"
#include "InspectorFrontendChannel.h"
44
#include "InspectorProfilerAgent.h"
45
#include "InspectorState.h"
46
#include "InspectorStateClient.h"
47
#include "InstrumentingAgents.h"
48
#include "WorkerConsoleAgent.h"
49
#include "WorkerContext.h"
50
#include "WorkerDebuggerAgent.h"
51
#include "WorkerReportingProxy.h"
52
#include "WorkerRuntimeAgent.h"
53
#include "WorkerThread.h"
54 55 56 57 58 59
#include <wtf/PassOwnPtr.h>

namespace WebCore {

namespace {

60 61 62 63 64 65 66 67 68 69 70 71 72
class PageInspectorProxy : public InspectorFrontendChannel {
public:
    explicit PageInspectorProxy(WorkerContext* workerContext) : m_workerContext(workerContext) { }
    virtual ~PageInspectorProxy() { }
private:
    virtual bool sendMessageToFrontend(const String& message)
    {
        m_workerContext->thread()->workerReportingProxy().postMessageToPageInspector(message);
        return true;
    }
    WorkerContext* m_workerContext;
};

73 74 75 76 77 78 79 80 81 82 83 84 85 86
class WorkerStateClient : public InspectorStateClient {
public:
    WorkerStateClient(WorkerContext* context) : m_workerContext(context) { }
    virtual ~WorkerStateClient() { }

private:
    virtual void updateInspectorStateCookie(const String& cookie)
    {
        m_workerContext->thread()->workerReportingProxy().updateInspectorStateCookie(cookie);
    }

    WorkerContext* m_workerContext;
};

87 88 89 90
}

WorkerInspectorController::WorkerInspectorController(WorkerContext* workerContext)
    : m_workerContext(workerContext)
91 92
    , m_stateClient(adoptPtr(new WorkerStateClient(workerContext)))
    , m_state(adoptPtr(new InspectorState(m_stateClient.get())))
93
    , m_instrumentingAgents(adoptPtr(new InstrumentingAgents()))
94
    , m_injectedScriptManager(InjectedScriptManager::createForWorker())
95 96
{

97 98 99
    m_runtimeAgent = WorkerRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), workerContext);
    m_consoleAgent = WorkerConsoleAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get());

100
#if ENABLE(JAVASCRIPT_DEBUGGER)
101
    m_debuggerAgent = WorkerDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), workerContext, m_injectedScriptManager.get());
102
    m_profilerAgent = InspectorProfilerAgent::create(m_instrumentingAgents.get(), m_consoleAgent.get(), workerContext, m_state.get(), m_injectedScriptManager.get());
103
#endif
104

105 106
    m_injectedScriptManager->injectedScriptHost()->init(0
        , 0
107
#if ENABLE(SQL_DATABASE)
108 109 110 111
        , 0
#endif
        , 0
    );
112 113 114 115

#if ENABLE(JAVASCRIPT_DEBUGGER)
    m_runtimeAgent->setScriptDebugServer(&m_debuggerAgent->scriptDebugServer());
#endif
116 117 118 119
}
 
WorkerInspectorController::~WorkerInspectorController()
{
120
    disconnectFrontend();
121 122
}

123
void WorkerInspectorController::connectFrontend()
124 125 126
{
    ASSERT(!m_frontend);
    m_state->unmute();
127 128
    m_frontendChannel = adoptPtr(new PageInspectorProxy(m_workerContext));
    m_frontend = adoptPtr(new InspectorFrontend(m_frontendChannel.get()));
129
    m_backendDispatcher = InspectorBackendDispatcher::create(m_frontendChannel.get());
130
    m_consoleAgent->registerInDispatcher(m_backendDispatcher.get());
131
#if ENABLE(JAVASCRIPT_DEBUGGER)
132
    m_debuggerAgent->registerInDispatcher(m_backendDispatcher.get());
133
    m_profilerAgent->registerInDispatcher(m_backendDispatcher.get());
134
#endif
135
    m_runtimeAgent->registerInDispatcher(m_backendDispatcher.get());
136 137 138

#if ENABLE(JAVASCRIPT_DEBUGGER)
    m_debuggerAgent->setFrontend(m_frontend.get());
139
    m_profilerAgent->setFrontend(m_frontend.get());
140
#endif
141
    m_consoleAgent->setFrontend(m_frontend.get());
142 143 144 145 146 147
}

void WorkerInspectorController::disconnectFrontend()
{
    if (!m_frontend)
        return;
148
    m_backendDispatcher->clearFrontend();
149 150 151 152 153 154
    m_backendDispatcher.clear();
    // Destroying agents would change the state, but we don't want that.
    // Pre-disconnect state will be used to restore inspector agents.
    m_state->mute();
#if ENABLE(JAVASCRIPT_DEBUGGER)
    m_debuggerAgent->clearFrontend();
155
    m_profilerAgent->clearFrontend();
156
#endif
157
    m_consoleAgent->clearFrontend();
158 159

    m_frontend.clear();
160
    m_frontendChannel.clear();
161 162
}

163 164
void WorkerInspectorController::restoreInspectorStateFromCookie(const String& inspectorCookie)
{
165
    ASSERT(!m_frontend);
166 167 168 169 170
    connectFrontend();
    m_state->loadFromCookie(inspectorCookie);

#if ENABLE(JAVASCRIPT_DEBUGGER)
    m_debuggerAgent->restore();
171
    m_profilerAgent->restore();
172
#endif
173
    m_consoleAgent->restore();
174 175
}

176 177 178 179 180 181
void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
{
    if (m_backendDispatcher)
        m_backendDispatcher->dispatch(message);
}

182
#if ENABLE(JAVASCRIPT_DEBUGGER)
183 184
void WorkerInspectorController::resume()
{
185 186
    ErrorString unused;
    m_runtimeAgent->run(&unused);
187
}
188
#endif
189

190 191 192
}

#endif