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
3 // found in the LICENSE file.
5 #include "chromecast/browser/cast_content_window.h"
7 #include "base/command_line.h"
8 #include "base/threading/thread_restrictions.h"
9 #include "chromecast/base/chromecast_switches.h"
10 #include "chromecast/base/metrics/cast_metrics_helper.h"
11 #include "chromecast/browser/cast_browser_process.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 "ipc/ipc_message.h"
18 #include "chromecast/graphics/cast_screen.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/layout_manager.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_tree_host.h"
25 namespace chromecast
{
28 class CastFillLayout
: public aura::LayoutManager
{
30 explicit CastFillLayout(aura::Window
* root
) : root_(root
) {}
31 ~CastFillLayout() override
{}
34 void OnWindowResized() override
{}
36 void OnWindowAddedToLayout(aura::Window
* child
) override
{
37 child
->SetBounds(root_
->bounds());
40 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
42 void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
44 void OnChildWindowVisibilityChanged(aura::Window
* child
,
45 bool visible
) override
{}
47 void SetChildBounds(aura::Window
* child
,
48 const gfx::Rect
& requested_bounds
) override
{
49 SetChildBoundsDirect(child
, requested_bounds
);
54 DISALLOW_COPY_AND_ASSIGN(CastFillLayout
);
58 CastContentWindow::CastContentWindow() {}
60 CastContentWindow::~CastContentWindow() {
62 window_tree_host_
.reset();
63 // We don't delete the screen here to avoid a CHECK failure when
64 // the screen size is queried periodically for metric gathering. b/18101124
68 void CastContentWindow::CreateWindowTree(
69 const gfx::Size
& initial_size
,
70 content::WebContents
* web_contents
) {
72 // Aura initialization
73 CastScreen
* cast_screen
=
74 shell::CastBrowserProcess::GetInstance()->cast_screen();
75 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE
))
76 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, cast_screen
);
77 if (cast_screen
->GetPrimaryDisplay().size() != initial_size
)
78 cast_screen
->UpdateDisplaySize(initial_size
);
80 CHECK(aura::Env::GetInstance());
81 window_tree_host_
.reset(
82 aura::WindowTreeHost::Create(gfx::Rect(initial_size
)));
83 window_tree_host_
->InitHost();
84 window_tree_host_
->window()->SetLayoutManager(
85 new CastFillLayout(window_tree_host_
->window()));
87 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
88 if (command_line
->HasSwitch(switches::kEnableTransparentBackground
)) {
89 window_tree_host_
->compositor()->SetBackgroundColor(SK_ColorTRANSPARENT
);
90 window_tree_host_
->compositor()->SetHostHasTransparentBackground(true);
92 window_tree_host_
->compositor()->SetBackgroundColor(SK_ColorBLACK
);
94 window_tree_host_
->Show();
96 // Add and show content's view/window
97 aura::Window
* content_window
= web_contents
->GetNativeView();
98 aura::Window
* parent
= window_tree_host_
->window();
99 if (!parent
->Contains(content_window
)) {
100 parent
->AddChild(content_window
);
102 content_window
->Show();
106 scoped_ptr
<content::WebContents
> CastContentWindow::CreateWebContents(
107 const gfx::Size
& initial_size
,
108 content::BrowserContext
* browser_context
) {
109 content::WebContents::CreateParams
create_params(browser_context
, NULL
);
110 create_params
.routing_id
= MSG_ROUTING_NONE
;
111 create_params
.initial_size
= initial_size
;
112 content::WebContents
* web_contents
= content::WebContents::Create(
114 content::WebContentsObserver::Observe(web_contents
);
115 return make_scoped_ptr(web_contents
);
118 void CastContentWindow::DidFirstVisuallyNonEmptyPaint() {
119 metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint();
122 void CastContentWindow::MediaPaused() {
123 metrics::CastMetricsHelper::GetInstance()->LogMediaPause();
126 void CastContentWindow::MediaStartedPlaying() {
127 metrics::CastMetricsHelper::GetInstance()->LogMediaPlay();
130 void CastContentWindow::RenderViewCreated(
131 content::RenderViewHost
* render_view_host
) {
132 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
133 if (command_line
->HasSwitch(switches::kEnableTransparentBackground
)) {
134 content::RenderWidgetHostView
* view
= render_view_host
->GetView();
136 view
->SetBackgroundColor(SK_ColorTRANSPARENT
);
140 } // namespace chromecast