summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/chrome_launcher_impl.cc
blob: e811582dce0a4d4550d6478e24d1cddcec509319 (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
// Copyright (c) 2012 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/chromedriver/chrome_launcher_impl.h"

#include <string>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/process.h"
#include "base/process_util.h"
#include "base/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/test/chromedriver/chrome.h"
#include "chrome/test/chromedriver/chrome_finder.h"
#include "chrome/test/chromedriver/chrome_impl.h"
#include "chrome/test/chromedriver/net/url_request_context_getter.h"
#include "chrome/test/chromedriver/status.h"
#include "chrome/test/chromedriver/version.h"

ChromeLauncherImpl::ChromeLauncherImpl(
    URLRequestContextGetter* context_getter,
    const SyncWebSocketFactory& socket_factory)
    : context_getter_(context_getter),
      socket_factory_(socket_factory) {}

ChromeLauncherImpl::~ChromeLauncherImpl() {}

Status ChromeLauncherImpl::Launch(
    const base::FilePath& chrome_exe,
    scoped_ptr<Chrome>* chrome) {
  base::FilePath program = chrome_exe;
  if (program.empty()) {
    if (!FindChrome(&program))
      return Status(kUnknownError, "cannot find Chrome binary");
  }

  int port = 33081;
  CommandLine command(program);
  command.AppendSwitchASCII("remote-debugging-port", base::IntToString(port));
  command.AppendSwitch("no-first-run");
  command.AppendSwitch("enable-logging");
  command.AppendSwitchASCII("logging-level", "1");
  base::ScopedTempDir user_data_dir;
  if (!user_data_dir.CreateUniqueTempDir())
    return Status(kUnknownError, "cannot create temp dir for user data dir");
  command.AppendSwitchPath("user-data-dir", user_data_dir.path());
  command.AppendArg("data:text/html;charset=utf-8,");

  base::LaunchOptions options;
  base::ProcessHandle process;
  if (!base::LaunchProcess(command, options, &process))
    return Status(kUnknownError, "chrome failed to start");
  scoped_ptr<ChromeImpl> chrome_impl(new ChromeImpl(
      process, context_getter_, &user_data_dir, port, socket_factory_));
  Status status = chrome_impl->Init();
  if (status.IsError())
    return status;
  chrome->reset(chrome_impl.release());
  return Status(kOk);
}