devtools_client.cc revision f2477e01787aa58f445919b809d89e252beef54f
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_client.h"
6
7#include "base/command_line.h"
8#include "base/message_loop/message_loop.h"
9#include "base/strings/utf_string_conversions.h"
10#include "content/common/devtools_messages.h"
11#include "content/public/common/content_switches.h"
12#include "content/public/common/url_constants.h"
13#include "content/renderer/render_thread_impl.h"
14#include "content/renderer/render_view_impl.h"
15#include "third_party/WebKit/public/platform/WebFloatPoint.h"
16#include "third_party/WebKit/public/platform/WebString.h"
17#include "third_party/WebKit/public/web/WebDevToolsFrontend.h"
18#include "ui/base/ui_base_switches.h"
19
20using blink::WebDevToolsFrontend;
21using blink::WebString;
22
23namespace content {
24
25DevToolsClient::DevToolsClient(RenderViewImpl* render_view)
26    : RenderViewObserver(render_view) {
27  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
28  web_tools_frontend_.reset(
29      WebDevToolsFrontend::create(
30          render_view->webview(),
31          this,
32          ASCIIToUTF16(command_line.GetSwitchValueASCII(switches::kLang))));
33}
34
35DevToolsClient::~DevToolsClient() {
36}
37
38bool DevToolsClient::OnMessageReceived(const IPC::Message& message) {
39  DCHECK(RenderThreadImpl::current());
40
41  bool handled = true;
42  IPC_BEGIN_MESSAGE_MAP(DevToolsClient, message)
43    IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend,
44                        OnDispatchOnInspectorFrontend)
45    IPC_MESSAGE_UNHANDLED(handled = false);
46  IPC_END_MESSAGE_MAP()
47
48  return handled;
49}
50
51void DevToolsClient::sendMessageToBackend(const WebString& message)  {
52  Send(new DevToolsAgentMsg_DispatchOnInspectorBackend(routing_id(),
53                                                       message.utf8()));
54}
55
56void DevToolsClient::sendMessageToEmbedder(const WebString& message) {
57  Send(new DevToolsHostMsg_DispatchOnEmbedder(routing_id(),
58                                              message.utf8()));
59}
60
61bool DevToolsClient::isUnderTest() {
62  return RenderThreadImpl::current()->layout_test_mode();
63}
64
65void DevToolsClient::OnDispatchOnInspectorFrontend(const std::string& message) {
66  web_tools_frontend_->dispatchOnInspectorFrontend(
67      WebString::fromUTF8(message));
68}
69
70}  // namespace content
71