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

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

Issue 2374513002: Add ui devtools server (Closed)
Patch Set: Initial commit for ui devtools server 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/format_macros.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "net/base/net_errors.h"
14 #include "net/log/net_log.h"
15 #include "net/server/http_server_request_info.h"
16 #include "net/socket/server_socket.h"
17 #include "net/socket/tcp_server_socket.h"
18
19 namespace ui {
20 namespace devtools {
21
22 namespace {
23 const char kChromeDeveloperToolsPrefix[] =
24 "chrome-devtools://devtools/bundled/inspector.html?ws=";
25 }
26
27 UiDevToolsServer::UiDevToolsServer()
28 : thread_(new base::Thread("UiDevToolsServerThread")) {}
29
30 UiDevToolsServer::~UiDevToolsServer() {}
31
32 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) {
33 if (thread_ && thread_->IsRunning()) {
sadrul 2016/10/17 16:05:08 No {}
Sarmad Hashmi 2016/10/17 16:58:21 Done.
34 return;
35 }
36 // Start IO thread upon which all the methods will run
37 base::Thread::Options options;
38 options.message_loop_type = base::MessageLoop::TYPE_IO;
39 if (thread_->StartWithOptions(options)) {
40 thread_->task_runner()->PostTask(
41 FROM_HERE, base::Bind(&UiDevToolsServer::StartServer,
42 base::Unretained(this), address_string, port));
43 }
44 } // namespace
sadrul 2016/10/17 16:05:08 // namespace should move up above (in line 25)
Sarmad Hashmi 2016/10/17 16:58:21 Done.
45
46 void UiDevToolsServer::StartServer(const std::string& address_string,
47 uint16_t port) {
48 std::unique_ptr<net::ServerSocket> socket(
49 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
50 if (socket->ListenWithAddressAndPort(address_string, port, 1) != net::OK) {
51 return;
sadrul 2016/10/17 16:05:08 No {} The '1' in the param-list is a little bit c
Sarmad Hashmi 2016/10/17 16:58:21 Done.
52 }
53 server_.reset(new net::HttpServer(std::move(socket), this));
sadrul 2016/10/17 16:05:08 server_ = base::MakeUnique<net::HttpServer>(std::m
Sarmad Hashmi 2016/10/17 16:58:21 Done.
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::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
65 clients_.push_back(std::move(client));
66 }
67
68 // HttpServer::Delegate Implementation
69 void UiDevToolsServer::OnConnect(int connection_id) {
70 NOTIMPLEMENTED();
71 }
72
73 void UiDevToolsServer::OnHttpRequest(int connection_id,
74 const net::HttpServerRequestInfo& info) {
75 // Display a simple html page with all the clients and the corresponding
76 // devtools links
77 // TODO(mhashmi): Remove and display all clients under chrome://inspect/#other
78 if (info.path.empty() || info.path == "/") {
79 std::string clientHTML = "<html>";
80 clientHTML +=
81 "<h3>Copy paste the corresponding links in your browser to inspect "
82 "them:</h3>";
83 for (ClientsList::size_type i = 0; i != clients_.size(); i++) {
84 net::IPEndPoint ip;
85 server_->GetLocalAddress(&ip);
86 clientHTML += base::StringPrintf(
87 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>",
88 clients_[i]->name_.c_str(), kChromeDeveloperToolsPrefix,
89 ip.ToString().c_str(), i);
90 }
91 clientHTML += "</html>";
92 thread_->task_runner()->PostTask(
93 FROM_HERE,
94 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()),
95 connection_id, clientHTML, "text/html"));
96 }
97 }
98
99 void UiDevToolsServer::OnWebSocketRequest(
100 int connection_id,
101 const net::HttpServerRequestInfo& info) {
102 size_t target_id;
103 if (info.path.empty() ||
104 !base::StringToSizeT(info.path.substr(1), &target_id) ||
105 target_id > clients_.size())
106 return;
107
108 UiDevToolsClient* client = clients_[target_id].get();
109 // Only one user can inspect the client at a time
110 if (client->connected())
111 return;
112 client->connection_id = connection_id;
113 connections_[connection_id] = client;
114 thread_->task_runner()->PostTask(
115 FROM_HERE,
116 base::Bind(&net::HttpServer::AcceptWebSocket,
117 base::Unretained(server_.get()), connection_id, info));
118 }
119
120 void UiDevToolsServer::OnWebSocketMessage(int connection_id,
121 const std::string& data) {
122 ConnectionsMap::iterator it = connections_.find(connection_id);
123 if (it == connections_.end())
sadrul 2016/10/17 16:05:08 Should this be a DCHECK() that it != connections_.
Sarmad Hashmi 2016/10/17 16:58:21 Done. Makes sense since a connection_id that never
124 return;
125
126 UiDevToolsClient* client = it->second;
127 if (!client) {
128 // Client shut down, close the connection
129 connections_.erase(it);
130 thread_->task_runner()->PostTask(
131 FROM_HERE, base::Bind(&net::HttpServer::Close,
132 base::Unretained(server_.get()), connection_id));
133 return;
134 }
135
136 thread_->task_runner()->PostTask(
137 FROM_HERE,
138 base::Bind(&UiDevToolsClient::Dispatch, base::Unretained(client), data));
139 }
140
141 void UiDevToolsServer::OnClose(int connection_id) {
142 ConnectionsMap::iterator it = connections_.find(connection_id);
143 if (it == connections_.end())
sadrul 2016/10/17 16:05:08 Should this be a DCHECK that it != connections_.en
Sarmad Hashmi 2016/10/17 16:58:21 Done.
144 return;
145 UiDevToolsClient* client = it->second;
146 if (client)
147 client->connection_id = UiDevToolsClient::kNotConnected;
148 connections_.erase(it);
149 }
150
151 } // namespace devtools
152 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698