summaryrefslogtreecommitdiffstats
path: root/headless/public/headless_browser.h
blob: a94a90c7541df50567bad71262beff2b6aa2d40c (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2015 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.

#ifndef HEADLESS_PUBLIC_HEADLESS_BROWSER_H_
#define HEADLESS_PUBLIC_HEADLESS_BROWSER_H_

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "headless/public/headless_export.h"

namespace base {
class SingleThreadTaskRunner;

namespace trace_event {
class TraceConfig;
}
}

namespace gfx {
class Size;
}

namespace net {
class URLRequestContextGetter;
}

namespace headless {
class WebContents;

class HEADLESS_EXPORT HeadlessBrowser {
 public:
  static HeadlessBrowser* Get();

  struct Options;

  // Main routine for running browser.
  // Takes command line args and callback to run as soon as browser starts.
  static int Run(
      const Options& options,
      const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback);

  // Create a new browser tab.
  virtual scoped_ptr<WebContents> CreateWebContents(const gfx::Size& size) = 0;

  virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserMainThread() = 0;
  virtual scoped_refptr<base::SingleThreadTaskRunner> RendererMainThread() = 0;

  // Requests browser to stop as soon as possible.
  // |Run| will return as soon as browser stops.
  virtual void Stop() = 0;

  virtual void StartTracing(const base::trace_event::TraceConfig& trace_config,
                            const base::Closure& on_tracing_started) = 0;
  virtual void StopTracing(const std::string& log_file_name,
                           const base::Closure& on_tracing_stopped) = 0;

 protected:
  virtual ~HeadlessBrowser() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser);
};

struct HeadlessBrowser::Options {
  ~Options();

  class Builder;

  // Command line options to be passed to browser.
  int argc;
  const char** argv;

  std::string user_agent;
  std::string navigator_platform;

  static const int kInvalidPort = -1;
  // If not null, create start devtools for remote debugging
  // on specified port.
  int devtools_http_port;

  // Optional URLRequestContextGetter for customizing network stack.
  // Allows overriding:
  // - Cookie storage
  // - HTTP cache
  // - SSL config
  // - Proxy service
  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter;

  scoped_ptr<base::MessagePump> message_pump;

 private:
  Options(int argc, const char** argv);
};

class HeadlessBrowser::Options::Builder {
 public:
  Builder(int argc, const char** argv);
  ~Builder();

  Builder& SetUserAgent(const std::string& user_agent);
  Builder& EnableDevToolsServer(int port);
  Builder& SetURLRequestContextGetter(
      scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);

  Options Build();

 private:
  Options options_;

  DISALLOW_COPY_AND_ASSIGN(Builder);
};

}  // namespace headless

#endif  // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_