[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 1 | // 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] | e4ac5df | 2009-03-17 15:33:11 | [diff] [blame] | 5 | #include "chrome/renderer/devtools_agent.h" |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 6 | |
[email protected] | 7b4c0d6 | 2009-04-03 10:06:57 | [diff] [blame] | 7 | #include "chrome/common/devtools_messages.h" |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 8 | #include "chrome/common/render_messages.h" |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 9 | #include "chrome/renderer/render_view.h" |
[email protected] | 611cad4 | 2009-03-16 18:51:34 | [diff] [blame] | 10 | #include "webkit/glue/webdevtoolsagent.h" |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 11 | |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame^] | 12 | // static |
| 13 | std::map<int, DevToolsAgent*> DevToolsAgent::agent_for_routing_id_; |
| 14 | |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 15 | DevToolsAgent::DevToolsAgent(int routing_id, RenderView* view) |
[email protected] | 24b3b4d | 2009-04-01 14:26:40 | [diff] [blame] | 16 | : routing_id_(routing_id), |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 17 | view_(view) { |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame^] | 18 | agent_for_routing_id_[routing_id] = this; |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | DevToolsAgent::~DevToolsAgent() { |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame^] | 22 | agent_for_routing_id_.erase(routing_id_); |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 25 | // Called on the Renderer thread. |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 26 | bool DevToolsAgent::OnMessageReceived(const IPC::Message& message) { |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 27 | bool handled = true; |
| 28 | IPC_BEGIN_MESSAGE_MAP(DevToolsAgent, message) |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 29 | IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Detach, OnDetach) |
[email protected] | 611cad4 | 2009-03-16 18:51:34 | [diff] [blame] | 30 | IPC_MESSAGE_HANDLER(DevToolsAgentMsg_RpcMessage, OnRpcMessage) |
[email protected] | d0ef30f4 | 2009-03-24 13:54:28 | [diff] [blame] | 31 | IPC_MESSAGE_HANDLER(DevToolsAgentMsg_InspectElement, OnInspectElement) |
[email protected] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 32 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 33 | IPC_END_MESSAGE_MAP() |
| 34 | return handled; |
| 35 | } |
| 36 | |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 37 | void 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] | b2abac7 | 2009-02-26 12:39:28 | [diff] [blame] | 42 | } |
| 43 | |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame^] | 44 | int DevToolsAgent::GetHostId() { |
| 45 | return routing_id_; |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | DevToolsAgent* 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] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 54 | } |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame^] | 55 | return NULL; |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 58 | void DevToolsAgent::OnDetach() { |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 59 | WebDevToolsAgent* web_agent = GetWebAgent(); |
| 60 | if (web_agent) { |
| 61 | web_agent->Detach(); |
| 62 | } |
| 63 | } |
| 64 | |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 65 | void DevToolsAgent::OnRpcMessage(const std::string& raw_msg) { |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 66 | WebDevToolsAgent* web_agent = GetWebAgent(); |
| 67 | if (web_agent) { |
| 68 | web_agent->DispatchMessageFromClient(raw_msg); |
| 69 | } |
[email protected] | 611cad4 | 2009-03-16 18:51:34 | [diff] [blame] | 70 | } |
[email protected] | 63a5c2a2 | 2009-03-26 11:12:53 | [diff] [blame] | 71 | |
[email protected] | b75b7d07 | 2009-04-06 13:47:00 | [diff] [blame] | 72 | void DevToolsAgent::OnInspectElement(int x, int y) { |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 73 | WebDevToolsAgent* web_agent = GetWebAgent(); |
| 74 | if (web_agent) { |
[email protected] | 90ca369 | 2009-04-09 16:09:43 | [diff] [blame] | 75 | web_agent->Attach(); |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 76 | web_agent->InspectElement(x, y); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | WebDevToolsAgent* DevToolsAgent::GetWebAgent() { |
[email protected] | 63a5c2a2 | 2009-03-26 11:12:53 | [diff] [blame] | 81 | WebView* web_view = view_->webview(); |
| 82 | if (!web_view) |
[email protected] | 2573a23 | 2009-03-27 12:36:21 | [diff] [blame] | 83 | return NULL; |
| 84 | return web_view->GetWebDevToolsAgent(); |
[email protected] | 63a5c2a2 | 2009-03-26 11:12:53 | [diff] [blame] | 85 | } |