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> |
| 9 |
8 #include "headless/public/headless_export.h" | 10 #include "headless/public/headless_export.h" |
9 #include "net/url_request/url_request_job_factory.h" | 11 #include "net/url_request/url_request_job_factory.h" |
10 | 12 |
11 namespace headless { | 13 namespace headless { |
12 class HeadlessBrowserImpl; | 14 class HeadlessBrowserImpl; |
13 | 15 |
14 using ProtocolHandlerMap = std::unordered_map< | 16 using ProtocolHandlerMap = std::unordered_map< |
15 std::string, | 17 std::string, |
16 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>; | 18 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>; |
17 | 19 |
18 // Represents an isolated session with a unique cache, cookies, and other | 20 // Represents an isolated session with a unique cache, cookies, and other |
19 // profile/session related data. | 21 // profile/session related data. |
20 class HEADLESS_EXPORT HeadlessBrowserContext { | 22 class HEADLESS_EXPORT HeadlessBrowserContext { |
21 public: | 23 public: |
22 class Builder; | 24 class Builder; |
23 | 25 |
24 virtual ~HeadlessBrowserContext() {} | 26 virtual ~HeadlessBrowserContext() {} |
25 | 27 |
26 // TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931). | 28 // TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931). |
27 | 29 |
28 protected: | 30 protected: |
29 HeadlessBrowserContext() {} | 31 HeadlessBrowserContext() {} |
30 | 32 |
31 private: | 33 private: |
32 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContext); | 34 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContext); |
33 }; | 35 }; |
34 | 36 |
| 37 // TODO(alexclarke): We should support this builder for the default context. |
35 class HEADLESS_EXPORT HeadlessBrowserContext::Builder { | 38 class HEADLESS_EXPORT HeadlessBrowserContext::Builder { |
36 public: | 39 public: |
37 Builder(Builder&&); | 40 Builder(Builder&&); |
38 ~Builder(); | 41 ~Builder(); |
39 | 42 |
40 // Set custom network protocol handlers. These can be used to override URL | 43 // Set custom network protocol handlers. These can be used to override URL |
41 // fetching for different network schemes. | 44 // fetching for different network schemes. |
42 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers); | 45 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers); |
43 | 46 |
| 47 // Specify JS mojo module bindings to be installed, one per mojom file. |
| 48 // Note a single mojom file could potentially define many interfaces. |
| 49 // |mojom_name| the name including path of the .mojom file. |
| 50 // |js_bindings| compiletime generated javascript bindings. Typically loaded |
| 51 // from gen/path/name.mojom.js. |
| 52 Builder& AddJsMojoBindings(const std::string& mojom_name, |
| 53 const std::string& js_bindings); |
| 54 |
| 55 // 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 |
| 57 // 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 |
| 59 // framework which serves the web content from disk that's likely ok. |
| 60 // |
| 61 // 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 |
| 63 // things you intend have access to mojo. |
| 64 Builder& EnableUnsafeNetworkAccessWithMojoBindings( |
| 65 bool enable_http_and_https_if_mojo_used); |
| 66 |
44 std::unique_ptr<HeadlessBrowserContext> Build(); | 67 std::unique_ptr<HeadlessBrowserContext> Build(); |
45 | 68 |
46 private: | 69 private: |
47 friend class HeadlessBrowserImpl; | 70 friend class HeadlessBrowserImpl; |
48 | 71 |
| 72 struct MojoBindings { |
| 73 MojoBindings(); |
| 74 MojoBindings(const std::string& mojom_name, const std::string& js_bindings); |
| 75 ~MojoBindings(); |
| 76 |
| 77 std::string mojom_name; |
| 78 std::string js_bindings; |
| 79 |
| 80 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(MojoBindings); |
| 82 }; |
| 83 |
49 explicit Builder(HeadlessBrowserImpl* browser); | 84 explicit Builder(HeadlessBrowserImpl* browser); |
50 | 85 |
51 HeadlessBrowserImpl* browser_; | 86 HeadlessBrowserImpl* browser_; |
52 ProtocolHandlerMap protocol_handlers_; | 87 ProtocolHandlerMap protocol_handlers_; |
| 88 std::list<MojoBindings> mojo_bindings_; |
| 89 bool enable_http_and_https_if_mojo_used_; |
53 | 90 |
54 DISALLOW_COPY_AND_ASSIGN(Builder); | 91 DISALLOW_COPY_AND_ASSIGN(Builder); |
55 }; | 92 }; |
56 | 93 |
57 } // namespace headless | 94 } // namespace headless |
58 | 95 |
59 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ | 96 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ |
OLD | NEW |