summaryrefslogtreecommitdiffstats
path: root/chrome/test/layout_test_http_server.cc
blob: cac126a7c45681f5cea6ae8439d87ecc2bbf9e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/layout_test_http_server.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/string_number_conversions.h"

#if defined(OS_WIN)
#include "base/win/windows_version.h"
#endif

namespace {

bool PrepareCommandLine(CommandLine* cmd_line) {
  FilePath src_path;
  if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_path))
    return false;

  cmd_line->SetProgram(FilePath(FILE_PATH_LITERAL("python")));

  FilePath script_path(src_path);
  script_path = script_path.AppendASCII("third_party");
  script_path = script_path.AppendASCII("WebKit");
  script_path = script_path.AppendASCII("Tools");
  script_path = script_path.AppendASCII("Scripts");
  script_path = script_path.AppendASCII("new-run-webkit-httpd");

  cmd_line->AppendArgPath(script_path);
  return true;
}

}  // namespace

LayoutTestHttpServer::LayoutTestHttpServer(const FilePath& root_directory,
                                           int port)
    : root_directory_(root_directory),
      port_(port),
      running_(false) {
}

LayoutTestHttpServer::~LayoutTestHttpServer() {
  if (running_ && !Stop())
    LOG(ERROR) << "LayoutTestHttpServer failed to stop.";
}

bool LayoutTestHttpServer::Start() {
  if (running_) {
    LOG(ERROR) << "LayoutTestHttpServer already running.";
    return false;
  }

  CommandLine cmd_line(CommandLine::NO_PROGRAM);
  if (!PrepareCommandLine(&cmd_line))
    return false;
  cmd_line.AppendArg("--server=start");
  cmd_line.AppendArg("--register_cygwin");
  cmd_line.AppendArgNative(FILE_PATH_LITERAL("--root=") +
                           root_directory_.value());
  cmd_line.AppendArg("--port=" + base::IntToString(port_));

  FilePath layout_tests_dir;
  if (!PathService::Get(base::DIR_SOURCE_ROOT, &layout_tests_dir))
    return false;
  layout_tests_dir = layout_tests_dir.AppendASCII("chrome")
                                     .AppendASCII("test")
                                     .AppendASCII("data")
                                     .AppendASCII("layout_tests")
                                     .AppendASCII("LayoutTests");
  cmd_line.AppendArgNative(FILE_PATH_LITERAL("--layout_tests_dir=") +
                           layout_tests_dir.value());

  // For Windows 7, if we start the lighttpd server on the foreground mode,
  // it will mess up with the command window and cause conhost.exe to crash. To
  // work around this, we start the http server on the background mode.
#if defined(OS_WIN)
  if (base::win::GetVersion() >= base::win::VERSION_WIN7)
    cmd_line.AppendArg("--run_background");
#endif

  running_ = base::LaunchApp(cmd_line, true, false, NULL);
  return running_;
}

bool LayoutTestHttpServer::Stop() {
  if (!running_) {
    LOG(ERROR) << "LayoutTestHttpServer not running.";
    return false;
  }

  CommandLine cmd_line(CommandLine::NO_PROGRAM);
  if (!PrepareCommandLine(&cmd_line))
    return false;
  cmd_line.AppendArg("--server=stop");
  bool stopped = base::LaunchApp(cmd_line, true, false, NULL);
  running_ = !stopped;
  return stopped;
}