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