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