| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_bindings.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/json/string_escape.h" | 11 #include "base/json/string_escape.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 32 #include "net/url_request/url_fetcher_response_writer.h" | 32 #include "net/url_request/url_fetcher_response_writer.h" |
| 33 | 33 |
| 34 #if !defined(OS_ANDROID) | 34 #if !defined(OS_ANDROID) |
| 35 #include "content/public/browser/devtools_frontend_host.h" | 35 #include "content/public/browser/devtools_frontend_host.h" |
| 36 #endif | 36 #endif |
| 37 | 37 |
| 38 namespace content { | 38 namespace content { |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 | |
| 43 // ResponseWriter ------------------------------------------------------------- | 42 // ResponseWriter ------------------------------------------------------------- |
| 44 | 43 |
| 45 class ResponseWriter : public net::URLFetcherResponseWriter { | 44 class ResponseWriter : public net::URLFetcherResponseWriter { |
| 46 public: | 45 public: |
| 47 ResponseWriter(base::WeakPtr<ShellDevToolsFrontend> shell_devtools_, | 46 ResponseWriter(base::WeakPtr<ShellDevToolsBindings> devtools_bindings_, |
| 48 int stream_id); | 47 int stream_id); |
| 49 ~ResponseWriter() override; | 48 ~ResponseWriter() override; |
| 50 | 49 |
| 51 // URLFetcherResponseWriter overrides: | 50 // URLFetcherResponseWriter overrides: |
| 52 int Initialize(const net::CompletionCallback& callback) override; | 51 int Initialize(const net::CompletionCallback& callback) override; |
| 53 int Write(net::IOBuffer* buffer, | 52 int Write(net::IOBuffer* buffer, |
| 54 int num_bytes, | 53 int num_bytes, |
| 55 const net::CompletionCallback& callback) override; | 54 const net::CompletionCallback& callback) override; |
| 56 int Finish(int net_error, const net::CompletionCallback& callback) override; | 55 int Finish(int net_error, const net::CompletionCallback& callback) override; |
| 57 | 56 |
| 58 private: | 57 private: |
| 59 base::WeakPtr<ShellDevToolsFrontend> shell_devtools_; | 58 base::WeakPtr<ShellDevToolsBindings> devtools_bindings_; |
| 60 int stream_id_; | 59 int stream_id_; |
| 61 | 60 |
| 62 DISALLOW_COPY_AND_ASSIGN(ResponseWriter); | 61 DISALLOW_COPY_AND_ASSIGN(ResponseWriter); |
| 63 }; | 62 }; |
| 64 | 63 |
| 65 ResponseWriter::ResponseWriter( | 64 ResponseWriter::ResponseWriter( |
| 66 base::WeakPtr<ShellDevToolsFrontend> shell_devtools, | 65 base::WeakPtr<ShellDevToolsBindings> shell_devtools, |
| 67 int stream_id) | 66 int stream_id) |
| 68 : shell_devtools_(shell_devtools), | 67 : devtools_bindings_(shell_devtools), stream_id_(stream_id) {} |
| 69 stream_id_(stream_id) { | |
| 70 } | |
| 71 | 68 |
| 72 ResponseWriter::~ResponseWriter() { | 69 ResponseWriter::~ResponseWriter() {} |
| 73 } | |
| 74 | 70 |
| 75 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { | 71 int ResponseWriter::Initialize(const net::CompletionCallback& callback) { |
| 76 return net::OK; | 72 return net::OK; |
| 77 } | 73 } |
| 78 | 74 |
| 79 int ResponseWriter::Write(net::IOBuffer* buffer, | 75 int ResponseWriter::Write(net::IOBuffer* buffer, |
| 80 int num_bytes, | 76 int num_bytes, |
| 81 const net::CompletionCallback& callback) { | 77 const net::CompletionCallback& callback) { |
| 82 std::string chunk = std::string(buffer->data(), num_bytes); | 78 std::string chunk = std::string(buffer->data(), num_bytes); |
| 83 if (!base::IsStringUTF8(chunk)) | 79 if (!base::IsStringUTF8(chunk)) |
| 84 return num_bytes; | 80 return num_bytes; |
| 85 | 81 |
| 86 base::Value* id = new base::Value(stream_id_); | 82 base::Value* id = new base::Value(stream_id_); |
| 87 base::Value* chunkValue = new base::Value(chunk); | 83 base::Value* chunkValue = new base::Value(chunk); |
| 88 | 84 |
| 89 content::BrowserThread::PostTask( | 85 content::BrowserThread::PostTask( |
| 90 content::BrowserThread::UI, FROM_HERE, | 86 content::BrowserThread::UI, FROM_HERE, |
| 91 base::Bind(&ShellDevToolsFrontend::CallClientFunction, | 87 base::Bind(&ShellDevToolsBindings::CallClientFunction, devtools_bindings_, |
| 92 shell_devtools_, "DevToolsAPI.streamWrite", | 88 "DevToolsAPI.streamWrite", base::Owned(id), |
| 93 base::Owned(id), base::Owned(chunkValue), nullptr)); | 89 base::Owned(chunkValue), nullptr)); |
| 94 return num_bytes; | 90 return num_bytes; |
| 95 } | 91 } |
| 96 | 92 |
| 97 int ResponseWriter::Finish(int net_error, | 93 int ResponseWriter::Finish(int net_error, |
| 98 const net::CompletionCallback& callback) { | 94 const net::CompletionCallback& callback) { |
| 99 return net::OK; | 95 return net::OK; |
| 100 } | 96 } |
| 101 | 97 |
| 102 static GURL GetFrontendURL() { | |
| 103 int port = ShellDevToolsManagerDelegate::GetHttpHandlerPort(); | |
| 104 return GURL( | |
| 105 base::StringPrintf("http://127.0.0.1:%d/devtools/inspector.html", port)); | |
| 106 } | |
| 107 | |
| 108 } // namespace | 98 } // namespace |
| 109 | 99 |
| 110 // This constant should be in sync with | 100 // This constant should be in sync with |
| 111 // the constant at devtools_ui_bindings.cc. | 101 // the constant at devtools_ui_bindings.cc. |
| 112 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; | 102 const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; |
| 113 | 103 |
| 114 // static | 104 void ShellDevToolsBindings::InspectElementAt(int x, int y) { |
| 115 ShellDevToolsFrontend* ShellDevToolsFrontend::Show( | |
| 116 WebContents* inspected_contents) { | |
| 117 Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(), | |
| 118 GURL(), | |
| 119 NULL, | |
| 120 gfx::Size()); | |
| 121 ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend( | |
| 122 shell, | |
| 123 inspected_contents); | |
| 124 shell->LoadURL(GetFrontendURL()); | |
| 125 return devtools_frontend; | |
| 126 } | |
| 127 | |
| 128 void ShellDevToolsFrontend::Activate() { | |
| 129 frontend_shell_->ActivateContents(web_contents()); | |
| 130 } | |
| 131 | |
| 132 void ShellDevToolsFrontend::Focus() { | |
| 133 web_contents()->Focus(); | |
| 134 } | |
| 135 | |
| 136 void ShellDevToolsFrontend::InspectElementAt(int x, int y) { | |
| 137 if (agent_host_) { | 105 if (agent_host_) { |
| 138 agent_host_->InspectElement(this, x, y); | 106 agent_host_->InspectElement(this, x, y); |
| 139 } else { | 107 } else { |
| 140 inspect_element_at_x_ = x; | 108 inspect_element_at_x_ = x; |
| 141 inspect_element_at_y_ = y; | 109 inspect_element_at_y_ = y; |
| 142 } | 110 } |
| 143 } | 111 } |
| 144 | 112 |
| 145 void ShellDevToolsFrontend::Close() { | 113 ShellDevToolsBindings::ShellDevToolsBindings(WebContents* devtools_contents, |
| 146 frontend_shell_->Close(); | 114 WebContents* inspected_contents, |
| 115 ShellDevToolsDelegate* delegate) |
| 116 : WebContentsObserver(devtools_contents), |
| 117 inspected_contents_(inspected_contents), |
| 118 delegate_(delegate), |
| 119 inspect_element_at_x_(-1), |
| 120 inspect_element_at_y_(-1), |
| 121 weak_factory_(this) {} |
| 122 |
| 123 ShellDevToolsBindings::~ShellDevToolsBindings() { |
| 124 for (const auto& pair : pending_requests_) |
| 125 delete pair.first; |
| 126 if (agent_host_) |
| 127 agent_host_->DetachClient(this); |
| 147 } | 128 } |
| 148 | 129 |
| 149 void ShellDevToolsFrontend::DisconnectFromTarget() { | 130 void ShellDevToolsBindings::RenderViewCreated( |
| 150 if (!agent_host_) | 131 RenderViewHost* render_view_host) { |
| 151 return; | 132 CreateFrontendHost(); |
| 152 agent_host_->DetachClient(this); | |
| 153 agent_host_ = NULL; | |
| 154 } | |
| 155 | |
| 156 ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell, | |
| 157 WebContents* inspected_contents) | |
| 158 : WebContentsObserver(frontend_shell->web_contents()), | |
| 159 frontend_shell_(frontend_shell), | |
| 160 inspected_contents_(inspected_contents), | |
| 161 inspect_element_at_x_(-1), | |
| 162 inspect_element_at_y_(-1), | |
| 163 weak_factory_(this) { | |
| 164 } | |
| 165 | |
| 166 ShellDevToolsFrontend::~ShellDevToolsFrontend() { | |
| 167 for (const auto& pair : pending_requests_) | |
| 168 delete pair.first; | |
| 169 } | 133 } |
| 170 | 134 |
| 171 #if !defined(OS_ANDROID) | 135 #if !defined(OS_ANDROID) |
| 172 void ShellDevToolsFrontend::RenderViewCreated( | 136 void ShellDevToolsBindings::CreateFrontendHost() { |
| 173 RenderViewHost* render_view_host) { | |
| 174 if (!frontend_host_) { | 137 if (!frontend_host_) { |
| 175 frontend_host_.reset(DevToolsFrontendHost::Create( | 138 frontend_host_.reset(DevToolsFrontendHost::Create( |
| 176 web_contents()->GetMainFrame(), | 139 web_contents()->GetMainFrame(), |
| 177 base::Bind(&ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend, | 140 base::Bind(&ShellDevToolsBindings::HandleMessageFromDevToolsFrontend, |
| 178 base::Unretained(this)))); | 141 base::Unretained(this)))); |
| 179 } | 142 } |
| 180 } | 143 } |
| 181 #endif | 144 #endif |
| 182 | 145 |
| 183 #if defined(OS_ANDROID) | 146 #if defined(OS_ANDROID) |
| 184 void ShellDevToolsFrontend::RenderViewCreated( | 147 void ShellDevToolsBindings::CreateFrontendHost() {} |
| 185 RenderViewHost* render_view_host) { | |
| 186 // No devtools frontend for android | |
| 187 } | |
| 188 #endif | 148 #endif |
| 189 | 149 |
| 190 void ShellDevToolsFrontend::DocumentAvailableInMainFrame() { | 150 void ShellDevToolsBindings::DocumentAvailableInMainFrame() { |
| 191 agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents_); | 151 agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents_); |
| 192 agent_host_->AttachClient(this); | 152 agent_host_->AttachClient(this); |
| 193 if (inspect_element_at_x_ != -1) { | 153 if (inspect_element_at_x_ != -1) { |
| 194 agent_host_->InspectElement( | 154 agent_host_->InspectElement(this, inspect_element_at_x_, |
| 195 this, inspect_element_at_x_, inspect_element_at_y_); | 155 inspect_element_at_y_); |
| 196 inspect_element_at_x_ = -1; | 156 inspect_element_at_x_ = -1; |
| 197 inspect_element_at_y_ = -1; | 157 inspect_element_at_y_ = -1; |
| 198 } | 158 } |
| 199 } | 159 } |
| 200 | 160 |
| 201 void ShellDevToolsFrontend::WebContentsDestroyed() { | 161 void ShellDevToolsBindings::WebContentsDestroyed() { |
| 202 if (agent_host_) | 162 if (agent_host_) |
| 203 agent_host_->DetachClient(this); | 163 agent_host_->DetachClient(this); |
| 204 delete this; | |
| 205 } | 164 } |
| 206 | 165 |
| 207 void ShellDevToolsFrontend::SetPreferences(const std::string& json) { | 166 void ShellDevToolsBindings::SetPreferences(const std::string& json) { |
| 208 preferences_.Clear(); | 167 preferences_.Clear(); |
| 209 if (json.empty()) | 168 if (json.empty()) |
| 210 return; | 169 return; |
| 211 base::DictionaryValue* dict = nullptr; | 170 base::DictionaryValue* dict = nullptr; |
| 212 std::unique_ptr<base::Value> parsed = base::JSONReader::Read(json); | 171 std::unique_ptr<base::Value> parsed = base::JSONReader::Read(json); |
| 213 if (!parsed || !parsed->GetAsDictionary(&dict)) | 172 if (!parsed || !parsed->GetAsDictionary(&dict)) |
| 214 return; | 173 return; |
| 215 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 174 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| 216 if (!it.value().IsType(base::Value::Type::STRING)) | 175 if (!it.value().IsType(base::Value::Type::STRING)) |
| 217 continue; | 176 continue; |
| 218 preferences_.SetWithoutPathExpansion(it.key(), it.value().CreateDeepCopy()); | 177 preferences_.SetWithoutPathExpansion(it.key(), it.value().CreateDeepCopy()); |
| 219 } | 178 } |
| 220 } | 179 } |
| 221 | 180 |
| 222 void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( | 181 void ShellDevToolsBindings::HandleMessageFromDevToolsFrontend( |
| 223 const std::string& message) { | 182 const std::string& message) { |
| 224 if (!agent_host_) | 183 if (!agent_host_) |
| 225 return; | 184 return; |
| 226 std::string method; | 185 std::string method; |
| 227 base::ListValue* params = NULL; | 186 base::ListValue* params = NULL; |
| 228 base::DictionaryValue* dict = NULL; | 187 base::DictionaryValue* dict = NULL; |
| 229 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); | 188 std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); |
| 230 if (!parsed_message || | 189 if (!parsed_message || !parsed_message->GetAsDictionary(&dict) || |
| 231 !parsed_message->GetAsDictionary(&dict) || | |
| 232 !dict->GetString("method", &method)) { | 190 !dict->GetString("method", &method)) { |
| 233 return; | 191 return; |
| 234 } | 192 } |
| 235 int request_id = 0; | 193 int request_id = 0; |
| 236 dict->GetInteger("id", &request_id); | 194 dict->GetInteger("id", &request_id); |
| 237 dict->GetList("params", ¶ms); | 195 dict->GetList("params", ¶ms); |
| 238 | 196 |
| 239 if (method == "dispatchProtocolMessage" && params && params->GetSize() == 1) { | 197 if (method == "dispatchProtocolMessage" && params && params->GetSize() == 1) { |
| 240 if (!agent_host_ || !agent_host_->IsAttached()) | 198 if (!agent_host_ || !agent_host_->IsAttached()) |
| 241 return; | 199 return; |
| 242 std::string protocol_message; | 200 std::string protocol_message; |
| 243 if (!params->GetString(0, &protocol_message)) | 201 if (!params->GetString(0, &protocol_message)) |
| 244 return; | 202 return; |
| 245 agent_host_->DispatchProtocolMessage(this, protocol_message); | 203 agent_host_->DispatchProtocolMessage(this, protocol_message); |
| 246 } else if (method == "loadCompleted") { | 204 } else if (method == "loadCompleted") { |
| 247 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( | 205 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 248 base::ASCIIToUTF16("DevToolsAPI.setUseSoftMenu(true);")); | 206 base::ASCIIToUTF16("DevToolsAPI.setUseSoftMenu(true);")); |
| 249 } else if (method == "loadNetworkResource" && params->GetSize() == 3) { | 207 } else if (method == "loadNetworkResource" && params->GetSize() == 3) { |
| 250 // TODO(pfeldman): handle some of the embedder messages in content. | 208 // TODO(pfeldman): handle some of the embedder messages in content. |
| 251 std::string url; | 209 std::string url; |
| 252 std::string headers; | 210 std::string headers; |
| 253 int stream_id; | 211 int stream_id; |
| 254 if (!params->GetString(0, &url) || | 212 if (!params->GetString(0, &url) || !params->GetString(1, &headers) || |
| 255 !params->GetString(1, &headers) || | |
| 256 !params->GetInteger(2, &stream_id)) { | 213 !params->GetInteger(2, &stream_id)) { |
| 257 return; | 214 return; |
| 258 } | 215 } |
| 259 | 216 |
| 260 GURL gurl(url); | 217 GURL gurl(url); |
| 261 if (!gurl.is_valid()) { | 218 if (!gurl.is_valid()) { |
| 262 base::DictionaryValue response; | 219 base::DictionaryValue response; |
| 263 response.SetInteger("statusCode", 404); | 220 response.SetInteger("statusCode", 404); |
| 264 SendMessageAck(request_id, &response); | 221 SendMessageAck(request_id, &response); |
| 265 return; | 222 return; |
| 266 } | 223 } |
| 267 | 224 |
| 268 net::URLFetcher* fetcher = | 225 net::URLFetcher* fetcher = |
| 269 net::URLFetcher::Create(gurl, net::URLFetcher::GET, this).release(); | 226 net::URLFetcher::Create(gurl, net::URLFetcher::GET, this).release(); |
| 270 pending_requests_[fetcher] = request_id; | 227 pending_requests_[fetcher] = request_id; |
| 271 fetcher->SetRequestContext( | 228 fetcher->SetRequestContext(BrowserContext::GetDefaultStoragePartition( |
| 272 BrowserContext::GetDefaultStoragePartition( | 229 web_contents()->GetBrowserContext()) |
| 273 web_contents()->GetBrowserContext())-> | 230 ->GetURLRequestContext()); |
| 274 GetURLRequestContext()); | |
| 275 fetcher->SetExtraRequestHeaders(headers); | 231 fetcher->SetExtraRequestHeaders(headers); |
| 276 fetcher->SaveResponseWithWriter( | 232 fetcher->SaveResponseWithWriter( |
| 277 std::unique_ptr<net::URLFetcherResponseWriter>( | 233 std::unique_ptr<net::URLFetcherResponseWriter>( |
| 278 new ResponseWriter(weak_factory_.GetWeakPtr(), stream_id))); | 234 new ResponseWriter(weak_factory_.GetWeakPtr(), stream_id))); |
| 279 fetcher->Start(); | 235 fetcher->Start(); |
| 280 return; | 236 return; |
| 281 } else if (method == "getPreferences") { | 237 } else if (method == "getPreferences") { |
| 282 SendMessageAck(request_id, &preferences_); | 238 SendMessageAck(request_id, &preferences_); |
| 283 return; | 239 return; |
| 284 } else if (method == "setPreference") { | 240 } else if (method == "setPreference") { |
| 285 std::string name; | 241 std::string name; |
| 286 std::string value; | 242 std::string value; |
| 287 if (!params->GetString(0, &name) || | 243 if (!params->GetString(0, &name) || !params->GetString(1, &value)) { |
| 288 !params->GetString(1, &value)) { | |
| 289 return; | 244 return; |
| 290 } | 245 } |
| 291 preferences_.SetStringWithoutPathExpansion(name, value); | 246 preferences_.SetStringWithoutPathExpansion(name, value); |
| 292 } else if (method == "removePreference") { | 247 } else if (method == "removePreference") { |
| 293 std::string name; | 248 std::string name; |
| 294 if (!params->GetString(0, &name)) | 249 if (!params->GetString(0, &name)) |
| 295 return; | 250 return; |
| 296 preferences_.RemoveWithoutPathExpansion(name, nullptr); | 251 preferences_.RemoveWithoutPathExpansion(name, nullptr); |
| 297 } else if (method == "requestFileSystems") { | 252 } else if (method == "requestFileSystems") { |
| 298 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( | 253 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 299 base::ASCIIToUTF16("DevToolsAPI.fileSystemsLoaded([]);")); | 254 base::ASCIIToUTF16("DevToolsAPI.fileSystemsLoaded([]);")); |
| 300 } else if (method == "reattach") { | 255 } else if (method == "reattach") { |
| 301 agent_host_->DetachClient(this); | 256 agent_host_->DetachClient(this); |
| 302 agent_host_->AttachClient(this); | 257 agent_host_->AttachClient(this); |
| 303 } else { | 258 } else { |
| 304 return; | 259 return; |
| 305 } | 260 } |
| 306 | 261 |
| 307 if (request_id) | 262 if (request_id) |
| 308 SendMessageAck(request_id, nullptr); | 263 SendMessageAck(request_id, nullptr); |
| 309 } | 264 } |
| 310 | 265 |
| 311 void ShellDevToolsFrontend::DispatchProtocolMessage( | 266 void ShellDevToolsBindings::DispatchProtocolMessage( |
| 312 DevToolsAgentHost* agent_host, const std::string& message) { | 267 DevToolsAgentHost* agent_host, |
| 313 | 268 const std::string& message) { |
| 314 if (message.length() < kMaxMessageChunkSize) { | 269 if (message.length() < kMaxMessageChunkSize) { |
| 315 std::string param; | 270 std::string param; |
| 316 base::EscapeJSONString(message, true, ¶m); | 271 base::EscapeJSONString(message, true, ¶m); |
| 317 std::string code = "DevToolsAPI.dispatchMessage(" + param + ");"; | 272 std::string code = "DevToolsAPI.dispatchMessage(" + param + ");"; |
| 318 base::string16 javascript = base::UTF8ToUTF16(code); | 273 base::string16 javascript = base::UTF8ToUTF16(code); |
| 319 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript); | 274 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript); |
| 320 return; | 275 return; |
| 321 } | 276 } |
| 322 | 277 |
| 323 size_t total_size = message.length(); | 278 size_t total_size = message.length(); |
| 324 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { | 279 for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) { |
| 325 std::string param; | 280 std::string param; |
| 326 base::EscapeJSONString(message.substr(pos, kMaxMessageChunkSize), true, | 281 base::EscapeJSONString(message.substr(pos, kMaxMessageChunkSize), true, |
| 327 ¶m); | 282 ¶m); |
| 328 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + "," + | 283 std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + "," + |
| 329 std::to_string(pos ? 0 : total_size) + ");"; | 284 std::to_string(pos ? 0 : total_size) + ");"; |
| 330 base::string16 javascript = base::UTF8ToUTF16(code); | 285 base::string16 javascript = base::UTF8ToUTF16(code); |
| 331 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript); | 286 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(javascript); |
| 332 } | 287 } |
| 333 } | 288 } |
| 334 | 289 |
| 335 void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) { | 290 void ShellDevToolsBindings::OnURLFetchComplete(const net::URLFetcher* source) { |
| 336 // TODO(pfeldman): this is a copy of chrome's devtools_ui_bindings.cc. | 291 // TODO(pfeldman): this is a copy of chrome's devtools_ui_bindings.cc. |
| 337 // We should handle some of the commands including this one in content. | 292 // We should handle some of the commands including this one in content. |
| 338 DCHECK(source); | 293 DCHECK(source); |
| 339 PendingRequestsMap::iterator it = pending_requests_.find(source); | 294 PendingRequestsMap::iterator it = pending_requests_.find(source); |
| 340 DCHECK(it != pending_requests_.end()); | 295 DCHECK(it != pending_requests_.end()); |
| 341 | 296 |
| 342 base::DictionaryValue response; | 297 base::DictionaryValue response; |
| 343 base::DictionaryValue* headers = new base::DictionaryValue(); | 298 base::DictionaryValue* headers = new base::DictionaryValue(); |
| 344 net::HttpResponseHeaders* rh = source->GetResponseHeaders(); | 299 net::HttpResponseHeaders* rh = source->GetResponseHeaders(); |
| 345 response.SetInteger("statusCode", rh ? rh->response_code() : 200); | 300 response.SetInteger("statusCode", rh ? rh->response_code() : 200); |
| 346 response.Set("headers", headers); | 301 response.Set("headers", headers); |
| 347 | 302 |
| 348 size_t iterator = 0; | 303 size_t iterator = 0; |
| 349 std::string name; | 304 std::string name; |
| 350 std::string value; | 305 std::string value; |
| 351 while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value)) | 306 while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value)) |
| 352 headers->SetString(name, value); | 307 headers->SetString(name, value); |
| 353 | 308 |
| 354 SendMessageAck(it->second, &response); | 309 SendMessageAck(it->second, &response); |
| 355 pending_requests_.erase(it); | 310 pending_requests_.erase(it); |
| 356 delete source; | 311 delete source; |
| 357 } | 312 } |
| 358 | 313 |
| 359 void ShellDevToolsFrontend::CallClientFunction( | 314 void ShellDevToolsBindings::CallClientFunction(const std::string& function_name, |
| 360 const std::string& function_name, | 315 const base::Value* arg1, |
| 361 const base::Value* arg1, | 316 const base::Value* arg2, |
| 362 const base::Value* arg2, | 317 const base::Value* arg3) { |
| 363 const base::Value* arg3) { | |
| 364 std::string javascript = function_name + "("; | 318 std::string javascript = function_name + "("; |
| 365 if (arg1) { | 319 if (arg1) { |
| 366 std::string json; | 320 std::string json; |
| 367 base::JSONWriter::Write(*arg1, &json); | 321 base::JSONWriter::Write(*arg1, &json); |
| 368 javascript.append(json); | 322 javascript.append(json); |
| 369 if (arg2) { | 323 if (arg2) { |
| 370 base::JSONWriter::Write(*arg2, &json); | 324 base::JSONWriter::Write(*arg2, &json); |
| 371 javascript.append(", ").append(json); | 325 javascript.append(", ").append(json); |
| 372 if (arg3) { | 326 if (arg3) { |
| 373 base::JSONWriter::Write(*arg3, &json); | 327 base::JSONWriter::Write(*arg3, &json); |
| 374 javascript.append(", ").append(json); | 328 javascript.append(", ").append(json); |
| 375 } | 329 } |
| 376 } | 330 } |
| 377 } | 331 } |
| 378 javascript.append(");"); | 332 javascript.append(");"); |
| 379 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( | 333 web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 380 base::UTF8ToUTF16(javascript)); | 334 base::UTF8ToUTF16(javascript)); |
| 381 } | 335 } |
| 382 | 336 |
| 383 void ShellDevToolsFrontend::SendMessageAck(int request_id, | 337 void ShellDevToolsBindings::SendMessageAck(int request_id, |
| 384 const base::Value* arg) { | 338 const base::Value* arg) { |
| 385 base::Value id_value(request_id); | 339 base::Value id_value(request_id); |
| 386 CallClientFunction("DevToolsAPI.embedderMessageAck", | 340 CallClientFunction("DevToolsAPI.embedderMessageAck", &id_value, arg, nullptr); |
| 387 &id_value, arg, nullptr); | |
| 388 } | 341 } |
| 389 | 342 |
| 390 void ShellDevToolsFrontend::AgentHostClosed( | 343 void ShellDevToolsBindings::AgentHostClosed(DevToolsAgentHost* agent_host, |
| 391 DevToolsAgentHost* agent_host, bool replaced) { | 344 bool replaced) { |
| 392 agent_host_ = nullptr; | 345 agent_host_ = nullptr; |
| 393 frontend_shell_->Close(); | 346 if (delegate_) |
| 347 delegate_->Close(); |
| 394 } | 348 } |
| 395 | 349 |
| 396 } // namespace content | 350 } // namespace content |
| OLD | NEW |