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 "content/public/browser/render_view_host.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "net/base/filename_util.h" |
| 13 #include "ui/aura/env.h" |
| 14 #include "ui/aura/layout_manager.h" |
| 15 #include "ui/aura/test/test_screen.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 // TODO(lcwu): We only need a minimal implementation of gfx::screen |
| 94 // and aura's TestScreen will do for us now. We should change to use |
| 95 // ozone's screen implementation when it is ready. |
| 96 aura::TestScreen* screen = aura::TestScreen::Create(initial_size); |
| 97 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen); |
| 98 CHECK(aura::Env::GetInstance()); |
| 99 window_tree_host_.reset( |
| 100 aura::WindowTreeHost::Create(gfx::Rect(initial_size))); |
| 101 window_tree_host_->InitHost(); |
| 102 window_tree_host_->window()->SetLayoutManager( |
| 103 new FillLayout(window_tree_host_->window())); |
| 104 window_tree_host_->Show(); |
| 105 |
| 106 // Create a WebContents |
| 107 content::WebContents::CreateParams create_params(browser_context_, NULL); |
| 108 create_params.routing_id = MSG_ROUTING_NONE; |
| 109 create_params.initial_size = initial_size; |
| 110 // TODO(lcwu): Before we pull in the Chromecast's application management |
| 111 // code, we (temporarily) create a webcontents object here. Once the |
| 112 // application management code is here, a new webcontents object (and |
| 113 // hence the renderer) will be created whenever a new application is |
| 114 // launched, regarless of whether the new app has the same domain as the |
| 115 // previous one. |
| 116 web_contents_.reset(content::WebContents::Create(create_params)); |
| 117 |
| 118 // Add and show content's view/window |
| 119 aura::Window* content_window = web_contents_->GetNativeView(); |
| 120 aura::Window* parent = window_tree_host_->window(); |
| 121 if (!parent->Contains(content_window)) { |
| 122 parent->AddChild(content_window); |
| 123 } |
| 124 content_window->Show(); |
| 125 |
| 126 web_contents_->GetController().LoadURL(GetStartupURL(), |
| 127 content::Referrer(), |
| 128 content::PAGE_TRANSITION_TYPED, |
| 129 std::string()); |
| 130 |
| 131 stopped_ = false; |
| 132 } |
| 133 |
| 134 void CastService::Stop() { |
| 135 DCHECK(thread_checker_->CalledOnValidThread()); |
| 136 |
| 137 web_contents_->GetRenderViewHost()->ClosePage(); |
| 138 window_tree_host_.reset(); |
| 139 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL); |
| 140 aura::Env::DeleteInstance(); |
| 141 web_contents_.reset(); |
| 142 stopped_ = true; |
| 143 } |
| 144 |
| 145 } // namespace chromecast |
OLD | NEW |