Skip to content

Commit

Permalink
Re-implement the screensaver to use WebView instead of ExtensionDialo…
Browse files Browse the repository at this point in the history
…gHost.

Use WebView to render the screensaver extension instead of ExtensionDialogHost. Using the RenderViewGone override to detect termination of the renderer process to restart it.
Added browser tests.

R=ben@chromium.org,sky@chromium.org
BUG=chromium-os:28211
TEST=Tested that the screensaver comes up; tested the reload via crashing the extension renderer with SIG_ABRT; also ran browser tests.

Review URL: https://chromiumcodereview.appspot.com/10191010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@135369 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
rkc@chromium.org committed May 4, 2012
1 parent fcec95a commit d54f8a4
Show file tree
Hide file tree
Showing 40 changed files with 749 additions and 272 deletions.
10 changes: 10 additions & 0 deletions ash/ash.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
'../base/base.gyp:base_i18n',
'../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations',
'../build/temp_gyp/googleurl.gyp:googleurl',
'../content/content.gyp:content',
'../content/content.gyp:content_browser',
'../ipc/ipc.gyp:ipc',
'../net/net.gyp:net',
'../skia/skia.gyp:skia',
'../third_party/icu/icu.gyp:icui18n',
Expand Down Expand Up @@ -111,6 +114,8 @@
'monitor/secondary_monitor_view.h',
'screen_ash.cc',
'screen_ash.h',
'screensaver/screensaver_view.cc',
'screensaver/screensaver_view.h',
'screenshot_delegate.h',
'shell.cc',
'shell.h',
Expand Down Expand Up @@ -323,6 +328,8 @@
'../base/base.gyp:base',
'../base/base.gyp:test_support_base',
'../chrome/chrome_resources.gyp:packed_resources',
'../content/content.gyp:content_browser',
'../content/content.gyp:test_support_content',
'../build/temp_gyp/googleurl.gyp:googleurl',
'../skia/skia.gyp:skia',
'../testing/gtest.gyp:gtest',
Expand All @@ -337,6 +344,7 @@
'../ui/ui.gyp:ui_resources',
'../ui/ui.gyp:ui_resources_standard',
'../ui/views/views.gyp:views',
'../ui/views/views.gyp:test_support_views',
'ash',
],
'sources': [
Expand All @@ -356,6 +364,7 @@
'launcher/launcher_unittest.cc',
'launcher/launcher_view_unittest.cc',
'monitor/multi_monitor_manager_unittest.cc',
'screensaver/screensaver_view_unittest.cc',
'shell_unittest.cc',
'test/ash_test_base.cc',
'test/ash_test_base.h',
Expand Down Expand Up @@ -457,6 +466,7 @@
'../ui/ui.gyp:ui_resources_standard',
'../ui/views/views.gyp:views',
'../ui/views/views.gyp:views_examples_lib',
'../ui/views/views.gyp:test_support_views',
'ash',
],
'sources': [
Expand Down
3 changes: 3 additions & 0 deletions ash/screensaver/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_rules = [
"+content/public/browser",
]
140 changes: 140 additions & 0 deletions ash/screensaver/screensaver_view.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) 2012 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 "ash/screensaver/screensaver_view.h"

#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "base/bind.h"
#include "base/logging.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "ui/gfx/screen.h"
#include "ui/aura/root_window.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/widget/widget.h"

using content::BrowserThread;

namespace {

ash::internal::ScreensaverView* g_instance = NULL;

} // namespace

