1 // Copyright (c) 2012 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.
5 #include "ash/screensaver/screensaver_view.h"
8 #include "ash/shell_delegate.h"
10 #include "base/logging.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/web_contents.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/gfx/screen.h"
16 #include "ui/views/controls/webview/webview.h"
17 #include "ui/views/layout/fill_layout.h"
18 #include "ui/views/widget/widget.h"
20 using content::BrowserThread
;
24 ash::ScreensaverView
* g_instance
= NULL
;
26 // Do not restart the screensaver again if it has
27 // terminated kMaxTerminations times already.
28 const int kMaxTerminations
= 3;
34 void ShowScreensaver(const GURL
& url
) { ScreensaverView::ShowScreensaver(url
); }
36 void CloseScreensaver() { ScreensaverView::CloseScreensaver(); }
38 bool IsScreensaverShown() { return ScreensaverView::IsScreensaverShown(); }
41 void ScreensaverView::ShowScreensaver(const GURL
& url
) {
43 g_instance
= new ScreensaverView(url
);
49 void ScreensaverView::CloseScreensaver() {
57 bool ScreensaverView::IsScreensaverShown() {
58 return g_instance
&& g_instance
->IsScreensaverShowingURL(g_instance
->url_
);
61 bool ScreensaverView::IsScreensaverShowingURL(const GURL
& url
) {
62 return screensaver_webview_
&&
63 screensaver_webview_
->web_contents() &&
64 (screensaver_webview_
->web_contents()->GetURL() == url
);
67 ////////////////////////////////////////////////////////////////////////////////
68 // ScreensaverView, views::WidgetDelegateView implementation.
69 views::View
* ScreensaverView::GetContentsView() {
73 ////////////////////////////////////////////////////////////////////////////////
74 // ScreensaverView, content::WebContentsObserver implementation.
75 void ScreensaverView::RenderProcessGone(
76 base::TerminationStatus status
) {
77 LOG(ERROR
) << "Screensaver terminated with status " << status
;
80 if (termination_count_
< kMaxTerminations
) {
81 LOG(ERROR
) << termination_count_
82 << " terminations is under the threshold of "
84 << "; reloading Screensaver.";
87 LOG(ERROR
) << "Exceeded termination threshold, closing Screensaver.";
88 ScreensaverView::CloseScreensaver();
92 ////////////////////////////////////////////////////////////////////////////////
93 // ScreensaverView private methods.
94 ScreensaverView::ScreensaverView(const GURL
& url
)
96 termination_count_(0),
97 screensaver_webview_(NULL
),
98 container_window_(NULL
) {
101 ScreensaverView::~ScreensaverView() {
104 void ScreensaverView::Show() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
106 // Add the WebView to our view.
107 AddChildWebContents();
112 void ScreensaverView::Close() {
114 GetWidget()->Close();
117 void ScreensaverView::AddChildWebContents() {
118 content::BrowserContext
* context
=
119 Shell::GetInstance()->delegate()->GetActiveBrowserContext();
120 screensaver_webview_
= new views::WebView(context
);
121 SetLayoutManager(new views::FillLayout
);
122 AddChildView(screensaver_webview_
);
125 content::WebContentsObserver::Observe(
126 screensaver_webview_
->GetWebContents());
129 void ScreensaverView::LoadScreensaver() {
130 screensaver_webview_
->GetWebContents()->GetController().LoadURL(
133 content::PAGE_TRANSITION_AUTO_TOPLEVEL
,
137 void ScreensaverView::ShowWindow() {
138 aura::Window
* root_window
= ash::Shell::GetPrimaryRootWindow();
139 gfx::Rect screen_rect
=
140 Shell::GetScreen()->GetDisplayNearestWindow(root_window
).bounds();
142 // We want to be the fullscreen topmost child of the root window.
143 // There should be nothing ever really that should show up on top of us.
144 container_window_
= new views::Widget();
145 views::Widget::InitParams
params(
146 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
147 params
.delegate
= this;
148 params
.parent
= root_window
;
149 container_window_
->Init(params
);
151 container_window_
->StackAtTop();
152 container_window_
->SetBounds(screen_rect
);
153 container_window_
->Show();
157 ScreensaverView
* ScreensaverView::GetInstance() {