OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "headless/lib/headless_web_contents_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/trace_event/trace_event.h" | |
10 #include "content/public/browser/render_frame_host.h" | |
11 #include "content/public/browser/render_process_host.h" | |
12 #include "content/public/browser/render_view_host.h" | |
13 #include "content/public/browser/render_widget_host_view.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "content/public/browser/web_contents_delegate.h" | |
16 #include "content/public/browser/web_contents_observer.h" | |
17 #include "content/public/common/bindings_policy.h" | |
18 #include "content/public/common/service_registry.h" | |
19 #include "content/public/renderer/render_frame.h" | |
20 #include "ui/aura/env.h" | |
21 #include "ui/aura/window.h" | |
22 #include "ui/aura/window_tree_host.h" | |
23 | |
24 namespace headless { | |
25 | |
26 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { | |
27 public: | |
28 Delegate() {} | |
29 | |
30 private: | |
31 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
32 }; | |
33 | |
34 HeadlessWebContentsImpl::HeadlessWebContentsImpl( | |
35 content::BrowserContext* browser_context, | |
36 const gfx::Size& initial_size) | |
37 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { | |
38 content::WebContents::CreateParams create_params(browser_context, nullptr); | |
39 create_params.initial_size = initial_size; | |
40 | |
41 web_contents_.reset(content::WebContents::Create(create_params)); | |
42 web_contents_->SetDelegate(web_contents_delegate_.get()); | |
43 | |
44 DCHECK(aura::Env::GetInstance()); | |
45 window_tree_host_.reset( | |
46 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); | |
47 window_tree_host_->InitHost(); | |
48 | |
49 aura::Window* contents = web_contents_->GetNativeView(); | |
50 aura::Window* parent = window_tree_host_->window(); | |
51 | |
52 if (!parent->Contains(contents)) { | |
53 parent->AddChild(contents); | |
54 contents->Show(); | |
55 } | |
56 | |
57 contents->SetBounds(gfx::Rect(initial_size)); | |
58 content::RenderWidgetHostView* host_view = | |
59 web_contents_->GetRenderWidgetHostView(); | |
60 if (host_view) | |
61 host_view->SetSize(initial_size); | |
62 } | |
63 | |
64 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { | |
65 web_contents_->Close(); | |
66 } | |
67 | |
68 void HeadlessWebContentsImpl::OpenURL(const GURL& url) { | |
69 content::NavigationController::LoadURLParams params(url); | |
70 params.transition_type = ui::PageTransitionFromInt( | |
71 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | |
72 web_contents_->GetController().LoadURLWithParams(params); | |
73 web_contents_->Focus(); | |
74 } | |
75 | |
76 content::WebContents* HeadlessWebContentsImpl::web_contents() { | |
77 return web_contents_.get(); | |
78 } | |
79 | |
80 class HeadlessWebContents::Observer::ObserverImpl | |
81 : public content::WebContentsObserver { | |
82 public: | |
83 ObserverImpl(content::WebContents* web_contents, | |
84 HeadlessWebContents::Observer* observer) | |
85 : content::WebContentsObserver(web_contents), observer_(observer) {} | |
86 ObserverImpl(HeadlessWebContents::Observer* observer) : observer_(observer) {} | |
87 | |
88 ~ObserverImpl() override {} | |
89 | |
90 void ObserveWebContents(content::WebContents* web_contents) { | |
91 DCHECK(!this->web_contents()); | |
92 Observe(web_contents); | |
93 } | |
94 | |
95 void DocumentOnLoadCompletedInMainFrame() override { | |
96 observer_->DocumentOnLoadCompletedInMainFrame(); | |
97 } | |
98 | |
99 private: | |
100 HeadlessWebContents::Observer* observer_; | |
alex clarke (OOO till 29th)
2016/02/10 17:49:22
I guess this isn't owned?
Sami
2016/02/10 18:47:38
Yep, the Observer owns an ObserverImpl and this is
| |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(ObserverImpl); | |
103 }; | |
104 | |
105 HeadlessWebContents::Observer::Observer(HeadlessWebContents* web_contents) | |
106 : observer_(new HeadlessWebContents::Observer::ObserverImpl( | |
107 static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents(), | |
alex clarke (OOO till 29th)
2016/02/10 17:49:22
I hope these casts are safe :)
Sami
2016/02/10 18:47:38
I've now made the base class only implementable by
| |
108 this)) {} | |
109 | |
110 HeadlessWebContents::Observer::Observer() | |
111 : observer_(new HeadlessWebContents::Observer::ObserverImpl(this)) {} | |
112 | |
113 HeadlessWebContents::Observer::~Observer() {} | |
114 | |
115 void HeadlessWebContents::Observer::Observe(HeadlessWebContents* web_contents) { | |
116 observer_->ObserveWebContents( | |
117 static_cast<HeadlessWebContentsImpl*>(web_contents)->web_contents()); | |
118 } | |
119 | |
120 } // namespace headless | |
OLD | NEW |