namespace ash {

void ShowScreensaver(const GURL& url) {
internal::ScreensaverView::ShowScreensaver(url);
}

void CloseScreensaver() {
internal::ScreensaverView::CloseScreensaver();
}

namespace internal {

// static
void ScreensaverView::ShowScreensaver(const GURL& url) {
if (!g_instance) {
g_instance = new ScreensaverView(url);
g_instance->Show();
}
}

// static
void ScreensaverView::CloseScreensaver() {
if (g_instance) {
g_instance->Close();
g_instance = NULL;
}
}

////////////////////////////////////////////////////////////////////////////////
// ScreensaverView, views::WidgetDelegateView implementation.
views::View* ScreensaverView::GetContentsView() {
return this;
}

////////////////////////////////////////////////////////////////////////////////
// ScreensaverView, content::WebContentsObserver implementation.
void ScreensaverView::RenderViewGone(
base::TerminationStatus status) {
LOG(ERROR) << "Screensaver terminated with status " << status
<< ", reloading.";
// Reload the screensaver url into the webcontents.
LoadScreensaver();
}

////////////////////////////////////////////////////////////////////////////////
// ScreensaverView private methods.
ScreensaverView::ScreensaverView(const GURL& url)
: url_(url),
screensaver_webview_(NULL),
container_window_(NULL) {
}

ScreensaverView::~ScreensaverView() {
}

void ScreensaverView::Show() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Add the WebView to our view.
AddChildWebContents();
// Show the window.
ShowWindow();
}

void ScreensaverView::Close() {
DCHECK(GetWidget());
GetWidget()->Close();
}

void ScreensaverView::AddChildWebContents() {
content::BrowserContext* context =
Shell::GetInstance()->delegate()->GetCurrentBrowserContext();
screensaver_webview_ = new views::WebView(context);
SetLayoutManager(new views::FillLayout);
AddChildView(screensaver_webview_);

LoadScreensaver();
content::WebContentsObserver::Observe(
screensaver_webview_->GetWebContents());
}

void ScreensaverView::LoadScreensaver() {
screensaver_webview_->GetWebContents()->GetController().LoadURL(
url_,
content::Referrer(),
content::PAGE_TRANSITION_START_PAGE,
std::string());
}

void ScreensaverView::ShowWindow() {
aura::RootWindow* root_window = ash::Shell::GetRootWindow();
gfx::Rect screen_rect =
gfx::Screen::GetMonitorNearestWindow(root_window).bounds();

// We want to be the fullscreen topmost child of the root window.
// There should be nothing ever really that should show up on top of us.
container_window_ = new views::Widget();
views::Widget::InitParams params(
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.delegate = this;
params.parent = root_window;
container_window_->Init(params);

container_window_->StackAtTop();
container_window_->SetBounds(screen_rect);
container_window_->Show();
}

// static
ScreensaverView* ScreensaverView::GetInstance() {
return g_instance;
}

} // namespace internal
} // namespace ash
87 changes: 87 additions & 0 deletions ash/screensaver/screensaver_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2012 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 ASH_SCREENSAVER_SCREENSAVER_VIEW_H_
#define ASH_SCREENSAVER_SCREENSAVER_VIEW_H_
#pragma once

#include "ash/ash_export.h"
#include "base/callback.h"
#include "content/public/browser/web_contents_observer.h"
#include "googleurl/src/gurl.h"
#include "ui/views/widget/widget_delegate.h"

namespace content {
class BrowserContent;
}

namespace views {
class WebView;
}

namespace ash {

namespace test {
class ScreensaverViewTest;
}

ASH_EXPORT void ShowScreensaver(const GURL& url);
ASH_EXPORT void CloseScreensaver();

typedef
base::Callback<views::WebView*(content::BrowserContext*)> WebViewFactory;

namespace internal {

// Shows a URL as a screensaver. The screensaver window is fullscreen,
// always on top of every other window and will reload the URL if the
// renderer crashes for any reason.
class ScreensaverView : public views::WidgetDelegateView,
public content::WebContentsObserver {
public:
static void ShowScreensaver(const GURL& url);
static void CloseScreensaver();

private:
friend class test::ScreensaverViewTest;

explicit ScreensaverView(const GURL& url);
virtual ~ScreensaverView();

// views::WidgetDelegate overrides.
virtual views::View* GetContentsView() OVERRIDE;

// content::WebContentsObserver overrides.
virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;

void Show();
void Close();

// Creates and adds web contents to our view.
void AddChildWebContents();
// Load the screensaver in the WebView's webcontent. If the webcontents
// don't exist, they'll be created by WebView.
void LoadScreensaver();
// Creates and shows a frameless full screen window containing our view.
void ShowWindow();

// For testing purposes.
static ASH_EXPORT ScreensaverView* GetInstance();

// URL to show in the screensaver.
GURL url_;

// Host for the extension that implements this dialog.
views::WebView* screensaver_webview_;

// Window that holds the screensaver webview.
views::Widget* container_window_;

DISALLOW_COPY_AND_ASSIGN(ScreensaverView);
};

} // namespace internal
} // namespace ash

