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

Side by Side Diff: components/ui_devtools/devtools_server.cc

Issue 2455833002: Pass task_runner_ to server and send 404 when incorrect url (Closed)
Patch Set: Created 4 years, 1 month 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 "components/ui_devtools/devtools_server.h" 5 #include "components/ui_devtools/devtools_server.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "components/ui_devtools/switches.h" 15 #include "components/ui_devtools/switches.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/log/net_log.h" 17 #include "net/log/net_log.h"
18 #include "net/server/http_server_request_info.h" 18 #include "net/server/http_server_request_info.h"
19 #include "net/socket/server_socket.h" 19 #include "net/socket/server_socket.h"
20 #include "net/socket/tcp_server_socket.h" 20 #include "net/socket/tcp_server_socket.h"
21 21
22 namespace ui { 22 namespace ui {
23 namespace devtools { 23 namespace devtools {
24 24
25 namespace { 25 namespace {
26 const char kChromeDeveloperToolsPrefix[] = 26 const char kChromeDeveloperToolsPrefix[] =
27 "chrome-devtools://devtools/bundled/inspector.html?ws="; 27 "chrome-devtools://devtools/bundled/inspector.html?ws=";
28 } // namespace 28 } // namespace
29 29
30 UiDevToolsServer::UiDevToolsServer() 30 UiDevToolsServer::UiDevToolsServer(
31 : thread_(new base::Thread("UiDevToolsServerThread")) {} 31 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
32 : task_runner_(task_runner) {
33 DCHECK(task_runner_);
34 }
32 35
33 UiDevToolsServer::~UiDevToolsServer() {} 36 UiDevToolsServer::~UiDevToolsServer() {}
34 37
35 // static 38 // static
36 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create() { 39 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create(
40 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
37 std::unique_ptr<UiDevToolsServer> server; 41 std::unique_ptr<UiDevToolsServer> server;
38 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) { 42 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) {
39 // TODO(mhashmi): Change port if more than one inspectable clients 43 // TODO(mhashmi): Change port if more than one inspectable clients
40 int port = 9223; // Default port is 9223 44 int port = 9223; // Default port is 9223
41 base::StringToInt( 45 base::StringToInt(
42 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 46 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
43 kEnableUiDevTools), 47 kEnableUiDevTools),
44 &port); 48 &port);
45 server.reset(new UiDevToolsServer()); 49 server.reset(new UiDevToolsServer(task_runner));
46 server->Start("127.0.0.1", port); 50 server->Start("127.0.0.1", port);
47 } 51 }
48 return server; 52 return server;
49 } 53 }
50 54
51 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { 55 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
52 clients_.push_back(std::move(client)); 56 clients_.push_back(std::move(client));
53 } 57 }
54 58
55 void UiDevToolsServer::SendOverWebSocket(int connection_id, 59 void UiDevToolsServer::SendOverWebSocket(int connection_id,
56 const String& message) { 60 const String& message) {
57 thread_->task_runner()->PostTask( 61 task_runner_->PostTask(
58 FROM_HERE, 62 FROM_HERE,
59 base::Bind(&net::HttpServer::SendOverWebSocket, 63 base::Bind(&net::HttpServer::SendOverWebSocket,
60 base::Unretained(server_.get()), connection_id, message)); 64 base::Unretained(server_.get()), connection_id, message));
61 } 65 }
62 66
63 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) { 67 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) {
64 if (thread_ && thread_->IsRunning()) 68 task_runner_->PostTask(
65 return; 69 FROM_HERE, base::Bind(&UiDevToolsServer::StartServer,
66 70 base::Unretained(this), address_string, port));
67 // Start IO thread upon which all the methods will run
68 base::Thread::Options options;
69 options.message_loop_type = base::MessageLoop::TYPE_IO;
70 if (thread_->StartWithOptions(options)) {
71 thread_->task_runner()->PostTask(
72 FROM_HERE, base::Bind(&UiDevToolsServer::StartServer,
73 base::Unretained(this), address_string, port));
74 }
75 } 71 }
76 72
77 void UiDevToolsServer::StartServer(const std::string& address_string, 73 void UiDevToolsServer::StartServer(const std::string& address_string,
78 uint16_t port) { 74 uint16_t port) {
79 std::unique_ptr<net::ServerSocket> socket( 75 std::unique_ptr<net::ServerSocket> socket(
80 new net::TCPServerSocket(nullptr, net::NetLogSource())); 76 new net::TCPServerSocket(nullptr, net::NetLogSource()));
81 constexpr int kBacklog = 1; 77 constexpr int kBacklog = 1;
82 if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) != 78 if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) !=
83 net::OK) 79 net::OK)
84 return; 80 return;
(...skipping 17 matching lines...) Expand all
102 "them:</h3>"; 98 "them:</h3>";
103 net::IPEndPoint ip; 99 net::IPEndPoint ip;
104 server_->GetLocalAddress(&ip); 100 server_->GetLocalAddress(&ip);
105 for (ClientsList::size_type i = 0; i != clients_.size(); i++) { 101 for (ClientsList::size_type i = 0; i != clients_.size(); i++) {
106 clientHTML += base::StringPrintf( 102 clientHTML += base::StringPrintf(
107 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>", 103 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>",
108 clients_[i]->name().c_str(), kChromeDeveloperToolsPrefix, 104 clients_[i]->name().c_str(), kChromeDeveloperToolsPrefix,
109 ip.ToString().c_str(), i); 105 ip.ToString().c_str(), i);
110 } 106 }
111 clientHTML += "</html>"; 107 clientHTML += "</html>";
112 thread_->task_runner()->PostTask( 108 task_runner_->PostTask(
113 FROM_HERE, 109 FROM_HERE,
114 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()), 110 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()),
115 connection_id, clientHTML, "text/html")); 111 connection_id, clientHTML, "text/html"));
112 return;
116 } 113 }
114 task_runner_->PostTask(
115 FROM_HERE, base::Bind(&net::HttpServer::Send404,
116 base::Unretained(server_.get()), connection_id));
117 } 117 }
118 118
119 void UiDevToolsServer::OnWebSocketRequest( 119 void UiDevToolsServer::OnWebSocketRequest(
120 int connection_id, 120 int connection_id,
121 const net::HttpServerRequestInfo& info) { 121 const net::HttpServerRequestInfo& info) {
122 size_t target_id = 0; 122 size_t target_id = 0;
123 if (info.path.empty() || 123 if (info.path.empty() ||
124 !base::StringToSizeT(info.path.substr(1), &target_id) || 124 !base::StringToSizeT(info.path.substr(1), &target_id) ||
125 target_id > clients_.size()) 125 target_id > clients_.size())
126 return; 126 return;
127 127
128 UiDevToolsClient* client = clients_[target_id].get(); 128 UiDevToolsClient* client = clients_[target_id].get();
129 // Only one user can inspect the client at a time 129 // Only one user can inspect the client at a time
130 if (client->connected()) 130 if (client->connected())
131 return; 131 return;
132 client->set_connection_id(connection_id); 132 client->set_connection_id(connection_id);
133 connections_[connection_id] = client; 133 connections_[connection_id] = client;
134 thread_->task_runner()->PostTask( 134 task_runner_->PostTask(
135 FROM_HERE, 135 FROM_HERE,
136 base::Bind(&net::HttpServer::AcceptWebSocket, 136 base::Bind(&net::HttpServer::AcceptWebSocket,
137 base::Unretained(server_.get()), connection_id, info)); 137 base::Unretained(server_.get()), connection_id, info));
138 } 138 }
139 139
140 void UiDevToolsServer::OnWebSocketMessage(int connection_id, 140 void UiDevToolsServer::OnWebSocketMessage(int connection_id,
141 const std::string& data) { 141 const std::string& data) {
142 ConnectionsMap::iterator it = connections_.find(connection_id); 142 ConnectionsMap::iterator it = connections_.find(connection_id);
143 DCHECK(it != connections_.end()); 143 DCHECK(it != connections_.end());
144 UiDevToolsClient* client = it->second; 144 UiDevToolsClient* client = it->second;
145 DCHECK(client); 145 DCHECK(client);
146 thread_->task_runner()->PostTask( 146 task_runner_->PostTask(FROM_HERE, base::Bind(&UiDevToolsClient::Dispatch,
147 FROM_HERE, 147 base::Unretained(client), data));
148 base::Bind(&UiDevToolsClient::Dispatch, base::Unretained(client), data));
149 } 148 }
150 149
151 void UiDevToolsServer::OnClose(int connection_id) { 150 void UiDevToolsServer::OnClose(int connection_id) {
152 ConnectionsMap::iterator it = connections_.find(connection_id); 151 ConnectionsMap::iterator it = connections_.find(connection_id);
153 DCHECK(it != connections_.end()); 152 if (it == connections_.end())
153 return;
sadrul 2016/10/28 02:32:27 Hm, any idea why this happens?
Sarmad Hashmi 2016/10/28 17:02:46 So, I realized we need this either way because if
154 UiDevToolsClient* client = it->second; 154 UiDevToolsClient* client = it->second;
155 DCHECK(client); 155 DCHECK(client);
156 client->set_connection_id(UiDevToolsClient::kNotConnected); 156 client->set_connection_id(UiDevToolsClient::kNotConnected);
157 connections_.erase(it); 157 connections_.erase(it);
158 } 158 }
159 159
160 } // namespace devtools 160 } // namespace devtools
161 } // namespace ui 161 } // namespace ui
OLDNEW
« components/ui_devtools/devtools_server.h ('K') | « components/ui_devtools/devtools_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698