Skip to content

Commit

Permalink
Build-level separation of default CastService implementation.
Browse files Browse the repository at this point in the history
The default implementation (CastServiceSimple) starts up a single page
passed in on the command line.

R=damienv@chromium.org,lcwu@chromium.org,jam@chromium.org
BUG=336640

Review URL: https://codereview.chromium.org/397143003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283812 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
gunsch@chromium.org committed Jul 17, 2014
1 parent 4f1ded8 commit 791733d
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 133 deletions.
7 changes: 6 additions & 1 deletion chromecast/chromecast.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@
'internal/chromecast_internal.gyp:cast_service_internal',
],
}, {
'dependencies': [
'../base/base.gyp:base',
'../content/content.gyp:content',
],
'sources': [
'service/cast_platform_init_stub.cc',
'service/cast_service_simple.cc',
'service/cast_service_simple.h',
],
}],
],
Expand Down
13 changes: 0 additions & 13 deletions chromecast/service/cast_platform_init_stub.cc

This file was deleted.

109 changes: 3 additions & 106 deletions chromecast/service/cast_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,11 @@

#include "chromecast/service/cast_service.h"

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/logging.h"
#include "base/threading/thread_checker.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/filename_util.h"
#include "ui/aura/env.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/test/test_screen.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.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]));
}

class FillLayout : public aura::LayoutManager {
public:
explicit FillLayout(aura::Window* root) : root_(root) {}
virtual ~FillLayout() {}

private:
// aura::LayoutManager:
virtual void OnWindowResized() OVERRIDE {}

virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
child->SetBounds(root_->bounds());
}

virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}

virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}

virtual void OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) OVERRIDE {}

virtual void SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) OVERRIDE {
SetChildBoundsDirect(child, requested_bounds);
}

aura::Window* root_;

DISALLOW_COPY_AND_ASSIGN(FillLayout);
};

} // namespace

CastService::CastService(content::BrowserContext* browser_context)
: browser_context_(browser_context),
stopped_(true),
Expand All @@ -80,60 +20,17 @@ CastService::~CastService() {
DCHECK(stopped_);
}

void CastService::Initialize() {
PlatformInitialize();
}

void CastService::Start() {
DCHECK(thread_checker_->CalledOnValidThread());

Initialize();

// Aura initialization
gfx::Size initial_size = gfx::Size(1280, 720);
// TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal
// implementation of gfx::screen and aura's TestScreen will do for now.
// Change the code to use ozone's screen implementation when it is ready.
aura::TestScreen* screen = aura::TestScreen::Create(initial_size);
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
CHECK(aura::Env::GetInstance());
window_tree_host_.reset(
aura::WindowTreeHost::Create(gfx::Rect(initial_size)));
window_tree_host_->InitHost();
window_tree_host_->window()->SetLayoutManager(
new FillLayout(window_tree_host_->window()));
window_tree_host_->Show();

// Create a WebContents
content::WebContents::CreateParams create_params(browser_context_, NULL);
create_params.routing_id = MSG_ROUTING_NONE;
create_params.initial_size = initial_size;
web_contents_.reset(content::WebContents::Create(create_params));

// Add and show content's view/window
aura::Window* content_window = web_contents_->GetNativeView();
aura::Window* parent = window_tree_host_->window();
if (!parent->Contains(content_window)) {
parent->AddChild(content_window);
}
content_window->Show();

web_contents_->GetController().LoadURL(GetStartupURL(),
content::Referrer(),
content::PAGE_TRANSITION_TYPED,
std::string());

stopped_ = false;
StartInternal();
}

void CastService::Stop() {
DCHECK(thread_checker_->CalledOnValidThread());

web_contents_->GetRenderViewHost()->ClosePage();
window_tree_host_.reset();
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
aura::Env::DeleteInstance();
web_contents_.reset();
StopInternal();
stopped_ = true;
}

Expand Down
23 changes: 11 additions & 12 deletions chromecast/service/cast_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,38 @@
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"

namespace aura {
class WindowTreeHost;
}

namespace base {
class ThreadChecker;
}

namespace content{
class BrowserContext;
class WebContents;
}

