Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / ui / sad_tab_helper.cc
blobdad8204a5aa5bb0f337aee68a63bbe186cc4a554
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 "chrome/browser/ui/sad_tab_helper.h"
7 #include "base/logging.h"
8 #include "chrome/browser/browser_shutdown.h"
9 #include "chrome/browser/ui/sad_tab_types.h"
10 #include "content/public/browser/notification_source.h"
11 #include "content/public/browser/notification_types.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_view.h"
15 #if defined(OS_MACOSX)
16 #include "chrome/browser/ui/cocoa/tab_contents/sad_tab_controller.h"
17 #elif defined(TOOLKIT_VIEWS)
18 #include "chrome/browser/ui/views/sad_tab_view.h"
19 #include "ui/views/widget/widget.h"
20 #elif defined(TOOLKIT_GTK)
21 #include <gtk/gtk.h>
23 #include "chrome/browser/ui/gtk/sad_tab_gtk.h"
24 #include "chrome/browser/ui/gtk/tab_contents/chrome_web_contents_view_delegate_gtk.h"
25 #endif
27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SadTabHelper)
29 SadTabHelper::SadTabHelper(content::WebContents* web_contents)
30 : content::WebContentsObserver(web_contents) {
31 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
32 content::Source<content::WebContents>(web_contents));
35 SadTabHelper::~SadTabHelper() {
38 bool SadTabHelper::HasSadTab() const {
39 return sad_tab_.get() != NULL;
42 void SadTabHelper::RenderViewGone(base::TerminationStatus status) {
43 // Only show the sad tab if we're not in browser shutdown, so that WebContents
44 // objects that are not in a browser (e.g., HTML dialogs) and thus are
45 // visible do not flash a sad tab page.
46 if (browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID)
47 return;
49 if (HasSadTab())
50 return;
52 if (status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION ||
53 status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ||
54 status == base::TERMINATION_STATUS_PROCESS_CRASHED)
55 InstallSadTab(status);
58 void SadTabHelper::Observe(int type,
59 const content::NotificationSource& source,
60 const content::NotificationDetails& details) {
61 switch (type) {
62 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
63 if (HasSadTab()) {
64 #if defined(OS_MACOSX)
65 sad_tab_controller_mac::RemoveSadTab(sad_tab_.get());
66 #elif defined(TOOLKIT_VIEWS)
67 sad_tab_->Close();
68 // See http://crbug.com/117668. When the Widget is being destructed, we
69 // want calls to sad_tab() to return NULL.
70 scoped_ptr<views::Widget> local_sad_tab;
71 local_sad_tab.swap(sad_tab_);
72 #elif defined(TOOLKIT_GTK)
73 GtkWidget* expanded_container =
74 ChromeWebContentsViewDelegateGtk::GetFor(web_contents())->
75 expanded_container();
76 gtk_container_remove(
77 GTK_CONTAINER(expanded_container), sad_tab_->widget());
78 #else
79 #error Unknown platform
80 #endif
81 sad_tab_.reset();
83 break;
85 default:
86 NOTREACHED() << "Got a notification we didn't register for.";
90 void SadTabHelper::InstallSadTab(base::TerminationStatus status) {
91 #if defined(TOOLKIT_VIEWS) || defined(TOOLKIT_GTK)
92 chrome::SadTabKind kind =
93 (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED) ?
94 chrome::SAD_TAB_KIND_KILLED : chrome::SAD_TAB_KIND_CRASHED;
95 #endif
96 #if defined(OS_MACOSX)
97 sad_tab_.reset(
98 sad_tab_controller_mac::CreateSadTabController(web_contents()));
99 #elif defined(TOOLKIT_VIEWS)
100 views::Widget::InitParams sad_tab_params(
101 views::Widget::InitParams::TYPE_CONTROL);
102 // It is not possible to create a native_widget_win that has no parent in
103 // and later re-parent it.
104 // TODO(avi): This is a cheat. Can this be made cleaner?
105 sad_tab_params.parent = web_contents()->GetView()->GetNativeView();
106 #if defined(OS_WIN) && !defined(USE_AURA)
107 // Crash data indicates we can get here when the parent is no longer valid.
108 // Attempting to create a child window with a bogus parent crashes. So, we
109 // don't show a sad tab in this case in hopes the tab is in the process of
110 // shutting down.
111 if (!IsWindow(sad_tab_params.parent))
112 return;
113 #endif
114 sad_tab_params.ownership =
115 views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
116 sad_tab_.reset(new views::Widget);
117 sad_tab_->Init(sad_tab_params);
118 sad_tab_->SetContentsView(new SadTabView(web_contents(), kind));
120 views::Widget::ReparentNativeView(
121 sad_tab_->GetNativeView(), web_contents()->GetView()->GetNativeView());
122 gfx::Rect bounds;
123 web_contents()->GetView()->GetContainerBounds(&bounds);
124 sad_tab_->SetBounds(gfx::Rect(bounds.size()));
125 #elif defined(TOOLKIT_GTK)
126 sad_tab_.reset(new SadTabGtk(web_contents(), kind));
127 GtkWidget* expanded_container =
128 ChromeWebContentsViewDelegateGtk::GetFor(web_contents())->
129 expanded_container();
130 gtk_container_add(GTK_CONTAINER(expanded_container), sad_tab_->widget());
131 gtk_widget_show(sad_tab_->widget());
132 #else
133 #error Unknown platform
134 #endif