[DevTools] Fixed double delete of ShellDevToolsDelegate
[chromium-blink-merge.git] / content / shell / browser / shell_devtools_frontend.cc
blob01bc22ab926f3b8a2dca3d9e73793e617bd2aa85
1 // Copyright 2013 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.
5 #include "content/shell/browser/shell_devtools_frontend.h"
7 #include "base/command_line.h"
8 #include "base/json/json_reader.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/devtools_http_handler.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_client.h"
18 #include "content/shell/browser/shell.h"
19 #include "content/shell/browser/shell_browser_context.h"
20 #include "content/shell/browser/shell_browser_main_parts.h"
21 #include "content/shell/browser/shell_content_browser_client.h"
22 #include "content/shell/browser/shell_devtools_manager_delegate.h"
23 #include "content/shell/browser/webkit_test_controller.h"
24 #include "content/shell/common/shell_switches.h"
25 #include "net/base/filename_util.h"
27 namespace content {
29 // static
30 ShellDevToolsFrontend* ShellDevToolsFrontend::Show(
31 WebContents* inspected_contents) {
32 scoped_refptr<DevToolsAgentHost> agent(
33 DevToolsAgentHost::GetOrCreateFor(inspected_contents));
34 Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(),
35 GURL(),
36 NULL,
37 MSG_ROUTING_NONE,
38 gfx::Size());
39 ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend(
40 shell,
41 agent.get());
43 DevToolsHttpHandler* http_handler = ShellContentBrowserClient::Get()
44 ->shell_browser_main_parts()
45 ->devtools_http_handler();
46 shell->LoadURL(http_handler->GetFrontendURL());
48 return devtools_frontend;
51 void ShellDevToolsFrontend::Activate() {
52 frontend_shell_->ActivateContents(web_contents());
55 void ShellDevToolsFrontend::Focus() {
56 web_contents()->Focus();
59 void ShellDevToolsFrontend::InspectElementAt(int x, int y) {
60 agent_host_->InspectElement(x, y);
63 void ShellDevToolsFrontend::Close() {
64 frontend_shell_->Close();
67 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
68 DevToolsAgentHost* agent_host)
69 : WebContentsObserver(frontend_shell->web_contents()),
70 frontend_shell_(frontend_shell),
71 agent_host_(agent_host) {
74 ShellDevToolsFrontend::~ShellDevToolsFrontend() {
77 void ShellDevToolsFrontend::RenderViewCreated(
78 RenderViewHost* render_view_host) {
79 if (!frontend_host_) {
80 frontend_host_.reset(DevToolsFrontendHost::Create(render_view_host, this));
81 agent_host_->AttachClient(this);
85 void ShellDevToolsFrontend::DocumentOnLoadCompletedInMainFrame() {
86 web_contents()->GetMainFrame()->ExecuteJavaScript(
87 base::ASCIIToUTF16("InspectorFrontendAPI.setUseSoftMenu(true);"));
90 void ShellDevToolsFrontend::WebContentsDestroyed() {
91 agent_host_->DetachClient();
92 delete this;
95 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(
96 const std::string& message) {
97 std::string method;
98 std::string browser_message;
99 int id = 0;
101 base::ListValue* params = NULL;
102 base::DictionaryValue* dict = NULL;
103 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message));
104 if (!parsed_message ||
105 !parsed_message->GetAsDictionary(&dict) ||
106 !dict->GetString("method", &method) ||
107 !dict->GetList("params", &params)) {
108 return;
111 if (method != "sendMessageToBrowser" ||
112 params->GetSize() != 1 ||
113 !params->GetString(0, &browser_message)) {
114 return;
116 dict->GetInteger("id", &id);
118 agent_host_->DispatchProtocolMessage(browser_message);
120 if (id) {
121 std::string code = "InspectorFrontendAPI.embedderMessageAck(" +
122 base::IntToString(id) + ",\"\");";
123 base::string16 javascript = base::UTF8ToUTF16(code);
124 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
128 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend(
129 const std::string& message) {
130 agent_host_->DispatchProtocolMessage(message);
133 void ShellDevToolsFrontend::DispatchProtocolMessage(
134 DevToolsAgentHost* agent_host, const std::string& message) {
135 std::string code = "InspectorFrontendAPI.dispatchMessage(" + message + ");";
136 base::string16 javascript = base::UTF8ToUTF16(code);
137 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
140 void ShellDevToolsFrontend::AgentHostClosed(
141 DevToolsAgentHost* agent_host, bool replaced) {
142 frontend_shell_->Close();
145 } // namespace content