summaryrefslogtreecommitdiffstats
path: root/webkit/glue/devtools/debugger_agent_manager.cc
blob: 41461c8ad4c1307f8daa4d12ebb53a56f460b20c (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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"

#include "Frame.h"
#include "PageGroupLoadDeferrer.h"
#include "v8_proxy.h"
#include <wtf/HashSet.h>
#undef LOG

#include "base/json_reader.h"
#include "base/json_writer.h"
#include "base/string_util.h"
#include "base/values.h"
#include "webkit/glue/devtools/debugger_agent_impl.h"
#include "webkit/glue/devtools/debugger_agent_manager.h"
#include "webkit/glue/webdevtoolsagent_impl.h"
#include "webkit/glue/webview_impl.h"

#if USE(V8)
#include "v8/include/v8-debug.h"
#endif

WebDevToolsAgent::MessageLoopDispatchHandler
    DebuggerAgentManager::message_loop_dispatch_handler_ = NULL;

// static
bool DebuggerAgentManager::in_host_dispatch_handler_ = false;

// static
DebuggerAgentManager::DeferrersMap DebuggerAgentManager::page_deferrers_;

namespace {

class CallerIdWrapper : public v8::Debug::ClientData {
 public:
  CallerIdWrapper() : caller_is_mananager_(true), caller_id_(0) {}
  explicit CallerIdWrapper(int caller_id)
    : caller_is_mananager_(false),
      caller_id_(caller_id) {}
  ~CallerIdWrapper() {}
  bool caller_is_mananager() const { return caller_is_mananager_; }
  int caller_id() const { return caller_id_; }
 private:
  bool caller_is_mananager_;
  int caller_id_;
  DISALLOW_COPY_AND_ASSIGN(CallerIdWrapper);
};

}  // namespace


// static
void DebuggerAgentManager::V8DebugMessageHandler(const uint16_t* message,
                                                 int length,
                                                 v8::Debug::ClientData* data) {
#if USE(V8)
  std::wstring out(reinterpret_cast<const wchar_t*>(message), length);
  std::string out_utf8 = WideToUTF8(out);
  DebuggerAgentManager::DebuggerOutput(out_utf8, data);
#endif
}

void DebuggerAgentManager::V8DebugHostDispatchHandler() {
  if (!DebuggerAgentManager::message_loop_dispatch_handler_ ||
      !attached_agents_) {
    return;
  }
  if (in_host_dispatch_handler_) {
    return;
  }
  in_host_dispatch_handler_ = true;

  Vector<WebViewImpl*> views;
  // 1. Disable active objects and input events.
  for (AttachedAgentsSet::iterator it = attached_agents_->begin();
       it != attached_agents_->end();
       ++it) {
    DebuggerAgentImpl* agent = *it;
    page_deferrers_.set(
        agent->web_view(),
        new WebCore::PageGroupLoadDeferrer(agent->GetPage(), true));
    views.append(agent->web_view());
    agent->web_view()->SetIgnoreInputEvents(true);
  }

  // 2. Process messages.
  DebuggerAgentManager::message_loop_dispatch_handler_();

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

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

// static
DebuggerAgentManager::AttachedAgentsSet*
    DebuggerAgentManager::attached_agents_ = NULL;

// static
void DebuggerAgentManager::DebugAttach(DebuggerAgentImpl* debugger_agent) {
#if USE(V8)
  if (!attached_agents_) {
    attached_agents_ = new AttachedAgentsSet();
    v8::Debug::SetMessageHandler(
        &DebuggerAgentManager::V8DebugMessageHandler,
        false /* don't create separate thread for sending debugger output */);
    v8::Debug::SetHostDispatchHandler(
        &DebuggerAgentManager::V8DebugHostDispatchHandler, 100 /* ms */);
  }
  attached_agents_->add(debugger_agent);
#endif
}

// static
void DebuggerAgentManager::DebugDetach(DebuggerAgentImpl* debugger_agent) {
#if USE(V8)
  if (!attached_agents_) {
    NOTREACHED();
    return;
  }
  DCHECK(attached_agents_->contains(debugger_agent));
  attached_agents_->remove(debugger_agent);
  if (attached_agents_->isEmpty()) {
    delete attached_agents_;
    attached_agents_ = NULL;
    // Note that we do not empty handlers while in dispatch - we schedule
    // continue and do removal once we are out of the dispatch.
    if (!in_host_dispatch_handler_) {
      v8::Debug::SetMessageHandler(NULL);
      v8::Debug::SetHostDispatchHandler(NULL);
    } else if (FindAgentForCurrentV8Context() == debugger_agent) {
      // Force continue just in case to handle close while on a breakpoint.
      SendContinueCommandToV8();
    }
  }
#endif
}

// static
void DebuggerAgentManager::DebugBreak(DebuggerAgentImpl* debugger_agent) {
#if USE(V8)
  DCHECK(attached_agents_->contains(debugger_agent));
  v8::Debug::DebugBreak();
#endif
}

// static
void DebuggerAgentManager::DebuggerOutput(const std::string& out,
                                          v8::Debug::ClientData* caller_data) {
  // If caller_data is not NULL the message is a response to a debugger command.
  if (caller_data) {
    CallerIdWrapper* wrapper = static_cast<CallerIdWrapper*>(caller_data);
    if (wrapper->caller_is_mananager()) {
      // Just ignore messages sent by this manager.
      return;
    }
    DebuggerAgentImpl* debugger_agent = FindDebuggerAgentForToolsAgent(
        wrapper->caller_id());
    if (debugger_agent) {
      debugger_agent->DebuggerOutput(out);
    }
    return;
  } // Otherwise it's an event message.

  // Agent that should be used for sending events is determined based
  // on the active Frame.
  DebuggerAgentImpl* agent = FindAgentForCurrentV8Context();
  if (!agent) {
    // Autocontinue execution on break and exception  events if there is no
    // handler.
    SendContinueCommandToV8();
    return;
  }
  agent->DebuggerOutput(out);
}

// static
void DebuggerAgentManager::ExecuteDebuggerCommand(
    const std::string& command,
    int caller_id) {
  SendCommandToV8(UTF8ToWide(command), new CallerIdWrapper(caller_id));
}

// static
void DebuggerAgentManager::SetMessageLoopDispatchHandler(
    WebDevToolsAgent::MessageLoopDispatchHandler handler) {
  message_loop_dispatch_handler_ = handler;
}

// static
void DebuggerAgentManager::OnWebViewClosed(WebViewImpl* webview) {
  if (page_deferrers_.contains(webview)) {
    delete page_deferrers_.get(webview);
    page_deferrers_.remove(webview);
  }
}

// static
void DebuggerAgentManager::SendCommandToV8(const std::wstring& cmd,
                                           v8::Debug::ClientData* data) {
#if USE(V8)
  v8::Debug::SendCommand(reinterpret_cast<const uint16_t*>(cmd.data()),
                         cmd.length(),
                         data);
#endif
}

void DebuggerAgentManager::SendContinueCommandToV8() {
  std::wstring continue_cmd(
      L"{\"seq\":1,\"type\":\"request\",\"command\":\"continue\"}");
  SendCommandToV8(continue_cmd, new CallerIdWrapper());
}

// static
DebuggerAgentImpl* DebuggerAgentManager::FindAgentForCurrentV8Context() {
  if (!attached_agents_) {
    return NULL;
  }
  DCHECK(!attached_agents_->isEmpty());

  WebCore::Frame* frame = WebCore::V8Proxy::retrieveFrameForEnteredContext();
  if (!frame) {
    return NULL;
  }
  WebCore::Page* page = frame->page();
  for (AttachedAgentsSet::iterator it = attached_agents_->begin();
       it != attached_agents_->end(); ++it) {
    if ((*it)->GetPage() == page) {
      return *it;
    }
  }
  return NULL;
}

DebuggerAgentImpl* DebuggerAgentManager::FindDebuggerAgentForToolsAgent(
    int caller_id) {
  for (AttachedAgentsSet::iterator it = attached_agents_->begin();
       it != attached_agents_->end(); ++it) {
    if ((*it)->webdevtools_agent()->host_id() == caller_id) {
      return *it;
    }
  }
  return NULL;
}