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.
5 #include "content/test/layout_test_http_server.h"
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"
16 #include "base/win/windows_version.h"
22 bool PrepareCommandLine(CommandLine
* cmd_line
) {
23 base::FilePath src_path
;
24 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &src_path
))
27 if (!GetPythonCommand(cmd_line
))
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("new-run-webkit-httpd");
37 cmd_line
->AppendArgPath(script_path
);
43 LayoutTestHttpServer::LayoutTestHttpServer(const base::FilePath
& root_directory
,
45 : root_directory_(root_directory
),
50 LayoutTestHttpServer::~LayoutTestHttpServer() {
51 if (running_
&& !Stop())
52 LOG(ERROR
) << "LayoutTestHttpServer failed to stop.";
55 bool LayoutTestHttpServer::Start() {
57 LOG(ERROR
) << "LayoutTestHttpServer already running.";
61 CommandLine
cmd_line(CommandLine::NO_PROGRAM
);
62 if (!PrepareCommandLine(&cmd_line
))
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_
));
70 base::FilePath layout_tests_dir
;
71 if (!PathService::Get(DIR_LAYOUT_TESTS
, &layout_tests_dir
))
73 cmd_line
.AppendArgNative(FILE_PATH_LITERAL("--layout_tests_dir=") +
74 layout_tests_dir
.value());
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");
83 job_handle_
.Set(CreateJobObject(NULL
, NULL
));
84 if (!job_handle_
.IsValid()) {
85 LOG(ERROR
) << "Could not create JobObject.";
89 if (!base::SetJobObjectAsKillOnJobClose(job_handle_
.Get())) {
90 LOG(ERROR
) << "Could not SetInformationJobObject.";
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
98 base::LaunchOptions options
;
101 options
.job_handle
= job_handle_
.Get();
103 running_
= base::LaunchProcess(cmd_line
, options
, NULL
);
107 bool LayoutTestHttpServer::Stop() {
109 LOG(ERROR
) << "LayoutTestHttpServer not running.";
113 CommandLine
cmd_line(CommandLine::NO_PROGRAM
);
114 if (!PrepareCommandLine(&cmd_line
))
116 cmd_line
.AppendArg("--server=stop");
118 base::LaunchOptions options
;
121 options
.job_handle
= job_handle_
.Get();
123 bool stopped
= base::LaunchProcess(cmd_line
, options
, NULL
);
127 // Close the job object handle now. This should clean up
128 // any orphaned processes.
135 } // namespace content