OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 5 #ifndef HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
6 #define HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 6 #define HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <unordered_map> | 10 #include <unordered_map> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "headless/public/headless_browser_context.h" | 17 #include "headless/public/headless_browser_context.h" |
17 #include "headless/public/headless_export.h" | 18 #include "headless/public/headless_export.h" |
18 #include "headless/public/headless_web_contents.h" | 19 #include "headless/public/headless_web_contents.h" |
19 #include "net/base/host_port_pair.h" | 20 #include "net/base/host_port_pair.h" |
20 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
21 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 class MessagePump; | 25 class MessagePump; |
25 class SingleThreadTaskRunner; | 26 class SingleThreadTaskRunner; |
26 } | 27 } |
27 | 28 |
28 namespace headless { | 29 namespace headless { |
29 | 30 |
30 // This class represents the global headless browser instance. To get a pointer | 31 // This class represents the global headless browser instance. To get a pointer |
31 // to one, call |HeadlessBrowserMain| to initiate the browser main loop. An | 32 // to one, call |HeadlessBrowserMain| to initiate the browser main loop. An |
32 // instance of |HeadlessBrowser| will be passed to the callback given to that | 33 // instance of |HeadlessBrowser| will be passed to the callback given to that |
33 // function. | 34 // function. |
34 class HEADLESS_EXPORT HeadlessBrowser { | 35 class HEADLESS_EXPORT HeadlessBrowser { |
35 public: | 36 public: |
36 struct Options; | 37 struct Options; |
37 | 38 |
38 // Open a new tab. Returns a builder object which can be used to set | 39 // Create a new browser context which can be used to create tabs and isolate |
39 // properties for the new tab. | 40 // from one another. |
40 virtual HeadlessWebContents::Builder CreateWebContentsBuilder() = 0; | 41 virtual HeadlessBrowserContext::Builder CreateBrowserContextBuilder() = 0; |
41 | |
42 // Deprecated. Use CreateWebContentsBuilder() instead. | |
43 virtual HeadlessWebContents* CreateWebContents(const GURL& initial_url, | |
44 const gfx::Size& size) = 0; | |
45 | 42 |
46 virtual std::vector<HeadlessWebContents*> GetAllWebContents() = 0; | 43 virtual std::vector<HeadlessWebContents*> GetAllWebContents() = 0; |
47 | 44 |
48 // Returns the HeadlessWebContents associated with the | 45 // Returns the HeadlessWebContents associated with the |
49 // |devtools_agent_host_id| if any. Otherwise returns null. | 46 // |devtools_agent_host_id| if any. Otherwise returns null. |
50 virtual HeadlessWebContents* GetWebContentsForDevtoolsAgentHostId( | 47 virtual HeadlessWebContents* GetWebContentsForDevtoolsAgentHostId( |
51 const std::string& devtools_agent_host_id) = 0; | 48 const std::string& devtools_agent_host_id) = 0; |
52 | 49 |
53 // Returns a task runner for submitting work to the browser main thread. | 50 // Returns a task runner for submitting work to the browser main thread. |
54 virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserMainThread() | 51 virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserMainThread() |
55 const = 0; | 52 const = 0; |
56 | 53 |
57 // Returns a task runner for submitting work to the browser file thread. | 54 // Returns a task runner for submitting work to the browser file thread. |
58 virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserFileThread() | 55 virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserFileThread() |
59 const = 0; | 56 const = 0; |
60 | 57 |
61 // Requests browser to stop as soon as possible. |Run| will return as soon as | 58 // Requests browser to stop as soon as possible. |Run| will return as soon as |
62 // browser stops. | 59 // browser stops. |
63 virtual void Shutdown() = 0; | 60 virtual void Shutdown() = 0; |
64 | 61 |
65 // Create a new browser context, which can be used to isolate | |
66 // HeadlessWebContents from one another. | |
67 virtual HeadlessBrowserContext::Builder CreateBrowserContextBuilder() = 0; | |
68 | |
69 protected: | 62 protected: |
70 HeadlessBrowser() {} | 63 HeadlessBrowser() {} |
71 virtual ~HeadlessBrowser() {} | 64 virtual ~HeadlessBrowser() {} |
72 | 65 |
73 private: | 66 private: |
74 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser); | 67 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser); |
75 }; | 68 }; |
76 | 69 |
77 // Embedding API overrides for the headless browser. | 70 // Embedding API overrides for the headless browser. |
78 struct HeadlessBrowser::Options { | 71 struct HeadlessBrowser::Options { |
79 class Builder; | 72 class Builder; |
80 | 73 |
81 Options(Options&& options); | 74 Options(Options&& options); |
82 ~Options(); | 75 ~Options(); |
83 | 76 |
84 Options& operator=(Options&& options); | 77 Options& operator=(Options&& options); |
85 | 78 |
86 // Command line options to be passed to browser. | 79 // Command line options to be passed to browser. |
87 int argc; | 80 int argc; |
88 const char** argv; | 81 const char** argv; |
89 | 82 |
90 std::string user_agent; | |
91 std::string navigator_platform; | |
92 | |
93 // Address at which DevTools should listen for connections. Disabled by | 83 // Address at which DevTools should listen for connections. Disabled by |
94 // default. | 84 // default. |
95 net::IPEndPoint devtools_endpoint; | 85 net::IPEndPoint devtools_endpoint; |
96 | 86 |
97 // Address of the HTTP/HTTPS proxy server to use. The system proxy settings | |
98 // are used by default. | |
99 net::HostPortPair proxy_server; | |
100 | |
101 // Optional message pump that overrides the default. Must outlive the browser. | 87 // Optional message pump that overrides the default. Must outlive the browser. |
102 base::MessagePump* message_pump; | 88 base::MessagePump* message_pump; |
103 | 89 |
104 // Comma-separated list of rules that control how hostnames are mapped. See | |
105 // chrome::switches::kHostRules for a description for the format. | |
106 std::string host_resolver_rules; | |
107 | |
108 // Run the browser in single process mode instead of using separate renderer | 90 // Run the browser in single process mode instead of using separate renderer |
109 // processes as per default. Note that this also disables any sandboxing of | 91 // processes as per default. Note that this also disables any sandboxing of |
110 // web content, which can be a security risk. | 92 // web content, which can be a security risk. |
111 bool single_process_mode; | 93 bool single_process_mode; |
112 | 94 |
113 // Run the browser without renderer sandbox. This option can be | 95 // Run the browser without renderer sandbox. This option can be |
114 // a security risk and should be used with caution. | 96 // a security risk and should be used with caution. |
115 bool disable_sandbox; | 97 bool disable_sandbox; |
116 | 98 |
117 // Custom network protocol handlers. These can be used to override URL | |
118 // fetching for different network schemes. | |
119 ProtocolHandlerMap protocol_handlers; | |
120 | |
121 // Choose the GL implementation to use for rendering. A suitable | 99 // Choose the GL implementation to use for rendering. A suitable |
122 // implementantion is selected by default. Setting this to an empty | 100 // implementantion is selected by default. Setting this to an empty |
123 // string can be used to disable GL rendering (e.g., WebGL support). | 101 // string can be used to disable GL rendering (e.g., WebGL support). |
124 std::string gl_implementation; | 102 std::string gl_implementation; |
125 | 103 |
| 104 // Default per-context options, can be specialized on per-context basis. |
| 105 |
| 106 std::string user_agent; |
| 107 |
| 108 // Address of the HTTP/HTTPS proxy server to use. The system proxy settings |
| 109 // are used by default. |
| 110 net::HostPortPair proxy_server; |
| 111 |
| 112 // Comma-separated list of rules that control how hostnames are mapped. See |
| 113 // chrome::switches::kHostRules for a description for the format. |
| 114 std::string host_resolver_rules; |
| 115 |
126 // Default window size. This is also used to create the window tree host and | 116 // Default window size. This is also used to create the window tree host and |
127 // as initial screen size. Defaults to 800x600. | 117 // as initial screen size. Defaults to 800x600. |
128 gfx::Size window_size; | 118 gfx::Size window_size; |
129 | 119 |
| 120 // Path to user data directory, where browser will look for its state. |
| 121 // If empty, default directory (where the binary is located) will be used. |
| 122 base::FilePath user_data_dir; |
| 123 |
| 124 // Reminder: when adding a new field here, do not forget to add it to |
| 125 // HeadlessBrowserContextOptions (where appropriate). |
130 private: | 126 private: |
131 Options(int argc, const char** argv); | 127 Options(int argc, const char** argv); |
132 | 128 |
133 DISALLOW_COPY_AND_ASSIGN(Options); | 129 DISALLOW_COPY_AND_ASSIGN(Options); |
134 }; | 130 }; |
135 | 131 |
136 class HeadlessBrowser::Options::Builder { | 132 class HeadlessBrowser::Options::Builder { |
137 public: | 133 public: |
138 Builder(int argc, const char** argv); | 134 Builder(int argc, const char** argv); |
139 Builder(); | 135 Builder(); |
140 ~Builder(); | 136 ~Builder(); |
141 | 137 |
142 Builder& SetUserAgent(const std::string& user_agent); | 138 // Browser-wide settings. |
| 139 |
143 Builder& EnableDevToolsServer(const net::IPEndPoint& endpoint); | 140 Builder& EnableDevToolsServer(const net::IPEndPoint& endpoint); |
144 Builder& SetMessagePump(base::MessagePump* message_pump); | 141 Builder& SetMessagePump(base::MessagePump* message_pump); |
| 142 Builder& SetSingleProcessMode(bool single_process_mode); |
| 143 Builder& SetDisableSandbox(bool disable_sandbox); |
| 144 Builder& SetGLImplementation(const std::string& gl_implementation); |
| 145 |
| 146 // Per-context settings. |
| 147 |
| 148 Builder& SetUserAgent(const std::string& user_agent); |
145 Builder& SetProxyServer(const net::HostPortPair& proxy_server); | 149 Builder& SetProxyServer(const net::HostPortPair& proxy_server); |
146 Builder& SetHostResolverRules(const std::string& host_resolver_rules); | 150 Builder& SetHostResolverRules(const std::string& host_resolver_rules); |
147 Builder& SetSingleProcessMode(bool single_process_mode); | |
148 Builder& SetDisableSandbox(bool disable_sandbox); | |
149 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers); | |
150 Builder& SetGLImplementation(const std::string& gl_implementation); | |
151 Builder& SetWindowSize(const gfx::Size& window_size); | 151 Builder& SetWindowSize(const gfx::Size& window_size); |
| 152 Builder& SetUserDataDir(const base::FilePath& user_data_dir); |
152 | 153 |
153 Options Build(); | 154 Options Build(); |
154 | 155 |
155 private: | 156 private: |
156 Options options_; | 157 Options options_; |
157 | 158 |
158 DISALLOW_COPY_AND_ASSIGN(Builder); | 159 DISALLOW_COPY_AND_ASSIGN(Builder); |
159 }; | 160 }; |
160 | 161 |
161 // The headless browser may need to create child processes (e.g., a renderer | 162 // The headless browser may need to create child processes (e.g., a renderer |
(...skipping 23 matching lines...) Expand all Loading... |
185 // the main loop, it will only return after HeadlessBrowser::Shutdown() is | 186 // the main loop, it will only return after HeadlessBrowser::Shutdown() is |
186 // called, returning the exit code for the process. It is not possible to | 187 // called, returning the exit code for the process. It is not possible to |
187 // initialize the browser again after it has been torn down. | 188 // initialize the browser again after it has been torn down. |
188 int HeadlessBrowserMain( | 189 int HeadlessBrowserMain( |
189 HeadlessBrowser::Options options, | 190 HeadlessBrowser::Options options, |
190 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback); | 191 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback); |
191 | 192 |
192 } // namespace headless | 193 } // namespace headless |
193 | 194 |
194 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 195 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
OLD | NEW |