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

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: sadruls comments 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
« no previous file with comments | « components/ui_devtools/devtools_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 10 matching lines...) Expand all
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 : thread_(new base::Thread("UiDevToolsServerThread")) {
32 base::Thread::Options options;
33 options.message_loop_type = base::MessageLoop::TYPE_IO;
34 CHECK(thread_->StartWithOptions(options));
35 task_runner_ = thread_->task_runner();
36 }
37
38 UiDevToolsServer::UiDevToolsServer(
39 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
40 : task_runner_(task_runner) {
41 DCHECK(task_runner_);
sadrul 2016/10/29 17:14:59 You can remove the default ctor, and optionally cr
Sarmad Hashmi 2016/10/29 17:27:26 Done.
42 }
32 43
33 UiDevToolsServer::~UiDevToolsServer() {} 44 UiDevToolsServer::~UiDevToolsServer() {}
34 45
35 // static 46 // static
36 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create() { 47 std::unique_ptr<UiDevToolsServer> UiDevToolsServer::Create(
48 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
37 std::unique_ptr<UiDevToolsServer> server; 49 std::unique_ptr<UiDevToolsServer> server;
38 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) { 50 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableUiDevTools)) {
39 // TODO(mhashmi): Change port if more than one inspectable clients 51 // TODO(mhashmi): Change port if more than one inspectable clients
40 int port = 9223; // Default port is 9223 52 int port = 9223; // Default port is 9223
41 base::StringToInt( 53 base::StringToInt(
42 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 54 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
43 kEnableUiDevTools), 55 kEnableUiDevTools),
44 &port); 56 &port);
45 server.reset(new UiDevToolsServer()); 57 server.reset(task_runner ? new UiDevToolsServer(task_runner)
58 : new UiDevToolsServer());
46 server->Start("127.0.0.1", port); 59 server->Start("127.0.0.1", port);
47 } 60 }
48 return server; 61 return server;
49 } 62 }
50 63
51 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) { 64 void UiDevToolsServer::AttachClient(std::unique_ptr<UiDevToolsClient> client) {
52 clients_.push_back(std::move(client)); 65 clients_.push_back(std::move(client));
53 } 66 }
54 67
55 void UiDevToolsServer::SendOverWebSocket(int connection_id, 68 void UiDevToolsServer::SendOverWebSocket(int connection_id,
56 const String& message) { 69 const String& message) {
57 thread_->task_runner()->PostTask( 70 task_runner_->PostTask(
58 FROM_HERE, 71 FROM_HERE,
59 base::Bind(&net::HttpServer::SendOverWebSocket, 72 base::Bind(&net::HttpServer::SendOverWebSocket,
60 base::Unretained(server_.get()), connection_id, message)); 73 base::Unretained(server_.get()), connection_id, message));
61 } 74 }
62 75
63 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) { 76 void UiDevToolsServer::Start(const std::string& address_string, uint16_t port) {
64 if (thread_ && thread_->IsRunning()) 77 task_runner_->PostTask(
65 return; 78 FROM_HERE, base::Bind(&UiDevToolsServer::StartServer,
66 79 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 } 80 }
76 81
77 void UiDevToolsServer::StartServer(const std::string& address_string, 82 void UiDevToolsServer::StartServer(const std::string& address_string,
78 uint16_t port) { 83 uint16_t port) {
79 std::unique_ptr<net::ServerSocket> socket( 84 std::unique_ptr<net::ServerSocket> socket(
sadrul 2016/10/29 17:14:59 Early return if |server_| is already set. (or if t
Sarmad Hashmi 2016/10/29 17:27:26 Done.
80 new net::TCPServerSocket(nullptr, net::NetLogSource())); 85 new net::TCPServerSocket(nullptr, net::NetLogSource()));
81 constexpr int kBacklog = 1; 86 constexpr int kBacklog = 1;
82 if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) != 87 if (socket->ListenWithAddressAndPort(address_string, port, kBacklog) !=
83 net::OK) 88 net::OK)
84 return; 89 return;
85 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this); 90 server_ = base::MakeUnique<net::HttpServer>(std::move(socket), this);
86 } 91 }
87 92
88 // HttpServer::Delegate Implementation 93 // HttpServer::Delegate Implementation
89 void UiDevToolsServer::OnConnect(int connection_id) { 94 void UiDevToolsServer::OnConnect(int connection_id) {
(...skipping 12 matching lines...) Expand all
102 "them:</h3>"; 107 "them:</h3>";
103 net::IPEndPoint ip; 108 net::IPEndPoint ip;
104 server_->GetLocalAddress(&ip); 109 server_->GetLocalAddress(&ip);
105 for (ClientsList::size_type i = 0; i != clients_.size(); i++) { 110 for (ClientsList::size_type i = 0; i != clients_.size(); i++) {
106 clientHTML += base::StringPrintf( 111 clientHTML += base::StringPrintf(
107 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>", 112 "<p><strong>%s</strong> (%s%s/%" PRIuS ")</p>",
108 clients_[i]->name().c_str(), kChromeDeveloperToolsPrefix, 113 clients_[i]->name().c_str(), kChromeDeveloperToolsPrefix,
109 ip.ToString().c_str(), i); 114 ip.ToString().c_str(), i);
110 } 115 }
111 clientHTML += "</html>"; 116 clientHTML += "</html>";
112 thread_->task_runner()->PostTask( 117 task_runner_->PostTask(
113 FROM_HERE, 118 FROM_HERE,
114 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()), 119 base::Bind(&net::HttpServer::Send200, base::Unretained(server_.get()),
115 connection_id, clientHTML, "text/html")); 120 connection_id, clientHTML, "text/html"));
121 return;
116 } 122 }
123 task_runner_->PostTask(
124 FROM_HERE, base::Bind(&net::HttpServer::Send404,
125 base::Unretained(server_.get()), connection_id));
117 } 126 }
118 127
119 void UiDevToolsServer::OnWebSocketRequest( 128 void UiDevToolsServer::OnWebSocketRequest(
120 int connection_id, 129 int connection_id,
121 const net::HttpServerRequestInfo& info) { 130 const net::HttpServerRequestInfo& info) {
122 size_t target_id = 0; 131 size_t target_id = 0;
123 if (info.path.empty() || 132 if (info.path.empty() ||
124 !base::StringToSizeT(info.path.substr(1), &target_id) || 133 !base::StringToSizeT(info.path.substr(1), &target_id) ||
125 target_id > clients_.size()) 134 target_id > clients_.size())
126 return; 135 return;
127 136
128 UiDevToolsClient* client = clients_[target_id].get(); 137 UiDevToolsClient* client = clients_[target_id].get();
129 // Only one user can inspect the client at a time 138 // Only one user can inspect the client at a time
130 if (client->connected()) 139 if (client->connected())
131 return; 140 return;
132 client->set_connection_id(connection_id); 141 client->set_connection_id(connection_id);
133 connections_[connection_id] = client; 142 connections_[connection_id] = client;
134 thread_->task_runner()->PostTask( 143 task_runner_->PostTask(
135 FROM_HERE, 144 FROM_HERE,
136 base::Bind(&net::HttpServer::AcceptWebSocket, 145 base::Bind(&net::HttpServer::AcceptWebSocket,
137 base::Unretained(server_.get()), connection_id, info)); 146 base::Unretained(server_.get()), connection_id, info));
138 } 147 }
139 148
140 void UiDevToolsServer::OnWebSocketMessage(int connection_id, 149 void UiDevToolsServer::OnWebSocketMessage(int connection_id,
141 const std::string& data) { 150 const std::string& data) {
142 ConnectionsMap::iterator it = connections_.find(connection_id); 151 ConnectionsMap::iterator it = connections_.find(connection_id);
143 DCHECK(it != connections_.end()); 152 DCHECK(it != connections_.end());
144 UiDevToolsClient* client = it->second; 153 UiDevToolsClient* client = it->second;
145 DCHECK(client); 154 DCHECK(client);
146 thread_->task_runner()->PostTask( 155 task_runner_->PostTask(FROM_HERE, base::Bind(&UiDevToolsClient::Dispatch,
147 FROM_HERE, 156 base::Unretained(client), data));
148 base::Bind(&UiDevToolsClient::Dispatch, base::Unretained(client), data));
149 } 157 }
150 158
151 void UiDevToolsServer::OnClose(int connection_id) { 159 void UiDevToolsServer::OnClose(int connection_id) {
152 ConnectionsMap::iterator it = connections_.find(connection_id); 160 ConnectionsMap::iterator it = connections_.find(connection_id);
153 DCHECK(it != connections_.end()); 161 if (it == connections_.end())
162 return;
154 UiDevToolsClient* client = it->second; 163 UiDevToolsClient* client = it->second;
155 DCHECK(client); 164 DCHECK(client);
156 client->set_connection_id(UiDevToolsClient::kNotConnected); 165 client->set_connection_id(UiDevToolsClient::kNotConnected);
157 connections_.erase(it); 166 connections_.erase(it);
158 } 167 }
159 168
160 } // namespace devtools 169 } // namespace devtools
161 } // namespace ui 170 } // namespace ui
OLDNEW
« no previous file with comments | « components/ui_devtools/devtools_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698