[MacViews] Show comboboxes with a native NSMenu
[chromium-blink-merge.git] / chromecast / browser / cast_content_window.cc
blob146679c764994edc46f2bc43712bf47ffa1caf7d
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 {
24 namespace shell {
26 #if defined(USE_AURA)
27 class CastFillLayout : public aura::LayoutManager {
28 public:
29 explicit CastFillLayout(aura::Window* root) : root_(root) {}
30 ~CastFillLayout() override {}
32 private:
33 void OnWindowResized() override {}
35 void OnWindowAddedToLayout(aura::Window* child) override {
36 child->SetBounds(root_->bounds());
39 void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
41 void OnWindowRemovedFromLayout(aura::Window* child) override {}
43 void OnChildWindowVisibilityChanged(aura::Window* child,
44 bool visible) override {}
46 void SetChildBounds(aura::Window* child,
47 const gfx::Rect& requested_bounds) override {
48 SetChildBoundsDirect(child, requested_bounds);
51 aura::Window* root_;
53 DISALLOW_COPY_AND_ASSIGN(CastFillLayout);
55 #endif
57 CastContentWindow::CastContentWindow() : transparent_(false) {
60 CastContentWindow::~CastContentWindow() {
61 #if defined(USE_AURA)
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
65 #endif
68 void CastContentWindow::CreateWindowTree(
69 const gfx::Size& initial_size,
70 content::WebContents* web_contents) {
71 #if defined(USE_AURA)
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 if (transparent_) {
88 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorTRANSPARENT);
89 window_tree_host_->compositor()->SetHostHasTransparentBackground(true);
90 } else {
91 window_tree_host_->compositor()->SetBackgroundColor(SK_ColorBLACK);
93 window_tree_host_->Show();
95 // Add and show content's view/window
96 aura::Window* content_window = web_contents->GetNativeView();
97 aura::Window* parent = window_tree_host_->window();
98 if (!parent->Contains(content_window)) {
99 parent->AddChild(content_window);
101 content_window->Show();
102 #endif
105 scoped_ptr<content::WebContents> CastContentWindow::CreateWebContents(
106 const gfx::Size& initial_size,
107 content::BrowserContext* browser_context) {
108 content::WebContents::CreateParams create_params(browser_context, NULL);
109 create_params.routing_id = MSG_ROUTING_NONE;
110 create_params.initial_size = initial_size;
111 content::WebContents* web_contents = content::WebContents::Create(
112 create_params);
113 content::WebContentsObserver::Observe(web_contents);
114 return make_scoped_ptr(web_contents);
117 void CastContentWindow::DidFirstVisuallyNonEmptyPaint() {
118 metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint();
121 void CastContentWindow::MediaPaused() {
122 metrics::CastMetricsHelper::GetInstance()->LogMediaPause();
125 void CastContentWindow::MediaStartedPlaying() {
126 metrics::CastMetricsHelper::GetInstance()->LogMediaPlay();
129 void CastContentWindow::RenderViewCreated(
130 content::RenderViewHost* render_view_host) {
131 content::RenderWidgetHostView* view = render_view_host->GetView();
132 if (view)
133 view->SetBackgroundColor(transparent_ ? SK_ColorTRANSPARENT
134 : SK_ColorBLACK);
137 } // namespace shell
138 } // namespace chromecast