OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chromecast/browser/cast_content_window_linux.h" | 5 #include "chromecast/browser/cast_content_window_linux.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/threading/thread_restrictions.h" | |
10 #include "chromecast/base/metrics/cast_metrics_helper.h" | 9 #include "chromecast/base/metrics/cast_metrics_helper.h" |
11 #include "chromecast/base/version.h" | 10 #include "chromecast/base/version.h" |
12 #include "chromecast/browser/cast_browser_process.h" | 11 #include "chromecast/browser/cast_browser_process.h" |
13 #include "chromecast/graphics/cast_vsync_settings.h" | 12 #include "chromecast/graphics/cast_window_manager.h" |
14 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
15 #include "content/public/browser/render_widget_host.h" | 14 #include "content/public/browser/render_widget_host.h" |
16 #include "content/public/browser/render_widget_host_view.h" | 15 #include "content/public/browser/render_widget_host_view.h" |
17 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
18 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
19 #include "ui/display/display.h" | 18 #include "ui/display/display.h" |
20 #include "ui/display/screen.h" | 19 #include "ui/display/screen.h" |
21 | 20 |
22 #if defined(USE_AURA) | 21 #if defined(USE_AURA) |
23 #include "chromecast/graphics/cast_screen.h" | |
24 #include "ui/aura/env.h" | |
25 #include "ui/aura/layout_manager.h" | |
26 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
27 #include "ui/aura/window_tree_host.h" | |
28 #include "ui/aura/window_tree_host_platform.h" | |
29 #include "ui/base/ime/input_method.h" | |
30 #endif | 23 #endif |
31 | 24 |
32 namespace chromecast { | 25 namespace chromecast { |
33 namespace shell { | 26 namespace shell { |
34 | 27 |
35 #if defined(USE_AURA) | |
36 class CastFillLayout : public aura::LayoutManager { | |
37 public: | |
38 explicit CastFillLayout(aura::Window* root) : root_(root) {} | |
39 ~CastFillLayout() override {} | |
40 | |
41 private: | |
42 void OnWindowResized() override {} | |
43 | |
44 void OnWindowAddedToLayout(aura::Window* child) override { | |
45 child->SetBounds(root_->bounds()); | |
46 } | |
47 | |
48 void OnWillRemoveWindowFromLayout(aura::Window* child) override {} | |
49 | |
50 void OnWindowRemovedFromLayout(aura::Window* child) override {} | |
51 | |
52 void OnChildWindowVisibilityChanged(aura::Window* child, | |
53 bool visible) override {} | |
54 | |
55 void SetChildBounds(aura::Window* child, | |
56 const gfx::Rect& requested_bounds) override { | |
57 SetChildBoundsDirect(child, requested_bounds); | |
58 } | |
59 | |
60 aura::Window* root_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(CastFillLayout); | |
63 }; | |
64 | |
65 // An aura::WindowTreeHost that correctly converts input events. | |
66 class CastWindowTreeHost : public aura::WindowTreeHostPlatform { | |
67 public: | |
68 CastWindowTreeHost(bool enable_input, const gfx::Rect& bounds); | |
69 ~CastWindowTreeHost() override; | |
70 | |
71 // aura::WindowTreeHostPlatform implementation: | |
72 void DispatchEvent(ui::Event* event) override; | |
73 | |
74 private: | |
75 const bool enable_input_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(CastWindowTreeHost); | |
78 }; | |
79 | |
80 CastWindowTreeHost::CastWindowTreeHost(bool enable_input, | |
81 const gfx::Rect& bounds) | |
82 : WindowTreeHostPlatform(bounds), enable_input_(enable_input) {} | |
83 | |
84 CastWindowTreeHost::~CastWindowTreeHost() {} | |
85 | |
86 void CastWindowTreeHost::DispatchEvent(ui::Event* event) { | |
87 if (!enable_input_) { | |
88 return; | |
89 } | |
90 | |
91 if (event->IsKeyEvent()) { | |
92 // Convert a RawKeyDown into a character insertion; otherwise | |
93 // the WebContents will ignore most keyboard input. | |
94 GetInputMethod()->DispatchKeyEvent(event->AsKeyEvent()); | |
95 } else { | |
96 WindowTreeHostPlatform::DispatchEvent(event); | |
97 } | |
98 } | |
99 #endif | |
100 | |
101 // static | 28 // static |
102 std::unique_ptr<CastContentWindow> CastContentWindow::Create( | 29 std::unique_ptr<CastContentWindow> CastContentWindow::Create( |
103 CastContentWindow::Delegate* delegate) { | 30 CastContentWindow::Delegate* delegate) { |
104 DCHECK(delegate); | 31 DCHECK(delegate); |
105 return base::WrapUnique(new CastContentWindowLinux()); | 32 return base::WrapUnique(new CastContentWindowLinux()); |
106 } | 33 } |
107 | 34 |
108 CastContentWindowLinux::CastContentWindowLinux() : transparent_(false) {} | 35 CastContentWindowLinux::CastContentWindowLinux() : transparent_(false) {} |
109 | 36 |
110 CastContentWindowLinux::~CastContentWindowLinux() { | 37 CastContentWindowLinux::~CastContentWindowLinux() {} |
111 #if defined(USE_AURA) | |
112 CastVSyncSettings::GetInstance()->RemoveObserver(this); | |
113 window_tree_host_.reset(); | |
114 // We don't delete the screen here to avoid a CHECK failure when | |
115 // the screen size is queried periodically for metric gathering. b/18101124 | |
116 #endif | |
117 } | |
118 | 38 |
119 void CastContentWindowLinux::SetTransparent() { | 39 void CastContentWindowLinux::SetTransparent() { |
120 DCHECK(!window_tree_host_); | |
121 transparent_ = true; | 40 transparent_ = true; |
122 } | 41 } |
123 | 42 |
124 void CastContentWindowLinux::ShowWebContents( | |
125 content::WebContents* web_contents) { | |
126 #if defined(USE_AURA) | |
127 // Aura initialization | |
128 DCHECK(display::Screen::GetScreen()); | |
129 gfx::Size display_size = | |
130 display::Screen::GetScreen()->GetPrimaryDisplay().GetSizeInPixel(); | |
131 CHECK(aura::Env::GetInstance()); | |
132 window_tree_host_.reset(new CastWindowTreeHost( | |
133 CAST_IS_DEBUG_BUILD() /* enable input */, gfx::Rect(display_size))); | |
134 window_tree_host_->InitHost(); | |
135 window_tree_host_->window()->Show(); | |
136 window_tree_host_->window()->SetLayoutManager( | |
137 new CastFillLayout(window_tree_host_->window())); | |
138 | |
139 if (transparent_) { | |
140 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorTRANSPARENT); | |
141 window_tree_host_->compositor()->SetHostHasTransparentBackground(true); | |
142 } else { | |
143 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorBLACK); | |
144 } | |
145 | |
146 CastVSyncSettings::GetInstance()->AddObserver(this); | |
147 window_tree_host_->compositor()->SetAuthoritativeVSyncInterval( | |
148 CastVSyncSettings::GetInstance()->GetVSyncInterval()); | |
149 | |
150 window_tree_host_->Show(); | |
151 | |
152 // Add and show content's view/window | |
153 aura::Window* content_window = web_contents->GetNativeView(); | |
154 aura::Window* parent = window_tree_host_->window(); | |
155 if (!parent->Contains(content_window)) { | |
156 parent->AddChild(content_window); | |
157 } | |
158 content_window->Show(); | |
159 #endif | |
160 } | |
161 | |
162 std::unique_ptr<content::WebContents> CastContentWindowLinux::CreateWebContents( | 43 std::unique_ptr<content::WebContents> CastContentWindowLinux::CreateWebContents( |
163 content::BrowserContext* browser_context) { | 44 content::BrowserContext* browser_context) { |
164 CHECK(display::Screen::GetScreen()); | 45 CHECK(display::Screen::GetScreen()); |
165 gfx::Size display_size = | 46 gfx::Size display_size = |
166 display::Screen::GetScreen()->GetPrimaryDisplay().size(); | 47 display::Screen::GetScreen()->GetPrimaryDisplay().size(); |
167 | 48 |
168 content::WebContents::CreateParams create_params(browser_context, NULL); | 49 content::WebContents::CreateParams create_params(browser_context, NULL); |
169 create_params.routing_id = MSG_ROUTING_NONE; | 50 create_params.routing_id = MSG_ROUTING_NONE; |
170 create_params.initial_size = display_size; | 51 create_params.initial_size = display_size; |
171 content::WebContents* web_contents = | 52 content::WebContents* web_contents = |
172 content::WebContents::Create(create_params); | 53 content::WebContents::Create(create_params); |
173 | 54 |
174 #if defined(USE_AURA) | 55 #if defined(USE_AURA) |
175 // Resize window | 56 // Resize window |
176 aura::Window* content_window = web_contents->GetNativeView(); | 57 aura::Window* content_window = web_contents->GetNativeView(); |
177 content_window->SetBounds( | 58 content_window->SetBounds( |
178 gfx::Rect(display_size.width(), display_size.height())); | 59 gfx::Rect(display_size.width(), display_size.height())); |
179 #endif | 60 #endif |
180 | 61 |
181 content::WebContentsObserver::Observe(web_contents); | 62 content::WebContentsObserver::Observe(web_contents); |
182 return base::WrapUnique(web_contents); | 63 return base::WrapUnique(web_contents); |
183 } | 64 } |
184 | 65 |
| 66 void CastContentWindowLinux::ShowWebContents( |
| 67 content::WebContents* web_contents, |
| 68 CastWindowManager* window_manager) { |
| 69 DCHECK(window_manager); |
| 70 window_manager->AddWindow(web_contents->GetNativeView()); |
| 71 web_contents->GetNativeView()->Show(); |
| 72 } |
| 73 |
185 void CastContentWindowLinux::DidFirstVisuallyNonEmptyPaint() { | 74 void CastContentWindowLinux::DidFirstVisuallyNonEmptyPaint() { |
186 metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint(); | 75 metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint(); |
187 } | 76 } |
188 | 77 |
189 void CastContentWindowLinux::MediaStartedPlaying( | 78 void CastContentWindowLinux::MediaStartedPlaying( |
190 const MediaPlayerInfo& media_info, | 79 const MediaPlayerInfo& media_info, |
191 const MediaPlayerId& id) { | 80 const MediaPlayerId& id) { |
192 metrics::CastMetricsHelper::GetInstance()->LogMediaPlay(); | 81 metrics::CastMetricsHelper::GetInstance()->LogMediaPlay(); |
193 } | 82 } |
194 | 83 |
195 void CastContentWindowLinux::MediaStoppedPlaying( | 84 void CastContentWindowLinux::MediaStoppedPlaying( |
196 const MediaPlayerInfo& media_info, | 85 const MediaPlayerInfo& media_info, |
197 const MediaPlayerId& id) { | 86 const MediaPlayerId& id) { |
198 metrics::CastMetricsHelper::GetInstance()->LogMediaPause(); | 87 metrics::CastMetricsHelper::GetInstance()->LogMediaPause(); |
199 } | 88 } |
200 | 89 |
201 void CastContentWindowLinux::RenderViewCreated( | 90 void CastContentWindowLinux::RenderViewCreated( |
202 content::RenderViewHost* render_view_host) { | 91 content::RenderViewHost* render_view_host) { |
203 content::RenderWidgetHostView* view = | 92 content::RenderWidgetHostView* view = |
204 render_view_host->GetWidget()->GetView(); | 93 render_view_host->GetWidget()->GetView(); |
205 if (view) { | 94 if (view) { |
206 view->SetBackgroundColor(transparent_ ? SK_ColorTRANSPARENT | 95 view->SetBackgroundColor(transparent_ ? SK_ColorTRANSPARENT |
207 : SK_ColorBLACK); | 96 : SK_ColorBLACK); |
208 } | 97 } |
209 } | 98 } |
210 | 99 |
211 void CastContentWindowLinux::OnVSyncIntervalChanged(base::TimeDelta interval) { | |
212 #if defined(USE_AURA) | |
213 window_tree_host_->compositor()->SetAuthoritativeVSyncInterval(interval); | |
214 #endif | |
215 } | |
216 | |
217 } // namespace shell | 100 } // namespace shell |
218 } // namespace chromecast | 101 } // namespace chromecast |
OLD | NEW |