summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/devtools_agent.cc
blob: d1e81e2aa2b78ea9d183da4155449347c0ef7a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/devtools_agent.h"

#include "chrome/common/devtools_messages.h"
#include "chrome/common/render_messages.h"
#include "chrome/renderer/devtools_agent_filter.h"
#include "chrome/renderer/render_view.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDevToolsAgent.h"
#include "third_party/WebKit/WebKit/chromium/public/WebPoint.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
#include "webkit/glue/glue_util.h"

using WebKit::WebDevToolsAgent;
using WebKit::WebPoint;
using WebKit::WebString;
using WebKit::WebView;

// static
std::map<int, DevToolsAgent*> DevToolsAgent::agent_for_routing_id_;

DevToolsAgent::DevToolsAgent(int routing_id, RenderView* render_view)
    : routing_id_(routing_id),
      render_view_(render_view) {
  agent_for_routing_id_[routing_id] = this;
}

DevToolsAgent::~DevToolsAgent() {
  agent_for_routing_id_.erase(routing_id_);
}

void DevToolsAgent::OnNavigate() {
  WebDevToolsAgent* web_agent = GetWebAgent();
  if (web_agent) {
    web_agent->didNavigate();
  }
}

// Called on the Renderer thread.
bool DevToolsAgent::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(DevToolsAgent, message)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Attach, OnAttach)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Detach, OnDetach)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_RpcMessage, OnRpcMessage)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_InspectElement, OnInspectElement)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_SetApuAgentEnabled,
                        OnSetApuAgentEnabled)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void DevToolsAgent::sendMessageToFrontend(const WebString& class_name,
                                          const WebString& method_name,
                                          const WebString& param1,
                                          const WebString& param2,
                                          const WebString& param3) {
  IPC::Message* m = new ViewHostMsg_ForwardToDevToolsClient(
      routing_id_,
      DevToolsClientMsg_RpcMessage(
          class_name.utf8(),
          method_name.utf8(),
          param1.utf8(),
          param2.utf8(),
          param3.utf8()));
  render_view_->Send(m);
}

int DevToolsAgent::hostIdentifier() {
  return routing_id_;
}

void DevToolsAgent::forceRepaint() {
  render_view_->GenerateFullRepaint();
}

void DevToolsAgent::runtimeFeatureStateChanged(const WebKit::WebString& feature,
                                               bool enabled) {
  render_view_->Send(new ViewHostMsg_DevToolsRuntimeFeatureStateChanged(
      routing_id_,
      feature.utf8(),
      enabled));
}

// static
DevToolsAgent* DevToolsAgent::FromHostId(int host_id) {
  std::map<int, DevToolsAgent*>::iterator it =
      agent_for_routing_id_.find(host_id);
  if (it != agent_for_routing_id_.end()) {
    return it->second;
  }
  return NULL;
}

void DevToolsAgent::OnAttach(const std::vector<std::string>& runtime_features) {
  WebDevToolsAgent* web_agent = GetWebAgent();
  if (web_agent) {
    web_agent->attach();
    for (std::vector<std::string>::const_iterator it = runtime_features.begin();
         it != runtime_features.end(); ++it) {
      web_agent->setRuntimeFeatureEnabled(WebString::fromUTF8(*it), true);
    }
  }
}

void DevToolsAgent::OnDetach() {
  WebDevToolsAgent* web_agent = GetWebAgent();
  if (web_agent) {
    web_agent->detach();
  }
}

void DevToolsAgent::OnRpcMessage(const std::string& class_name,
                                 const std::string& method_name,
                                 const std::string& param1,
                                 const std::string& param2,
                                 const std::string& param3) {
  WebDevToolsAgent* web_agent = GetWebAgent();
  if (web_agent) {
    web_agent->dispatchMessageFromFrontend(
        WebString::fromUTF8(class_name),
        WebString::fromUTF8(method_name),
        WebString::fromUTF8(param1),
        WebString::fromUTF8(param2),
        WebString::fromUTF8(param3));
  }
}

void DevToolsAgent::OnInspectElement(int x, int y) {
  WebDevToolsAgent* web_agent = GetWebAgent();
  if (web_agent) {
    web_agent->attach();
    web_agent->inspectElementAt(WebPoint(x, y));
  }
}

void DevToolsAgent::OnSetApuAgentEnabled(bool enabled) {
  WebDevToolsAgent* web_agent = GetWebAgent();
  if (web_agent) {
    web_agent->setRuntimeFeatureEnabled(
        webkit_glue::StdStringToWebString("apu-agent"),
        enabled);
  }
}

WebDevToolsAgent* DevToolsAgent::GetWebAgent() {
  WebView* web_view = render_view_->webview();
  if (!web_view)
    return NULL;
  return web_view->devToolsAgent();
}

// static
void WebKit::WebDevToolsAgentClient::sendMessageToFrontendOnIOThread(
    const WebString& class_name,
    const WebString& method_name,
    const WebString& param1,
    const WebString& param2,
    const WebString& param3) {
    DevToolsAgentFilter::SendRpcMessage(
        class_name.utf8(),
        method_name.utf8(),
        param1.utf8(),
        param2.utf8(),
        param3.utf8());
}