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/service/cast_service_simple.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/macros.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/web_contents.h"
12 #include "net/base/filename_util.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/layout_manager.h"
15 #include "ui/aura/test/test_screen.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/gfx/size.h"
21 namespace chromecast
{
25 GURL
GetStartupURL() {
26 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
27 const base::CommandLine::StringVector
& args
= command_line
->GetArgs();
30 return GURL("http://www.google.com/");
33 if (url
.is_valid() && url
.has_scheme())
36 return net::FilePathToFileURL(base::FilePath(args
[0]));
39 class FillLayout
: public aura::LayoutManager
{
41 explicit FillLayout(aura::Window
* root
) : root_(root
) {}
42 virtual ~FillLayout() {}
45 // aura::LayoutManager:
46 virtual void OnWindowResized() OVERRIDE
{}
48 virtual void OnWindowAddedToLayout(aura::Window
* child
) OVERRIDE
{
49 child
->SetBounds(root_
->bounds());
52 virtual void OnWillRemoveWindowFromLayout(aura::Window
* child
) OVERRIDE
{}
54 virtual void OnWindowRemovedFromLayout(aura::Window
* child
) OVERRIDE
{}
56 virtual void OnChildWindowVisibilityChanged(aura::Window
* child
,
57 bool visible
) OVERRIDE
{}
59 virtual void SetChildBounds(aura::Window
* child
,
60 const gfx::Rect
& requested_bounds
) OVERRIDE
{
61 SetChildBoundsDirect(child
, requested_bounds
);
66 DISALLOW_COPY_AND_ASSIGN(FillLayout
);
72 CastService
* CastService::Create(content::BrowserContext
* browser_context
) {
73 return new CastServiceSimple(browser_context
);
76 CastServiceSimple::CastServiceSimple(content::BrowserContext
* browser_context
)
77 : CastService(browser_context
) {
80 CastServiceSimple::~CastServiceSimple() {
83 void CastServiceSimple::Initialize() {
86 void CastServiceSimple::StartInternal() {
87 // Aura initialization
88 gfx::Size initial_size
= gfx::Size(1280, 720);
89 // TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal
90 // implementation of gfx::screen and aura's TestScreen will do for now.
91 // Change the code to use ozone's screen implementation when it is ready.
92 aura::TestScreen
* screen
= aura::TestScreen::Create(initial_size
);
93 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen
);
94 CHECK(aura::Env::GetInstance());
95 window_tree_host_
.reset(
96 aura::WindowTreeHost::Create(gfx::Rect(initial_size
)));
97 window_tree_host_
->InitHost();
98 window_tree_host_
->window()->SetLayoutManager(
99 new FillLayout(window_tree_host_
->window()));
100 window_tree_host_
->Show();
102 // Create a WebContents
103 content::WebContents::CreateParams
create_params(browser_context(), NULL
);
104 create_params
.routing_id
= MSG_ROUTING_NONE
;
105 create_params
.initial_size
= initial_size
;
106 web_contents_
.reset(content::WebContents::Create(create_params
));
108 // Add and show content's view/window
109 aura::Window
* content_window
= web_contents_
->GetNativeView();
110 aura::Window
* parent
= window_tree_host_
->window();
111 if (!parent
->Contains(content_window
)) {
112 parent
->AddChild(content_window
);
114 content_window
->Show();
116 web_contents_
->GetController().LoadURL(GetStartupURL(),
118 ui::PAGE_TRANSITION_TYPED
,
122 void CastServiceSimple::StopInternal() {
123 web_contents_
->GetRenderViewHost()->ClosePage();
124 window_tree_host_
.reset();
125 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, NULL
);
126 aura::Env::DeleteInstance();
127 web_contents_
.reset();
130 } // namespace chromecast