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/threading/thread_checker.h" |
| 10 #include "chromecast/ui/gfx/screen_cast.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/window.h" |
| 17 #include "ui/aura/window_tree_host.h" |
| 18 #include "ui/gfx/size.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace chromecast { |
| 22 |
| 23 namespace { |
| 24 |
| 25 GURL GetStartupURL() { |
| 26 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 27 const base::CommandLine::StringVector& args = command_line->GetArgs(); |
| 28 |
| 29 if (args.empty()) |
| 30 return GURL("http://www.google.com/"); |
| 31 |
| 32 GURL url(args[0]); |
| 33 if (url.is_valid() && url.has_scheme()) |
| 34 return url; |
| 35 |
| 36 return net::FilePathToFileURL(base::FilePath(args[0])); |
| 37 } |
| 38 |
| 39 class FillLayout : public aura::LayoutManager { |
| 40 public: |
| 41 explicit FillLayout(aura::Window* root) : root_(root) {} |
| 42 virtual ~FillLayout() {} |
| 43 |
| 44 private: |
| 45 // aura::LayoutManager: |
| 46 virtual void OnWindowResized() OVERRIDE {} |
| 47 |
| 48 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { |
| 49 child->SetBounds(root_->bounds()); |
| 50 } |
| 51 |
| 52 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
| 53 |
| 54 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} |
| 55 |
| 56 virtual void OnChildWindowVisibilityChanged(aura::Window* child, |
| 57 bool visible) OVERRIDE {} |
| 58 |
| 59 virtual void SetChildBounds(aura::Window* child, |
| 60 const gfx::Rect& requested_bounds) OVERRIDE { |
| 61 SetChildBoundsDirect(child, requested_bounds); |
| 62 } |
| 63 |
| 64 aura::Window* root_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(FillLayout); |
| 67 }; |
| 68 |
| 69 } // namespace |
| 70 |
| 71 CastService::CastService(content::BrowserContext* browser_context) |
| 72 : browser_context_(browser_context), |
| 73 stopped_(true), |
| 74 thread_checker_(new base::ThreadChecker()) { |
| 75 } |
| 76 |
| 77 CastService::~CastService() { |
| 78 DCHECK(thread_checker_->CalledOnValidThread()); |
| 79 DCHECK(stopped_); |
| 80 } |
| 81 |
| 82 void CastService::Initialize() { |
| 83 PlatformInitialize(); |
| 84 } |
| 85 |
| 86 void CastService::Start() { |
| 87 DCHECK(thread_checker_->CalledOnValidThread()); |
| 88 |
| 89 Initialize(); |
| 90 |
| 91 // Aura initialization |
| 92 gfx::Size initial_size = gfx::Size(1280, 720); |
| 93 ScreenCast* screen = ScreenCast::Create(initial_size); |
| 94 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen); |
| 95 CHECK(aura::Env::GetInstance()); |
| 96 window_tree_host_.reset( |
| 97 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); |
| 98 window_tree_host_->InitHost(); |
| 99 window_tree_host_->window()->SetLayoutManager( |
| 100 new FillLayout(window_tree_host_->window())); |
| 101 window_tree_host_->Show(); |
| 102 |
| 103 // Create a WebContents |
| 104 content::WebContents::CreateParams create_params(browser_context_, NULL); |
| 105 create_params.routing_id = MSG_ROUTING_NONE; |
| 106 create_params.initial_size = initial_size; |
| 107 // TODO(lcwu): Before we pull in the Chromecast's application management |
| 108 // code, we (temporarily) create a webcontents object here. Once the |
| 109 // application management code is here, a new webcontents object (and |
| 110 // hence the renderer) will be created whenever a new application is |
| 111 // launched, regarless of whether the new app has the same domain as the |
| 112 // previous one. |
| 113 web_contents_.reset(content::WebContents::Create(create_params)); |
| 114 |
| 115 // Add and show content's view/window |
| 116 aura::Window* content_window = web_contents_->GetNativeView(); |
| 117 aura::Window* parent = window_tree_host_->window(); |
| 118 if (!parent->Contains(content_window)) { |
| 119 parent->AddChild(content_window); |
| 120 } |
| 121 content_window->Show(); |
| 122 |
| 123 web_contents_->GetController().LoadURL(GetStartupURL(), |
| 124 content::Referrer(), |
| 125 content::PAGE_TRANSITION_TYPED, |
| 126 std::string()); |
| 127 |
| 128 stopped_ = false; |
| 129 } |
| 130 |
| 131 void CastService::Stop() { |
| 132 DCHECK(thread_checker_->CalledOnValidThread()); |
| 133 |
| 134 web_contents_->GetRenderViewHost()->ClosePage(); |
| 135 window_tree_host_.reset(); |
| 136 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); |
| 137 aura::Env::DeleteInstance(); |
| 138 web_contents_.reset(); |
| 139 stopped_ = true; |
| 140 } |
| 141 |
| 142 } // namespace chromecast |
OLD | NEW |