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