[email protected] | 9cddb1a2 | 2011-11-15 15:04:27 | [diff] [blame^] | 1 | // Copyright (c) 2011 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 "chrome/browser/ui/sad_tab_observer.h" |
| 6 | |
| 7 | #include "chrome/browser/browser_shutdown.h" |
| 8 | #include "content/browser/tab_contents/tab_contents.h" |
| 9 | #include "content/browser/tab_contents/tab_contents_view.h" |
| 10 | #include "content/public/browser/notification_source.h" |
| 11 | #include "content/public/browser/notification_types.h" |
| 12 | |
| 13 | #if defined(OS_MACOSX) |
| 14 | #include "chrome/browser/ui/cocoa/tab_contents/sad_tab_controller.h" |
| 15 | #elif defined(TOOLKIT_VIEWS) |
| 16 | #include "chrome/browser/ui/views/sad_tab_view.h" |
| 17 | #include "chrome/browser/ui/views/tab_contents/tab_contents_view_views.h" |
| 18 | #elif defined(TOOLKIT_GTK) |
| 19 | #include "chrome/browser/ui/gtk/sad_tab_gtk.h" |
| 20 | #endif |
| 21 | |
| 22 | SadTabObserver::SadTabObserver(TabContents* tab_contents) |
| 23 | : TabContentsObserver(tab_contents) { |
| 24 | registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_CONNECTED, |
| 25 | content::Source<TabContents>(tab_contents)); |
| 26 | } |
| 27 | |
| 28 | SadTabObserver::~SadTabObserver() { |
| 29 | } |
| 30 | |
| 31 | void SadTabObserver::RenderViewGone(base::TerminationStatus status) { |
| 32 | // Only show the sad tab if we're not in browser shutdown, so that TabContents |
| 33 | // objects that are not in a browser (e.g., HTML dialogs) and thus are |
| 34 | // visible do not flash a sad tab page. |
| 35 | if (browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) |
| 36 | return; |
| 37 | |
| 38 | if (HasSadTab()) |
| 39 | return; |
| 40 | |
| 41 | gfx::NativeView view = AcquireSadTab(status); |
| 42 | tab_contents()->view()->InstallOverlayView(view); |
| 43 | } |
| 44 | |
| 45 | void SadTabObserver::Observe(int type, |
| 46 | const content::NotificationSource& source, |
| 47 | const content::NotificationDetails& details) { |
| 48 | switch (type) { |
| 49 | case content::NOTIFICATION_TAB_CONTENTS_CONNECTED: |
| 50 | if (HasSadTab()) { |
| 51 | tab_contents()->view()->RemoveOverlayView(); |
| 52 | ReleaseSadTab(); |
| 53 | } |
| 54 | break; |
| 55 | |
| 56 | default: |
| 57 | NOTREACHED() << "Got a notification we didn't register for."; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | gfx::NativeView SadTabObserver::AcquireSadTab(base::TerminationStatus status) { |
| 62 | #if defined(OS_MACOSX) |
| 63 | sad_tab_.reset( |
| 64 | sad_tab_controller_mac::CreateSadTabController(tab_contents())); |
| 65 | return sad_tab_controller_mac::GetViewOfSadTabController(sad_tab_.get()); |
| 66 | #elif defined(TOOLKIT_VIEWS) |
| 67 | SadTabView::Kind kind = |
| 68 | status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ? |
| 69 | SadTabView::KILLED : SadTabView::CRASHED; |
| 70 | views::Widget::InitParams sad_tab_params( |
| 71 | views::Widget::InitParams::TYPE_CONTROL); |
| 72 | // It is not possible to create a widget that has no parent, and later |
| 73 | // re-parent it. TODO(avi): This is a cheat. Can this be made cleaner? |
| 74 | sad_tab_params.parent_widget = |
| 75 | static_cast<TabContentsViewViews*>(tab_contents()->view()); |
| 76 | sad_tab_params.ownership = |
| 77 | views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 78 | sad_tab_.reset(new views::Widget); |
| 79 | sad_tab_->Init(sad_tab_params); |
| 80 | sad_tab_->SetContentsView(new SadTabView(tab_contents(), kind)); |
| 81 | return sad_tab_->GetNativeView(); |
| 82 | #elif defined(TOOLKIT_GTK) |
| 83 | sad_tab_.reset(new SadTabGtk( |
| 84 | tab_contents(), |
| 85 | status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED |
| 86 | ? SadTabGtk::KILLED |
| 87 | : SadTabGtk::CRASHED)); |
| 88 | return sad_tab_->widget(); |
| 89 | #else |
| 90 | #error Unknown platform |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | void SadTabObserver::ReleaseSadTab() { |
| 95 | sad_tab_.reset(); |
| 96 | } |
| 97 | |
| 98 | bool SadTabObserver::HasSadTab() { |
| 99 | return sad_tab_.get() != NULL; |
| 100 | } |