OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/devtools/devtools_server.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "net/log/net_log.h" |
| 14 #include "net/server/http_server_request_info.h" |
| 15 #include "net/socket/server_socket.h" |
| 16 #include "net/socket/tcp_server_socket.h" |
| 17 |
| 18 namespace ui { |
| 19 namespace devtools { |
| 20 |
| 21 namespace { |
| 22 // Each attached client has an id associated with it, so that |
| 23 // the user can inspect based on the id |
| 24 uint32_t ClientIds = 0; |
| 25 const char kChromeDeveloperToolsPrefix[] = |
| 26 "chrome-devtools://devtools/bundled/inspector.html?ws="; |
| 27 } |
| 28 |
| 29 UiDevToolsServer::UiDevToolsServer() |
| 30 : thread_(new base::Thread("UiDevToolsServerThread")) {} |
| 31 |
| 32 UiDevToolsServer::~UiDevToolsServer() {} |
| 33 |
| 34 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) { |
| 35 if (thread_ && thread_->IsRunning()) { |
| 36 return; |
| 37 } |
| 38 // Start IO thread upon which all the methods will run |
| 39 base::Thread::Options options; |
| 40 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 41 if (thread_->StartWithOptions(options)) { |
| 42 thread_->task_runner()->PostTask( |
| 43 FROM_HERE, base::Bind(&UiDevToolsServer::StartServer, |
| 44 base::Unretained(this), address_string, port)); |
| 45 } |
| 46 } // namespace |
| 47 |
| 48 void UiDevToolsServer::StartServer(const std::string& address_string, |
| 49 uint16_t port) { |
| 50 std::unique_ptr<net::ServerSocket> socket( |
| 51 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
| 52 if (socket->ListenWithAddressAndPort(address_string, port, 1) != net::OK) { |
| 53 return; |
| 54 } |
| 55 server_.reset(new net::HttpServer(std::move(socket), this)); |
| 56 } |
| 57 |
| 58 void UiDevToolsServer::SendOverWebSocket(int connection_id, |
| 59 const String& message) { |
| 60 thread_->task_runner()->PostTask( |
| 61 FROM_HERE, |
| 62 base::Bind(&net::HttpServer::SendOverWebSocket, |
| 63 base::Unretained(server_.get()), connection_id, message)); |
| 64 } |
| 65 |
| 66 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { |
| 67 clients_[ClientIds++] = std::move(client); |
| 68 } |
| 69 |
| 70 // HttpServer::Delegate Implementation |
| 71 void UiDevToolsServer::OnConnect(int connection_id) { |
| 72 NOTIMPLEMENTED(); |
| 73 } |
| 74 |
| 75 void UiDevToolsServer::OnHttpRequest(int connection_id, |
| 76 const net::HttpServerRequestInfo& info) { |
| 77 // Display a simple html page with all the clients and the corresponding |
| 78 // devtools links |
| 79 // TODO(mhashmi): Remove and display all clients under chrome://inspect/#other |
| 80 if (info.path.empty() || info.path == "/") { |
| 81 std::string clientHTML = "<html>"; |
| 82 clientHTML += |
| 83 "<h3>Copy paste the corresponding links in your browser to inspect " |
| 84 "them:</h3>"; |
| 85 for (ClientsMap::iterator it = clients_.begin(); it != clients_.end(); |
| 86 it++) { |
| 87 net::IPEndPoint ip; |
| 88 server_->GetLocalAddress(&ip); |
| 89 clientHTML += base::StringPrintf( |
| 90 "<p><strong>%s</strong> (%s%s/%d)</p>", it->second->name_.c_str(), |
| 91 kChromeDeveloperToolsPrefix, ip.ToString().c_str(), it->first); |
| 92 } |
| 93 clientHTML += "</html>"; |
| 94 thread_->task_runner()->PostTask( |
| 95 FROM_HERE, |
| 96 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()), |
| 97 connection_id, clientHTML, "text/html")); |
| 98 } |
| 99 } |
| 100 |
| 101 void UiDevToolsServer::OnWebSocketRequest( |
| 102 int connection_id, |
| 103 const net::HttpServerRequestInfo& info) { |
| 104 if (info.path.empty()) |
| 105 return; |
| 106 int target_id; |
| 107 if (!base::StringToInt(info.path.substr(1), &target_id)) { |
| 108 return; |
| 109 } |
| 110 ClientsMap::iterator it = clients_.find(target_id); |
| 111 if (it == clients_.end()) |
| 112 return; |
| 113 if (it->second->connected_client_id > 0) |
| 114 return; |
| 115 it->second->connected_client_id = connection_id; |
| 116 connections_[connection_id] = target_id; |
| 117 thread_->task_runner()->PostTask( |
| 118 FROM_HERE, |
| 119 base::Bind(&net::HttpServer::AcceptWebSocket, |
| 120 base::Unretained(server_.get()), connection_id, info)); |
| 121 } |
| 122 |
| 123 void UiDevToolsServer::OnWebSocketMessage(int connection_id, |
| 124 const std::string& data) { |
| 125 ConnectionsMap::iterator it = connections_.find(connection_id); |
| 126 if (it == connections_.end()) |
| 127 return; |
| 128 ClientsMap::iterator clientIt = clients_.find(it->second); |
| 129 if (clientIt == clients_.end()) |
| 130 return; |
| 131 thread_->task_runner()->PostTask( |
| 132 FROM_HERE, base::Bind(&UiDevToolsClient::Dispatch, |
| 133 base::Unretained(clientIt->second.get()), data)); |
| 134 } |
| 135 |
| 136 void UiDevToolsServer::OnClose(int connection_id) { |
| 137 ConnectionsMap::iterator it = connections_.find(connection_id); |
| 138 if (it == connections_.end()) |
| 139 return; |
| 140 int client_id = it->second; |
| 141 connections_.erase(it); |
| 142 ClientsMap::iterator clientIt = clients_.find(client_id); |
| 143 clientIt->second->connected_client_id = -1; |
| 144 } |
| 145 |
| 146 } // namespace devtools |
| 147 } // namespace ui |
OLD | NEW |