blob: 918bb736e2830da43ecce20b3f3ab0d175afcfaa [file] [log] [blame]
[email protected]b2abac72009-02-26 12:39:281// Copyright (c) 2009 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.
4
[email protected]e4ac5df2009-03-17 15:33:115#include "chrome/renderer/devtools_agent.h"
[email protected]b2abac72009-02-26 12:39:286
[email protected]7b4c0d62009-04-03 10:06:577#include "chrome/common/devtools_messages.h"
[email protected]b2abac72009-02-26 12:39:288#include "chrome/common/render_messages.h"
[email protected]b2abac72009-02-26 12:39:289#include "chrome/renderer/render_view.h"
[email protected]611cad42009-03-16 18:51:3410#include "webkit/glue/webdevtoolsagent.h"
[email protected]b2abac72009-02-26 12:39:2811
[email protected]a8624712009-04-17 00:51:3512// static
13std::map<int, DevToolsAgent*> DevToolsAgent::agent_for_routing_id_;
14
[email protected]b75b7d072009-04-06 13:47:0015DevToolsAgent::DevToolsAgent(int routing_id, RenderView* view)
[email protected]24b3b4d2009-04-01 14:26:4016 : routing_id_(routing_id),
[email protected]b75b7d072009-04-06 13:47:0017 view_(view) {
[email protected]a8624712009-04-17 00:51:3518 agent_for_routing_id_[routing_id] = this;
[email protected]b2abac72009-02-26 12:39:2819}
20
21DevToolsAgent::~DevToolsAgent() {
[email protected]a8624712009-04-17 00:51:3522 agent_for_routing_id_.erase(routing_id_);
[email protected]b2abac72009-02-26 12:39:2823}
24
[email protected]b75b7d072009-04-06 13:47:0025// Called on the Renderer thread.
[email protected]b2abac72009-02-26 12:39:2826bool DevToolsAgent::OnMessageReceived(const IPC::Message& message) {
[email protected]b2abac72009-02-26 12:39:2827 bool handled = true;
28 IPC_BEGIN_MESSAGE_MAP(DevToolsAgent, message)
[email protected]2573a232009-03-27 12:36:2129 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Detach, OnDetach)
[email protected]611cad42009-03-16 18:51:3430 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_RpcMessage, OnRpcMessage)
[email protected]d0ef30f42009-03-24 13:54:2831 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_InspectElement, OnInspectElement)
[email protected]b2abac72009-02-26 12:39:2832 IPC_MESSAGE_UNHANDLED(handled = false)
33 IPC_END_MESSAGE_MAP()
34 return handled;
35}
36
[email protected]b75b7d072009-04-06 13:47:0037void DevToolsAgent::SendMessageToClient(const std::string& raw_msg) {
38 IPC::Message* m = new ViewHostMsg_ForwardToDevToolsClient(
39 routing_id_,
40 DevToolsClientMsg_RpcMessage(raw_msg));
41 view_->Send(m);
[email protected]b2abac72009-02-26 12:39:2842}
43
[email protected]a8624712009-04-17 00:51:3544int DevToolsAgent::GetHostId() {
45 return routing_id_;
46}
47
48// static
49DevToolsAgent* DevToolsAgent::FromHostId(int host_id) {
50 std::map<int, DevToolsAgent*>::iterator it =
51 agent_for_routing_id_.find(host_id);
52 if (it != agent_for_routing_id_.end()) {
53 return it->second;
[email protected]2573a232009-03-27 12:36:2154 }
[email protected]a8624712009-04-17 00:51:3555 return NULL;
[email protected]2573a232009-03-27 12:36:2156}
57
[email protected]b75b7d072009-04-06 13:47:0058void DevToolsAgent::OnDetach() {
[email protected]2573a232009-03-27 12:36:2159 WebDevToolsAgent* web_agent = GetWebAgent();
60 if (web_agent) {
61 web_agent->Detach();
62 }
63}
64
[email protected]b75b7d072009-04-06 13:47:0065void DevToolsAgent::OnRpcMessage(const std::string& raw_msg) {
[email protected]2573a232009-03-27 12:36:2166 WebDevToolsAgent* web_agent = GetWebAgent();
67 if (web_agent) {
68 web_agent->DispatchMessageFromClient(raw_msg);
69 }
[email protected]611cad42009-03-16 18:51:3470}
[email protected]63a5c2a22009-03-26 11:12:5371
[email protected]b75b7d072009-04-06 13:47:0072void DevToolsAgent::OnInspectElement(int x, int y) {
[email protected]2573a232009-03-27 12:36:2173 WebDevToolsAgent* web_agent = GetWebAgent();
74 if (web_agent) {
[email protected]90ca3692009-04-09 16:09:4375 web_agent->Attach();
[email protected]2573a232009-03-27 12:36:2176 web_agent->InspectElement(x, y);
77 }
78}
79
80WebDevToolsAgent* DevToolsAgent::GetWebAgent() {
[email protected]63a5c2a22009-03-26 11:12:5381 WebView* web_view = view_->webview();
82 if (!web_view)
[email protected]2573a232009-03-27 12:36:2183 return NULL;
84 return web_view->GetWebDevToolsAgent();
[email protected]63a5c2a22009-03-26 11:12:5385}