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

Side by Side Diff: headless/lib/browser/headless_web_contents_impl.cc

Issue 2043603004: headless: Introduce a browser context (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address nits Created 4 years, 6 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 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 #include "headless/lib/browser/headless_web_contents_impl.h" 5 #include "headless/lib/browser/headless_web_contents_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/memory/weak_ptr.h" 9 #include "base/memory/weak_ptr.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
11 #include "content/public/browser/devtools_agent_host.h" 11 #include "content/public/browser/devtools_agent_host.h"
12 #include "content/public/browser/navigation_handle.h" 12 #include "content/public/browser/navigation_handle.h"
13 #include "content/public/browser/render_frame_host.h" 13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_process_host.h" 14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h" 15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/render_widget_host_view.h" 16 #include "content/public/browser/render_widget_host_view.h"
17 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_delegate.h" 18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_observer.h" 19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/common/bindings_policy.h" 20 #include "content/public/common/bindings_policy.h"
21 #include "content/public/common/service_registry.h" 21 #include "content/public/common/service_registry.h"
22 #include "content/public/renderer/render_frame.h" 22 #include "content/public/renderer/render_frame.h"
23 #include "headless/lib/browser/headless_browser_context_impl.h"
23 #include "headless/lib/browser/headless_browser_impl.h" 24 #include "headless/lib/browser/headless_browser_impl.h"
25 #include "headless/lib/browser/headless_browser_main_parts.h"
24 #include "headless/lib/browser/headless_devtools_client_impl.h" 26 #include "headless/lib/browser/headless_devtools_client_impl.h"
25 #include "ui/aura/window.h" 27 #include "ui/aura/window.h"
26 28
27 namespace headless { 29 namespace headless {
28 30
29 class WebContentsObserverAdapter : public content::WebContentsObserver { 31 class WebContentsObserverAdapter : public content::WebContentsObserver {
30 public: 32 public:
31 WebContentsObserverAdapter(content::WebContents* web_contents, 33 WebContentsObserverAdapter(content::WebContents* web_contents,
32 HeadlessWebContents::Observer* observer) 34 HeadlessWebContents::Observer* observer)
33 : content::WebContentsObserver(web_contents), observer_(observer) {} 35 : content::WebContentsObserver(web_contents), observer_(observer) {}
(...skipping 24 matching lines...) Expand all
58 HeadlessWebContentsImpl::CreateFromWebContents(new_contents, browser_)); 60 HeadlessWebContentsImpl::CreateFromWebContents(new_contents, browser_));
59 } 61 }
60 62
61 private: 63 private:
62 HeadlessBrowserImpl* browser_; // Not owned. 64 HeadlessBrowserImpl* browser_; // Not owned.
63 DISALLOW_COPY_AND_ASSIGN(Delegate); 65 DISALLOW_COPY_AND_ASSIGN(Delegate);
64 }; 66 };
65 67
66 // static 68 // static
67 std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create( 69 std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create(
68 content::BrowserContext* context, 70 HeadlessWebContents::Builder* builder,
69 aura::Window* parent_window, 71 aura::Window* parent_window,
70 const gfx::Size& initial_size,
71 HeadlessBrowserImpl* browser) { 72 HeadlessBrowserImpl* browser) {
73 content::BrowserContext* context =
74 HeadlessBrowserContextImpl::From(builder->browser_context_);
72 content::WebContents::CreateParams create_params(context, nullptr); 75 content::WebContents::CreateParams create_params(context, nullptr);
73 create_params.initial_size = initial_size; 76 create_params.initial_size = builder->window_size_;
74 77
75 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = 78 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
76 base::WrapUnique(new HeadlessWebContentsImpl( 79 base::WrapUnique(new HeadlessWebContentsImpl(
77 content::WebContents::Create(create_params), browser)); 80 content::WebContents::Create(create_params), browser));
78 81
79 headless_web_contents->InitializeScreen(parent_window, initial_size); 82 headless_web_contents->InitializeScreen(parent_window, builder->window_size_);
80 83 if (!headless_web_contents->OpenURL(builder->initial_url_))
84 return nullptr;
81 return headless_web_contents; 85 return headless_web_contents;
82 } 86 }
83 87
84 // static 88 // static
85 std::unique_ptr<HeadlessWebContentsImpl> 89 std::unique_ptr<HeadlessWebContentsImpl>
86 HeadlessWebContentsImpl::CreateFromWebContents( 90 HeadlessWebContentsImpl::CreateFromWebContents(
87 content::WebContents* web_contents, 91 content::WebContents* web_contents,
88 HeadlessBrowserImpl* browser) { 92 HeadlessBrowserImpl* browser) {
89 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = 93 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents =
90 base::WrapUnique(new HeadlessWebContentsImpl(web_contents, browser)); 94 base::WrapUnique(new HeadlessWebContentsImpl(web_contents, browser));
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 162
159 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) { 163 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) {
160 DCHECK(agent_host_); 164 DCHECK(agent_host_);
161 HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get()); 165 HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get());
162 } 166 }
163 167
164 content::WebContents* HeadlessWebContentsImpl::web_contents() const { 168 content::WebContents* HeadlessWebContentsImpl::web_contents() const {
165 return web_contents_.get(); 169 return web_contents_.get();
166 } 170 }
167 171
172 HeadlessWebContents::Builder::Builder(HeadlessBrowserImpl* browser)
173 : browser_(browser),
174 browser_context_(
175 browser->browser_main_parts()->default_browser_context()) {}
176
177 HeadlessWebContents::Builder::~Builder() = default;
178
179 HeadlessWebContents::Builder::Builder(Builder&&) = default;
180
181 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetInitialURL(
182 const GURL& initial_url) {
183 initial_url_ = initial_url;
184 return *this;
185 }
186
187 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetWindowSize(
188 const gfx::Size& size) {
189 window_size_ = size;
190 return *this;
191 }
192
193 HeadlessWebContents::Builder& HeadlessWebContents::Builder::SetBrowserContext(
194 HeadlessBrowserContext* browser_context) {
195 browser_context_ = browser_context;
196 return *this;
197 }
198
199 HeadlessWebContents* HeadlessWebContents::Builder::Build() {
200 return browser_->CreateWebContents(this);
201 }
202
168 } // namespace headless 203 } // namespace headless
OLDNEW
« no previous file with comments | « headless/lib/browser/headless_web_contents_impl.h ('k') | headless/lib/headless_browser_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698