Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: headless/public/headless_browser_context.h

Issue 2181413002: [headless] Remove default browser context. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Small fixes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.
Sami 2016/08/03 11:20:31 Please add a note saying when a browser context is
altimin 2016/08/03 11:54:10 Done.
22 class HEADLESS_EXPORT HeadlessBrowserContext { 30 class HEADLESS_EXPORT HeadlessBrowserContext {
23 public: 31 public:
24 class Builder; 32 class Builder;
25 33
26 virtual ~HeadlessBrowserContext() {} 34 virtual ~HeadlessBrowserContext() {}
27 35
36 // Open a new tab. Returns a builder object which can be used to set
37 // properties for the new tab.
38 virtual HeadlessWebContents::Builder CreateWebContentsBuilder() = 0;
39
40 virtual std::vector<HeadlessWebContents*> GetAllWebContents() = 0;
41
28 // TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931). 42 // TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931).
29 43
30 protected: 44 protected:
31 HeadlessBrowserContext() {} 45 HeadlessBrowserContext() {}
32 46
33 private: 47 private:
34 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContext); 48 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContext);
35 }; 49 };
36 50
37 // TODO(alexclarke): We should support this builder for the default context.
38 class HEADLESS_EXPORT HeadlessBrowserContext::Builder { 51 class HEADLESS_EXPORT HeadlessBrowserContext::Builder {
39 public: 52 public:
40 Builder(Builder&&); 53 Builder(Builder&&);
41 ~Builder(); 54 ~Builder();
42 55
43 // Set custom network protocol handlers. These can be used to override URL 56 // Set custom network protocol handlers. These can be used to override URL
44 // fetching for different network schemes. 57 // fetching for different network schemes.
45 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers); 58 Builder& SetProtocolHandlers(ProtocolHandlerMap protocol_handlers);
46 59
47 // Specify JS mojo module bindings to be installed, one per mojom file. 60 // Specify JS mojo module bindings to be installed, one per mojom file.
48 // Note a single mojom file could potentially define many interfaces. 61 // Note a single mojom file could potentially define many interfaces.
49 // |mojom_name| the name including path of the .mojom file. 62 // |mojom_name| the name including path of the .mojom file.
50 // |js_bindings| compiletime generated javascript bindings. Typically loaded 63 // |js_bindings| compiletime generated javascript bindings. Typically loaded
51 // from gen/path/name.mojom.js. 64 // from gen/path/name.mojom.js.
52 Builder& AddJsMojoBindings(const std::string& mojom_name, 65 Builder& AddJsMojoBindings(const std::string& mojom_name,
53 const std::string& js_bindings); 66 const std::string& js_bindings);
54 67
55 // By default if you add mojo bindings, http and https are disabled because 68 // 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 69 // 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 70 // 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 71 // 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. 72 // framework which serves the web content from disk that's likely ok.
60 // 73 //
61 // That said, best pratice is to add a ProtocolHandler to serve the 74 // 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 75 // webcontent over a custom protocol. That way you can be sure that only the
63 // things you intend have access to mojo. 76 // things you intend have access to mojo.
64 Builder& EnableUnsafeNetworkAccessWithMojoBindings( 77 Builder& EnableUnsafeNetworkAccessWithMojoBindings(
65 bool enable_http_and_https_if_mojo_used); 78 bool enable_http_and_https_if_mojo_used);
66 79
80 // Set HeadlessBrowser::Builder::SetUserAgent.
Sami 2016/08/03 11:20:31 How about formatting this thusly: // By default H
altimin 2016/08/03 11:54:10 Done.
81 Builder& SetUserAgent(const std::string& user_agent);
82
83 // See HeadlessBrowser::Builder::SetProxyServer.
84 Builder& SetProxyServer(const net::HostPortPair& proxy_server);
85
86 // See HeadlessBrowser::Builder::SetHostResolverRules.
87 Builder& SetHostResolverRules(const std::string& host_resolver_rules);
88
89 // See HeadlessBrowser::Builder::SetUserDataDir.
90 Builder& SetUserDataDir(const base::FilePath& user_data_dir);
91
67 std::unique_ptr<HeadlessBrowserContext> Build(); 92 std::unique_ptr<HeadlessBrowserContext> Build();
68 93
69 private: 94 private:
70 friend class HeadlessBrowserImpl; 95 friend class HeadlessBrowserImpl;
71 96
97 explicit Builder(HeadlessBrowserImpl* browser);
98
72 struct MojoBindings { 99 struct MojoBindings {
73 MojoBindings(); 100 MojoBindings();
74 MojoBindings(const std::string& mojom_name, const std::string& js_bindings); 101 MojoBindings(const std::string& mojom_name, const std::string& js_bindings);
75 ~MojoBindings(); 102 ~MojoBindings();
76 103
77 std::string mojom_name; 104 std::string mojom_name;
78 std::string js_bindings; 105 std::string js_bindings;
79 106
80 private: 107 private:
81 DISALLOW_COPY_AND_ASSIGN(MojoBindings); 108 DISALLOW_COPY_AND_ASSIGN(MojoBindings);
82 }; 109 };
83 110
84 explicit Builder(HeadlessBrowserImpl* browser); 111 HeadlessBrowserImpl* browser_;
112 std::unique_ptr<HeadlessBrowserContextOptions> options_;
85 113
86 HeadlessBrowserImpl* browser_;
87 ProtocolHandlerMap protocol_handlers_;
88 std::list<MojoBindings> mojo_bindings_; 114 std::list<MojoBindings> mojo_bindings_;
89 bool enable_http_and_https_if_mojo_used_; 115 bool enable_http_and_https_if_mojo_used_;
90 116
91 DISALLOW_COPY_AND_ASSIGN(Builder); 117 DISALLOW_COPY_AND_ASSIGN(Builder);
92 }; 118 };
93 119
94 } // namespace headless 120 } // namespace headless
95 121
96 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_ 122 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698