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