| 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 <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
| 23 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 24 #include "base/threading/thread_local.h" | 24 #include "base/threading/thread_local.h" |
| 25 #include "chrome/test/chromedriver/chrome/log.h" | |
| 26 #include "chrome/test/chromedriver/chrome/version.h" | 25 #include "chrome/test/chromedriver/chrome/version.h" |
| 26 #include "chrome/test/chromedriver/logging.h" |
| 27 #include "chrome/test/chromedriver/server/http_handler.h" | 27 #include "chrome/test/chromedriver/server/http_handler.h" |
| 28 #include "net/base/ip_endpoint.h" | 28 #include "net/base/ip_endpoint.h" |
| 29 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 30 #include "net/server/http_server.h" | 30 #include "net/server/http_server.h" |
| 31 #include "net/server/http_server_request_info.h" | 31 #include "net/server/http_server_request_info.h" |
| 32 #include "net/server/http_server_response_info.h" | 32 #include "net/server/http_server_response_info.h" |
| 33 #include "net/socket/tcp_listen_socket.h" | 33 #include "net/socket/tcp_listen_socket.h" |
| 34 | 34 |
| 35 #if defined(OS_POSIX) | |
| 36 #include <fcntl.h> | |
| 37 #include <unistd.h> | |
| 38 #endif | |
| 39 | |
| 40 namespace { | 35 namespace { |
| 41 | 36 |
| 42 typedef base::Callback< | 37 typedef base::Callback< |
| 43 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> | 38 void(const net::HttpServerRequestInfo&, const HttpResponseSenderFunc&)> |
| 44 HttpRequestHandlerFunc; | 39 HttpRequestHandlerFunc; |
| 45 | 40 |
| 46 class HttpServer : public net::HttpServer::Delegate { | 41 class HttpServer : public net::HttpServer::Delegate { |
| 47 public: | 42 public: |
| 48 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) | 43 explicit HttpServer(const HttpRequestHandlerFunc& handle_request_func) |
| 49 : handle_request_func_(handle_request_func), | 44 : handle_request_func_(handle_request_func), |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 void StartServerOnIOThread(int port, | 127 void StartServerOnIOThread(int port, |
| 133 const HttpRequestHandlerFunc& handle_request_func) { | 128 const HttpRequestHandlerFunc& handle_request_func) { |
| 134 scoped_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); | 129 scoped_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); |
| 135 if (!temp_server->Start(port)) { | 130 if (!temp_server->Start(port)) { |
| 136 printf("Port not available. Exiting...\n"); | 131 printf("Port not available. Exiting...\n"); |
| 137 exit(1); | 132 exit(1); |
| 138 } | 133 } |
| 139 lazy_tls_server.Pointer()->Set(temp_server.release()); | 134 lazy_tls_server.Pointer()->Set(temp_server.release()); |
| 140 } | 135 } |
| 141 | 136 |
| 142 void RunServer(Log::Level log_level, int port, const std::string& url_base, | 137 void RunServer(int port, const std::string& url_base, int adb_port) { |
| 143 int adb_port) { | |
| 144 base::Thread io_thread("ChromeDriver IO"); | 138 base::Thread io_thread("ChromeDriver IO"); |
| 145 CHECK(io_thread.StartWithOptions( | 139 CHECK(io_thread.StartWithOptions( |
| 146 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 140 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 147 | 141 |
| 148 base::MessageLoop cmd_loop; | 142 base::MessageLoop cmd_loop; |
| 149 base::RunLoop cmd_run_loop; | 143 base::RunLoop cmd_run_loop; |
| 150 Logger log(log_level); | |
| 151 HttpHandler handler(cmd_run_loop.QuitClosure(), | 144 HttpHandler handler(cmd_run_loop.QuitClosure(), |
| 152 io_thread.message_loop_proxy(), | 145 io_thread.message_loop_proxy(), |
| 153 &log, | |
| 154 url_base, | 146 url_base, |
| 155 adb_port); | 147 adb_port); |
| 156 HttpRequestHandlerFunc handle_request_func = | 148 HttpRequestHandlerFunc handle_request_func = |
| 157 base::Bind(&HandleRequestOnCmdThread, &handler); | 149 base::Bind(&HandleRequestOnCmdThread, &handler); |
| 158 | 150 |
| 159 io_thread.message_loop() | 151 io_thread.message_loop() |
| 160 ->PostTask(FROM_HERE, | 152 ->PostTask(FROM_HERE, |
| 161 base::Bind(&StartServerOnIOThread, | 153 base::Bind(&StartServerOnIOThread, |
| 162 port, | 154 port, |
| 163 base::Bind(&HandleRequestOnIOThread, | 155 base::Bind(&HandleRequestOnIOThread, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 178 int main(int argc, char *argv[]) { | 170 int main(int argc, char *argv[]) { |
| 179 CommandLine::Init(argc, argv); | 171 CommandLine::Init(argc, argv); |
| 180 | 172 |
| 181 base::AtExitManager at_exit; | 173 base::AtExitManager at_exit; |
| 182 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 174 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 183 | 175 |
| 184 // Parse command line flags. | 176 // Parse command line flags. |
| 185 int port = 9515; | 177 int port = 9515; |
| 186 int adb_port = 5037; | 178 int adb_port = 5037; |
| 187 std::string url_base; | 179 std::string url_base; |
| 188 base::FilePath log_path; | |
| 189 Log::Level log_level = Log::kError; | |
| 190 if (cmd_line->HasSwitch("h") || cmd_line->HasSwitch("help")) { | 180 if (cmd_line->HasSwitch("h") || cmd_line->HasSwitch("help")) { |
| 191 std::string options; | 181 std::string options; |
| 192 const char* kOptionAndDescriptions[] = { | 182 const char* kOptionAndDescriptions[] = { |
| 193 "port=PORT", "port to listen on", | 183 "port=PORT", "port to listen on", |
| 194 "adb-port=PORT", "adb server port", | 184 "adb-port=PORT", "adb server port", |
| 195 "log-path=FILE", "write server log to file instead of stderr, " | 185 "log-path=FILE", "write server log to file instead of stderr, " |
| 196 "increases log level to INFO", | 186 "increases log level to INFO", |
| 197 "verbose", "log verbosely", | 187 "verbose", "log verbosely", |
| 198 "silent", "log nothing", | 188 "silent", "log nothing", |
| 199 "url-base", "base URL path prefix for commands, e.g. wd/url", | 189 "url-base", "base URL path prefix for commands, e.g. wd/url", |
| (...skipping 18 matching lines...) Expand all Loading... |
| 218 printf("Invalid adb-port. Exiting...\n"); | 208 printf("Invalid adb-port. Exiting...\n"); |
| 219 return 1; | 209 return 1; |
| 220 } | 210 } |
| 221 } | 211 } |
| 222 if (cmd_line->HasSwitch("url-base")) | 212 if (cmd_line->HasSwitch("url-base")) |
| 223 url_base = cmd_line->GetSwitchValueASCII("url-base"); | 213 url_base = cmd_line->GetSwitchValueASCII("url-base"); |
| 224 if (url_base.empty() || url_base[0] != '/') | 214 if (url_base.empty() || url_base[0] != '/') |
| 225 url_base = "/" + url_base; | 215 url_base = "/" + url_base; |
| 226 if (url_base[url_base.length() - 1] != '/') | 216 if (url_base[url_base.length() - 1] != '/') |
| 227 url_base = url_base + "/"; | 217 url_base = url_base + "/"; |
| 228 if (cmd_line->HasSwitch("log-path")) { | |
| 229 log_level = Log::kLog; | |
| 230 log_path = cmd_line->GetSwitchValuePath("log-path"); | |
| 231 #if defined(OS_WIN) | |
| 232 FILE* redir_stderr = _wfreopen(log_path.value().c_str(), L"w", stderr); | |
| 233 #else | |
| 234 FILE* redir_stderr = freopen(log_path.value().c_str(), "w", stderr); | |
| 235 #endif | |
| 236 if (!redir_stderr) { | |
| 237 printf("Failed to redirect stderr to log file. Exiting...\n"); | |
| 238 return 1; | |
| 239 } | |
| 240 } | |
| 241 if (cmd_line->HasSwitch("verbose")) { | |
| 242 log_level = Log::kDebug; | |
| 243 } else { | |
| 244 #if defined(OS_POSIX) | |
| 245 // Close stderr on exec, so that Chrome log spew doesn't confuse users. | |
| 246 fcntl(STDERR_FILENO, F_SETFD, FD_CLOEXEC); | |
| 247 #endif | |
| 248 } | |
| 249 if (!cmd_line->HasSwitch("silent")) { | 218 if (!cmd_line->HasSwitch("silent")) { |
| 250 printf( | 219 printf( |
| 251 "Starting ChromeDriver (v%s) on port %d\n", kChromeDriverVersion, port); | 220 "Starting ChromeDriver (v%s) on port %d\n", kChromeDriverVersion, port); |
| 252 fflush(stdout); | 221 fflush(stdout); |
| 253 } | 222 } |
| 254 | 223 |
| 224 if (!InitLogging()) { |
| 225 printf("Unable to initialize logging. Exiting...\n"); |
| 226 return 1; |
| 227 } |
| 255 | 228 |
| 256 logging::LoggingSettings settings; | 229 RunServer(port, url_base, adb_port); |
| 257 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
| 258 bool success = logging::InitLogging(settings); | |
| 259 if (!success) { | |
| 260 PLOG(ERROR) << "Unable to initialize logging"; | |
| 261 } | |
| 262 logging::SetLogItems(false, // enable_process_id | |
| 263 false, // enable_thread_id | |
| 264 false, // enable_timestamp | |
| 265 false); // enable_tickcount | |
| 266 if (!cmd_line->HasSwitch("verbose")) | |
| 267 logging::SetMinLogLevel(logging::LOG_FATAL); | |
| 268 | |
| 269 RunServer(log_level, port, url_base, adb_port); | |
| 270 return 0; | 230 return 0; |
| 271 } | 231 } |
| OLD | NEW |