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 <unordered_set> | 11 #include <unordered_set> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "headless/public/headless_browser_context.h" | 18 #include "headless/public/headless_browser_context.h" |
19 #include "headless/public/headless_export.h" | 19 #include "headless/public/headless_export.h" |
20 #include "headless/public/headless_web_contents.h" | 20 #include "headless/public/headless_web_contents.h" |
21 #include "net/base/host_port_pair.h" | 21 #include "net/base/host_port_pair.h" |
22 #include "net/base/ip_endpoint.h" | 22 #include "net/base/ip_endpoint.h" |
23 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
24 | 24 |
| 25 #if defined(OS_WIN) |
| 26 #include "sandbox/win/src/sandbox_types.h" |
| 27 #endif |
| 28 |
25 namespace base { | 29 namespace base { |
26 class MessagePump; | 30 class MessagePump; |
27 class SingleThreadTaskRunner; | 31 class SingleThreadTaskRunner; |
28 } | 32 } |
29 | 33 |
30 namespace headless { | 34 namespace headless { |
31 | 35 |
32 // This class represents the global headless browser instance. To get a pointer | 36 // This class represents the global headless browser instance. To get a pointer |
33 // to one, call |HeadlessBrowserMain| to initiate the browser main loop. An | 37 // to one, call |HeadlessBrowserMain| to initiate the browser main loop. An |
34 // instance of |HeadlessBrowser| will be passed to the callback given to that | 38 // instance of |HeadlessBrowser| will be passed to the callback given to that |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 92 |
89 protected: | 93 protected: |
90 HeadlessBrowser() {} | 94 HeadlessBrowser() {} |
91 virtual ~HeadlessBrowser() {} | 95 virtual ~HeadlessBrowser() {} |
92 | 96 |
93 private: | 97 private: |
94 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser); | 98 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser); |
95 }; | 99 }; |
96 | 100 |
97 // Embedding API overrides for the headless browser. | 101 // Embedding API overrides for the headless browser. |
98 struct HeadlessBrowser::Options { | 102 struct HEADLESS_EXPORT HeadlessBrowser::Options { |
99 class Builder; | 103 class Builder; |
100 | 104 |
101 Options(Options&& options); | 105 Options(Options&& options); |
102 ~Options(); | 106 ~Options(); |
103 | 107 |
104 Options& operator=(Options&& options); | 108 Options& operator=(Options&& options); |
105 | 109 |
106 // Command line options to be passed to browser. | 110 // Command line options to be passed to browser. |
107 int argc; | 111 int argc; |
108 const char** argv; | 112 const char** argv; |
109 | 113 |
| 114 #if defined(OS_WIN) |
| 115 // Set hardware instance if available, otherwise it defaults to 0. |
| 116 HINSTANCE instance; |
| 117 |
| 118 // Set with sandbox information. This has to be already initialized. |
| 119 sandbox::SandboxInterfaceInfo* sandbox_info; |
| 120 #endif |
| 121 |
110 // Address at which DevTools should listen for connections. Disabled by | 122 // Address at which DevTools should listen for connections. Disabled by |
111 // default. Mutually exclusive with devtools_socket_fd. | 123 // default. Mutually exclusive with devtools_socket_fd. |
112 net::IPEndPoint devtools_endpoint; | 124 net::IPEndPoint devtools_endpoint; |
113 | 125 |
114 // The fd of an already-open socket inherited from a parent process. Disabled | 126 // The fd of an already-open socket inherited from a parent process. Disabled |
115 // by default. Mutually exclusive with devtools_endpoint. | 127 // by default. Mutually exclusive with devtools_endpoint. |
116 size_t devtools_socket_fd; | 128 size_t devtools_socket_fd; |
117 | 129 |
118 // A single way to test whether the devtools server has been requested. | 130 // A single way to test whether the devtools server has been requested. |
119 bool DevtoolsServerEnabled(); | 131 bool DevtoolsServerEnabled(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 base::FilePath crash_dumps_dir; | 190 base::FilePath crash_dumps_dir; |
179 | 191 |
180 // Reminder: when adding a new field here, do not forget to add it to | 192 // Reminder: when adding a new field here, do not forget to add it to |
181 // HeadlessBrowserContextOptions (where appropriate). | 193 // HeadlessBrowserContextOptions (where appropriate). |
182 private: | 194 private: |
183 Options(int argc, const char** argv); | 195 Options(int argc, const char** argv); |
184 | 196 |
185 DISALLOW_COPY_AND_ASSIGN(Options); | 197 DISALLOW_COPY_AND_ASSIGN(Options); |
186 }; | 198 }; |
187 | 199 |
188 class HeadlessBrowser::Options::Builder { | 200 class HEADLESS_EXPORT HeadlessBrowser::Options::Builder { |
189 public: | 201 public: |
190 Builder(int argc, const char** argv); | 202 Builder(int argc, const char** argv); |
191 Builder(); | 203 Builder(); |
192 ~Builder(); | 204 ~Builder(); |
193 | 205 |
194 // Browser-wide settings. | 206 // Browser-wide settings. |
195 | 207 |
196 Builder& EnableDevToolsServer(const net::IPEndPoint& endpoint); | 208 Builder& EnableDevToolsServer(const net::IPEndPoint& endpoint); |
197 Builder& EnableDevToolsServer(const size_t socket_fd); | 209 Builder& EnableDevToolsServer(const size_t socket_fd); |
198 Builder& SetMessagePump(base::MessagePump* message_pump); | 210 Builder& SetMessagePump(base::MessagePump* message_pump); |
199 Builder& SetSingleProcessMode(bool single_process_mode); | 211 Builder& SetSingleProcessMode(bool single_process_mode); |
200 Builder& SetDisableSandbox(bool disable_sandbox); | 212 Builder& SetDisableSandbox(bool disable_sandbox); |
201 Builder& SetGLImplementation(const std::string& gl_implementation); | 213 Builder& SetGLImplementation(const std::string& gl_implementation); |
202 Builder& AddMojoServiceName(const std::string& mojo_service_name); | 214 Builder& AddMojoServiceName(const std::string& mojo_service_name); |
| 215 #if defined(OS_WIN) |
| 216 Builder& SetInstance(HINSTANCE instance); |
| 217 Builder& SetSandboxInfo(sandbox::SandboxInterfaceInfo* sandbox_info); |
| 218 #endif |
203 | 219 |
204 // Per-context settings. | 220 // Per-context settings. |
205 | 221 |
206 Builder& SetProductNameAndVersion( | 222 Builder& SetProductNameAndVersion( |
207 const std::string& product_name_and_version); | 223 const std::string& product_name_and_version); |
208 Builder& SetUserAgent(const std::string& user_agent); | 224 Builder& SetUserAgent(const std::string& user_agent); |
209 Builder& SetProxyServer(const net::HostPortPair& proxy_server); | 225 Builder& SetProxyServer(const net::HostPortPair& proxy_server); |
210 Builder& SetHostResolverRules(const std::string& host_resolver_rules); | 226 Builder& SetHostResolverRules(const std::string& host_resolver_rules); |
211 Builder& SetWindowSize(const gfx::Size& window_size); | 227 Builder& SetWindowSize(const gfx::Size& window_size); |
212 Builder& SetUserDataDir(const base::FilePath& user_data_dir); | 228 Builder& SetUserDataDir(const base::FilePath& user_data_dir); |
213 Builder& SetIncognitoMode(bool incognito_mode); | 229 Builder& SetIncognitoMode(bool incognito_mode); |
214 Builder& SetOverrideWebPreferencesCallback( | 230 Builder& SetOverrideWebPreferencesCallback( |
215 base::Callback<void(WebPreferences*)> callback); | 231 base::Callback<void(WebPreferences*)> callback); |
216 Builder& SetCrashReporterEnabled(bool enabled); | 232 Builder& SetCrashReporterEnabled(bool enabled); |
217 Builder& SetCrashDumpsDir(const base::FilePath& dir); | 233 Builder& SetCrashDumpsDir(const base::FilePath& dir); |
218 | 234 |
219 Options Build(); | 235 Options Build(); |
220 | 236 |
221 private: | 237 private: |
222 Options options_; | 238 Options options_; |
223 | 239 |
224 DISALLOW_COPY_AND_ASSIGN(Builder); | 240 DISALLOW_COPY_AND_ASSIGN(Builder); |
225 }; | 241 }; |
226 | 242 |
| 243 #if !defined(OS_WIN) |
227 // The headless browser may need to create child processes (e.g., a renderer | 244 // The headless browser may need to create child processes (e.g., a renderer |
228 // which runs web content). This is done by re-executing the parent process as | 245 // which runs web content). This is done by re-executing the parent process as |
229 // a zygote[1] and forking each child process from that zygote. | 246 // a zygote[1] and forking each child process from that zygote. |
230 // | 247 // |
231 // For this to work, the embedder should call RunChildProcess as soon as | 248 // For this to work, the embedder should call RunChildProcess as soon as |
232 // possible (i.e., before creating any threads) to pass control to the headless | 249 // possible (i.e., before creating any threads) to pass control to the headless |
233 // library. In a browser process this function will return immediately, but in a | 250 // library. In a browser process this function will return immediately, but in a |
234 // child process it will never return. For example: | 251 // child process it will never return. For example: |
235 // | 252 // |
236 // int main(int argc, const char** argv) { | 253 // int main(int argc, const char** argv) { |
237 // headless::RunChildProcessIfNeeded(argc, argv); | 254 // headless::RunChildProcessIfNeeded(argc, argv); |
238 // headless::HeadlessBrowser::Options::Builder builder(argc, argv); | 255 // headless::HeadlessBrowser::Options::Builder builder(argc, argv); |
239 // return headless::HeadlessBrowserMain( | 256 // return headless::HeadlessBrowserMain( |
240 // builder.Build(), | 257 // builder.Build(), |
241 // base::Callback<void(headless::HeadlessBrowser*)>()); | 258 // base::Callback<void(headless::HeadlessBrowser*)>()); |
242 // } | 259 // } |
243 // | 260 // |
244 // [1] | 261 // [1] |
245 // https://chromium.googlesource.com/chromium/src/+/master/docs/linux_zygote.md | 262 // https://chromium.googlesource.com/chromium/src/+/master/docs/linux_zygote.md |
246 void RunChildProcessIfNeeded(int argc, const char** argv); | 263 HEADLESS_EXPORT void RunChildProcessIfNeeded(int argc, const char** argv); |
| 264 #else |
| 265 // In Windows, the headless browser may need to create child processes. This is |
| 266 // done by re-executing the parent process which may have been initialized with |
| 267 // different libraries (e.g. child_dll). In this case, the embedder has to pass |
| 268 // the appropiate HINSTANCE and initalization sandbox_info to properly launch |
| 269 // the child process. |
| 270 HEADLESS_EXPORT void RunChildProcessIfNeeded( |
| 271 HINSTANCE instance, |
| 272 sandbox::SandboxInterfaceInfo* sandbox_info); |
| 273 #endif // !defined(OS_WIN) |
247 | 274 |
248 // Main entry point for running the headless browser. This function constructs | 275 // Main entry point for running the headless browser. This function constructs |
249 // the headless browser instance, passing it to the given | 276 // the headless browser instance, passing it to the given |
250 // |on_browser_start_callback| callback. Note that since this function executes | 277 // |on_browser_start_callback| callback. Note that since this function executes |
251 // the main loop, it will only return after HeadlessBrowser::Shutdown() is | 278 // the main loop, it will only return after HeadlessBrowser::Shutdown() is |
252 // called, returning the exit code for the process. It is not possible to | 279 // called, returning the exit code for the process. It is not possible to |
253 // initialize the browser again after it has been torn down. | 280 // initialize the browser again after it has been torn down. |
254 int HeadlessBrowserMain( | 281 HEADLESS_EXPORT int HeadlessBrowserMain( |
255 HeadlessBrowser::Options options, | 282 HeadlessBrowser::Options options, |
256 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback); | 283 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback); |
257 | 284 |
258 } // namespace headless | 285 } // namespace headless |
259 | 286 |
260 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 287 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
OLD | NEW |