namespace chromecast {

class CastService {
public:
explicit CastService(content::BrowserContext* browser_context);
static CastService* Create(content::BrowserContext* browser_context);

virtual ~CastService();

// Start/stop the cast service.
void Start();
void Stop();

private:
// Platform specific initialization if any.
static void PlatformInitialize();
protected:
explicit CastService(content::BrowserContext* browser_context);
virtual void Initialize() = 0;

void Initialize();
// Implementation-specific start/stop behavior.
virtual void StartInternal() = 0;
virtual void StopInternal() = 0;

content::BrowserContext* browser_context() const { return browser_context_; }

private:
content::BrowserContext* const browser_context_;
scoped_ptr<aura::WindowTreeHost> window_tree_host_;
scoped_ptr<content::WebContents> web_contents_;
bool stopped_;

const scoped_ptr<base::ThreadChecker> thread_checker_;
Expand Down
130 changes: 130 additions & 0 deletions chromecast/service/cast_service_simple.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2014 The Chromium Authors. 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_simple.h"

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/filename_util.h"
#include "ui/aura/env.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/test/test_screen.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.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]));
}

class FillLayout : public aura::LayoutManager {
public:
explicit FillLayout(aura::Window* root) : root_(root) {}
virtual ~FillLayout() {}

private:
// aura::LayoutManager:
virtual void OnWindowResized() OVERRIDE {}

virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
child->SetBounds(root_->bounds());
}

virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}

virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}

virtual void OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) OVERRIDE {}

virtual void SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) OVERRIDE {
SetChildBoundsDirect(child, requested_bounds);
}

aura::Window* root_;

DISALLOW_COPY_AND_ASSIGN(FillLayout);
};

} // namespace

// static
CastService* CastService::Create(content::BrowserContext* browser_context) {
return new CastServiceSimple(browser_context);
}

CastServiceSimple::CastServiceSimple(content::BrowserContext* browser_context)
: CastService(browser_context) {
}

CastServiceSimple::~CastServiceSimple() {
}

void CastServiceSimple::Initialize() {
}

void CastServiceSimple::StartInternal() {
// Aura initialization
gfx::Size initial_size = gfx::Size(1280, 720);
// TODO(lcwu): http://crbug.com/391074. Chromecast only needs a minimal
// implementation of gfx::screen and aura's TestScreen will do for now.
// Change the code to use ozone's screen implementation when it is ready.
aura::TestScreen* screen = aura::TestScreen::Create(initial_size);
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
CHECK(aura::Env::GetInstance());
window_tree_host_.reset(
aura::WindowTreeHost::Create(gfx::Rect(initial_size)));
window_tree_host_->InitHost();
window_tree_host_->window()->SetLayoutManager(
new FillLayout(window_tree_host_->window()));
window_tree_host_->Show();

// Create a WebContents
content::WebContents::CreateParams create_params(browser_context(), NULL);
create_params.routing_id = MSG_ROUTING_NONE;
create_params.initial_size = initial_size;
web_contents_.reset(content::WebContents::Create(create_params));

// Add and show content's view/window
aura::Window* content_window = web_contents_->GetNativeView();
aura::Window* parent = window_tree_host_->window();
if (!parent->Contains(content_window)) {
parent->AddChild(content_window);
}
content_window->Show();

web_contents_->GetController().LoadURL(GetStartupURL(),
content::Referrer(),
content::PAGE_TRANSITION_TYPED,
std::string());
}

void CastServiceSimple::StopInternal() {
web_contents_->GetRenderViewHost()->ClosePage();
window_tree_host_.reset();
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
aura::Env::DeleteInstance();
web_contents_.reset();
}

} // namespace chromecast
41 changes: 41 additions & 0 deletions chromecast/service/cast_service_simple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMECAST_SERVICE_CAST_SERVICE_SIMPLE_H_
#define CHROMECAST_SERVICE_CAST_SERVICE_SIMPLE_H_

#include "base/memory/scoped_ptr.h"
#include "chromecast/service/cast_service.h"

namespace aura {
class WindowTreeHost;
}

namespace content {
class WebContents;
}

namespace chromecast {

class CastServiceSimple : public CastService {
public:
explicit CastServiceSimple(content::BrowserContext* browser_context);
virtual ~CastServiceSimple();

protected:
// CastService implementation.
virtual void Initialize() OVERRIDE;
virtual void StartInternal() OVERRIDE;
virtual void StopInternal() OVERRIDE;

private:
scoped_ptr<aura::WindowTreeHost> window_tree_host_;
scoped_ptr<content::WebContents> web_contents_;

DISALLOW_COPY_AND_ASSIGN(CastServiceSimple);
};

} // namespace chromecast

#endif // CHROMECAST_SERVICE_CAST_SERVICE_SIMPLE_H_
2 changes: 1 addition & 1 deletion chromecast/shell/browser/cast_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void CastBrowserMainParts::PreMainMessageLoopRun() {

browser_context_.reset(new CastBrowserContext(url_request_context_factory_));

cast_service_.reset(new CastService(browser_context_.get()));
cast_service_.reset(CastService::Create(browser_context_.get()));
cast_service_->Start();
}

Expand Down

0 comments on commit 791733d

Please sign in to comment.