1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/devtools/devtools_agent.h"
9 #include "base/debug/trace_event.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/process/process.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "content/common/devtools_messages.h"
15 #include "content/common/frame_messages.h"
16 #include "content/common/gpu/gpu_messages.h"
17 #include "content/common/view_messages.h"
18 #include "content/renderer/devtools/devtools_agent_filter.h"
19 #include "content/renderer/devtools/devtools_client.h"
20 #include "content/renderer/render_thread_impl.h"
21 #include "content/renderer/render_view_impl.h"
22 #include "third_party/WebKit/public/platform/WebPoint.h"
23 #include "third_party/WebKit/public/platform/WebString.h"
24 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
25 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
26 #include "third_party/WebKit/public/web/WebDevToolsAgent.h"
27 #include "third_party/WebKit/public/web/WebDeviceEmulationParams.h"
28 #include "third_party/WebKit/public/web/WebFrame.h"
29 #include "third_party/WebKit/public/web/WebSettings.h"
30 #include "third_party/WebKit/public/web/WebView.h"
32 #if defined(USE_TCMALLOC)
33 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
36 using blink::WebConsoleMessage;
37 using blink::WebDevToolsAgent;
38 using blink::WebDevToolsAgentClient;
39 using blink::WebFrame;
40 using blink::WebPoint;
41 using blink::WebString;
42 using blink::WebCString;
43 using blink::WebVector;
46 using base::debug::TraceLog;
47 using base::debug::TraceOptions;
51 base::subtle::AtomicWord DevToolsAgent::event_callback_;
55 class WebKitClientMessageLoopImpl
56 : public WebDevToolsAgentClient::WebKitClientMessageLoop {
58 WebKitClientMessageLoopImpl() : message_loop_(base::MessageLoop::current()) {}
59 virtual ~WebKitClientMessageLoopImpl() { message_loop_ = NULL; }
61 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_);
64 virtual void quitNow() {
65 message_loop_->QuitNow();
68 base::MessageLoop* message_loop_;
71 typedef std::map<int, DevToolsAgent*> IdToAgentMap;
72 base::LazyInstance<IdToAgentMap>::Leaky
73 g_agent_for_routing_id = LAZY_INSTANCE_INITIALIZER;
77 DevToolsAgent::DevToolsAgent(RenderViewImpl* render_view)
78 : RenderViewObserver(render_view),
80 is_devtools_client_(false),
81 gpu_route_id_(MSG_ROUTING_NONE),
82 paused_in_mouse_move_(false) {
83 g_agent_for_routing_id.Get()[routing_id()] = this;
85 render_view->webview()->setDevToolsAgentClient(this);
88 DevToolsAgent::~DevToolsAgent() {
89 g_agent_for_routing_id.Get().erase(routing_id());
90 resetTraceEventCallback();
93 // Called on the Renderer thread.
94 bool DevToolsAgent::OnMessageReceived(const IPC::Message& message) {
96 IPC_BEGIN_MESSAGE_MAP(DevToolsAgent, message)
97 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Attach, OnAttach)
98 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Reattach, OnReattach)
99 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Detach, OnDetach)
100 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend,
101 OnDispatchOnInspectorBackend)
102 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_InspectElement, OnInspectElement)
103 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_AddMessageToConsole,
104 OnAddMessageToConsole)
105 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_GpuTasksChunk, OnGpuTasksChunk)
106 IPC_MESSAGE_HANDLER(DevToolsMsg_SetupDevToolsClient, OnSetupDevToolsClient)
107 IPC_MESSAGE_UNHANDLED(handled = false)
108 IPC_END_MESSAGE_MAP()
110 if (message.type() == FrameMsg_Navigate::ID ||
111 message.type() == ViewMsg_Close::ID)
112 ContinueProgram(); // Don't want to swallow the message.
117 void DevToolsAgent::sendMessageToInspectorFrontend(
118 const blink::WebString& message) {
119 Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(routing_id(),
123 long DevToolsAgent::processId() {
124 return base::Process::Current().pid();
127 int DevToolsAgent::debuggerId() {
131 void DevToolsAgent::saveAgentRuntimeState(
132 const blink::WebString& state) {
133 Send(new DevToolsHostMsg_SaveAgentRuntimeState(routing_id(), state.utf8()));
136 blink::WebDevToolsAgentClient::WebKitClientMessageLoop*
137 DevToolsAgent::createClientMessageLoop() {
138 return new WebKitClientMessageLoopImpl();
141 void DevToolsAgent::willEnterDebugLoop() {
142 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view());
143 paused_in_mouse_move_ = impl->SendAckForMouseMoveFromDebugger();
146 void DevToolsAgent::didExitDebugLoop() {
147 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view());
148 if (paused_in_mouse_move_) {
149 impl->IgnoreAckForMouseMoveFromDebugger();
150 paused_in_mouse_move_ = false;
154 void DevToolsAgent::resetTraceEventCallback()
156 TraceLog::GetInstance()->SetEventCallbackDisabled();
157 base::subtle::NoBarrier_Store(&event_callback_, 0);
160 void DevToolsAgent::setTraceEventCallback(const WebString& category_filter,
161 TraceEventCallback cb) {
162 TraceLog* trace_log = TraceLog::GetInstance();
163 base::subtle::NoBarrier_Store(&event_callback_,
164 reinterpret_cast<base::subtle::AtomicWord>(cb));
166 trace_log->SetEventCallbackEnabled(base::debug::CategoryFilter(
167 category_filter.utf8()), TraceEventCallbackWrapper);
169 trace_log->SetEventCallbackDisabled();
173 void DevToolsAgent::enableTracing(const WebString& category_filter) {
174 TraceLog* trace_log = TraceLog::GetInstance();
175 trace_log->SetEnabled(base::debug::CategoryFilter(category_filter.utf8()),
176 TraceLog::RECORDING_MODE,
180 void DevToolsAgent::disableTracing() {
181 TraceLog::GetInstance()->SetDisabled();
185 void DevToolsAgent::TraceEventCallbackWrapper(
186 base::TimeTicks timestamp,
188 const unsigned char* category_group_enabled,
190 unsigned long long id,
192 const char* const arg_names[],
193 const unsigned char arg_types[],
194 const unsigned long long arg_values[],
195 unsigned char flags) {
196 TraceEventCallback callback =
197 reinterpret_cast<TraceEventCallback>(
198 base::subtle::NoBarrier_Load(&event_callback_));
200 double timestamp_seconds = (timestamp - base::TimeTicks()).InSecondsF();
201 callback(phase, category_group_enabled, name, id, num_args,
202 arg_names, arg_types, arg_values, flags, timestamp_seconds);
206 void DevToolsAgent::startGPUEventsRecording() {
207 GpuChannelHost* gpu_channel_host =
208 RenderThreadImpl::current()->GetGpuChannel();
209 if (!gpu_channel_host)
211 DCHECK(gpu_route_id_ == MSG_ROUTING_NONE);
212 int32 route_id = gpu_channel_host->GenerateRouteID();
213 bool succeeded = false;
214 gpu_channel_host->Send(
215 new GpuChannelMsg_DevToolsStartEventsRecording(route_id, &succeeded));
218 gpu_route_id_ = route_id;
219 gpu_channel_host->AddRoute(gpu_route_id_, AsWeakPtr());
223 void DevToolsAgent::stopGPUEventsRecording() {
224 GpuChannelHost* gpu_channel_host =
225 RenderThreadImpl::current()->GetGpuChannel();
226 if (!gpu_channel_host || gpu_route_id_ == MSG_ROUTING_NONE)
228 gpu_channel_host->Send(new GpuChannelMsg_DevToolsStopEventsRecording());
229 gpu_channel_host->RemoveRoute(gpu_route_id_);
230 gpu_route_id_ = MSG_ROUTING_NONE;
233 void DevToolsAgent::OnGpuTasksChunk(const std::vector<GpuTaskInfo>& tasks) {
234 WebDevToolsAgent* web_agent = GetWebAgent();
237 for (size_t i = 0; i < tasks.size(); i++) {
238 const GpuTaskInfo& task = tasks[i];
239 WebDevToolsAgent::GPUEvent event(
240 task.timestamp, task.phase, task.foreign, task.gpu_memory_used_bytes);
241 event.limitGPUMemoryBytes = task.gpu_memory_limit_bytes;
242 web_agent->processGPUEvent(event);
246 void DevToolsAgent::enableDeviceEmulation(
247 const blink::WebDeviceEmulationParams& params) {
248 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view());
249 impl->EnableScreenMetricsEmulation(params);
252 void DevToolsAgent::disableDeviceEmulation() {
253 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view());
254 impl->DisableScreenMetricsEmulation();
258 DevToolsAgent* DevToolsAgent::FromRoutingId(int routing_id) {
259 IdToAgentMap::iterator it = g_agent_for_routing_id.Get().find(routing_id);
260 if (it != g_agent_for_routing_id.Get().end()) {
266 void DevToolsAgent::OnAttach(const std::string& host_id) {
267 WebDevToolsAgent* web_agent = GetWebAgent();
269 web_agent->attach(WebString::fromUTF8(host_id));
274 void DevToolsAgent::OnReattach(const std::string& host_id,
275 const std::string& agent_state) {
276 WebDevToolsAgent* web_agent = GetWebAgent();
278 web_agent->reattach(WebString::fromUTF8(host_id),
279 WebString::fromUTF8(agent_state));
284 void DevToolsAgent::OnDetach() {
285 WebDevToolsAgent* web_agent = GetWebAgent();
288 is_attached_ = false;
292 void DevToolsAgent::OnDispatchOnInspectorBackend(const std::string& message) {
293 TRACE_EVENT0("devtools", "DevToolsAgent::OnDispatchOnInspectorBackend");
294 WebDevToolsAgent* web_agent = GetWebAgent();
296 web_agent->dispatchOnInspectorBackend(WebString::fromUTF8(message));
299 void DevToolsAgent::OnInspectElement(
300 const std::string& host_id, int x, int y) {
301 WebDevToolsAgent* web_agent = GetWebAgent();
303 web_agent->attach(WebString::fromUTF8(host_id));
304 web_agent->inspectElementAt(WebPoint(x, y));
309 void DevToolsAgent::OnAddMessageToConsole(ConsoleMessageLevel level,
310 const std::string& message) {
311 WebView* web_view = render_view()->GetWebView();
315 WebFrame* main_frame = web_view->mainFrame();
319 WebConsoleMessage::Level target_level = WebConsoleMessage::LevelLog;
321 case CONSOLE_MESSAGE_LEVEL_DEBUG:
322 target_level = WebConsoleMessage::LevelDebug;
324 case CONSOLE_MESSAGE_LEVEL_LOG:
325 target_level = WebConsoleMessage::LevelLog;
327 case CONSOLE_MESSAGE_LEVEL_WARNING:
328 target_level = WebConsoleMessage::LevelWarning;
330 case CONSOLE_MESSAGE_LEVEL_ERROR:
331 target_level = WebConsoleMessage::LevelError;
334 main_frame->addMessageToConsole(
335 WebConsoleMessage(target_level, WebString::fromUTF8(message)));
338 void DevToolsAgent::ContinueProgram() {
339 WebDevToolsAgent* web_agent = GetWebAgent();
341 web_agent->continueProgram();
344 void DevToolsAgent::OnSetupDevToolsClient() {
345 // We only want to register once per render view.
346 if (is_devtools_client_)
348 is_devtools_client_ = true;
349 new DevToolsClient(static_cast<RenderViewImpl*>(render_view()));
352 WebDevToolsAgent* DevToolsAgent::GetWebAgent() {
353 WebView* web_view = render_view()->GetWebView();
356 return web_view->devToolsAgent();
359 bool DevToolsAgent::IsAttached() {
363 } // namespace content