Last user_story -> story change in telemetry.
[chromium-blink-merge.git] / chromecast / browser / cast_content_window.cc
blobe58a2d41463bb7a7506e08dc736dcbef3e581d91
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 "chromecast/browser/cast_browser_process.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
13 #include "ipc/ipc_message.h"
15 #if defined(USE_AURA)
16 #include "chromecast/graphics/cast_screen.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/layout_manager.h"
19 #include "ui/aura/window.h"
20 #include "ui/aura/window_tree_host.h"
21 #endif
23 namespace chromecast {
25 #if defined(USE_AURA)
26 class CastFillLayout : public aura::LayoutManager {
27 public:
28 explicit CastFillLayout(aura::Window* root) : root_(root) {}
29 ~CastFillLayout() override {}
31 private:
32 void OnWindowResized() override {}
34 void OnWindowAddedToLayout(aura::Window* child) override {
35 child->SetBounds(root_->bounds());
38 void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
40 void OnWindowRemovedFromLayout(aura::Window* child) override {}
42 void OnChildWindowVisibilityChanged(aura::Window* child,
43 bool visible) override {}
45 void SetChildBounds(aura::Window* child,
46 const gfx::Rect& requested_bounds) override {
47 SetChildBoundsDirect(child, requested_bounds);
50 aura::Window* root_;
52 DISALLOW_COPY_AND_ASSIGN(CastFillLayout);
54 #endif
56 CastContentWindow::CastContentWindow() : transparent_(false) {
59 CastContentWindow::~CastContentWindow() {
60 #if defined(USE_AURA)
61 window_tree_host_.reset();
62 // We don't delete the screen here to avoid a CHECK failure when
63 // the screen size is queried periodically for metric gathering. b/18101124
64 #endif
67 void CastContentWindow::CreateWindowTree(
68 const gfx::Size& initial_size,
69 content::WebContents* web_contents) {
70 #if defined(USE_AURA)
71 // Aura initialization
72 CastScreen* cast_screen =
73 shell::CastBrowserProcess::GetInstance()->cast_screen();
74 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
75 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, cast_screen);
76 if (cast_screen->GetPrimaryDisplay().size() != initial_size)
77 cast_screen->UpdateDisplaySize(initial_size);
79 CHECK(aura::Env::GetInstance());
80 window_tree_host_.reset(
81 aura::WindowTreeHost::Create(gfx::Rect(initial_size)));
82 window_tree_host_->InitHost();
83 window_tree_host_->window()->SetLayoutManager(
84 new CastFillLayout(window_tree_host_->window()));
86 if (transparent_) {
87 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorTRANSPARENT);
88 window_tree_host_->compositor()->SetHostHasTransparentBackground(true);
89 } else {
90 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorBLACK);
92 window_tree_host_->Show();
94 // Add and show content's view/window
95 aura::Window* content_window = web_contents->GetNativeView();
96 aura::Window* parent = window_tree_host_->window();
97 if (!parent->Contains(content_window)) {
98 parent->AddChild(content_window);
100 content_window->Show();
101 #endif
104 scoped_ptr<content::WebContents> CastContentWindow::CreateWebContents(
105 const gfx::Size& initial_size,
106 content::BrowserContext* browser_context) {
107 content::WebContents::CreateParams create_params(browser_context, NULL);
108 create_params.routing_id = MSG_ROUTING_NONE;
109 create_params.initial_size = initial_size;
110 content::WebContents* web_contents = content::WebContents::Create(
111 create_params);
112 content::WebContentsObserver::Observe(web_contents);
113 return make_scoped_ptr(web_contents);
116 void CastContentWindow::DidFirstVisuallyNonEmptyPaint() {
117 metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint();
120 void CastContentWindow::MediaPaused() {
121 metrics::CastMetricsHelper::GetInstance()->LogMediaPause();
124 void CastContentWindow::MediaStartedPlaying() {
125 metrics::CastMetricsHelper::GetInstance()->LogMediaPlay();
128 void CastContentWindow::RenderViewCreated(
129 content::RenderViewHost* render_view_host) {
130 content::RenderWidgetHostView* view = render_view_host->GetView();
131 if (view)
132 view->SetBackgroundColor(transparent_ ? SK_ColorTRANSPARENT
133 : SK_ColorBLACK);
136 } // namespace chromecast