OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/test/base/layout_test_http_server.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/logging.h" | |
9 #include "base/path_service.h" | |
10 #include "base/process_util.h" | |
11 #include "base/string_number_conversions.h" | |
12 #include "content/public/common/content_paths.h" | |
13 #include "net/test/python_utils.h" | |
14 | |
15 #if defined(OS_WIN) | |
16 #include "base/win/windows_version.h" | |
17 #endif | |
18 | |
19 namespace { | |
20 | |
21 bool PrepareCommandLine(CommandLine* cmd_line) { | |
22 FilePath src_path; | |
23 if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_path)) | |
24 return false; | |
25 | |
26 FilePath python_runtime; | |
27 if (!GetPythonRunTime(&python_runtime)) | |
28 return false; | |
29 cmd_line->SetProgram(python_runtime); | |
30 | |
31 FilePath script_path(src_path); | |
32 script_path = script_path.AppendASCII("third_party"); | |
33 script_path = script_path.AppendASCII("WebKit"); | |
34 script_path = script_path.AppendASCII("Tools"); | |
35 script_path = script_path.AppendASCII("Scripts"); | |
36 script_path = script_path.AppendASCII("new-run-webkit-httpd"); | |
37 | |
38 cmd_line->AppendArgPath(script_path); | |
39 return true; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 LayoutTestHttpServer::LayoutTestHttpServer(const FilePath& root_directory, | |
45 int port) | |
46 : root_directory_(root_directory), | |
47 port_(port), | |
48 running_(false) { | |
49 } | |
50 | |
51 LayoutTestHttpServer::~LayoutTestHttpServer() { | |
52 if (running_ && !Stop()) | |
53 LOG(ERROR) << "LayoutTestHttpServer failed to stop."; | |
54 } | |
55 | |
56 bool LayoutTestHttpServer::Start() { | |
57 if (running_) { | |
58 LOG(ERROR) << "LayoutTestHttpServer already running."; | |
59 return false; | |
60 } | |
61 | |
62 CommandLine cmd_line(CommandLine::NO_PROGRAM); | |
63 if (!PrepareCommandLine(&cmd_line)) | |
64 return false; | |
65 cmd_line.AppendArg("--server=start"); | |
66 cmd_line.AppendArg("--register_cygwin"); | |
67 cmd_line.AppendArgNative(FILE_PATH_LITERAL("--root=") + | |
68 root_directory_.value()); | |
69 cmd_line.AppendArg("--port=" + base::IntToString(port_)); | |
70 | |
71 FilePath layout_tests_dir; | |
72 if (!PathService::Get(content::DIR_LAYOUT_TESTS, &layout_tests_dir)) | |
73 return false; | |
74 cmd_line.AppendArgNative(FILE_PATH_LITERAL("--layout_tests_dir=") + | |
75 layout_tests_dir.value()); | |
76 | |
77 #if defined(OS_WIN) | |
78 // For Windows 7, if we start the lighttpd server on the foreground mode, | |
79 // it will mess up with the command window and cause conhost.exe to crash. To | |
80 // work around this, we start the http server on the background mode. | |
81 if (base::win::GetVersion() >= base::win::VERSION_WIN7) | |
82 cmd_line.AppendArg("--run_background"); | |
83 | |
84 job_handle_.Set(CreateJobObject(NULL, NULL)); | |
85 if (!job_handle_.IsValid()) { | |
86 LOG(ERROR) << "Could not create JobObject."; | |
87 return false; | |
88 } | |
89 | |
90 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { | |
91 LOG(ERROR) << "Could not SetInformationJobObject."; | |
92 return false; | |
93 } | |
94 #endif | |
95 | |
96 // The Python script waits for the server to start responding to requests, | |
97 // then exits. So we want to wait for the Python script to exit before | |
98 // continuing. | |
99 base::LaunchOptions options; | |
100 options.wait = true; | |
101 #if defined(OS_WIN) | |
102 options.job_handle = job_handle_.Get(); | |
103 #endif | |
104 running_ = base::LaunchProcess(cmd_line, options, NULL); | |
105 return running_; | |
106 } | |
107 | |
108 bool LayoutTestHttpServer::Stop() { | |
109 if (!running_) { | |
110 LOG(ERROR) << "LayoutTestHttpServer not running."; | |
111 return false; | |
112 } | |
113 | |
114 CommandLine cmd_line(CommandLine::NO_PROGRAM); | |
115 if (!PrepareCommandLine(&cmd_line)) | |
116 return false; | |
117 cmd_line.AppendArg("--server=stop"); | |
118 | |
119 base::LaunchOptions options; | |
120 options.wait = true; | |
121 #if defined(OS_WIN) | |
122 options.job_handle = job_handle_.Get(); | |
123 #endif | |
124 bool stopped = base::LaunchProcess(cmd_line, options, NULL); | |
125 running_ = !stopped; | |
126 | |
127 #if defined(OS_WIN) | |
128 // Close the job object handle now. This should clean up | |
129 // any orphaned processes. | |
130 job_handle_.Close(); | |
131 #endif | |
132 | |
133 return stopped; | |
134 } | |
OLD | NEW |