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 #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_impl.h" |
23 #include "headless/lib/browser/headless_devtools_client_impl.h" | 24 #include "headless/lib/browser/headless_devtools_client_impl.h" |
24 #include "ui/aura/window.h" | 25 #include "ui/aura/window.h" |
25 | 26 |
26 namespace headless { | 27 namespace headless { |
27 | 28 |
28 class WebContentsObserverAdapter : public content::WebContentsObserver { | 29 class WebContentsObserverAdapter : public content::WebContentsObserver { |
29 public: | 30 public: |
30 WebContentsObserverAdapter(content::WebContents* web_contents, | 31 WebContentsObserverAdapter(content::WebContents* web_contents, |
31 HeadlessWebContents::Observer* observer) | 32 HeadlessWebContents::Observer* observer) |
32 : content::WebContentsObserver(web_contents), observer_(observer) {} | 33 : content::WebContentsObserver(web_contents), observer_(observer) {} |
(...skipping 16 matching lines...) Expand all Loading... |
49 } | 50 } |
50 | 51 |
51 private: | 52 private: |
52 HeadlessWebContents::Observer* observer_; // Not owned. | 53 HeadlessWebContents::Observer* observer_; // Not owned. |
53 | 54 |
54 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter); | 55 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverAdapter); |
55 }; | 56 }; |
56 | 57 |
57 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { | 58 class HeadlessWebContentsImpl::Delegate : public content::WebContentsDelegate { |
58 public: | 59 public: |
59 Delegate() {} | 60 explicit Delegate(HeadlessBrowserImpl* browser) : browser_(browser) {} |
| 61 |
| 62 void WebContentsCreated(content::WebContents* source_contents, |
| 63 int opener_render_frame_id, |
| 64 const std::string& frame_name, |
| 65 const GURL& target_url, |
| 66 content::WebContents* new_contents) override { |
| 67 browser_->RegisterWebContents( |
| 68 HeadlessWebContentsImpl::CreateFromWebContents(new_contents, browser_)); |
| 69 } |
60 | 70 |
61 private: | 71 private: |
| 72 HeadlessBrowserImpl* browser_; // Not owned. |
62 DISALLOW_COPY_AND_ASSIGN(Delegate); | 73 DISALLOW_COPY_AND_ASSIGN(Delegate); |
63 }; | 74 }; |
64 | 75 |
65 HeadlessWebContentsImpl::HeadlessWebContentsImpl( | 76 // static |
66 content::BrowserContext* browser_context, | 77 std::unique_ptr<HeadlessWebContentsImpl> HeadlessWebContentsImpl::Create( |
| 78 content::BrowserContext* context, |
67 aura::Window* parent_window, | 79 aura::Window* parent_window, |
68 const gfx::Size& initial_size) | 80 const gfx::Size& initial_size, |
69 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate()) { | 81 HeadlessBrowserImpl* browser) { |
70 content::WebContents::CreateParams create_params(browser_context, nullptr); | 82 content::WebContents::CreateParams create_params(context, nullptr); |
71 create_params.initial_size = initial_size; | 83 create_params.initial_size = initial_size; |
72 | 84 |
73 web_contents_.reset(content::WebContents::Create(create_params)); | 85 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
74 web_contents_->SetDelegate(web_contents_delegate_.get()); | 86 base::WrapUnique(new HeadlessWebContentsImpl( |
| 87 content::WebContents::Create(create_params), browser)); |
75 | 88 |
| 89 headless_web_contents->InitializeScreen(parent_window, initial_size); |
| 90 |
| 91 return headless_web_contents; |
| 92 } |
| 93 |
| 94 // static |
| 95 std::unique_ptr<HeadlessWebContentsImpl> |
| 96 HeadlessWebContentsImpl::CreateFromWebContents( |
| 97 content::WebContents* web_contents, |
| 98 HeadlessBrowserImpl* browser) { |
| 99 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
| 100 base::WrapUnique(new HeadlessWebContentsImpl(web_contents, browser)); |
| 101 |
| 102 return headless_web_contents; |
| 103 } |
| 104 |
| 105 void HeadlessWebContentsImpl::InitializeScreen(aura::Window* parent_window, |
| 106 const gfx::Size& initial_size) { |
76 aura::Window* contents = web_contents_->GetNativeView(); | 107 aura::Window* contents = web_contents_->GetNativeView(); |
77 DCHECK(!parent_window->Contains(contents)); | 108 DCHECK(!parent_window->Contains(contents)); |
78 parent_window->AddChild(contents); | 109 parent_window->AddChild(contents); |
79 contents->Show(); | 110 contents->Show(); |
80 | 111 |
81 contents->SetBounds(gfx::Rect(initial_size)); | 112 contents->SetBounds(gfx::Rect(initial_size)); |
82 content::RenderWidgetHostView* host_view = | 113 content::RenderWidgetHostView* host_view = |
83 web_contents_->GetRenderWidgetHostView(); | 114 web_contents_->GetRenderWidgetHostView(); |
84 if (host_view) | 115 if (host_view) |
85 host_view->SetSize(initial_size); | 116 host_view->SetSize(initial_size); |
86 } | 117 } |
87 | 118 |
| 119 HeadlessWebContentsImpl::HeadlessWebContentsImpl( |
| 120 content::WebContents* web_contents, |
| 121 HeadlessBrowserImpl* browser) |
| 122 : web_contents_delegate_(new HeadlessWebContentsImpl::Delegate(browser)), |
| 123 web_contents_(web_contents), |
| 124 browser_(browser) { |
| 125 web_contents_->SetDelegate(web_contents_delegate_.get()); |
| 126 } |
| 127 |
88 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { | 128 HeadlessWebContentsImpl::~HeadlessWebContentsImpl() { |
89 web_contents_->Close(); | 129 web_contents_->Close(); |
90 } | 130 } |
91 | 131 |
92 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { | 132 bool HeadlessWebContentsImpl::OpenURL(const GURL& url) { |
93 if (!url.is_valid()) | 133 if (!url.is_valid()) |
94 return false; | 134 return false; |
95 content::NavigationController::LoadURLParams params(url); | 135 content::NavigationController::LoadURLParams params(url); |
96 params.transition_type = ui::PageTransitionFromInt( | 136 params.transition_type = ui::PageTransitionFromInt( |
97 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | 137 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
98 web_contents_->GetController().LoadURLWithParams(params); | 138 web_contents_->GetController().LoadURLWithParams(params); |
99 web_contents_->Focus(); | 139 web_contents_->Focus(); |
100 return true; | 140 return true; |
101 } | 141 } |
102 | 142 |
| 143 void HeadlessWebContentsImpl::Close() { |
| 144 browser_->DestroyWebContents(this); |
| 145 } |
| 146 |
103 void HeadlessWebContentsImpl::AddObserver(Observer* observer) { | 147 void HeadlessWebContentsImpl::AddObserver(Observer* observer) { |
104 DCHECK(observer_map_.find(observer) == observer_map_.end()); | 148 DCHECK(observer_map_.find(observer) == observer_map_.end()); |
105 observer_map_[observer] = base::WrapUnique( | 149 observer_map_[observer] = base::WrapUnique( |
106 new WebContentsObserverAdapter(web_contents_.get(), observer)); | 150 new WebContentsObserverAdapter(web_contents_.get(), observer)); |
107 } | 151 } |
108 | 152 |
109 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { | 153 void HeadlessWebContentsImpl::RemoveObserver(Observer* observer) { |
110 ObserverMap::iterator it = observer_map_.find(observer); | 154 ObserverMap::iterator it = observer_map_.find(observer); |
111 DCHECK(it != observer_map_.end()); | 155 DCHECK(it != observer_map_.end()); |
112 observer_map_.erase(it); | 156 observer_map_.erase(it); |
(...skipping 12 matching lines...) Expand all Loading... |
125 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) { | 169 void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) { |
126 DCHECK(agent_host_); | 170 DCHECK(agent_host_); |
127 HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get()); | 171 HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get()); |
128 } | 172 } |
129 | 173 |
130 content::WebContents* HeadlessWebContentsImpl::web_contents() const { | 174 content::WebContents* HeadlessWebContentsImpl::web_contents() const { |
131 return web_contents_.get(); | 175 return web_contents_.get(); |
132 } | 176 } |
133 | 177 |
134 } // namespace headless | 178 } // namespace headless |
OLD | NEW |