| OLD | NEW |
| (Empty) |
| 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 | |
| 22 namespace chromecast { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 GURL 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 | |
| 40 class 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 | |
| 73 CastService* CastService::Create( | |
| 74 content::BrowserContext* browser_context, | |
| 75 net::URLRequestContextGetter* request_context_getter, | |
| 76 const OptInStatsChangedCallback& opt_in_stats_callback) { | |
| 77 return new CastServiceSimple(browser_context, opt_in_stats_callback); | |
| 78 } | |
| 79 | |
| 80 CastServiceSimple::CastServiceSimple( | |
| 81 content::BrowserContext* browser_context, | |
| 82 const OptInStatsChangedCallback& opt_in_stats_callback) | |
| 83 : CastService(browser_context, opt_in_stats_callback) { | |
| 84 } | |
| 85 | |
| 86 CastServiceSimple::~CastServiceSimple() { | |
| 87 } | |
| 88 | |
| 89 void CastServiceSimple::Initialize() { | |
| 90 } | |
| 91 | |
| 92 void CastServiceSimple::StartInternal() { | |
| 93 // Aura initialization | |
| 94 gfx::Size initial_size = gfx::Size(1280, 720); | |
| 95 // TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal | |
| 96 // implementation of gfx::screen and aura's TestScreen will do for now. | |
| 97 // Change the code to use ozone's screen implementation when it is ready. | |
| 98 aura::TestScreen* screen = aura::TestScreen::Create(initial_size); | |
| 99 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen); | |
| 100 CHECK(aura::Env::GetInstance()); | |
| 101 window_tree_host_.reset( | |
| 102 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); | |
| 103 window_tree_host_->InitHost(); | |
| 104 window_tree_host_->window()->SetLayoutManager( | |
| 105 new FillLayout(window_tree_host_->window())); | |
| 106 window_tree_host_->Show(); | |
| 107 | |
| 108 // Create a WebContents | |
| 109 content::WebContents::CreateParams create_params(browser_context(), NULL); | |
| 110 create_params.routing_id = MSG_ROUTING_NONE; | |
| 111 create_params.initial_size = initial_size; | |
| 112 web_contents_.reset(content::WebContents::Create(create_params)); | |
| 113 | |
| 114 // Add and show content's view/window | |
| 115 aura::Window* content_window = web_contents_->GetNativeView(); | |
| 116 aura::Window* parent = window_tree_host_->window(); | |
| 117 if (!parent->Contains(content_window)) { | |
| 118 parent->AddChild(content_window); | |
| 119 } | |
| 120 content_window->Show(); | |
| 121 | |
| 122 web_contents_->GetController().LoadURL(GetStartupURL(), | |
| 123 content::Referrer(), | |
| 124 ui::PAGE_TRANSITION_TYPED, | |
| 125 std::string()); | |
| 126 } | |
| 127 | |
| 128 void CastServiceSimple::StopInternal() { | |
| 129 web_contents_->GetRenderViewHost()->ClosePage(); | |
| 130 window_tree_host_.reset(); | |
| 131 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); | |
| 132 aura::Env::DeleteInstance(); | |
| 133 web_contents_.reset(); | |
| 134 } | |
| 135 | |
| 136 } // namespace chromecast | |
| OLD | NEW |