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/threading/thread_restrictions.h"
8 #include "chromecast/base/metrics/cast_metrics_helper.h"
9 #include "content/public/browser/web_contents.h"
10 #include "ipc/ipc_message.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/layout_manager.h"
15 #include "ui/aura/test/test_focus_client.h"
16 #include "ui/aura/test/test_screen.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_tree_host.h"
21 namespace chromecast
{
24 class CastFillLayout
: public aura::LayoutManager
{
26 explicit CastFillLayout(aura::Window
* root
) : root_(root
) {}
27 ~CastFillLayout() override
{}
30 void OnWindowResized() override
{}
32 void OnWindowAddedToLayout(aura::Window
* child
) override
{
33 child
->SetBounds(root_
->bounds());
36 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
38 void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
40 void OnChildWindowVisibilityChanged(aura::Window
* child
,
41 bool visible
) override
{}
43 void SetChildBounds(aura::Window
* child
,
44 const gfx::Rect
& requested_bounds
) override
{
45 SetChildBoundsDirect(child
, requested_bounds
);
50 DISALLOW_COPY_AND_ASSIGN(CastFillLayout
);
54 CastContentWindow::CastContentWindow() {}
56 CastContentWindow::~CastContentWindow() {
58 window_tree_host_
.reset();
59 // We don't delete the screen here to avoid a CHECK failure when
60 // the screen size is queried periodically for metric gathering. b/18101124
64 void CastContentWindow::CreateWindowTree(
65 const gfx::Size
& initial_size
,
66 content::WebContents
* web_contents
) {
68 // Aura initialization
69 // TODO(lcwu): We only need a minimal implementation of gfx::Screen
70 // and aura's TestScreen will do for us now. We should change to use
71 // ozone's screen implementation when it is ready.
72 gfx::Screen
* old_screen
=
73 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE
);
74 if (!old_screen
|| old_screen
->GetPrimaryDisplay().size() != initial_size
) {
75 gfx::Screen
* new_screen
= aura::TestScreen::Create(initial_size
);
76 DCHECK(new_screen
) << "New screen not created.";
77 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, new_screen
);
82 CHECK(aura::Env::GetInstance());
83 window_tree_host_
.reset(
84 aura::WindowTreeHost::Create(gfx::Rect(initial_size
)));
85 window_tree_host_
->InitHost();
86 window_tree_host_
->window()->SetLayoutManager(
87 new CastFillLayout(window_tree_host_
->window()));
88 window_tree_host_
->compositor()->SetBackgroundColor(SK_ColorBLACK
);
90 focus_client_
.reset(new aura::test::TestFocusClient());
91 aura::client::SetFocusClient(
92 window_tree_host_
->window(), focus_client_
.get());
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 } // namespace chromecast