Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "content/renderer/devtools/devtools_agent_filter.h" |
| 6 | |
| 7 | #include "base/bind.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include "content/common/devtools_messages.h" |
| 10 | #include "content/renderer/devtools/devtools_agent.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 11 | #include "third_party/WebKit/public/platform/WebString.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 12 | #include "third_party/WebKit/public/web/WebDevToolsAgent.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 13 | |
| 14 | using WebKit::WebDevToolsAgent; |
| 15 | using WebKit::WebString; |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class MessageImpl : public WebDevToolsAgent::MessageDescriptor { |
| 22 | public: |
| 23 | MessageImpl(const std::string& message, int host_id) |
| 24 | : msg(message), |
| 25 | host_id(host_id) { |
| 26 | } |
| 27 | virtual ~MessageImpl() {} |
| 28 | virtual WebDevToolsAgent* agent() { |
| 29 | DevToolsAgent* agent = DevToolsAgent::FromHostId(host_id); |
| 30 | if (!agent) |
| 31 | return 0; |
| 32 | return agent->GetWebAgent(); |
| 33 | } |
| 34 | virtual WebString message() { return WebString::fromUTF8(msg); } |
| 35 | private: |
| 36 | std::string msg; |
| 37 | int host_id; |
| 38 | }; |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | DevToolsAgentFilter::DevToolsAgentFilter() |
| 43 | : message_handled_(false), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 44 | render_thread_loop_(base::MessageLoop::current()), |
| 45 | current_routing_id_(0) {} |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 46 | |
| 47 | bool DevToolsAgentFilter::OnMessageReceived(const IPC::Message& message) { |
| 48 | // Dispatch debugger commands directly from IO. |
| 49 | message_handled_ = true; |
| 50 | current_routing_id_ = message.routing_id(); |
| 51 | IPC_BEGIN_MESSAGE_MAP(DevToolsAgentFilter, message) |
| 52 | IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend, |
| 53 | OnDispatchOnInspectorBackend) |
| 54 | IPC_MESSAGE_UNHANDLED(message_handled_ = false) |
| 55 | IPC_END_MESSAGE_MAP() |
| 56 | return message_handled_; |
| 57 | } |
| 58 | |
| 59 | DevToolsAgentFilter::~DevToolsAgentFilter() {} |
| 60 | |
| 61 | void DevToolsAgentFilter::OnDispatchOnInspectorBackend( |
| 62 | const std::string& message) { |
| 63 | if (!WebDevToolsAgent::shouldInterruptForMessage( |
| 64 | WebString::fromUTF8(message))) { |
| 65 | message_handled_ = false; |
| 66 | return; |
| 67 | } |
| 68 | WebDevToolsAgent::interruptAndDispatch( |
| 69 | new MessageImpl(message, current_routing_id_)); |
| 70 | |
| 71 | render_thread_loop_->PostTask( |
| 72 | FROM_HERE, base::Bind(&WebDevToolsAgent::processPendingMessages)); |
| 73 | } |
| 74 | |
| 75 | } // namespace content |