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 "net/url_request/url_request_context_getter.h"
14 #include "ui/aura/env.h"
15 #include "ui/aura/layout_manager.h"
16 #include "ui/aura/test/test_screen.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/gfx/size.h"
22 namespace chromecast {
26 GURL GetStartupURL() {
27 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
28 const base::CommandLine::StringVector& args = command_line->GetArgs();
31 return GURL("http://www.google.com/");
34 if (url.is_valid() && url.has_scheme())
37 return net::FilePathToFileURL(base::FilePath(args[0]));
40 class FillLayout : public aura::LayoutManager {
42 explicit FillLayout(aura::Window* root) : root_(root) {}
43 virtual ~FillLayout() {}
46 // aura::LayoutManager:
47 virtual void OnWindowResized() OVERRIDE {}
49 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
50 child->SetBounds(root_->bounds());
53 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
55 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
57 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
58 bool visible) OVERRIDE {}
60 virtual void SetChildBounds(aura::Window* child,
61 const gfx::Rect& requested_bounds) OVERRIDE {
62 SetChildBoundsDirect(child, requested_bounds);
67 DISALLOW_COPY_AND_ASSIGN(FillLayout);
73 CastService* CastService::Create(
74 content::BrowserContext* browser_context,
75 net::URLRequestContextGetter* request_context_getter) {
76 return new CastServiceSimple(browser_context);
79 CastServiceSimple::CastServiceSimple(content::BrowserContext* browser_context)
80 : CastService(browser_context) {
83 CastServiceSimple::~CastServiceSimple() {
86 void CastServiceSimple::Initialize() {
89 void CastServiceSimple::StartInternal() {
90 // Aura initialization
91 gfx::Size initial_size = gfx::Size(1280, 720);
92 // TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal
93 // implementation of gfx::screen and aura's TestScreen will do for now.
94 // Change the code to use ozone's screen implementation when it is ready.
95 aura::TestScreen* screen = aura::TestScreen::Create(initial_size);
96 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
97 CHECK(aura::Env::GetInstance());
98 window_tree_host_.reset(
99 aura::WindowTreeHost::Create(gfx::Rect(initial_size)));
100 window_tree_host_->InitHost();
101 window_tree_host_->window()->SetLayoutManager(
102 new FillLayout(window_tree_host_->window()));
103 window_tree_host_->Show();
105 // Create a WebContents
106 content::WebContents::CreateParams create_params(browser_context(), NULL);
107 create_params.routing_id = MSG_ROUTING_NONE;
108 create_params.initial_size = initial_size;
109 web_contents_.reset(content::WebContents::Create(create_params));
111 // Add and show content's view/window
112 aura::Window* content_window = web_contents_->GetNativeView();
113 aura::Window* parent = window_tree_host_->window();
114 if (!parent->Contains(content_window)) {
115 parent->AddChild(content_window);
117 content_window->Show();
119 web_contents_->GetController().LoadURL(GetStartupURL(),
121 ui::PAGE_TRANSITION_TYPED,
125 void CastServiceSimple::StopInternal() {
126 web_contents_->GetRenderViewHost()->ClosePage();
127 window_tree_host_.reset();
128 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
129 aura::Env::DeleteInstance();
130 web_contents_.reset();
133 } // namespace chromecast