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_WEB_CONTENTS_H_ | 5 #ifndef HEADLESS_PUBLIC_HEADLESS_WEB_CONTENTS_H_ |
6 #define HEADLESS_PUBLIC_HEADLESS_WEB_CONTENTS_H_ | 6 #define HEADLESS_PUBLIC_HEADLESS_WEB_CONTENTS_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
| 9 #include <string> |
| 10 #include <utility> |
9 | 11 |
10 #include "base/callback.h" | 12 #include "base/callback.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "headless/public/headless_export.h" | 14 #include "headless/public/headless_export.h" |
13 #include "mojo/public/cpp/bindings/interface_request.h" | 15 #include "mojo/public/cpp/bindings/interface_request.h" |
14 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
16 | 18 |
17 namespace headless { | 19 namespace headless { |
18 class HeadlessBrowserContext; | 20 class HeadlessBrowserContextImpl; |
19 class HeadlessBrowserImpl; | 21 class HeadlessBrowserImpl; |
20 class HeadlessDevToolsTarget; | 22 class HeadlessDevToolsTarget; |
21 | 23 |
22 // Class representing contents of a browser tab. Should be accessed from browser | 24 // Class representing contents of a browser tab. Should be accessed from browser |
23 // main thread. | 25 // main thread. |
24 class HEADLESS_EXPORT HeadlessWebContents { | 26 class HEADLESS_EXPORT HeadlessWebContents { |
25 public: | 27 public: |
26 class Builder; | 28 class Builder; |
27 | 29 |
28 virtual ~HeadlessWebContents() {} | 30 virtual ~HeadlessWebContents() {} |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 74 |
73 // Set an initial URL to ensure that the renderer gets initialized and | 75 // Set an initial URL to ensure that the renderer gets initialized and |
74 // eventually becomes ready to be inspected. See | 76 // eventually becomes ready to be inspected. See |
75 // HeadlessWebContents::Observer::DevToolsTargetReady. The default URL is | 77 // HeadlessWebContents::Observer::DevToolsTargetReady. The default URL is |
76 // about:blank. | 78 // about:blank. |
77 Builder& SetInitialURL(const GURL& initial_url); | 79 Builder& SetInitialURL(const GURL& initial_url); |
78 | 80 |
79 // Specify the initial window size (default is 800x600). | 81 // Specify the initial window size (default is 800x600). |
80 Builder& SetWindowSize(const gfx::Size& size); | 82 Builder& SetWindowSize(const gfx::Size& size); |
81 | 83 |
82 // Set a browser context for storing session data (e.g., cookies, cache, local | |
83 // storage) for the tab. Several tabs can share the same browser context. If | |
84 // unset, the default browser context will be used. The browser context must | |
85 // outlive this HeadlessWebContents. | |
86 Builder& SetBrowserContext(HeadlessBrowserContext* browser_context); | |
87 | |
88 // Specify an embedder provided Mojo service to be installed. The | 84 // Specify an embedder provided Mojo service to be installed. The |
89 // |service_factory| callback is called on demand by Mojo to instantiate the | 85 // |service_factory| callback is called on demand by Mojo to instantiate the |
90 // service if a client asks for it. | 86 // service if a client asks for it. |
91 template <typename Interface> | 87 template <typename Interface> |
92 Builder& AddMojoService( | 88 Builder& AddMojoService( |
93 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& | 89 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& |
94 service_factory) { | 90 service_factory) { |
95 return AddMojoService( | 91 return AddMojoService( |
96 Interface::Name_, | 92 Interface::Name_, |
97 base::Bind(&Builder::ForwardToServiceFactory<Interface>, | 93 base::Bind(&Builder::ForwardToServiceFactory<Interface>, |
98 service_factory)); | 94 service_factory)); |
99 } | 95 } |
100 Builder& AddMojoService(const std::string& service_name, | 96 Builder& AddMojoService(const std::string& service_name, |
101 const base::Callback<void( | 97 const base::Callback<void( |
102 mojo::ScopedMessagePipeHandle)>& service_factory); | 98 mojo::ScopedMessagePipeHandle)>& service_factory); |
103 | 99 |
104 // The returned object is owned by HeadlessBrowser. Call | 100 // The returned object is owned by HeadlessBrowser. Call |
105 // HeadlessWebContents::Close() to dispose it. | 101 // HeadlessWebContents::Close() to dispose it. |
106 HeadlessWebContents* Build(); | 102 HeadlessWebContents* Build(); |
107 | 103 |
108 private: | 104 private: |
109 friend class HeadlessBrowserImpl; | 105 friend class HeadlessBrowserImpl; |
| 106 friend class HeadlessBrowserContextImpl; |
110 friend class HeadlessWebContentsImpl; | 107 friend class HeadlessWebContentsImpl; |
111 | 108 |
| 109 explicit Builder(HeadlessBrowserContextImpl* browser_context); |
| 110 |
112 template <typename Interface> | 111 template <typename Interface> |
113 static void ForwardToServiceFactory( | 112 static void ForwardToServiceFactory( |
114 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& | 113 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& |
115 service_factory, | 114 service_factory, |
116 mojo::ScopedMessagePipeHandle handle) { | 115 mojo::ScopedMessagePipeHandle handle) { |
117 service_factory.Run(mojo::MakeRequest<Interface>(std::move(handle))); | 116 service_factory.Run(mojo::MakeRequest<Interface>(std::move(handle))); |
118 } | 117 } |
119 | 118 |
120 struct MojoService { | 119 struct MojoService { |
121 MojoService(); | 120 MojoService(); |
122 MojoService(const std::string& service_name, | 121 MojoService(const std::string& service_name, |
123 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& | 122 const base::Callback<void(mojo::ScopedMessagePipeHandle)>& |
124 service_factory); | 123 service_factory); |
125 ~MojoService(); | 124 ~MojoService(); |
126 | 125 |
127 std::string service_name; | 126 std::string service_name; |
128 base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory; | 127 base::Callback<void(mojo::ScopedMessagePipeHandle)> service_factory; |
129 | 128 |
130 private: | 129 private: |
131 DISALLOW_COPY_AND_ASSIGN(MojoService); | 130 DISALLOW_COPY_AND_ASSIGN(MojoService); |
132 }; | 131 }; |
133 | 132 |
134 explicit Builder(HeadlessBrowserImpl* browser); | 133 HeadlessBrowserContextImpl* browser_context_; |
135 | |
136 HeadlessBrowserImpl* browser_; | |
137 GURL initial_url_ = GURL("about:blank"); | 134 GURL initial_url_ = GURL("about:blank"); |
138 gfx::Size window_size_ = gfx::Size(800, 600); | 135 gfx::Size window_size_ = gfx::Size(800, 600); |
139 HeadlessBrowserContext* browser_context_; | |
140 std::list<MojoService> mojo_services_; | 136 std::list<MojoService> mojo_services_; |
141 | 137 |
142 DISALLOW_COPY_AND_ASSIGN(Builder); | 138 DISALLOW_COPY_AND_ASSIGN(Builder); |
143 }; | 139 }; |
144 | 140 |
145 } // namespace headless | 141 } // namespace headless |
146 | 142 |
147 #endif // HEADLESS_PUBLIC_HEADLESS_WEB_CONTENTS_H_ | 143 #endif // HEADLESS_PUBLIC_HEADLESS_WEB_CONTENTS_H_ |
OLD | NEW |