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
|
// 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 <string>
#include "Document.h"
#include "EventListener.h"
#include "InspectorController.h"
#include "Node.h"
#include "Page.h"
#include "PlatformString.h"
#include "ScriptValue.h"
#include <wtf/OwnPtr.h>
#undef LOG
#include "base/values.h"
#include "webkit/glue/devtools/debugger_agent_impl.h"
#include "webkit/glue/devtools/debugger_agent_manager.h"
#include "webkit/glue/devtools/dom_agent_impl.h"
#include "webkit/glue/devtools/net_agent_impl.h"
#include "webkit/glue/glue_util.h"
#include "webkit/glue/webdatasource.h"
#include "webkit/glue/webdevtoolsagent_delegate.h"
#include "webkit/glue/webdevtoolsagent_impl.h"
#include "webkit/glue/weburlrequest.h"
#include "webkit/glue/webview_impl.h"
using WebCore::Document;
using WebCore::InspectorController;
using WebCore::Node;
using WebCore::Page;
using WebCore::ScriptValue;
using WebCore::String;
// Maximum size of the console message cache.
static const size_t kMaxConsoleMessages = 200;
WebDevToolsAgentImpl::WebDevToolsAgentImpl(
WebViewImpl* web_view_impl,
WebDevToolsAgentDelegate* delegate)
: delegate_(delegate),
web_view_impl_(web_view_impl),
document_(NULL),
attached_(false) {
debugger_agent_delegate_stub_.set(new DebuggerAgentDelegateStub(this));
dom_agent_delegate_stub_.set(new DomAgentDelegateStub(this));
net_agent_delegate_stub_.set(new NetAgentDelegateStub(this));
tools_agent_delegate_stub_.set(new ToolsAgentDelegateStub(this));
}
WebDevToolsAgentImpl::~WebDevToolsAgentImpl() {
}
void WebDevToolsAgentImpl::Attach() {
if (attached_) {
return;
}
debugger_agent_impl_.set(
new DebuggerAgentImpl(web_view_impl_,
debugger_agent_delegate_stub_.get(),
this));
dom_agent_impl_.set(new DomAgentImpl(dom_agent_delegate_stub_.get()));
net_agent_impl_.set(new NetAgentImpl(net_agent_delegate_stub_.get()));
// We are potentially attaching to the running page -> init agents with
// Document if any.
Page* page = web_view_impl_->page();
Document* doc = page->mainFrame()->document();
if (doc) {
debugger_agent_impl_->SetDocument(doc);
dom_agent_impl_->SetDocument(doc, true);
net_agent_impl_->SetDocument(doc);
}
// Populate console.
for (Vector<ConsoleMessage>::iterator it = console_log_.begin();
it != console_log_.end(); ++it) {
tools_agent_delegate_stub_->AddMessageToConsole(
it->message,
it->source_id,
it->line_no);
}
attached_ = true;
}
void WebDevToolsAgentImpl::Detach() {
debugger_agent_impl_.set(NULL);
dom_agent_impl_.set(NULL);
net_agent_impl_.set(NULL);
attached_ = false;
}
void WebDevToolsAgentImpl::SetMainFrameDocumentReady(bool ready) {
if (!attached_) {
return;
}
// We were attached prior to the page load -> init agents with Document.
Document* doc;
if (ready) {
Page* page = web_view_impl_->page();
doc = page->mainFrame()->document();
} else {
doc = NULL;
}
debugger_agent_impl_->SetDocument(doc);
dom_agent_impl_->SetDocument(doc, false);
net_agent_impl_->SetDocument(doc);
}
void WebDevToolsAgentImpl::DidCommitLoadForFrame(
WebViewImpl* webview,
WebFrame* frame,
bool is_new_navigation) {
if (!attached_) {
return;
}
WebDataSource* ds = frame->GetDataSource();
const WebRequest& request = ds->GetRequest();
GURL url = ds->HasUnreachableURL() ?
ds->GetUnreachableURL() :
request.GetURL();
tools_agent_delegate_stub_->FrameNavigate(
url.possibly_invalid_spec(),
webview->GetMainFrame() == frame);
}
void WebDevToolsAgentImpl::AddMessageToConsole(
const String& message,
const String& source_id,
unsigned int line_no) {
ConsoleMessage cm(message, source_id, line_no);
console_log_.append(cm);
if (console_log_.size() >= kMaxConsoleMessages) {
// Batch shifts to save ticks.
console_log_.remove(0, kMaxConsoleMessages / 5);
}
if (attached_) {
tools_agent_delegate_stub_->AddMessageToConsole(
message,
source_id,
line_no);
}
}
void WebDevToolsAgentImpl::HighlightDOMNode(int node_id) {
if (!attached_) {
return;
}
Node* node = dom_agent_impl_->GetNodeForId(node_id);
if (!node) {
return;
}
Page* page = web_view_impl_->page();
page->inspectorController()->highlight(node);
}
void WebDevToolsAgentImpl::HideDOMNodeHighlight() {
Page* page = web_view_impl_->page();
page->inspectorController()->hideHighlight();
}
void WebDevToolsAgentImpl::EvaluateJavaScript(int call_id, const String& js) {
Page* page = web_view_impl_->page();
if (!page->mainFrame()) {
return;
}
ScriptValue result = page->mainFrame()->loader()->executeScript(js);
String result_string;
if (!result.hasNoValue()) {
result_string = result.toString(NULL);
}
tools_agent_delegate_stub_->DidEvaluateJavaScript(call_id, result_string);
}
void WebDevToolsAgentImpl::ExecuteUtilityFunction(
int call_id,
const String& function_name,
int node_id,
const String& json_args) {
Node* node = dom_agent_impl_->GetNodeForId(node_id);
String result;
if (node) {
result = debugger_agent_impl_->ExecuteUtilityFunction(function_name, node,
json_args);
}
tools_agent_delegate_stub_->DidExecuteUtilityFunction(call_id,
result);
}
void WebDevToolsAgentImpl::ClearConsoleMessages() {
console_log_.clear();
}
void WebDevToolsAgentImpl::DispatchMessageFromClient(
const std::string& raw_msg) {
OwnPtr<ListValue> message(
static_cast<ListValue*>(DevToolsRpc::ParseMessage(raw_msg)));
if (ToolsAgentDispatch::Dispatch(this, *message.get())) {
return;
}
if (!attached_) {
return;
}
if (debugger_agent_impl_.get() &&
DebuggerAgentDispatch::Dispatch(
debugger_agent_impl_.get(),
*message.get())) {
return;
}
if (DomAgentDispatch::Dispatch(dom_agent_impl_.get(), *message.get())) {
return;
}
if (NetAgentDispatch::Dispatch(net_agent_impl_.get(), *message.get())) {
return;
}
}
void WebDevToolsAgentImpl::InspectElement(int x, int y) {
Node* node = web_view_impl_->GetNodeForWindowPos(x, y);
if (!node) {
return;
}
int node_id = dom_agent_impl_->PushNodePathToClient(node);
tools_agent_delegate_stub_->UpdateFocusedNode(node_id);
}
void WebDevToolsAgentImpl::SendRpcMessage(const std::string& raw_msg) {
delegate_->SendMessageToClient(raw_msg);
}
// static
void WebDevToolsAgent::ExecuteDebuggerCommand(
const std::string& command,
WebDevToolsAgent* webdevtools_agent) {
DebuggerAgentManager::ExecuteDebuggerCommand(command, webdevtools_agent);
}
|