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 "components/ui_devtools/devtools_server.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/command_line.h" | |
10 #include "base/format_macros.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "base/values.h" | |
15 #include "components/ui_devtools/switches.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/log/net_log.h" | |
18 #include "net/server/http_server_request_info.h" | |
19 #include "net/socket/server_socket.h" | |
20 #include "net/socket/tcp_server_socket.h" | |
21 | |
22 namespace ui { | |
23 namespace devtools { | |
24 | |
25 namespace { | |
26 const char kChromeDeveloperToolsPrefix[] = | |
27 "chrome-devtools://devtools/bundled/inspector.html?ws="; | |
28 } // namespace | |
29 | |
30 UiDevToolsServer::UiDevToolsServer() | |
31 : thread_(new base::Thread("UiDevToolsServerThread")) {} | |
32 | |
33 UiDevToolsServer::~UiDevToolsServer() {} | |
34 | |
35 // Returns an empty unique_ptr if ui devtools flag isn't enabled. | |
sadrul
2016/10/20 16:56:29
Remove this comment. The comment in the header is
Sarmad Hashmi
2016/10/20 21:49:22
Done.
| |
36 // static | |
37 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create() { | |
38 std::unique_ptr<UiDevToolsServer> server; | |
39 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) { | |
40 // TODO(mhashmi): Change port if more than one inspectable clients | |
41 int port = 9223; // Default port is 9223 | |
42 base::StringToInt( | |
43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
44 kEnableUiDevTools), | |
45 &port); | |
46 server.reset(new UiDevToolsServer()); | |
47 server->Start("127.0.0.1", port); | |
48 } | |
49 return server; | |
50 } | |
51 | |
52 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { | |
53 clients_.push_back(std::move(client)); | |
54 } | |
55 | |
56 void UiDevToolsServer::SendOverWebSocket(int connection_id, | |
57 const String& message) { | |
58 thread_->task_runner()->PostTask( | |
59 FROM_HERE, | |
60 base::Bind(&net::HttpServer::SendOverWebSocket, | |
61 base::Unretained(server_.get()), connection_id, message)); | |
62 } | |
63 | |
64 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) { | |
65 if (thread_ && thread_->IsRunning()) | |
66 return; | |
67 | |
68 // Start IO thread upon which all the methods will run | |
69 base::Thread::Options options; | |
70 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
71 if (thread_->StartWithOptions(options)) { | |
72 thread_->task_runner()->PostTask( | |
73 FROM_HERE, base::Bind(&UiDevToolsServer::StartServer, | |
74 base::Unretained(this), address_string, port)); | |
75 } | |
76 } | |
77 | |
78 void UiDevToolsServer::StartServer(const std::string& address_string, | |
79 uint16_t port) { | |
80 std::unique_ptr<net::ServerSocket> socket( | |
81 new net::TCPServerSocket(nullptr, net::NetLogSource())); | |
82 constexpr int kBacklog = 1; | |
83 if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) != | |
84 net::OK) | |
85 return; | |
86 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this); | |
87 } | |
88 | |
89 // HttpServer::Delegate Implementation | |
90 void UiDevToolsServer::OnConnect(int connection_id) { | |
91 NOTIMPLEMENTED(); | |
92 } | |
93 | |
94 void UiDevToolsServer::OnHttpRequest(int connection_id, | |
95 const net::HttpServerRequestInfo& info) { | |
96 // Display a simple html page with all the clients and the corresponding | |
97 // devtools links | |
98 // TODO(mhashmi): Remove and display all clients under chrome://inspect/#other | |
99 if (info.path.empty() || info.path == "/") { | |
100 std::string clientHTML = "<html>"; | |
101 clientHTML += | |
102 "<h3>Copy paste the corresponding links in your browser to inspect " | |
103 "them:</h3>"; | |
104 net::IPEndPoint ip; | |
105 server_->GetLocalAddress(&ip); | |
106 for (ClientsList::size_type i = 0; i != clients_.size(); i++) { | |
107 clientHTML += base::StringPrintf( | |
108 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>", | |
109 clients_[i]->name().c_str(), kChromeDeveloperToolsPrefix, | |
110 ip.ToString().c_str(), i); | |
111 } | |
112 clientHTML += "</html>"; | |
113 thread_->task_runner()->PostTask( | |
114 FROM_HERE, | |
115 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()), | |
116 connection_id, clientHTML, "text/html")); | |
117 } | |
118 } | |
119 | |
120 void UiDevToolsServer::OnWebSocketRequest( | |
121 int connection_id, | |
122 const net::HttpServerRequestInfo& info) { | |
123 size_t target_id; | |
124 if (info.path.empty() || | |
125 !base::StringToSizeT(info.path.substr(1), &target_id) || | |
126 target_id > clients_.size()) | |
127 return; | |
128 | |
129 UiDevToolsClient* client = clients_[target_id].get(); | |
130 // Only one user can inspect the client at a time | |
131 if (client->connected()) | |
132 return; | |
133 client->set_connection_id(connection_id); | |
134 connections_[connection_id] = client; | |
135 thread_->task_runner()->PostTask( | |
136 FROM_HERE, | |
137 base::Bind(&net::HttpServer::AcceptWebSocket, | |
138 base::Unretained(server_.get()), connection_id, info)); | |
139 } | |
140 | |
141 void UiDevToolsServer::OnWebSocketMessage(int connection_id, | |
142 const std::string& data) { | |
143 ConnectionsMap::iterator it = connections_.find(connection_id); | |
144 DCHECK(it != connections_.end()); | |
145 UiDevToolsClient* client = it->second; | |
146 DCHECK(client); | |
147 thread_->task_runner()->PostTask( | |
148 FROM_HERE, | |
149 base::Bind(&UiDevToolsClient::Dispatch, base::Unretained(client), data)); | |
150 } | |
151 | |
152 void UiDevToolsServer::OnClose(int connection_id) { | |
153 ConnectionsMap::iterator it = connections_.find(connection_id); | |
154 DCHECK(it != connections_.end()); | |
155 UiDevToolsClient* client = it->second; | |
156 DCHECK(client); | |
157 client->set_connection_id(UiDevToolsClient::kNotConnected); | |
158 connections_.erase(it); | |
159 } | |
160 | |
161 } // namespace devtools | |
162 } // namespace ui | |
OLD | NEW |