#endif // ASH_SCREENSAVER_SCREENSAVER_VIEW_H_
93 changes: 93 additions & 0 deletions ash/screensaver/screensaver_view_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2012 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 "ash/screensaver/screensaver_view.h"

#include "ash/test/ash_test_base.h"
#include "base/bind.h"
#include "content/public/browser/browser_context.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/test/webview_test_helper.h"

namespace ash {
namespace test {

class ScreensaverViewTest : public ash::test::AshTestBase {
public:
ScreensaverViewTest() {
url_ = GURL("http://www.google.com");
views_delegate_.reset(new views::TestViewsDelegate);
webview_test_helper_.reset(new views::WebViewTestHelper(message_loop()));
}

virtual ~ScreensaverViewTest() {}

virtual void SetUp() OVERRIDE {
AshTestBase::SetUp();
RunAllPendingInMessageLoop();
}

virtual void TearDown() OVERRIDE {
AshTestBase::TearDown();
}

void ExpectOpenScreensaver() {
internal::ScreensaverView* screensaver =
internal::ScreensaverView::GetInstance();
EXPECT_TRUE(screensaver != NULL);
if (!screensaver) return;

EXPECT_TRUE(screensaver->screensaver_webview_ != NULL);
if (!screensaver->screensaver_webview_) return;

EXPECT_TRUE(screensaver->screensaver_webview_->web_contents() != NULL);
if (!screensaver->screensaver_webview_->web_contents()) return;

EXPECT_EQ(screensaver->screensaver_webview_->web_contents()->GetURL(),
url_);
}

void ExpectClosedScreensaver() {
EXPECT_TRUE(internal::ScreensaverView::GetInstance() == NULL);
}

protected:
GURL url_;

private:
scoped_ptr<views::TestViewsDelegate> views_delegate_;
scoped_ptr<views::WebViewTestHelper> webview_test_helper_;

DISALLOW_COPY_AND_ASSIGN(ScreensaverViewTest);
};

TEST_F(ScreensaverViewTest, ShowScreensaverAndClose) {
ash::ShowScreensaver(url_);
RunAllPendingInMessageLoop();
ExpectOpenScreensaver();

ash::CloseScreensaver();
ExpectClosedScreensaver();
}

TEST_F(ScreensaverViewTest, OutOfOrderMultipleShowAndClose) {
ash::CloseScreensaver();
ExpectClosedScreensaver();

ash::ShowScreensaver(url_);
ExpectOpenScreensaver();
RunAllPendingInMessageLoop();
ash::ShowScreensaver(url_);
ExpectOpenScreensaver();
RunAllPendingInMessageLoop();

ash::CloseScreensaver();
ExpectClosedScreensaver();
ash::CloseScreensaver();
ExpectClosedScreensaver();
}

} // namespace test
} // namespace ash
4 changes: 4 additions & 0 deletions ash/shell/shell_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ void ShellDelegateImpl::OpenCrosh() {
void ShellDelegateImpl::OpenMobileSetup() {
}

content::BrowserContext* ShellDelegateImpl::GetCurrentBrowserContext() {
return Shell::GetInstance()->browser_context();
}

void ShellDelegateImpl::ToggleSpokenFeedback() {
}

Expand Down
1 change: 1 addition & 0 deletions ash/shell/shell_delegate_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ShellDelegateImpl : public ash::ShellDelegate {
virtual void OpenFileManager() OVERRIDE;
virtual void OpenCrosh() OVERRIDE;
virtual void OpenMobileSetup() OVERRIDE;
virtual content::BrowserContext* GetCurrentBrowserContext() OVERRIDE;
virtual void ToggleSpokenFeedback() OVERRIDE;
virtual ash::AppListViewDelegate* CreateAppListViewDelegate() OVERRIDE;
virtual void StartPartialScreenshot(
Expand Down
Loading

0 comments on commit d54f8a4

Please sign in to comment.