OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <stddef.h> |
| 6 #include <stdint.h> |
5 #include <stdio.h> | 7 #include <stdio.h> |
6 #include <locale> | 8 #include <locale> |
7 #include <string> | 9 #include <string> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
11 #include "base/bind.h" | 13 #include "base/bind.h" |
12 #include "base/callback.h" | 14 #include "base/callback.h" |
13 #include "base/command_line.h" | 15 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
15 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" |
17 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
19 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
20 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/string_split.h" | 24 #include "base/strings/string_split.h" |
22 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
23 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
24 #include "base/synchronization/waitable_event.h" | 27 #include "base/synchronization/waitable_event.h" |
25 #include "base/thread_task_runner_handle.h" | 28 #include "base/thread_task_runner_handle.h" |
26 #include "base/threading/thread.h" | 29 #include "base/threading/thread.h" |
27 #include "base/threading/thread_local.h" | 30 #include "base/threading/thread_local.h" |
| 31 #include "build/build_config.h" |
28 #include "chrome/test/chromedriver/logging.h" | 32 #include "chrome/test/chromedriver/logging.h" |
29 #include "chrome/test/chromedriver/net/port_server.h" | 33 #include "chrome/test/chromedriver/net/port_server.h" |
30 #include "chrome/test/chromedriver/server/http_handler.h" | 34 #include "chrome/test/chromedriver/server/http_handler.h" |
31 #include "chrome/test/chromedriver/version.h" | 35 #include "chrome/test/chromedriver/version.h" |
32 #include "net/base/ip_endpoint.h" | 36 #include "net/base/ip_endpoint.h" |
33 #include "net/base/net_errors.h" | 37 #include "net/base/net_errors.h" |
34 #include "net/server/http_server.h" | 38 #include "net/server/http_server.h" |
35 #include "net/server/http_server_request_info.h" | 39 #include "net/server/http_server_request_info.h" |
36 #include "net/server/http_server_response_info.h" | 40 #include "net/server/http_server_response_info.h" |
37 #include "net/socket/tcp_server_socket.h" | 41 #include "net/socket/tcp_server_socket.h" |
38 | 42 |
39 namespace { | 43 namespace { |
40 | 44 |
41 const char kLocalHostAddress[] = "127.0.0.1"; | 45 const char kLocalHostAddress[] = "127.0.0.1"; |
42 const int kBufferSize = 100 * 1024 * 1024; // 100 MB | 46 const int kBufferSize = 100 * 1024 * 1024; // 100 MB |
43 | 47 |
44 typedef base::Callback< | 48 typedef base::Callback< |
45 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> | 49 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> |
46 HttpRequestHandlerFunc; | 50 HttpRequestHandlerFunc; |
47 | 51 |
48 class HttpServer : public net::HttpServer::Delegate { | 52 class HttpServer : public net::HttpServer::Delegate { |
49 public: | 53 public: |
50 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) | 54 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) |
51 : handle_request_func_(handle_request_func), | 55 : handle_request_func_(handle_request_func), |
52 weak_factory_(this) {} | 56 weak_factory_(this) {} |
53 | 57 |
54 ~HttpServer() override {} | 58 ~HttpServer() override {} |
55 | 59 |
56 bool Start(uint16 port, bool allow_remote) { | 60 bool Start(uint16_t port, bool allow_remote) { |
57 std::string binding_ip = kLocalHostAddress; | 61 std::string binding_ip = kLocalHostAddress; |
58 if (allow_remote) | 62 if (allow_remote) |
59 binding_ip = "0.0.0.0"; | 63 binding_ip = "0.0.0.0"; |
60 scoped_ptr<net::ServerSocket> server_socket( | 64 scoped_ptr<net::ServerSocket> server_socket( |
61 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 65 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
62 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); | 66 server_socket->ListenWithAddressAndPort(binding_ip, port, 1); |
63 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 67 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
64 net::IPEndPoint address; | 68 net::IPEndPoint address; |
65 return server_->GetLocalAddress(&address) == net::OK; | 69 return server_->GetLocalAddress(&address) == net::OK; |
66 } | 70 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 base::LazyInstance<base::ThreadLocalPointer<HttpServer> > | 150 base::LazyInstance<base::ThreadLocalPointer<HttpServer> > |
147 lazy_tls_server = LAZY_INSTANCE_INITIALIZER; | 151 lazy_tls_server = LAZY_INSTANCE_INITIALIZER; |
148 | 152 |
149 void StopServerOnIOThread() { | 153 void StopServerOnIOThread() { |
150 // Note, |server| may be NULL. | 154 // Note, |server| may be NULL. |
151 HttpServer* server = lazy_tls_server.Pointer()->Get(); | 155 HttpServer* server = lazy_tls_server.Pointer()->Get(); |
152 lazy_tls_server.Pointer()->Set(NULL); | 156 lazy_tls_server.Pointer()->Set(NULL); |
153 delete server; | 157 delete server; |
154 } | 158 } |
155 | 159 |
156 void StartServerOnIOThread(uint16 port, | 160 void StartServerOnIOThread(uint16_t port, |
157 bool allow_remote, | 161 bool allow_remote, |
158 const HttpRequestHandlerFunc& handle_request_func) { | 162 const HttpRequestHandlerFunc& handle_request_func) { |
159 scoped_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); | 163 scoped_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); |
160 if (!temp_server->Start(port, allow_remote)) { | 164 if (!temp_server->Start(port, allow_remote)) { |
161 printf("Port not available. Exiting...\n"); | 165 printf("Port not available. Exiting...\n"); |
162 exit(1); | 166 exit(1); |
163 } | 167 } |
164 lazy_tls_server.Pointer()->Set(temp_server.release()); | 168 lazy_tls_server.Pointer()->Set(temp_server.release()); |
165 } | 169 } |
166 | 170 |
167 void RunServer(uint16 port, | 171 void RunServer(uint16_t port, |
168 bool allow_remote, | 172 bool allow_remote, |
169 const std::vector<std::string>& whitelisted_ips, | 173 const std::vector<std::string>& whitelisted_ips, |
170 const std::string& url_base, | 174 const std::string& url_base, |
171 int adb_port, | 175 int adb_port, |
172 scoped_ptr<PortServer> port_server) { | 176 scoped_ptr<PortServer> port_server) { |
173 base::Thread io_thread("ChromeDriver IO"); | 177 base::Thread io_thread("ChromeDriver IO"); |
174 CHECK(io_thread.StartWithOptions( | 178 CHECK(io_thread.StartWithOptions( |
175 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 179 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
176 | 180 |
177 base::MessageLoop cmd_loop; | 181 base::MessageLoop cmd_loop; |
(...skipping 27 matching lines...) Expand all Loading... |
205 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 209 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
206 | 210 |
207 #if defined(OS_LINUX) | 211 #if defined(OS_LINUX) |
208 // Select the locale from the environment by passing an empty string instead | 212 // Select the locale from the environment by passing an empty string instead |
209 // of the default "C" locale. This is particularly needed for the keycode | 213 // of the default "C" locale. This is particularly needed for the keycode |
210 // conversion code to work. | 214 // conversion code to work. |
211 setlocale(LC_ALL, ""); | 215 setlocale(LC_ALL, ""); |
212 #endif | 216 #endif |
213 | 217 |
214 // Parse command line flags. | 218 // Parse command line flags. |
215 uint16 port = 9515; | 219 uint16_t port = 9515; |
216 int adb_port = 5037; | 220 int adb_port = 5037; |
217 bool allow_remote = false; | 221 bool allow_remote = false; |
218 std::vector<std::string> whitelisted_ips; | 222 std::vector<std::string> whitelisted_ips; |
219 std::string url_base; | 223 std::string url_base; |
220 scoped_ptr<PortServer> port_server; | 224 scoped_ptr<PortServer> port_server; |
221 if (cmd_line->HasSwitch("h") || cmd_line->HasSwitch("help")) { | 225 if (cmd_line->HasSwitch("h") || cmd_line->HasSwitch("help")) { |
222 std::string options; | 226 std::string options; |
223 const char* const kOptionAndDescriptions[] = { | 227 const char* const kOptionAndDescriptions[] = { |
224 "port=PORT", "port to listen on", | 228 "port=PORT", "port to listen on", |
225 "adb-port=PORT", "adb server port", | 229 "adb-port=PORT", "adb server port", |
(...skipping 20 matching lines...) Expand all Loading... |
246 return 0; | 250 return 0; |
247 } | 251 } |
248 if (cmd_line->HasSwitch("port")) { | 252 if (cmd_line->HasSwitch("port")) { |
249 int cmd_line_port; | 253 int cmd_line_port; |
250 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("port"), | 254 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("port"), |
251 &cmd_line_port) || | 255 &cmd_line_port) || |
252 cmd_line_port < 0 || cmd_line_port > 65535) { | 256 cmd_line_port < 0 || cmd_line_port > 65535) { |
253 printf("Invalid port. Exiting...\n"); | 257 printf("Invalid port. Exiting...\n"); |
254 return 1; | 258 return 1; |
255 } | 259 } |
256 port = static_cast<uint16>(cmd_line_port); | 260 port = static_cast<uint16_t>(cmd_line_port); |
257 } | 261 } |
258 if (cmd_line->HasSwitch("adb-port")) { | 262 if (cmd_line->HasSwitch("adb-port")) { |
259 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("adb-port"), | 263 if (!base::StringToInt(cmd_line->GetSwitchValueASCII("adb-port"), |
260 &adb_port)) { | 264 &adb_port)) { |
261 printf("Invalid adb-port. Exiting...\n"); | 265 printf("Invalid adb-port. Exiting...\n"); |
262 return 1; | 266 return 1; |
263 } | 267 } |
264 } | 268 } |
265 if (cmd_line->HasSwitch("port-server")) { | 269 if (cmd_line->HasSwitch("port-server")) { |
266 #if defined(OS_LINUX) | 270 #if defined(OS_LINUX) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 } | 308 } |
305 | 309 |
306 if (!InitLogging()) { | 310 if (!InitLogging()) { |
307 printf("Unable to initialize logging. Exiting...\n"); | 311 printf("Unable to initialize logging. Exiting...\n"); |
308 return 1; | 312 return 1; |
309 } | 313 } |
310 RunServer(port, allow_remote, whitelisted_ips, | 314 RunServer(port, allow_remote, whitelisted_ips, |
311 url_base, adb_port, port_server.Pass()); | 315 url_base, adb_port, port_server.Pass()); |
312 return 0; | 316 return 0; |
313 } | 317 } |
OLD | NEW |