summaryrefslogtreecommitdiffstats
path: root/Source/WebKit/chromium/src/DebuggerAgentManager.cpp
blob: b76bcfe68327bf36675bc85daf587d460a3ecaae (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (C) 2010 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"
#include "DebuggerAgentManager.h"

#include "DebuggerAgentImpl.h"
#include "Frame.h"
#include "PageGroupLoadDeferrer.h"
#include "ScriptDebugServer.h"
#include "V8Proxy.h"
#include "WebDevToolsAgentImpl.h"
#include "WebFrameImpl.h"
#include "WebViewImpl.h"
#include <wtf/HashSet.h>
#include <wtf/Noncopyable.h>
#include <wtf/text/StringConcatenate.h>

namespace WebKit {

WebDevToolsAgent::MessageLoopDispatchHandler DebuggerAgentManager::s_messageLoopDispatchHandler = 0;

bool DebuggerAgentManager::s_inHostDispatchHandler = false;

DebuggerAgentManager::DeferrersMap DebuggerAgentManager::s_pageDeferrers;

bool DebuggerAgentManager::s_exposeV8DebuggerProtocol = false;

namespace {

class CallerIdWrapper : public v8::Debug::ClientData {
    WTF_MAKE_NONCOPYABLE(CallerIdWrapper);
public:
    CallerIdWrapper() : m_callerIsMananager(true), m_callerId(0) { }
    explicit CallerIdWrapper(int callerId)
        : m_callerIsMananager(false)
        , m_callerId(callerId) { }
    ~CallerIdWrapper() { }
    bool callerIsMananager() const { return m_callerIsMananager; }
    int callerId() const { return m_callerId; }
private:
    bool m_callerIsMananager;
    int m_callerId;
};

} // namespace


void DebuggerAgentManager::debugHostDispatchHandler()
{
    if (!s_messageLoopDispatchHandler || !s_attachedAgentsMap)
        return;

    if (s_inHostDispatchHandler)
        return;

    s_inHostDispatchHandler = true;

    Vector<WebViewImpl*> views;
    // 1. Disable active objects and input events.
    for (AttachedAgentsMap::iterator it = s_attachedAgentsMap->begin(); it != s_attachedAgentsMap->end(); ++it) {
        DebuggerAgentImpl* agent = it->second;
        s_pageDeferrers.set(agent->webView(), new WebCore::PageGroupLoadDeferrer(agent->page(), true));
        views.append(agent->webView());
        agent->webView()->setIgnoreInputEvents(true);
    }

    // 2. Process messages.
    s_messageLoopDispatchHandler();

    // 3. Bring things back.
    for (Vector<WebViewImpl*>::iterator it = views.begin(); it != views.end(); ++it) {
        if (s_pageDeferrers.contains(*it)) {
            // The view was not closed during the dispatch.
            (*it)->setIgnoreInputEvents(false);
        }
    }
    deleteAllValues(s_pageDeferrers);
    s_pageDeferrers.clear();

    s_inHostDispatchHandler = false;
    if (!s_attachedAgentsMap) {
        // Remove handlers if all agents were detached within host dispatch.
        v8::Debug::SetMessageHandler(0);
        v8::Debug::SetHostDispatchHandler(0);
    }
}

DebuggerAgentManager::AttachedAgentsMap* DebuggerAgentManager::s_attachedAgentsMap = 0;

void DebuggerAgentManager::debugAttach(DebuggerAgentImpl* debuggerAgent)
{
    if (!s_exposeV8DebuggerProtocol)
        return;
    if (!s_attachedAgentsMap) {
        s_attachedAgentsMap = new AttachedAgentsMap();
        v8::Debug::SetMessageHandler2(&DebuggerAgentManager::onV8DebugMessage);
        v8::Debug::SetHostDispatchHandler(&DebuggerAgentManager::debugHostDispatchHandler, 100 /* ms */);
    }
    int hostId = debuggerAgent->webdevtoolsAgent()->hostId();
    ASSERT(hostId);
    s_attachedAgentsMap->set(hostId, debuggerAgent);
}

void DebuggerAgentManager::debugDetach(DebuggerAgentImpl* debuggerAgent)
{
    if (!s_exposeV8DebuggerProtocol)
        return;
    if (!s_attachedAgentsMap) {
        ASSERT_NOT_REACHED();
        return;
    }
    int hostId = debuggerAgent->webdevtoolsAgent()->hostId();
    ASSERT(s_attachedAgentsMap->get(hostId) == debuggerAgent);
    bool isOnBreakpoint = (findAgentForCurrentV8Context() == debuggerAgent);
    s_attachedAgentsMap->remove(hostId);

    if (s_attachedAgentsMap->isEmpty()) {
        delete s_attachedAgentsMap;
        s_attachedAgentsMap = 0;
        // Note that we do not empty handlers while in dispatch - we schedule
        // continue and do removal once we are out of the dispatch. Also there is
        // no need to send continue command in this case since removing message
        // handler will cause debugger unload and all breakpoints will be cleared.
        if (!s_inHostDispatchHandler) {
            v8::Debug::SetMessageHandler2(0);
            v8::Debug::SetHostDispatchHandler(0);
        }
    } else {
      // Remove all breakpoints set by the agent.
      String clearBreakpointGroupCmd = makeString(
          "{\"seq\":1,\"type\":\"request\",\"command\":\"clearbreakpointgroup\","
              "\"arguments\":{\"groupId\":", String::number(hostId), "}}");
      sendCommandToV8(clearBreakpointGroupCmd, new CallerIdWrapper());

      if (isOnBreakpoint) {
          // Force continue if detach happened in nessted message loop while
          // debugger was paused on a breakpoint(as long as there are other
          // attached agents v8 will wait for explicit'continue' message).
          sendContinueCommandToV8();
      }
    }
}

void DebuggerAgentManager::onV8DebugMessage(const v8::Debug::Message& message)
{
    v8::HandleScope scope;
    v8::String::Value value(message.GetJSON());
    WTF::String out(reinterpret_cast<const UChar*>(*value), value.length());

    // If callerData is not 0 the message is a response to a debugger command.
    if (v8::Debug::ClientData* callerData = message.GetClientData()) {
        CallerIdWrapper* wrapper = static_cast<CallerIdWrapper*>(callerData);
        if (wrapper->callerIsMananager()) {
            // Just ignore messages sent by this manager.
            return;
        }
        DebuggerAgentImpl* debuggerAgent = debuggerAgentForHostId(wrapper->callerId());
        if (debuggerAgent)
            debuggerAgent->debuggerOutput(out);
        else if (!message.WillStartRunning()) {
            // Autocontinue execution if there is no handler.
            sendContinueCommandToV8();
        }
        return;
    } // Otherwise it's an event message.
    ASSERT(message.IsEvent());

    // Ignore unsupported event types.
    if (message.GetEvent() != v8::AfterCompile && message.GetEvent() != v8::Break && message.GetEvent() != v8::Exception)
        return;

    v8::Handle<v8::Context> context = message.GetEventContext();
    // If the context is from one of the inpected tabs it should have its context
    // data.
    if (context.IsEmpty()) {
        // Unknown context, skip the event.
        return;
    }

    // If the context is from one of the inpected tabs or injected extension
    // scripts it must have hostId in the data field.
    int hostId = WebCore::V8Proxy::contextDebugId(context);
    if (hostId != -1) {
        DebuggerAgentImpl* agent = debuggerAgentForHostId(hostId);
        if (agent) {
            if (agent->autoContinueOnException()
                && message.GetEvent() == v8::Exception) {
                sendContinueCommandToV8();
                return;
            }

            agent->debuggerOutput(out);
            return;
        }
    }

    if (!message.WillStartRunning()) {
        // Autocontinue execution on break and exception  events if there is no
        // handler.
        sendContinueCommandToV8();
    }
}

void DebuggerAgentManager::pauseScript()
{
    v8::Debug::DebugBreak();
}

void DebuggerAgentManager::executeDebuggerCommand(const WTF::String& command, int callerId)
{
    sendCommandToV8(command, new CallerIdWrapper(callerId));
}

void DebuggerAgentManager::setMessageLoopDispatchHandler(WebDevToolsAgent::MessageLoopDispatchHandler handler)
{
    s_messageLoopDispatchHandler = handler;
}

void DebuggerAgentManager::setExposeV8DebuggerProtocol(bool value)
{
    s_exposeV8DebuggerProtocol = value;
    WebCore::ScriptDebugServer::shared().setEnabled(!s_exposeV8DebuggerProtocol);
}

void DebuggerAgentManager::setHostId(WebFrameImpl* webframe, int hostId)
{
    ASSERT(hostId > 0);
    WebCore::V8Proxy* proxy = WebCore::V8Proxy::retrieve(webframe->frame());
    if (proxy)
        proxy->setContextDebugId(hostId);
}

void DebuggerAgentManager::onWebViewClosed(WebViewImpl* webview)
{
    if (s_pageDeferrers.contains(webview)) {
        delete s_pageDeferrers.get(webview);
        s_pageDeferrers.remove(webview);
    }
}

void DebuggerAgentManager::onNavigate()
{
    if (s_inHostDispatchHandler)
        DebuggerAgentManager::sendContinueCommandToV8();
}

void DebuggerAgentManager::sendCommandToV8(const WTF::String& cmd, v8::Debug::ClientData* data)
{
    v8::Debug::SendCommand(reinterpret_cast<const uint16_t*>(cmd.characters()), cmd.length(), data);
}

void DebuggerAgentManager::sendContinueCommandToV8()
{
    WTF::String continueCmd("{\"seq\":1,\"type\":\"request\",\"command\":\"continue\"}");
    sendCommandToV8(continueCmd, new CallerIdWrapper());
}

DebuggerAgentImpl* DebuggerAgentManager::findAgentForCurrentV8Context()
{
    if (!s_attachedAgentsMap)
        return 0;
    ASSERT(!s_attachedAgentsMap->isEmpty());

    WebCore::Frame* frame = WebCore::V8Proxy::retrieveFrameForEnteredContext();
    if (!frame)
        return 0;
    WebCore::Page* page = frame->page();
    for (AttachedAgentsMap::iterator it = s_attachedAgentsMap->begin(); it != s_attachedAgentsMap->end(); ++it) {
        if (it->second->page() == page)
            return it->second;
    }
    return 0;
}

DebuggerAgentImpl* DebuggerAgentManager::debuggerAgentForHostId(int hostId)
{
    if (!s_attachedAgentsMap)
        return 0;
    return s_attachedAgentsMap->get(hostId);
}

} // namespace WebKit