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.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/threading/thread_checker.h" |
| 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "net/base/filename_util.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 CastService::CastService(content::BrowserContext* browser_context) |
| 73 : browser_context_(browser_context), |
| 74 stopped_(true), |
| 75 thread_checker_(new base::ThreadChecker()) { |
| 76 } |
| 77 |
| 78 CastService::~CastService() { |
| 79 DCHECK(thread_checker_->CalledOnValidThread()); |
| 80 DCHECK(stopped_); |
| 81 } |
| 82 |
| 83 void CastService::Initialize() { |
| 84 PlatformInitialize(); |
| 85 } |
| 86 |
| 87 void CastService::Start() { |
| 88 DCHECK(thread_checker_->CalledOnValidThread()); |
| 89 |
| 90 Initialize(); |
| 91 |
| 92 // Aura initialization |
| 93 gfx::Size initial_size = gfx::Size(1280, 720); |
| 94 // TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal |
| 95 // implementation of gfx::screen and aura's TestScreen will do for now. |
| 96 // Change the code to use ozone's screen implementation when it is ready. |
| 97 aura::TestScreen* screen = aura::TestScreen::Create(initial_size); |
| 98 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen); |
| 99 CHECK(aura::Env::GetInstance()); |
| 100 window_tree_host_.reset( |
| 101 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); |
| 102 window_tree_host_->InitHost(); |
| 103 window_tree_host_->window()->SetLayoutManager( |
| 104 new FillLayout(window_tree_host_->window())); |
| 105 window_tree_host_->Show(); |
| 106 |
| 107 // Create a WebContents |
| 108 content::WebContents::CreateParams create_params(browser_context_, NULL); |
| 109 create_params.routing_id = MSG_ROUTING_NONE; |
| 110 create_params.initial_size = initial_size; |
| 111 web_contents_.reset(content::WebContents::Create(create_params)); |
| 112 |
| 113 // Add and show content's view/window |
| 114 aura::Window* content_window = web_contents_->GetNativeView(); |
| 115 aura::Window* parent = window_tree_host_->window(); |
| 116 if (!parent->Contains(content_window)) { |
| 117 parent->AddChild(content_window); |
| 118 } |
| 119 content_window->Show(); |
| 120 |
| 121 web_contents_->GetController().LoadURL(GetStartupURL(), |
| 122 content::Referrer(), |
| 123 content::PAGE_TRANSITION_TYPED, |
| 124 std::string()); |
| 125 |
| 126 stopped_ = false; |
| 127 } |
| 128 |
| 129 void CastService::Stop() { |
| 130 DCHECK(thread_checker_->CalledOnValidThread()); |
| 131 |
| 132 web_contents_->GetRenderViewHost()->ClosePage(); |
| 133 window_tree_host_.reset(); |
| 134 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); |
| 135 aura::Env::DeleteInstance(); |
| 136 web_contents_.reset(); |
| 137 stopped_ = true; |
| 138 } |
| 139 |
| 140 } // namespace chromecast |
OLD | NEW |