Chromium Code Reviews| 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/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/public/browser/devtools_http_handler.h" | 13 #include "content/public/browser/devtools_http_handler.h" |
| 14 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 15 #include "content/public/browser/render_view_host.h" | 15 #include "content/public/browser/render_view_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/content_client.h" | 17 #include "content/public/common/content_client.h" |
| 18 #include "content/shell/browser/shell.h" | 18 #include "content/shell/browser/shell.h" |
| 19 #include "content/shell/browser/shell_browser_context.h" | 19 #include "content/shell/browser/shell_browser_context.h" |
| 20 #include "content/shell/browser/shell_browser_main_parts.h" | 20 #include "content/shell/browser/shell_browser_main_parts.h" |
| 21 #include "content/shell/browser/shell_content_browser_client.h" | 21 #include "content/shell/browser/shell_content_browser_client.h" |
| 22 #include "content/shell/browser/shell_devtools_manager_delegate.h" | 22 #include "content/shell/browser/shell_devtools_manager_delegate.h" |
| 23 #include "content/shell/browser/webkit_test_controller.h" | 23 #include "content/shell/browser/webkit_test_controller.h" |
| 24 #include "content/shell/common/shell_switches.h" | 24 #include "content/shell/common/shell_switches.h" |
| 25 #include "net/base/filename_util.h" | 25 #include "net/base/filename_util.h" |
| 26 #include "net/http/http_response_headers.h" | |
| 27 #include "net/url_request/url_fetcher.h" | |
| 26 | 28 |
| 27 namespace content { | 29 namespace content { |
| 28 | 30 |
| 29 // This constant should be in sync with | 31 // This constant should be in sync with |
| 30 // the constant at devtools_ui_bindings.cc. | 32 // the constant at devtools_ui_bindings.cc. |
| 31 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; | 33 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; |
| 32 | 34 |
| 33 // static | 35 // static |
| 34 ShellDevToolsFrontend* ShellDevToolsFrontend::Show( | 36 ShellDevToolsFrontend* ShellDevToolsFrontend::Show( |
| 35 WebContents* inspected_contents) { | 37 WebContents* inspected_contents) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 if (agent_host_) | 106 if (agent_host_) |
| 105 agent_host_->DetachClient(); | 107 agent_host_->DetachClient(); |
| 106 delete this; | 108 delete this; |
| 107 } | 109 } |
| 108 | 110 |
| 109 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( | 111 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| 110 const std::string& message) { | 112 const std::string& message) { |
| 111 if (!agent_host_) | 113 if (!agent_host_) |
| 112 return; | 114 return; |
| 113 std::string method; | 115 std::string method; |
| 114 int id = 0; | |
| 115 base::ListValue* params = NULL; | 116 base::ListValue* params = NULL; |
| 116 base::DictionaryValue* dict = NULL; | 117 base::DictionaryValue* dict = NULL; |
| 117 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message)); | 118 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message)); |
| 118 if (!parsed_message || | 119 if (!parsed_message || |
| 119 !parsed_message->GetAsDictionary(&dict) || | 120 !parsed_message->GetAsDictionary(&dict) || |
| 120 !dict->GetString("method", &method)) { | 121 !dict->GetString("method", &method)) { |
| 121 return; | 122 return; |
| 122 } | 123 } |
| 124 int id = 0; | |
| 125 dict->GetInteger("id", &id); | |
| 123 dict->GetList("params", ¶ms); | 126 dict->GetList("params", ¶ms); |
| 124 | 127 |
| 125 std::string browser_message; | 128 std::string browser_message; |
| 126 if (method == "sendMessageToBrowser" && params && | 129 if (method == "sendMessageToBrowser" && params && |
| 127 params->GetSize() == 1 && params->GetString(0, &browser_message)) { | 130 params->GetSize() == 1 && params->GetString(0, &browser_message)) { |
| 128 agent_host_->DispatchProtocolMessage(browser_message); | 131 agent_host_->DispatchProtocolMessage(browser_message); |
| 129 } else if (method == "loadCompleted") { | 132 } else if (method == "loadCompleted") { |
| 130 web_contents()->GetMainFrame()->ExecuteJavaScript( | 133 web_contents()->GetMainFrame()->ExecuteJavaScript( |
| 131 base::ASCIIToUTF16("DevToolsAPI.setUseSoftMenu(true);")); | 134 base::ASCIIToUTF16("DevToolsAPI.setUseSoftMenu(true);")); |
| 135 } else if (method == "loadNetworkResource" && params->GetSize() == 2) { | |
| 136 // TODO(pfeldman): handle some of the embedder messages in content. | |
| 137 std::string url; | |
| 138 std::string headers; | |
| 139 if (!params->GetString(0, &url) || | |
| 140 !params->GetString(1, &headers)) { | |
| 141 return; | |
| 142 } | |
| 143 GURL gurl(url); | |
| 144 if (!gurl.is_valid()) { | |
| 145 std::string code = base::StringPrintf( | |
| 146 "DevToolsAPI.embedderMessageAck(%d, { statusCode: 404 });", id); | |
| 147 web_contents()->GetMainFrame()->ExecuteJavaScript( | |
| 148 base::UTF8ToUTF16(code)); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 net::URLFetcher* fetcher = | |
| 153 net::URLFetcher::Create(gurl, net::URLFetcher::GET, this); | |
|
dgozman
2015/02/27 12:22:04
Delete pending fetchers in destructor.
pfeldman
2015/02/27 12:54:47
Done.
| |
| 154 pending_requests_[fetcher] = id; | |
| 155 fetcher->SetRequestContext(web_contents()->GetBrowserContext()-> | |
| 156 GetRequestContext()); | |
| 157 fetcher->SetExtraRequestHeaders(headers); | |
| 158 fetcher->Start(); | |
| 159 return; | |
| 132 } else { | 160 } else { |
| 133 return; | 161 return; |
| 134 } | 162 } |
| 135 | 163 |
| 136 dict->GetInteger("id", &id); | |
| 137 if (id) { | 164 if (id) { |
| 138 std::string code = "DevToolsAPI.embedderMessageAck(" + | 165 std::string code = "DevToolsAPI.embedderMessageAck(" + |
| 139 base::IntToString(id) + ",\"\");"; | 166 base::IntToString(id) + ",\"\");"; |
| 140 base::string16 javascript = base::UTF8ToUTF16(code); | 167 base::string16 javascript = base::UTF8ToUTF16(code); |
| 141 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); | 168 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); |
| 142 } | 169 } |
| 143 } | 170 } |
| 144 | 171 |
| 145 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend( | 172 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend( |
| 146 const std::string& message) { | 173 const std::string& message) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 162 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { | 189 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { |
| 163 base::StringValue message_value(message.substr(pos, kMaxMessageChunkSize)); | 190 base::StringValue message_value(message.substr(pos, kMaxMessageChunkSize)); |
| 164 std::string param; | 191 std::string param; |
| 165 base::JSONWriter::Write(&message_value, ¶m); | 192 base::JSONWriter::Write(&message_value, ¶m); |
| 166 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + ");"; | 193 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + ");"; |
| 167 base::string16 javascript = base::UTF8ToUTF16(code); | 194 base::string16 javascript = base::UTF8ToUTF16(code); |
| 168 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); | 195 web_contents()->GetMainFrame()->ExecuteJavaScript(javascript); |
| 169 } | 196 } |
| 170 } | 197 } |
| 171 | 198 |
| 199 void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 200 // TODO(pfeldman): this is a copy of chrome's devtools_ui_bindings.cc. | |
| 201 // We should handle some of the commands including this one in content. | |
| 202 DCHECK(source); | |
| 203 PendingRequestsMap::iterator it = pending_requests_.find(source); | |
| 204 DCHECK(it != pending_requests_.end()); | |
| 205 | |
| 206 std::string body; | |
| 207 source->GetResponseAsString(&body); | |
| 208 | |
| 209 base::DictionaryValue response; | |
| 210 base::DictionaryValue* headers = new base::DictionaryValue(); | |
| 211 net::HttpResponseHeaders* rh = source->GetResponseHeaders(); | |
| 212 response.SetInteger("statusCode", rh ? rh->response_code() : 200); | |
| 213 response.Set("headers", headers); | |
| 214 response.SetString("body", body); | |
| 215 | |
| 216 void* iterator = NULL; | |
| 217 std::string name; | |
| 218 std::string value; | |
| 219 while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value)) | |
| 220 headers->SetString(name, value); | |
| 221 | |
| 222 std::string json; | |
| 223 base::JSONWriter::Write(&response, &json); | |
| 224 | |
| 225 std::string message = base::StringPrintf( | |
| 226 "DevToolsAPI.embedderMessageAck(%d, %s)", | |
| 227 it->second, | |
| 228 json.c_str()); | |
| 229 web_contents()->GetMainFrame()-> | |
| 230 ExecuteJavaScript(base::UTF8ToUTF16(message)); | |
| 231 | |
| 232 pending_requests_.erase(it); | |
| 233 } | |
| 234 | |
| 172 void ShellDevToolsFrontend::AttachTo(WebContents* inspected_contents) { | 235 void ShellDevToolsFrontend::AttachTo(WebContents* inspected_contents) { |
| 173 DisconnectFromTarget(); | 236 DisconnectFromTarget(); |
| 174 agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents); | 237 agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents); |
| 175 } | 238 } |
| 176 | 239 |
| 177 void ShellDevToolsFrontend::AgentHostClosed( | 240 void ShellDevToolsFrontend::AgentHostClosed( |
| 178 DevToolsAgentHost* agent_host, bool replaced) { | 241 DevToolsAgentHost* agent_host, bool replaced) { |
| 179 frontend_shell_->Close(); | 242 frontend_shell_->Close(); |
| 180 } | 243 } |
| 181 | 244 |
| 182 } // namespace content | 245 } // namespace content |
| OLD | NEW |