Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: ui/devtools/devtools_server.cc

Issue 2374513002: Add ui devtools server (Closed)
Patch Set: Fix all header if directives Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/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 "net/base/net_errors.h"
16 #include "net/log/net_log.h"
17 #include "net/server/http_server_request_info.h"
18 #include "net/socket/server_socket.h"
19 #include "net/socket/tcp_server_socket.h"
20 #include "ui/devtools/switches.h"
21
22 namespace ui {
23 namespace devtools {
24
25 namespace {
26 constexpr int kBacklog = 1;
27 const char kChromeDeveloperToolsPrefix[] =
28 "chrome-devtools://devtools/bundled/inspector.html?ws=";
29 } // namespace
30
31 UiDevToolsServer::UiDevToolsServer()
32 : thread_(new base::Thread("UiDevToolsServerThread")) {}
33
34 UiDevToolsServer::~UiDevToolsServer() {}
35
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;
42 base::StringToInt(
43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
44 kEnableUiDevTools),
45 &port);
sadrul 2016/10/18 01:09:15 Use some default port if the flag is there, but it
Sarmad Hashmi 2016/10/18 02:40:48 Done.
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));
sadrul 2016/10/18 01:09:15 It looks like we are never removing the client fro
Sarmad Hashmi 2016/10/18 02:40:48 It would be possible if in the future we detach cl
sadrul 2016/10/18 20:29:37 I think this is OK for now.
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 if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) !=
sadrul 2016/10/18 01:09:15 Define kBacklog just before this line here.
Sarmad Hashmi 2016/10/18 02:40:49 Done.
83 net::OK)
84 return;
85 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this);
86 }
87
88 // HttpServer::Delegate Implementation
89 void UiDevToolsServer::OnConnect(int connection_id) {
90 NOTIMPLEMENTED();
91 }
92
93 void UiDevToolsServer::OnHttpRequest(int connection_id,
94 const net::HttpServerRequestInfo& info) {
95 // Display a simple html page with all the clients and the corresponding
96 // devtools links
97 // TODO(mhashmi): Remove and display all clients under chrome://inspect/#other
98 if (info.path.empty() || info.path == "/") {
99 std::string clientHTML = "<html>";
100 clientHTML +=
101 "<h3>Copy paste the corresponding links in your browser to inspect "
102 "them:</h3>";
103 net::IPEndPoint ip;
104 server_->GetLocalAddress(&ip);
105 for (ClientsList::size_type i = 0; i != clients_.size(); i++) {
106 clientHTML += base::StringPrintf(
107 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>",
108 clients_[i]->name_.c_str(), kChromeDeveloperToolsPrefix,
109 ip.ToString().c_str(), i);
110 }
111 clientHTML += "</html>";
112 thread_->task_runner()->PostTask(
113 FROM_HERE,
114 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()),
115 connection_id, clientHTML, "text/html"));
116 }
117 }
118
119 void UiDevToolsServer::OnWebSocketRequest(
120 int connection_id,
121 const net::HttpServerRequestInfo& info) {
122 size_t target_id;
123 if (info.path.empty() ||
124 !base::StringToSizeT(info.path.substr(1), &target_id) ||
125 target_id > clients_.size())
126 return;
127
128 UiDevToolsClient* client = clients_[target_id].get();
129 // Only one user can inspect the client at a time
130 if (client->connected())
131 return;
132 client->connection_id_ = connection_id;
sadrul 2016/10/18 01:09:15 client->set_connection_id(connection_id) instead,
Sarmad Hashmi 2016/10/18 02:40:48 Done.
133 connections_[connection_id] = client;
134 thread_->task_runner()->PostTask(
135 FROM_HERE,
136 base::Bind(&net::HttpServer::AcceptWebSocket,
137 base::Unretained(server_.get()), connection_id, info));
138 }
139
140 void UiDevToolsServer::OnWebSocketMessage(int connection_id,
141 const std::string& data) {
142 ConnectionsMap::iterator it = connections_.find(connection_id);
143 DCHECK(it != connections_.end());
144 UiDevToolsClient* client = it->second;
145 if (!client) {
sadrul 2016/10/18 01:09:15 |client| should never be null here, right?
Sarmad Hashmi 2016/10/18 02:40:49 Can't it be null if the client is destroyed but th
sadrul 2016/10/18 20:29:37 Since UiDevToolsServer owns the client (and also t
Sarmad Hashmi 2016/10/19 23:56:02 Done.
146 // Client shut down, close the connection
147 connections_.erase(it);
148 thread_->task_runner()->PostTask(
149 FROM_HERE, base::Bind(&net::HttpServer::Close,
150 base::Unretained(server_.get()), connection_id));
151 return;
152 }
153
154 thread_->task_runner()->PostTask(
155 FROM_HERE,
156 base::Bind(&UiDevToolsClient::Dispatch, base::Unretained(client), data));
157 }
158
159 void UiDevToolsServer::OnClose(int connection_id) {
160 ConnectionsMap::iterator it = connections_.find(connection_id);
161 DCHECK(it != connections_.end());
162 UiDevToolsClient* client = it->second;
163 if (client)
sadrul 2016/10/18 01:09:15 ditto re client being non-null.
Sarmad Hashmi 2016/10/18 02:40:48 Replied above.
Sarmad Hashmi 2016/10/19 23:56:02 Done.
164 client->connection_id_ = UiDevToolsClient::kNotConnected;
165 connections_.erase(it);
166 }
167
168 } // namespace devtools
169 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698