OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_CONTEXT_H_ | 5 #ifndef HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ |
6 #define HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ | 6 #define HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 #include <unordered_map> |
| 12 #include <vector> |
9 | 13 |
| 14 #include "base/optional.h" |
10 #include "headless/public/headless_export.h" | 15 #include "headless/public/headless_export.h" |
| 16 #include "headless/public/headless_web_contents.h" |
| 17 #include "net/base/host_port_pair.h" |
11 #include "net/url_request/url_request_job_factory.h" | 18 #include "net/url_request/url_request_job_factory.h" |
12 | 19 |
13 namespace headless { | 20 namespace headless { |
14 class HeadlessBrowserImpl; | 21 class HeadlessBrowserImpl; |
| 22 class HeadlessBrowserContextOptions; |
15 | 23 |
16 using ProtocolHandlerMap = std::unordered_map< | 24 using ProtocolHandlerMap = std::unordered_map< |
17 std::string, | 25 std::string, |
18 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>; | 26 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>; |
19 | 27 |
20 // Represents an isolated session with a unique cache, cookies, and other | 28 // Represents an isolated session with a unique cache, cookies, and other |
21 // profile/session related data. | 29 // profile/session related data. |
| 30 // When browser context is deleted, all associated web contents are closed. |
22 class HEADLESS_EXPORT HeadlessBrowserContext { | 31 class HEADLESS_EXPORT HeadlessBrowserContext { |
23 public: | 32 public: |
24 class Builder; | 33 class Builder; |
25 | 34 |
26 virtual ~HeadlessBrowserContext() {} | 35 virtual ~HeadlessBrowserContext() {} |
27 | 36 |
| 37 // Open a new tab. Returns a builder object which can be used to set |
| 38 // properties for the new tab. |
| 39 virtual HeadlessWebContents::Builder CreateWebContentsBuilder() = 0; |
| 40 |
| 41 virtual std::vector<HeadlessWebContents*> GetAllWebContents() = 0; |
| 42 |
28 // TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931). | 43 // TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931). |
29 | 44 |
30 protected: | 45 protected: |
31 HeadlessBrowserContext() {} | 46 HeadlessBrowserContext() {} |
32 | 47 |
33 private: | 48 private: |
34 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContext); | 49 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContext); |
35 }; | 50 }; |
36 | 51 |
37 // TODO(alexclarke): We should support this builder for the default context. | |
38 class HEADLESS_EXPORT HeadlessBrowserContext::Builder { | 52 class HEADLESS_EXPORT HeadlessBrowserContext::Builder { |
39 public: | 53 public: |
40 Builder(Builder&&); | 54 Builder(Builder&&); |
41 ~Builder(); | 55 ~Builder(); |
42 | 56 |
43 // Set custom network protocol handlers. These can be used to override URL | 57 // Set custom network protocol handlers. These can be used to override URL |
44 // fetching for different network schemes. | 58 // fetching for different network schemes. |
45 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers); | 59 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers); |
46 | 60 |
47 // Specify JS mojo module bindings to be installed, one per mojom file. | 61 // Specify JS mojo module bindings to be installed, one per mojom file. |
48 // Note a single mojom file could potentially define many interfaces. | 62 // Note a single mojom file could potentially define many interfaces. |
49 // |mojom_name| the name including path of the .mojom file. | 63 // |mojom_name| the name including path of the .mojom file. |
50 // |js_bindings| compiletime generated javascript bindings. Typically loaded | 64 // |js_bindings| compiletime generated javascript bindings. Typically loaded |
51 // from gen/path/name.mojom.js. | 65 // from gen/path/name.mojom.js. |
52 Builder& AddJsMojoBindings(const std::string& mojom_name, | 66 Builder& AddJsMojoBindings(const std::string& mojom_name, |
53 const std::string& js_bindings); | 67 const std::string& js_bindings); |
54 | 68 |
55 // By default if you add mojo bindings, http and https are disabled because | 69 // By default if you add mojo bindings, http and https are disabled because |
56 // its almost certinly unsafe for arbitary sites on the internet to have | 70 // its almost certinly unsafe for arbitary sites on the internet to have |
57 // access to these bindings. If you know what you're doing it may be OK to | 71 // access to these bindings. If you know what you're doing it may be OK to |
58 // turn them back on. E.g. if headless_lib is being used in a testing | 72 // turn them back on. E.g. if headless_lib is being used in a testing |
59 // framework which serves the web content from disk that's likely ok. | 73 // framework which serves the web content from disk that's likely ok. |
60 // | 74 // |
61 // That said, best pratice is to add a ProtocolHandler to serve the | 75 // That said, best pratice is to add a ProtocolHandler to serve the |
62 // webcontent over a custom protocol. That way you can be sure that only the | 76 // webcontent over a custom protocol. That way you can be sure that only the |
63 // things you intend have access to mojo. | 77 // things you intend have access to mojo. |
64 Builder& EnableUnsafeNetworkAccessWithMojoBindings( | 78 Builder& EnableUnsafeNetworkAccessWithMojoBindings( |
65 bool enable_http_and_https_if_mojo_used); | 79 bool enable_http_and_https_if_mojo_used); |
66 | 80 |
| 81 // By default |HeadlessBrowserContext| inherits the following options from |
| 82 // the browser instance. The methods below can be used to override these |
| 83 // settings. See HeadlessBrowser::Options for their meaning. |
| 84 Builder& SetUserAgent(const std::string& user_agent); |
| 85 Builder& SetProxyServer(const net::HostPortPair& proxy_server); |
| 86 Builder& SetHostResolverRules(const std::string& host_resolver_rules); |
| 87 Builder& SetWindowSize(const gfx::Size& window_size); |
| 88 Builder& SetUserDataDir(const base::FilePath& user_data_dir); |
| 89 |
67 std::unique_ptr<HeadlessBrowserContext> Build(); | 90 std::unique_ptr<HeadlessBrowserContext> Build(); |
68 | 91 |
69 private: | 92 private: |
70 friend class HeadlessBrowserImpl; | 93 friend class HeadlessBrowserImpl; |
71 | 94 |
| 95 explicit Builder(HeadlessBrowserImpl* browser); |
| 96 |
72 struct MojoBindings { | 97 struct MojoBindings { |
73 MojoBindings(); | 98 MojoBindings(); |
74 MojoBindings(const std::string& mojom_name, const std::string& js_bindings); | 99 MojoBindings(const std::string& mojom_name, const std::string& js_bindings); |
75 ~MojoBindings(); | 100 ~MojoBindings(); |
76 | 101 |
77 std::string mojom_name; | 102 std::string mojom_name; |
78 std::string js_bindings; | 103 std::string js_bindings; |
79 | 104 |
80 private: | 105 private: |
81 DISALLOW_COPY_AND_ASSIGN(MojoBindings); | 106 DISALLOW_COPY_AND_ASSIGN(MojoBindings); |
82 }; | 107 }; |
83 | 108 |
84 explicit Builder(HeadlessBrowserImpl* browser); | 109 HeadlessBrowserImpl* browser_; |
| 110 std::unique_ptr<HeadlessBrowserContextOptions> options_; |
85 | 111 |
86 HeadlessBrowserImpl* browser_; | |
87 ProtocolHandlerMap protocol_handlers_; | |
88 std::list<MojoBindings> mojo_bindings_; | 112 std::list<MojoBindings> mojo_bindings_; |
89 bool enable_http_and_https_if_mojo_used_; | 113 bool enable_http_and_https_if_mojo_used_; |
90 | 114 |
91 DISALLOW_COPY_AND_ASSIGN(Builder); | 115 DISALLOW_COPY_AND_ASSIGN(Builder); |
92 }; | 116 }; |
93 | 117 |
94 } // namespace headless | 118 } // namespace headless |
95 | 119 |
96 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ | 120 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ |
OLD | NEW |