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 "content/public/browser/web_contents.h"
9 #include "ipc/ipc_message.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/layout_manager.h"
14 #include "ui/aura/test/test_screen.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_tree_host.h"
19 namespace chromecast
{
22 class CastFillLayout
: public aura::LayoutManager
{
24 explicit CastFillLayout(aura::Window
* root
) : root_(root
) {}
25 virtual ~CastFillLayout() {}
28 virtual void OnWindowResized() override
{}
30 virtual void OnWindowAddedToLayout(aura::Window
* child
) override
{
31 child
->SetBounds(root_
->bounds());
34 virtual void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
36 virtual void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
38 virtual void OnChildWindowVisibilityChanged(aura::Window
* child
,
39 bool visible
) override
{}
41 virtual void SetChildBounds(aura::Window
* child
,
42 const gfx::Rect
& requested_bounds
) override
{
43 SetChildBoundsDirect(child
, requested_bounds
);
48 DISALLOW_COPY_AND_ASSIGN(CastFillLayout
);
52 CastContentWindow::CastContentWindow() {}
54 CastContentWindow::~CastContentWindow() {
56 window_tree_host_
.reset();
57 // We don't delete the screen here to avoid a CHECK failure when
58 // the screen size is queried periodically for metric gathering. b/18101124
62 scoped_ptr
<content::WebContents
> CastContentWindow::Create(
63 const gfx::Size
& initial_size
,
64 content::BrowserContext
* browser_context
) {
66 // Aura initialization
67 // TODO(lcwu): We only need a minimal implementation of gfx::Screen
68 // and aura's TestScreen will do for us now. We should change to use
69 // ozone's screen implementation when it is ready.
70 gfx::Screen
* old_screen
=
71 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE
);
72 if (!old_screen
|| old_screen
->GetPrimaryDisplay().size() != initial_size
) {
73 gfx::Screen
* new_screen
= aura::TestScreen::Create(initial_size
);
74 DCHECK(new_screen
) << "New screen not created.";
75 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, new_screen
);
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()));
86 window_tree_host_
->Show();
89 content::WebContents::CreateParams
create_params(browser_context
, NULL
);
90 create_params
.routing_id
= MSG_ROUTING_NONE
;
91 create_params
.initial_size
= initial_size
;
92 content::WebContents
* web_contents
= content::WebContents::Create(
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();
105 return make_scoped_ptr(web_contents
);
108 } // namespace chromecast