OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 Google Inc. 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/gfx_plane.h" |
| 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "net/base/net_util.h" |
| 14 #include "ui/gfx/size.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace chromecast { |
| 18 |
| 19 namespace { |
| 20 |
| 21 GURL GetStartupURL() { |
| 22 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 23 const base::CommandLine::StringVector& args = command_line->GetArgs(); |
| 24 |
| 25 if (args.empty()) |
| 26 return GURL("http://www.google.com/"); |
| 27 |
| 28 GURL url(args[0]); |
| 29 if (url.is_valid() && url.has_scheme()) |
| 30 return url; |
| 31 |
| 32 return net::FilePathToFileURL(base::FilePath(args[0])); |
| 33 } |
| 34 |
| 35 gfx::chromecast::GfxPlane* GetGfxPlane(int resolution_height) { |
| 36 gfx::Size requested_screen_size(1280, 720); |
| 37 bool is_1080p_allowed = gfx::chromecast::GfxPlane::Is1080pAllowed(); |
| 38 if (resolution_height > 720 && is_1080p_allowed) { |
| 39 requested_screen_size = gfx::Size(1920, 1080); |
| 40 } |
| 41 |
| 42 gfx::chromecast::GfxPlane* plane = gfx::chromecast::GfxPlane::GetPrimary(); |
| 43 // If a different resolution is requested, we need to re-initialize graphics. |
| 44 if (plane && (plane->size() != requested_screen_size)) { |
| 45 gfx::chromecast::GfxPlane::DestroyPrimary(); |
| 46 plane = NULL; |
| 47 } |
| 48 |
| 49 if (!plane) { |
| 50 plane = gfx::chromecast::GfxPlane::CreatePrimary( |
| 51 *base::CommandLine::ForCurrentProcess(), requested_screen_size); |
| 52 } |
| 53 |
| 54 return plane; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 CastService::CastService(content::BrowserContext* browser_context) |
| 60 : browser_context_(browser_context), |
| 61 stopped_(true), |
| 62 thread_checker_(new base::ThreadChecker()) { |
| 63 } |
| 64 |
| 65 CastService::~CastService() { |
| 66 DCHECK(thread_checker_->CalledOnValidThread()); |
| 67 DCHECK(stopped_); |
| 68 } |
| 69 |
| 70 void CastService::Initialize() { |
| 71 PlatformInitialize(); |
| 72 } |
| 73 |
| 74 void CastService::Start() { |
| 75 DCHECK(thread_checker_->CalledOnValidThread()); |
| 76 |
| 77 Initialize(); |
| 78 |
| 79 gfx::chromecast::GfxPlane* plane = GetGfxPlane(720); |
| 80 DCHECK(plane); |
| 81 |
| 82 content::WebContents::CreateParams create_params(browser_context_, NULL); |
| 83 create_params.routing_id = MSG_ROUTING_NONE; |
| 84 create_params.initial_size = gfx::Size(plane->size().width(), |
| 85 plane->size().height()); |
| 86 // TODO(lcwu): Before we pull in the Chromecast's application management |
| 87 // code, we (temporarily) create a webcontents object here. Once the |
| 88 // application management code is here, a new webcontents object (and |
| 89 // hence the renderer) will be created whenever a new application is |
| 90 // launched, regarless of whether the new app has the same domain as the |
| 91 // previous one. |
| 92 web_contents_.reset(content::WebContents::Create(create_params)); |
| 93 web_contents_->GetController().LoadURL(GetStartupURL(), |
| 94 content::Referrer(), |
| 95 content::PAGE_TRANSITION_TYPED, |
| 96 std::string()); |
| 97 |
| 98 stopped_ = false; |
| 99 } |
| 100 |
| 101 void CastService::Stop() { |
| 102 DCHECK(thread_checker_->CalledOnValidThread()); |
| 103 |
| 104 web_contents_->GetRenderViewHost()->ClosePage(); |
| 105 stopped_ = true; |
| 106 } |
| 107 |
| 108 } // namespace chromecast |
OLD | NEW |