blob: 1613ad8c4773195c75aa0dc528e8aa8dfe390c00 [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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 Murdoch9ab55632013-07-18 11:57:30 +01008#include "base/message_loop/message_loop.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00009#include "content/common/devtools_messages.h"
10#include "content/renderer/devtools/devtools_agent.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010011#include "third_party/WebKit/public/platform/WebString.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010012#include "third_party/WebKit/public/web/WebDevToolsAgent.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000013
14using WebKit::WebDevToolsAgent;
15using WebKit::WebString;
16
17namespace content {
18
19namespace {
20
21class 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
42DevToolsAgentFilter::DevToolsAgentFilter()
43 : message_handled_(false),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010044 render_thread_loop_(base::MessageLoop::current()),
45 current_routing_id_(0) {}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000046
47bool 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
59DevToolsAgentFilter::~DevToolsAgentFilter() {}
60
61void 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