Index: chromecast/service/cast_service.cc |
diff --git a/chromecast/service/cast_service.cc b/chromecast/service/cast_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..03b6572296ad70646fc06e39585d260d61ffd5c7 |
--- /dev/null |
+++ b/chromecast/service/cast_service.cc |
@@ -0,0 +1,108 @@ |
+// Copyright (c) 2014 Google Inc. All Rights Reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromecast/service/cast_service.h" |
+ |
+#include "base/command_line.h" |
+#include "base/files/file_path.h" |
+#include "base/threading/thread_checker.h" |
+#include "chromecast/ui/gfx/gfx_plane.h" |
+#include "content/public/browser/render_view_host.h" |
+#include "content/public/browser/web_contents.h" |
+#include "net/base/net_util.h" |
+#include "ui/gfx/size.h" |
+#include "url/gurl.h" |
+ |
+namespace chromecast { |
+ |
+namespace { |
+ |
+GURL GetStartupURL() { |
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
+ const base::CommandLine::StringVector& args = command_line->GetArgs(); |
+ |
+ if (args.empty()) |
+ return GURL("http://www.google.com/"); |
+ |
+ GURL url(args[0]); |
+ if (url.is_valid() && url.has_scheme()) |
+ return url; |
+ |
+ return net::FilePathToFileURL(base::FilePath(args[0])); |
+} |
+ |
+gfx::chromecast::GfxPlane* GetGfxPlane(int resolution_height) { |
+ gfx::Size requested_screen_size(1280, 720); |
+ bool is_1080p_allowed = gfx::chromecast::GfxPlane::Is1080pAllowed(); |
+ if (resolution_height > 720 && is_1080p_allowed) { |
+ requested_screen_size = gfx::Size(1920, 1080); |
+ } |
+ |
+ gfx::chromecast::GfxPlane* plane = gfx::chromecast::GfxPlane::GetPrimary(); |
+ // If a different resolution is requested, we need to re-initialize graphics. |
+ if (plane && (plane->size() != requested_screen_size)) { |
+ gfx::chromecast::GfxPlane::DestroyPrimary(); |
+ plane = NULL; |
+ } |
+ |
+ if (!plane) { |
+ plane = gfx::chromecast::GfxPlane::CreatePrimary( |
+ *base::CommandLine::ForCurrentProcess(), requested_screen_size); |
+ } |
+ |
+ return plane; |
+} |
+ |
+} // namespace |
+ |
+CastService::CastService(content::BrowserContext* browser_context) |
+ : browser_context_(browser_context), |
+ stopped_(true), |
+ thread_checker_(new base::ThreadChecker()) { |
+} |
+ |
+CastService::~CastService() { |
+ DCHECK(thread_checker_->CalledOnValidThread()); |
+ DCHECK(stopped_); |
+} |
+ |
+void CastService::Initialize() { |
+ PlatformInitialize(); |
+} |
+ |
+void CastService::Start() { |
+ DCHECK(thread_checker_->CalledOnValidThread()); |
+ |
+ Initialize(); |
+ |
+ gfx::chromecast::GfxPlane* plane = GetGfxPlane(720); |
+ DCHECK(plane); |
+ |
+ content::WebContents::CreateParams create_params(browser_context_, NULL); |
+ create_params.routing_id = MSG_ROUTING_NONE; |
+ create_params.initial_size = gfx::Size(plane->size().width(), |
+ plane->size().height()); |
+ // TODO(lcwu): Before we pull in the Chromecast's application management |
+ // code, we (temporarily) create a webcontents object here. Once the |
+ // application management code is here, a new webcontents object (and |
+ // hence the renderer) will be created whenever a new application is |
+ // launched, regarless of whether the new app has the same domain as the |
+ // previous one. |
+ web_contents_.reset(content::WebContents::Create(create_params)); |
+ web_contents_->GetController().LoadURL(GetStartupURL(), |
+ content::Referrer(), |
+ content::PAGE_TRANSITION_TYPED, |
+ std::string()); |
+ |
+ stopped_ = false; |
+} |
+ |
+void CastService::Stop() { |
+ DCHECK(thread_checker_->CalledOnValidThread()); |
+ |
+ web_contents_->GetRenderViewHost()->ClosePage(); |
+ stopped_ = true; |
+} |
+ |
+} // namespace chromecast |