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"
8#include "base/message_loop/message_loop.h"
9#include "content/child/child_process.h"
10#include "content/common/devtools_messages.h"
11#include "content/renderer/devtools/devtools_agent.h"
12#include "third_party/WebKit/public/platform/WebString.h"
13#include "third_party/WebKit/public/web/WebDevToolsAgent.h"
14
15using blink::WebDevToolsAgent;
16using blink::WebString;
17
18namespace content {
19
20namespace {
21
22class MessageImpl : public WebDevToolsAgent::MessageDescriptor {
23 public:
24  MessageImpl(const std::string& message, int routing_id)
25      : msg_(message),
26        routing_id_(routing_id) {
27  }
28  virtual ~MessageImpl() {}
29  virtual WebDevToolsAgent* agent() {
30    DevToolsAgent* agent = DevToolsAgent::FromRoutingId(routing_id_);
31    if (!agent)
32      return 0;
33    return agent->GetWebAgent();
34  }
35  virtual WebString message() { return WebString::fromUTF8(msg_); }
36 private:
37  std::string msg_;
38  int routing_id_;
39};
40
41}  // namespace
42
43DevToolsAgentFilter::DevToolsAgentFilter()
44    : message_handled_(false),
45      render_thread_loop_(base::MessageLoop::current()),
46      io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()),
47      current_routing_id_(0) {}
48
49bool DevToolsAgentFilter::OnMessageReceived(const IPC::Message& message) {
50  // Dispatch debugger commands directly from IO.
51  message_handled_ = true;
52  current_routing_id_ = message.routing_id();
53  IPC_BEGIN_MESSAGE_MAP(DevToolsAgentFilter, message)
54    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend,
55                        OnDispatchOnInspectorBackend)
56    IPC_MESSAGE_UNHANDLED(message_handled_ = false)
57  IPC_END_MESSAGE_MAP()
58  return message_handled_;
59}
60
61DevToolsAgentFilter::~DevToolsAgentFilter() {}
62
63void DevToolsAgentFilter::OnDispatchOnInspectorBackend(
64    const std::string& message) {
65  if (embedded_worker_routes_.find(current_routing_id_) !=
66      embedded_worker_routes_.end()) {
67    message_handled_ = false;
68    return;
69  }
70  if (!WebDevToolsAgent::shouldInterruptForMessage(
71          WebString::fromUTF8(message))) {
72      message_handled_ = false;
73      return;
74  }
75  WebDevToolsAgent::interruptAndDispatch(
76      new MessageImpl(message, current_routing_id_));
77
78  render_thread_loop_->PostTask(
79      FROM_HERE, base::Bind(&WebDevToolsAgent::processPendingMessages));
80}
81
82void DevToolsAgentFilter::AddEmbeddedWorkerRouteOnMainThread(int32 routing_id) {
83  io_message_loop_proxy_->PostTask(
84      FROM_HERE,
85      base::Bind(
86          &DevToolsAgentFilter::AddEmbeddedWorkerRoute, this, routing_id));
87}
88
89void DevToolsAgentFilter::RemoveEmbeddedWorkerRouteOnMainThread(
90    int32 routing_id) {
91  io_message_loop_proxy_->PostTask(
92      FROM_HERE,
93      base::Bind(
94          &DevToolsAgentFilter::RemoveEmbeddedWorkerRoute, this, routing_id));
95}
96
97void DevToolsAgentFilter::AddEmbeddedWorkerRoute(int32 routing_id) {
98  embedded_worker_routes_.insert(routing_id);
99}
100
101void DevToolsAgentFilter::RemoveEmbeddedWorkerRoute(int32 routing_id) {
102  embedded_worker_routes_.erase(routing_id);
103}
104
105}  // namespace content
106