OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/shell/browser/shell_devtools_frontend.h" | 5 #include "content/shell/browser/shell_devtools_frontend.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/debug/stack_trace.h" | |
jochen (gone - plz use gerrit)
2015/02/03 11:28:35
not needed?
pfeldman
2015/02/03 11:34:38
Done.
| |
8 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "content/public/browser/devtools_http_handler.h" | 14 #include "content/public/browser/devtools_http_handler.h" |
14 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
15 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/content_client.h" | 18 #include "content/public/common/content_client.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 | 54 |
54 void ShellDevToolsFrontend::Activate() { | 55 void ShellDevToolsFrontend::Activate() { |
55 frontend_shell_->ActivateContents(web_contents()); | 56 frontend_shell_->ActivateContents(web_contents()); |
56 } | 57 } |
57 | 58 |
58 void ShellDevToolsFrontend::Focus() { | 59 void ShellDevToolsFrontend::Focus() { |
59 web_contents()->Focus(); | 60 web_contents()->Focus(); |
60 } | 61 } |
61 | 62 |
62 void ShellDevToolsFrontend::InspectElementAt(int x, int y) { | 63 void ShellDevToolsFrontend::InspectElementAt(int x, int y) { |
63 agent_host_->InspectElement(x, y); | 64 if (agent_host_) |
65 agent_host_->InspectElement(x, y); | |
64 } | 66 } |
65 | 67 |
66 void ShellDevToolsFrontend::Close() { | 68 void ShellDevToolsFrontend::Close() { |
67 frontend_shell_->Close(); | 69 frontend_shell_->Close(); |
68 } | 70 } |
69 | 71 |
72 void ShellDevToolsFrontend::SwitchToTarget(WebContents* inspected_contents) { | |
73 DisconnectFromTarget(); | |
74 frontend_shell_->Reload(); | |
75 agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents); | |
76 agent_host_->AttachClient(this); | |
77 } | |
78 | |
79 void ShellDevToolsFrontend::DisconnectFromTarget() { | |
80 if (!agent_host_) | |
81 return; | |
82 agent_host_->DetachClient(); | |
83 agent_host_ = nullptr; | |
84 } | |
85 | |
70 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell, | 86 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell, |
71 DevToolsAgentHost* agent_host) | 87 DevToolsAgentHost* agent_host) |
72 : WebContentsObserver(frontend_shell->web_contents()), | 88 : WebContentsObserver(frontend_shell->web_contents()), |
73 frontend_shell_(frontend_shell), | 89 frontend_shell_(frontend_shell), |
74 agent_host_(agent_host) { | 90 agent_host_(agent_host) { |
75 } | 91 } |
76 | 92 |
77 ShellDevToolsFrontend::~ShellDevToolsFrontend() { | 93 ShellDevToolsFrontend::~ShellDevToolsFrontend() { |
78 } | 94 } |
79 | 95 |
80 void ShellDevToolsFrontend::RenderViewCreated( | 96 void ShellDevToolsFrontend::RenderViewCreated( |
81 RenderViewHost* render_view_host) { | 97 RenderViewHost* render_view_host) { |
82 if (!frontend_host_) { | 98 if (!frontend_host_) { |
83 frontend_host_.reset( | 99 frontend_host_.reset( |
84 DevToolsFrontendHost::Create(web_contents()->GetMainFrame(), this)); | 100 DevToolsFrontendHost::Create(web_contents()->GetMainFrame(), this)); |
85 agent_host_->AttachClient(this); | 101 if (agent_host_) |
102 agent_host_->AttachClient(this); | |
86 } | 103 } |
87 } | 104 } |
88 | 105 |
89 void ShellDevToolsFrontend::WebContentsDestroyed() { | 106 void ShellDevToolsFrontend::WebContentsDestroyed() { |
90 agent_host_->DetachClient(); | 107 if (agent_host_) |
108 agent_host_->DetachClient(); | |
91 delete this; | 109 delete this; |
92 } | 110 } |
93 | 111 |
94 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( | 112 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
95 const std::string& message) { | 113 const std::string& message) { |
114 if (!agent_host_) | |
115 return; | |
96 std::string method; | 116 std::string method; |
97 int id = 0; | 117 int id = 0; |
98 base::ListValue* params = NULL; | 118 base::ListValue* params = NULL; |
99 base::DictionaryValue* dict = NULL; | 119 base::DictionaryValue* dict = NULL; |
100 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message)); | 120 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message)); |
101 if (!parsed_message || | 121 if (!parsed_message || |
102 !parsed_message->GetAsDictionary(&dict) || | 122 !parsed_message->GetAsDictionary(&dict) || |
103 !dict->GetString("method", &method)) { | 123 !dict->GetString("method", &method)) { |
104 return; | 124 return; |
105 } | 125 } |
(...skipping 14 matching lines...) Expand all Loading... | |
120 if (id) { | 140 if (id) { |
121 std::string code = "DevToolsAPI.embedderMessageAck(" + | 141 std::string code = "DevToolsAPI.embedderMessageAck(" + |
122 base::IntToString(id) + ",\"\");"; | 142 base::IntToString(id) + ",\"\");"; |
123 base::string16 javascript = base::UTF8ToUTF16(code); | 143 base::string16 javascript = base::UTF8ToUTF16(code); |
124 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); | 144 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); |
125 } | 145 } |
126 } | 146 } |
127 | 147 |
128 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend( | 148 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend( |
129 const std::string& message) { | 149 const std::string& message) { |
130 agent_host_->DispatchProtocolMessage(message); | 150 if (agent_host_) |
151 agent_host_->DispatchProtocolMessage(message); | |
131 } | 152 } |
132 | 153 |
133 void ShellDevToolsFrontend::DispatchProtocolMessage( | 154 void ShellDevToolsFrontend::DispatchProtocolMessage( |
134 DevToolsAgentHost* agent_host, const std::string& message) { | 155 DevToolsAgentHost* agent_host, const std::string& message) { |
135 | 156 |
136 if (message.length() < kMaxMessageChunkSize) { | 157 if (message.length() < kMaxMessageChunkSize) { |
137 base::string16 javascript = base::UTF8ToUTF16( | 158 base::string16 javascript = base::UTF8ToUTF16( |
138 "DevToolsAPI.dispatchMessage(" + message + ");"); | 159 "DevToolsAPI.dispatchMessage(" + message + ");"); |
139 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); | 160 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); |
140 return; | 161 return; |
141 } | 162 } |
142 | 163 |
143 base::FundamentalValue total_size(static_cast<int>(message.length())); | 164 base::FundamentalValue total_size(static_cast<int>(message.length())); |
144 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { | 165 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { |
145 base::StringValue message_value(message.substr(pos, kMaxMessageChunkSize)); | 166 base::StringValue message_value(message.substr(pos, kMaxMessageChunkSize)); |
146 std::string param; | 167 std::string param; |
147 base::JSONWriter::Write(&message_value, ¶m); | 168 base::JSONWriter::Write(&message_value, ¶m); |
148 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + ");"; | 169 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + ");"; |
149 base::string16 javascript = base::UTF8ToUTF16(code); | 170 base::string16 javascript = base::UTF8ToUTF16(code); |
150 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); | 171 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); |
151 } | 172 } |
152 } | 173 } |
153 | 174 |
154 void ShellDevToolsFrontend::AgentHostClosed( | 175 void ShellDevToolsFrontend::AgentHostClosed( |
155 DevToolsAgentHost* agent_host, bool replaced) { | 176 DevToolsAgentHost* agent_host, bool replaced) { |
156 frontend_shell_->Close(); | 177 frontend_shell_->Close(); |
157 } | 178 } |
158 | 179 |
159 } // namespace content | 180 } // namespace content |
OLD | NEW |