| 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/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
| 15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "content/browser/debugger/devtools_client_host.h" | |
| 21 #include "content/browser/debugger/devtools_manager.h" | |
| 22 #include "content/browser/tab_contents/tab_contents.h" | 20 #include "content/browser/tab_contents/tab_contents.h" |
| 23 #include "content/browser/tab_contents/tab_contents_observer.h" | 21 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 24 #include "content/common/devtools_messages.h" | 22 #include "content/common/devtools_messages.h" |
| 25 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/devtools_agent_host_registry.h" |
| 25 #include "content/public/browser/devtools_client_host.h" |
| 26 #include "content/public/browser/devtools_manager.h" |
| 26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
| 27 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
| 28 #include "net/base/io_buffer.h" | 29 #include "net/base/io_buffer.h" |
| 29 #include "net/server/http_server_request_info.h" | 30 #include "net/server/http_server_request_info.h" |
| 30 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
| 31 | 32 |
| 32 using content::BrowserThread; | 33 using content::BrowserThread; |
| 34 using content::DevToolsAgentHost; |
| 35 using content::DevToolsAgentHostRegistry; |
| 36 using content::DevToolsClientHost; |
| 37 using content::DevToolsManager; |
| 33 | 38 |
| 34 const int kBufferSize = 16 * 1024; | 39 const int kBufferSize = 16 * 1024; |
| 35 | 40 |
| 36 namespace { | 41 namespace { |
| 37 | 42 |
| 38 // An internal implementation of DevToolsClientHost that delegates | 43 // An internal implementation of DevToolsClientHost that delegates |
| 39 // messages sent for DevToolsClient to a DebuggerShell instance. | 44 // messages sent for DevToolsClient to a DebuggerShell instance. |
| 40 class DevToolsClientHostImpl : public DevToolsClientHost { | 45 class DevToolsClientHostImpl : public DevToolsClientHost { |
| 41 public: | 46 public: |
| 42 DevToolsClientHostImpl( | 47 DevToolsClientHostImpl( |
| 43 net::HttpServer* server, | 48 net::HttpServer* server, |
| 44 int connection_id) | 49 int connection_id) |
| 45 : server_(server), | 50 : server_(server), |
| 46 connection_id_(connection_id) { | 51 connection_id_(connection_id) { |
| 47 } | 52 } |
| 48 ~DevToolsClientHostImpl() {} | 53 ~DevToolsClientHostImpl() {} |
| 49 | 54 |
| 50 // DevToolsClientHost interface | 55 // DevToolsClientHost interface |
| 51 virtual void InspectedTabClosing() { | 56 virtual void InspectedTabClosing() { |
| 52 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
| 53 BrowserThread::IO, | 58 BrowserThread::IO, |
| 54 FROM_HERE, | 59 FROM_HERE, |
| 55 base::Bind(&net::HttpServer::Close, server_, connection_id_)); | 60 base::Bind(&net::HttpServer::Close, server_, connection_id_)); |
| 56 } | 61 } |
| 57 | 62 |
| 58 virtual void SendMessageToClient(const IPC::Message& msg) { | 63 virtual void DispatchOnInspectorFrontend(const std::string& data) { |
| 59 IPC_BEGIN_MESSAGE_MAP(DevToolsClientHostImpl, msg) | |
| 60 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | |
| 61 OnDispatchOnInspectorFrontend); | |
| 62 IPC_MESSAGE_UNHANDLED_ERROR() | |
| 63 IPC_END_MESSAGE_MAP() | |
| 64 } | |
| 65 | |
| 66 virtual void TabReplaced(TabContents* new_tab) { | |
| 67 } | |
| 68 | |
| 69 void NotifyCloseListener() { | |
| 70 DevToolsClientHost::NotifyCloseListener(); | |
| 71 } | |
| 72 private: | |
| 73 // Message handling routines | |
| 74 void OnDispatchOnInspectorFrontend(const std::string& data) { | |
| 75 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
| 76 BrowserThread::IO, | 65 BrowserThread::IO, |
| 77 FROM_HERE, | 66 FROM_HERE, |
| 78 base::Bind(&net::HttpServer::SendOverWebSocket, | 67 base::Bind(&net::HttpServer::SendOverWebSocket, |
| 79 server_, | 68 server_, |
| 80 connection_id_, | 69 connection_id_, |
| 81 data)); | 70 data)); |
| 82 } | 71 } |
| 83 | 72 |
| 73 virtual void TabReplaced(TabContents* new_tab) { |
| 74 } |
| 75 |
| 76 private: |
| 84 virtual void FrameNavigating(const std::string& url) {} | 77 virtual void FrameNavigating(const std::string& url) {} |
| 85 net::HttpServer* server_; | 78 net::HttpServer* server_; |
| 86 int connection_id_; | 79 int connection_id_; |
| 87 }; | 80 }; |
| 88 | 81 |
| 89 static int next_id = 1; | 82 static int next_id = 1; |
| 90 | 83 |
| 91 class TabContentsIDHelper : public TabContentsObserver { | 84 class TabContentsIDHelper : public TabContentsObserver { |
| 92 public: | 85 public: |
| 93 | 86 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 for (Tabs::iterator it = inspectable_tabs.begin(); | 283 for (Tabs::iterator it = inspectable_tabs.begin(); |
| 291 it != inspectable_tabs.end(); ++it) { | 284 it != inspectable_tabs.end(); ++it) { |
| 292 | 285 |
| 293 TabContents* tab_contents = *it; | 286 TabContents* tab_contents = *it; |
| 294 NavigationController& controller = tab_contents->controller(); | 287 NavigationController& controller = tab_contents->controller(); |
| 295 | 288 |
| 296 NavigationEntry* entry = controller.GetActiveEntry(); | 289 NavigationEntry* entry = controller.GetActiveEntry(); |
| 297 if (entry == NULL || !entry->url().is_valid()) | 290 if (entry == NULL || !entry->url().is_valid()) |
| 298 continue; | 291 continue; |
| 299 | 292 |
| 293 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 294 tab_contents->render_view_host()); |
| 300 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> | 295 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> |
| 301 GetDevToolsClientHostFor(tab_contents->render_view_host()); | 296 GetDevToolsClientHostFor(agent); |
| 302 PageInfo page_info; | 297 PageInfo page_info; |
| 303 page_info.id = TabContentsIDHelper::GetID(tab_contents); | 298 page_info.id = TabContentsIDHelper::GetID(tab_contents); |
| 304 page_info.attached = client_host != NULL; | 299 page_info.attached = client_host != NULL; |
| 305 page_info.url = entry->url().spec(); | 300 page_info.url = entry->url().spec(); |
| 306 page_info.title = UTF16ToUTF8(net::EscapeForHTML(entry->title())); | 301 page_info.title = UTF16ToUTF8(net::EscapeForHTML(entry->title())); |
| 307 page_info.thumbnail_url = "/thumb/" + entry->url().spec(); | 302 page_info.thumbnail_url = "/thumb/" + entry->url().spec(); |
| 308 page_info.favicon_url = entry->favicon().url().spec(); | 303 page_info.favicon_url = entry->favicon().url().spec(); |
| 309 page_list.push_back(page_info); | 304 page_list.push_back(page_info); |
| 310 } | 305 } |
| 311 return page_list; | 306 return page_list; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 return; | 356 return; |
| 362 } | 357 } |
| 363 | 358 |
| 364 TabContents* tab_contents = TabContentsIDHelper::GetTabContents(id); | 359 TabContents* tab_contents = TabContentsIDHelper::GetTabContents(id); |
| 365 if (tab_contents == NULL) { | 360 if (tab_contents == NULL) { |
| 366 Send500(connection_id, "No such page id: " + page_id); | 361 Send500(connection_id, "No such page id: " + page_id); |
| 367 return; | 362 return; |
| 368 } | 363 } |
| 369 | 364 |
| 370 DevToolsManager* manager = DevToolsManager::GetInstance(); | 365 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 371 if (manager->GetDevToolsClientHostFor(tab_contents->render_view_host())) { | 366 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 367 tab_contents->render_view_host()); |
| 368 if (manager->GetDevToolsClientHostFor(agent)) { |
| 372 Send500(connection_id, "Page with given id is being inspected: " + page_id); | 369 Send500(connection_id, "Page with given id is being inspected: " + page_id); |
| 373 return; | 370 return; |
| 374 } | 371 } |
| 375 | 372 |
| 376 DevToolsClientHostImpl* client_host = | 373 DevToolsClientHostImpl* client_host = |
| 377 new DevToolsClientHostImpl(server_, connection_id); | 374 new DevToolsClientHostImpl(server_, connection_id); |
| 378 connection_to_client_host_ui_[connection_id] = client_host; | 375 connection_to_client_host_ui_[connection_id] = client_host; |
| 379 | 376 |
| 380 manager->RegisterDevToolsClientHostFor( | 377 manager->RegisterDevToolsClientHostFor(agent, client_host); |
| 381 tab_contents->render_view_host(), | |
| 382 client_host); | |
| 383 | 378 |
| 384 AcceptWebSocket(connection_id, request); | 379 AcceptWebSocket(connection_id, request); |
| 385 } | 380 } |
| 386 | 381 |
| 387 void DevToolsHttpProtocolHandler::OnWebSocketMessageUI( | 382 void DevToolsHttpProtocolHandler::OnWebSocketMessageUI( |
| 388 int connection_id, | 383 int connection_id, |
| 389 const std::string& data) { | 384 const std::string& data) { |
| 390 ConnectionToClientHostMap::iterator it = | 385 ConnectionToClientHostMap::iterator it = |
| 391 connection_to_client_host_ui_.find(connection_id); | 386 connection_to_client_host_ui_.find(connection_id); |
| 392 if (it == connection_to_client_host_ui_.end()) | 387 if (it == connection_to_client_host_ui_.end()) |
| 393 return; | 388 return; |
| 394 | 389 |
| 395 DevToolsManager* manager = DevToolsManager::GetInstance(); | 390 DevToolsManager* manager = DevToolsManager::GetInstance(); |
| 396 manager->ForwardToDevToolsAgent( | 391 manager->DispatchOnInspectorBackend(it->second, data); |
| 397 it->second, | |
| 398 DevToolsAgentMsg_DispatchOnInspectorBackend(MSG_ROUTING_NONE, data)); | |
| 399 } | 392 } |
| 400 | 393 |
| 401 void DevToolsHttpProtocolHandler::OnCloseUI(int connection_id) { | 394 void DevToolsHttpProtocolHandler::OnCloseUI(int connection_id) { |
| 402 ConnectionToClientHostMap::iterator it = | 395 ConnectionToClientHostMap::iterator it = |
| 403 connection_to_client_host_ui_.find(connection_id); | 396 connection_to_client_host_ui_.find(connection_id); |
| 404 if (it != connection_to_client_host_ui_.end()) { | 397 if (it != connection_to_client_host_ui_.end()) { |
| 405 DevToolsClientHostImpl* client_host = | 398 DevToolsClientHostImpl* client_host = |
| 406 static_cast<DevToolsClientHostImpl*>(it->second); | 399 static_cast<DevToolsClientHostImpl*>(it->second); |
| 407 client_host->NotifyCloseListener(); | 400 DevToolsManager::GetInstance()->ClientHostClosing(client_host); |
| 408 delete client_host; | 401 delete client_host; |
| 409 connection_to_client_host_ui_.erase(connection_id); | 402 connection_to_client_host_ui_.erase(connection_id); |
| 410 } | 403 } |
| 411 } | 404 } |
| 412 | 405 |
| 413 void DevToolsHttpProtocolHandler::OnResponseStarted(net::URLRequest* request) { | 406 void DevToolsHttpProtocolHandler::OnResponseStarted(net::URLRequest* request) { |
| 414 RequestToSocketMap::iterator it = request_to_connection_io_.find(request); | 407 RequestToSocketMap::iterator it = request_to_connection_io_.find(request); |
| 415 if (it == request_to_connection_io_.end()) | 408 if (it == request_to_connection_io_.end()) |
| 416 return; | 409 return; |
| 417 | 410 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 } | 539 } |
| 547 | 540 |
| 548 void DevToolsHttpProtocolHandler::AcceptWebSocket( | 541 void DevToolsHttpProtocolHandler::AcceptWebSocket( |
| 549 int connection_id, | 542 int connection_id, |
| 550 const net::HttpServerRequestInfo& request) { | 543 const net::HttpServerRequestInfo& request) { |
| 551 BrowserThread::PostTask( | 544 BrowserThread::PostTask( |
| 552 BrowserThread::IO, FROM_HERE, | 545 BrowserThread::IO, FROM_HERE, |
| 553 base::Bind(&net::HttpServer::AcceptWebSocket, server_.get(), | 546 base::Bind(&net::HttpServer::AcceptWebSocket, server_.get(), |
| 554 connection_id, request)); | 547 connection_id, request)); |
| 555 } | 548 } |
| OLD | NEW |