Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "ash/screensaver/screensaver_view.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/shell_delegate.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "content/public/browser/browser_context.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "ui/gfx/screen.h" | |
| 14 #include "ui/aura/root_window.h" | |
| 15 #include "ui/views/layout/fill_layout.h" | |
| 16 #include "ui/views/controls/webview/webview.h" | |
| 17 #include "ui/views/widget/widget.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 ash::internal::ScreensaverView* g_instance = NULL; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace ash { | |
| 28 | |
| 29 void ShowScreensaver(const GURL& url) { | |
| 30 internal::ScreensaverView::ShowScreensaver(url); | |
| 31 } | |
| 32 | |
| 33 void CloseScreensaver() { | |
| 34 internal::ScreensaverView::CloseScreensaver(); | |
| 35 } | |
| 36 | |
| 37 namespace internal { | |
| 38 | |
| 39 WebViewFactory ScreensaverView::webview_factory_ = | |
| 40 base::Bind(ScreensaverView::CreateWebView); | |
| 41 | |
| 42 // static | |
| 43 void ScreensaverView::ShowScreensaver(const GURL& url) { | |
| 44 if (!g_instance) { | |
| 45 g_instance = new ScreensaverView(url); | |
| 46 g_instance->Show(); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 void ScreensaverView::CloseScreensaver() { | |
| 52 if (g_instance) { | |
| 53 g_instance->Close(); | |
| 54 g_instance = NULL; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 //////////////////////////////////////////////////////////////////////////////// | |
| 59 // ScreensaverView, views::WidgetDelegateView implementation. | |
| 60 views::View* ScreensaverView::GetContentsView() { | |
| 61 return this; | |
| 62 } | |
| 63 | |
| 64 //////////////////////////////////////////////////////////////////////////////// | |
| 65 // ScreensaverView, content::WebContentsObserver implementation. | |
| 66 void ScreensaverView::RenderViewGone( | |
| 67 base::TerminationStatus status) { | |
| 68 LOG(ERROR) << "Screensaver terminated with status " << status | |
| 69 << ", reloading."; | |
| 70 // Reload the screensaver url into the webcontents. | |
| 71 LoadScreensaver(); | |
| 72 } | |
| 73 | |
| 74 //////////////////////////////////////////////////////////////////////////////// | |
| 75 // ScreensaverView private methods. | |
| 76 ScreensaverView::ScreensaverView(const GURL& url) | |
| 77 : url_(url), | |
| 78 screensaver_webview_(NULL), | |
| 79 container_window_(NULL) { | |
| 80 } | |
| 81 | |
| 82 ScreensaverView::~ScreensaverView() { | |
| 83 } | |
| 84 | |
| 85 void ScreensaverView::Show() { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 87 // Add the WebView to our view. | |
| 88 AddChildWebContents(); | |
| 89 // Show the window. | |
| 90 ShowWindow(); | |
| 91 } | |
| 92 | |
| 93 void ScreensaverView::Close() { | |
| 94 DCHECK(GetWidget()); | |
| 95 GetWidget()->Close(); | |
| 96 } | |
| 97 | |
| 98 void ScreensaverView::AddChildWebContents() { | |
| 99 content::BrowserContext* context = | |
| 100 Shell::GetInstance()->delegate()->GetCurrentBrowserContext(); | |
| 101 screensaver_webview_ = webview_factory_.Run(context); | |
|
Ben Goodger (Google)
2012/05/01 16:57:42
See my note about how to do this without a factory
rkc
2012/05/02 00:51:03
Done.
| |
| 102 SetLayoutManager(new views::FillLayout); | |
| 103 AddChildView(screensaver_webview_); | |
| 104 | |
| 105 LoadScreensaver(); | |
| 106 content::WebContentsObserver::Observe( | |
| 107 screensaver_webview_->GetWebContents()); | |
| 108 } | |
| 109 | |
| 110 void ScreensaverView::LoadScreensaver() { | |
| 111 screensaver_webview_->GetWebContents()->GetController().LoadURL( | |
| 112 url_, | |
| 113 content::Referrer(), | |
| 114 content::PAGE_TRANSITION_START_PAGE, | |
| 115 std::string()); | |
| 116 } | |
| 117 | |
| 118 void ScreensaverView::ShowWindow() { | |
| 119 aura::RootWindow* root_window = ash::Shell::GetRootWindow(); | |
| 120 gfx::Rect screen_rect = | |
| 121 gfx::Screen::GetMonitorNearestWindow(root_window).bounds(); | |
| 122 | |
| 123 // We want to be the fullscreen topmost child of the root window. | |
| 124 // There should be nothing ever really that should show up on top of us. | |
| 125 container_window_ = new views::Widget(); | |
| 126 views::Widget::InitParams params( | |
| 127 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
| 128 params.delegate = this; | |
| 129 params.parent = root_window; | |
| 130 container_window_->Init(params); | |
| 131 | |
| 132 container_window_->StackAtTop(); | |
| 133 container_window_->SetBounds(screen_rect); | |
| 134 container_window_->Show(); | |
| 135 } | |
| 136 | |
| 137 // static | |
| 138 ScreensaverView* ScreensaverView::GetInstance() { | |
| 139 return g_instance; | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 views::WebView* ScreensaverView::CreateWebView( | |
| 144 content::BrowserContext* context) { | |
| 145 return new views::WebView(context); | |
| 146 } | |
| 147 | |
| 148 } // namespace internal | |
| 149 } // namespace ash | |
| OLD | NEW |