| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/debugger/devtools_http_protocol_handler.h" | 5 #include "content/browser/debugger/devtools_http_protocol_handler.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
| 14 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 15 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 : server_(server), | 45 : server_(server), |
| 45 connection_id_(connection_id) { | 46 connection_id_(connection_id) { |
| 46 } | 47 } |
| 47 ~DevToolsClientHostImpl() {} | 48 ~DevToolsClientHostImpl() {} |
| 48 | 49 |
| 49 // DevToolsClientHost interface | 50 // DevToolsClientHost interface |
| 50 virtual void InspectedTabClosing() { | 51 virtual void InspectedTabClosing() { |
| 51 BrowserThread::PostTask( | 52 BrowserThread::PostTask( |
| 52 BrowserThread::IO, | 53 BrowserThread::IO, |
| 53 FROM_HERE, | 54 FROM_HERE, |
| 54 NewRunnableMethod(server_, | 55 base::Bind(&net::HttpServer::Close, server_, connection_id_)); |
| 55 &net::HttpServer::Close, | |
| 56 connection_id_)); | |
| 57 } | 56 } |
| 58 | 57 |
| 59 virtual void SendMessageToClient(const IPC::Message& msg) { | 58 virtual void SendMessageToClient(const IPC::Message& msg) { |
| 60 IPC_BEGIN_MESSAGE_MAP(DevToolsClientHostImpl, msg) | 59 IPC_BEGIN_MESSAGE_MAP(DevToolsClientHostImpl, msg) |
| 61 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | 60 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
| 62 OnDispatchOnInspectorFrontend); | 61 OnDispatchOnInspectorFrontend); |
| 63 IPC_MESSAGE_UNHANDLED_ERROR() | 62 IPC_MESSAGE_UNHANDLED_ERROR() |
| 64 IPC_END_MESSAGE_MAP() | 63 IPC_END_MESSAGE_MAP() |
| 65 } | 64 } |
| 66 | 65 |
| 67 virtual void TabReplaced(TabContents* new_tab) { | 66 virtual void TabReplaced(TabContents* new_tab) { |
| 68 } | 67 } |
| 69 | 68 |
| 70 void NotifyCloseListener() { | 69 void NotifyCloseListener() { |
| 71 DevToolsClientHost::NotifyCloseListener(); | 70 DevToolsClientHost::NotifyCloseListener(); |
| 72 } | 71 } |
| 73 private: | 72 private: |
| 74 // Message handling routines | 73 // Message handling routines |
| 75 void OnDispatchOnInspectorFrontend(const std::string& data) { | 74 void OnDispatchOnInspectorFrontend(const std::string& data) { |
| 76 BrowserThread::PostTask( | 75 BrowserThread::PostTask( |
| 77 BrowserThread::IO, | 76 BrowserThread::IO, |
| 78 FROM_HERE, | 77 FROM_HERE, |
| 79 NewRunnableMethod(server_, | 78 base::Bind(&net::HttpServer::SendOverWebSocket, |
| 80 &net::HttpServer::SendOverWebSocket, | 79 server_, |
| 81 connection_id_, | 80 connection_id_, |
| 82 data)); | 81 data)); |
| 83 } | 82 } |
| 84 | 83 |
| 85 virtual void FrameNavigating(const std::string& url) {} | 84 virtual void FrameNavigating(const std::string& url) {} |
| 86 net::HttpServer* server_; | 85 net::HttpServer* server_; |
| 87 int connection_id_; | 86 int connection_id_; |
| 88 }; | 87 }; |
| 89 | 88 |
| 90 static int next_id = 1; | 89 static int next_id = 1; |
| 91 | 90 |
| 92 class TabContentsIDHelper : public TabContentsObserver { | 91 class TabContentsIDHelper : public TabContentsObserver { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 } | 157 } |
| 159 | 158 |
| 160 DevToolsHttpProtocolHandler::~DevToolsHttpProtocolHandler() { | 159 DevToolsHttpProtocolHandler::~DevToolsHttpProtocolHandler() { |
| 161 // Stop() must be called prior to this being called | 160 // Stop() must be called prior to this being called |
| 162 DCHECK(server_.get() == NULL); | 161 DCHECK(server_.get() == NULL); |
| 163 } | 162 } |
| 164 | 163 |
| 165 void DevToolsHttpProtocolHandler::Start() { | 164 void DevToolsHttpProtocolHandler::Start() { |
| 166 BrowserThread::PostTask( | 165 BrowserThread::PostTask( |
| 167 BrowserThread::IO, FROM_HERE, | 166 BrowserThread::IO, FROM_HERE, |
| 168 NewRunnableMethod(this, &DevToolsHttpProtocolHandler::Init)); | 167 base::Bind(&DevToolsHttpProtocolHandler::Init, this)); |
| 169 } | 168 } |
| 170 | 169 |
| 171 void DevToolsHttpProtocolHandler::Stop() { | 170 void DevToolsHttpProtocolHandler::Stop() { |
| 172 BrowserThread::PostTask( | 171 BrowserThread::PostTask( |
| 173 BrowserThread::IO, FROM_HERE, | 172 BrowserThread::IO, FROM_HERE, |
| 174 NewRunnableMethod(this, &DevToolsHttpProtocolHandler::Teardown)); | 173 base::Bind(&DevToolsHttpProtocolHandler::Teardown, this)); |
| 175 } | 174 } |
| 176 | 175 |
| 177 void DevToolsHttpProtocolHandler::OnHttpRequest( | 176 void DevToolsHttpProtocolHandler::OnHttpRequest( |
| 178 int connection_id, | 177 int connection_id, |
| 179 const net::HttpServerRequestInfo& info) { | 178 const net::HttpServerRequestInfo& info) { |
| 180 if (info.path == "/json") { | 179 if (info.path == "/json") { |
| 181 // Pages discovery json request. | 180 // Pages discovery json request. |
| 182 BrowserThread::PostTask( | 181 BrowserThread::PostTask( |
| 183 BrowserThread::UI, | 182 BrowserThread::UI, |
| 184 FROM_HERE, | 183 FROM_HERE, |
| 185 NewRunnableMethod(this, | 184 base::Bind(&DevToolsHttpProtocolHandler::OnJsonRequestUI, |
| 186 &DevToolsHttpProtocolHandler::OnJsonRequestUI, | 185 this, |
| 187 connection_id, | 186 connection_id, |
| 188 info)); | 187 info)); |
| 189 return; | 188 return; |
| 190 } | 189 } |
| 191 | 190 |
| 192 // Proxy static files from chrome-devtools://devtools/*. | 191 // Proxy static files from chrome-devtools://devtools/*. |
| 193 net::URLRequestContext* request_context = delegate_->GetURLRequestContext(); | 192 net::URLRequestContext* request_context = delegate_->GetURLRequestContext(); |
| 194 if (!request_context) { | 193 if (!request_context) { |
| 195 server_->Send404(connection_id); | 194 server_->Send404(connection_id); |
| 196 return; | 195 return; |
| 197 } | 196 } |
| 198 | 197 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 217 request->set_context(request_context); | 216 request->set_context(request_context); |
| 218 request->Start(); | 217 request->Start(); |
| 219 } | 218 } |
| 220 | 219 |
| 221 void DevToolsHttpProtocolHandler::OnWebSocketRequest( | 220 void DevToolsHttpProtocolHandler::OnWebSocketRequest( |
| 222 int connection_id, | 221 int connection_id, |
| 223 const net::HttpServerRequestInfo& request) { | 222 const net::HttpServerRequestInfo& request) { |
| 224 BrowserThread::PostTask( | 223 BrowserThread::PostTask( |
| 225 BrowserThread::UI, | 224 BrowserThread::UI, |
| 226 FROM_HERE, | 225 FROM_HERE, |
| 227 NewRunnableMethod( | 226 base::Bind( |
| 227 &DevToolsHttpProtocolHandler::OnWebSocketRequestUI, |
| 228 this, | 228 this, |
| 229 &DevToolsHttpProtocolHandler::OnWebSocketRequestUI, | |
| 230 connection_id, | 229 connection_id, |
| 231 request)); | 230 request)); |
| 232 } | 231 } |
| 233 | 232 |
| 234 void DevToolsHttpProtocolHandler::OnWebSocketMessage( | 233 void DevToolsHttpProtocolHandler::OnWebSocketMessage( |
| 235 int connection_id, | 234 int connection_id, |
| 236 const std::string& data) { | 235 const std::string& data) { |
| 237 BrowserThread::PostTask( | 236 BrowserThread::PostTask( |
| 238 BrowserThread::UI, | 237 BrowserThread::UI, |
| 239 FROM_HERE, | 238 FROM_HERE, |
| 240 NewRunnableMethod( | 239 base::Bind( |
| 240 &DevToolsHttpProtocolHandler::OnWebSocketMessageUI, |
| 241 this, | 241 this, |
| 242 &DevToolsHttpProtocolHandler::OnWebSocketMessageUI, | |
| 243 connection_id, | 242 connection_id, |
| 244 data)); | 243 data)); |
| 245 } | 244 } |
| 246 | 245 |
| 247 void DevToolsHttpProtocolHandler::OnClose(int connection_id) { | 246 void DevToolsHttpProtocolHandler::OnClose(int connection_id) { |
| 248 ConnectionToRequestsMap::iterator it = | 247 ConnectionToRequestsMap::iterator it = |
| 249 connection_to_requests_io_.find(connection_id); | 248 connection_to_requests_io_.find(connection_id); |
| 250 if (it != connection_to_requests_io_.end()) { | 249 if (it != connection_to_requests_io_.end()) { |
| 251 // Dispose delegating socket. | 250 // Dispose delegating socket. |
| 252 for (std::set<net::URLRequest*>::iterator it2 = it->second.begin(); | 251 for (std::set<net::URLRequest*>::iterator it2 = it->second.begin(); |
| 253 it2 != it->second.end(); ++it2) { | 252 it2 != it->second.end(); ++it2) { |
| 254 net::URLRequest* request = *it2; | 253 net::URLRequest* request = *it2; |
| 255 request->Cancel(); | 254 request->Cancel(); |
| 256 request_to_connection_io_.erase(request); | 255 request_to_connection_io_.erase(request); |
| 257 request_to_buffer_io_.erase(request); | 256 request_to_buffer_io_.erase(request); |
| 258 delete request; | 257 delete request; |
| 259 } | 258 } |
| 260 connection_to_requests_io_.erase(connection_id); | 259 connection_to_requests_io_.erase(connection_id); |
| 261 } | 260 } |
| 262 | 261 |
| 263 BrowserThread::PostTask( | 262 BrowserThread::PostTask( |
| 264 BrowserThread::UI, | 263 BrowserThread::UI, |
| 265 FROM_HERE, | 264 FROM_HERE, |
| 266 NewRunnableMethod( | 265 base::Bind( |
| 266 &DevToolsHttpProtocolHandler::OnCloseUI, |
| 267 this, | 267 this, |
| 268 &DevToolsHttpProtocolHandler::OnCloseUI, | |
| 269 connection_id)); | 268 connection_id)); |
| 270 } | 269 } |
| 271 | 270 |
| 272 struct PageInfo | 271 struct PageInfo |
| 273 { | 272 { |
| 274 int id; | 273 int id; |
| 275 std::string url; | 274 std::string url; |
| 276 bool attached; | 275 bool attached; |
| 277 std::string title; | 276 std::string title; |
| 278 std::string thumbnail_url; | 277 std::string thumbnail_url; |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 it2->second.erase(request); | 520 it2->second.erase(request); |
| 522 request_to_buffer_io_.erase(request); | 521 request_to_buffer_io_.erase(request); |
| 523 delete request; | 522 delete request; |
| 524 } | 523 } |
| 525 | 524 |
| 526 void DevToolsHttpProtocolHandler::Send200(int connection_id, | 525 void DevToolsHttpProtocolHandler::Send200(int connection_id, |
| 527 const std::string& data, | 526 const std::string& data, |
| 528 const std::string& mime_type) { | 527 const std::string& mime_type) { |
| 529 BrowserThread::PostTask( | 528 BrowserThread::PostTask( |
| 530 BrowserThread::IO, FROM_HERE, | 529 BrowserThread::IO, FROM_HERE, |
| 531 NewRunnableMethod(server_.get(), | 530 base::Bind(&net::HttpServer::Send200, |
| 532 &net::HttpServer::Send200, | 531 server_.get(), |
| 533 connection_id, | 532 connection_id, |
| 534 data, | 533 data, |
| 535 mime_type)); | 534 mime_type)); |
| 536 } | 535 } |
| 537 | 536 |
| 538 void DevToolsHttpProtocolHandler::Send404(int connection_id) { | 537 void DevToolsHttpProtocolHandler::Send404(int connection_id) { |
| 539 BrowserThread::PostTask( | 538 BrowserThread::PostTask( |
| 540 BrowserThread::IO, FROM_HERE, | 539 BrowserThread::IO, FROM_HERE, |
| 541 NewRunnableMethod(server_.get(), | 540 base::Bind(&net::HttpServer::Send404, server_.get(), connection_id)); |
| 542 &net::HttpServer::Send404, | |
| 543 connection_id)); | |
| 544 } | 541 } |
| 545 | 542 |
| 546 void DevToolsHttpProtocolHandler::Send500(int connection_id, | 543 void DevToolsHttpProtocolHandler::Send500(int connection_id, |
| 547 const std::string& message) { | 544 const std::string& message) { |
| 548 BrowserThread::PostTask( | 545 BrowserThread::PostTask( |
| 549 BrowserThread::IO, FROM_HERE, | 546 BrowserThread::IO, FROM_HERE, |
| 550 NewRunnableMethod(server_.get(), | 547 base::Bind(&net::HttpServer::Send500, server_.get(), connection_id, |
| 551 &net::HttpServer::Send500, | 548 message)); |
| 552 connection_id, | |
| 553 message)); | |
| 554 } | 549 } |
| 555 | 550 |
| 556 void DevToolsHttpProtocolHandler::AcceptWebSocket( | 551 void DevToolsHttpProtocolHandler::AcceptWebSocket( |
| 557 int connection_id, | 552 int connection_id, |
| 558 const net::HttpServerRequestInfo& request) { | 553 const net::HttpServerRequestInfo& request) { |
| 559 BrowserThread::PostTask( | 554 BrowserThread::PostTask( |
| 560 BrowserThread::IO, FROM_HERE, | 555 BrowserThread::IO, FROM_HERE, |
| 561 NewRunnableMethod(server_.get(), | 556 base::Bind(&net::HttpServer::AcceptWebSocket, server_.get(), |
| 562 &net::HttpServer::AcceptWebSocket, | 557 connection_id, request)); |
| 563 connection_id, | |
| 564 request)); | |
| 565 } | 558 } |
| OLD | NEW |