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. 4 5#include "chromecast/service/cast_service_simple.h" 6 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" 20#include "url/gurl.h" 21 22namespace chromecast { 23 24namespace { 25 26GURL GetStartupURL() { 27 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 28 const base::CommandLine::StringVector& args = command_line->GetArgs(); 29 30 if (args.empty()) 31 return GURL("http://www.google.com/"); 32 33 GURL url(args[0]); 34 if (url.is_valid() && url.has_scheme()) 35 return url; 36 37 return net::FilePathToFileURL(base::FilePath(args[0])); 38} 39 40class FillLayout : public aura::LayoutManager { 41 public: 42 explicit FillLayout(aura::Window* root) : root_(root) {} 43 virtual ~FillLayout() {} 44 45 private: 46 // aura::LayoutManager: 47 virtual void OnWindowResized() OVERRIDE {} 48 49 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { 50 child->SetBounds(root_->bounds()); 51 } 52 53 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} 54 55 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} 56 57 virtual void OnChildWindowVisibilityChanged(aura::Window* child, 58 bool visible) OVERRIDE {} 59 60 virtual void SetChildBounds(aura::Window* child, 61 const gfx::Rect& requested_bounds) OVERRIDE { 62 SetChildBoundsDirect(child, requested_bounds); 63 } 64 65 aura::Window* root_; 66 67 DISALLOW_COPY_AND_ASSIGN(FillLayout); 68}; 69 70} // namespace 71 72// static 73CastService* CastService::Create( 74 content::BrowserContext* browser_context, 75 net::URLRequestContextGetter* request_context_getter) { 76 return new CastServiceSimple(browser_context); 77} 78 79CastServiceSimple::CastServiceSimple(content::BrowserContext* browser_context) 80 : CastService(browser_context) { 81} 82 83CastServiceSimple::~CastServiceSimple() { 84} 85 86void CastServiceSimple::Initialize() { 87} 88 89void 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(); 104 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)); 110 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); 116 } 117 content_window->Show(); 118 119 web_contents_->GetController().LoadURL(GetStartupURL(), 120 content::Referrer(), 121 ui::PAGE_TRANSITION_TYPED, 122 std::string()); 123} 124 125void 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(); 131} 132 133} // namespace chromecast 134