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/views/sad_tab_view.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/feedback/feedback_util.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/chrome_pages.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_view.h"
20 #include "grit/generated_resources.h"
21 #include "grit/theme_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/views/background.h"
25 #include "ui/views/controls/button/label_button.h"
26 #include "ui/views/controls/image_view.h"
27 #include "ui/views/controls/label.h"
28 #include "ui/views/controls/link.h"
29 #include "ui/views/layout/grid_layout.h"
30 #include "ui/views/widget/widget.h"
32 using content::OpenURLParams
;
33 using content::WebContents
;
37 const int kPadding
= 20;
38 const float kMessageSize
= 0.65f
;
39 const SkColor kTextColor
= SK_ColorWHITE
;
40 const SkColor kCrashColor
= SkColorSetRGB(35, 48, 64);
41 const SkColor kKillColor
= SkColorSetRGB(57, 48, 88);
43 const char kCategoryTagCrash
[] = "Crash";
47 SadTabView::SadTabView(WebContents
* web_contents
, chrome::SadTabKind kind
)
48 : web_contents_(web_contents
),
54 reload_button_(NULL
) {
57 // Sometimes the user will never see this tab, so keep track of the total
58 // number of creation events to compare to display events.
59 // TODO(jamescook): Remove this after R20 stable. Keep it for now so we can
60 // compare R20 to earlier versions.
61 UMA_HISTOGRAM_COUNTS("SadTab.Created", kind_
);
63 // These stats should use the same counting approach and bucket size used for
64 // tab discard events in chromeos::OomPriorityManager so they can be
66 // TODO(jamescook): Maybe track time between sad tabs?
68 case chrome::SAD_TAB_KIND_CRASHED
: {
69 static int crashed
= 0;
71 UMA_HISTOGRAM_CUSTOM_COUNTS(
72 "Tabs.SadTab.CrashCreated", crashed
, 1, 1000, 50);
75 case chrome::SAD_TAB_KIND_KILLED
: {
76 static int killed
= 0;
78 UMA_HISTOGRAM_CUSTOM_COUNTS(
79 "Tabs.SadTab.KillCreated", killed
, 1, 1000, 50);
86 // Set the background color.
87 set_background(views::Background::CreateSolidBackground(
88 (kind_
== chrome::SAD_TAB_KIND_CRASHED
) ? kCrashColor
: kKillColor
));
90 views::GridLayout
* layout
= new views::GridLayout(this);
91 SetLayoutManager(layout
);
93 const int column_set_id
= 0;
94 views::ColumnSet
* columns
= layout
->AddColumnSet(column_set_id
);
95 columns
->AddPaddingColumn(1, kPadding
);
96 columns
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::CENTER
,
97 0, views::GridLayout::USE_PREF
, 0, 0);
98 columns
->AddPaddingColumn(1, kPadding
);
100 views::ImageView
* image
= new views::ImageView();
101 image
->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
102 (kind_
== chrome::SAD_TAB_KIND_CRASHED
) ? IDR_SAD_TAB
: IDR_KILLED_TAB
));
103 layout
->StartRowWithPadding(0, column_set_id
, 1, kPadding
);
104 layout
->AddView(image
);
106 views::Label
* title
= CreateLabel(l10n_util::GetStringUTF16(
107 (kind_
== chrome::SAD_TAB_KIND_CRASHED
) ?
108 IDS_SAD_TAB_TITLE
: IDS_KILLED_TAB_TITLE
));
109 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
110 title
->SetFontList(rb
.GetFontList(ui::ResourceBundle::MediumFont
));
111 layout
->StartRowWithPadding(0, column_set_id
, 0, kPadding
);
112 layout
->AddView(title
);
114 message_
= CreateLabel(l10n_util::GetStringUTF16(
115 (kind_
== chrome::SAD_TAB_KIND_CRASHED
) ?
116 IDS_SAD_TAB_MESSAGE
: IDS_KILLED_TAB_MESSAGE
));
117 message_
->SetMultiLine(true);
118 layout
->StartRowWithPadding(0, column_set_id
, 0, kPadding
);
119 layout
->AddView(message_
);
122 layout
->StartRowWithPadding(0, column_set_id
, 0, kPadding
);
123 reload_button_
= new views::LabelButton(
125 l10n_util::GetStringUTF16(IDS_SAD_TAB_RELOAD_LABEL
));
126 reload_button_
->SetStyle(views::Button::STYLE_BUTTON
);
127 layout
->AddView(reload_button_
);
129 help_link_
= CreateLink(l10n_util::GetStringUTF16(
130 (kind_
== chrome::SAD_TAB_KIND_CRASHED
) ?
131 IDS_SAD_TAB_HELP_LINK
: IDS_LEARN_MORE
));
133 if (kind_
== chrome::SAD_TAB_KIND_CRASHED
) {
135 base::string16
help_text(
136 l10n_util::GetStringFUTF16(IDS_SAD_TAB_HELP_MESSAGE
,
137 base::string16(), &offset
));
138 views::Label
* help_prefix
= CreateLabel(help_text
.substr(0, offset
));
139 views::Label
* help_suffix
= CreateLabel(help_text
.substr(offset
));
141 const int help_column_set_id
= 1;
142 views::ColumnSet
* help_columns
= layout
->AddColumnSet(help_column_set_id
);
143 help_columns
->AddPaddingColumn(1, kPadding
);
144 // Center three middle columns for the help's [prefix][link][suffix].
145 for (size_t column
= 0; column
< 3; column
++)
146 help_columns
->AddColumn(views::GridLayout::CENTER
,
147 views::GridLayout::CENTER
, 0, views::GridLayout::USE_PREF
, 0, 0);
148 help_columns
->AddPaddingColumn(1, kPadding
);
150 layout
->StartRowWithPadding(0, help_column_set_id
, 0, kPadding
);
151 layout
->AddView(help_prefix
);
152 layout
->AddView(help_link_
);
153 layout
->AddView(help_suffix
);
155 layout
->StartRowWithPadding(0, column_set_id
, 0, kPadding
);
156 layout
->AddView(help_link_
);
158 feedback_link_
= CreateLink(
159 l10n_util::GetStringUTF16(IDS_KILLED_TAB_FEEDBACK_LINK
));
160 layout
->StartRowWithPadding(0, column_set_id
, 0, kPadding
);
161 layout
->AddView(feedback_link_
);
164 layout
->AddPaddingRow(1, kPadding
);
167 SadTabView::~SadTabView() {}
169 void SadTabView::LinkClicked(views::Link
* source
, int event_flags
) {
170 DCHECK(web_contents_
);
171 if (source
== help_link_
) {
172 GURL
help_url((kind_
== chrome::SAD_TAB_KIND_CRASHED
) ?
173 chrome::kCrashReasonURL
: chrome::kKillReasonURL
);
174 OpenURLParams
params(
175 help_url
, content::Referrer(), CURRENT_TAB
,
176 content::PAGE_TRANSITION_LINK
, false);
177 web_contents_
->OpenURL(params
);
178 } else if (source
== feedback_link_
) {
179 chrome::ShowFeedbackPage(
180 chrome::FindBrowserWithWebContents(web_contents_
),
181 l10n_util::GetStringUTF8(IDS_KILLED_TAB_FEEDBACK_MESSAGE
),
182 std::string(kCategoryTagCrash
));
186 void SadTabView::ButtonPressed(views::Button
* sender
,
187 const ui::Event
& event
) {
188 DCHECK(web_contents_
);
189 DCHECK_EQ(reload_button_
, sender
);
190 web_contents_
->GetController().Reload(true);
193 void SadTabView::Layout() {
194 // Specify the maximum message width explicitly.
195 message_
->SizeToFit(static_cast<int>(width() * kMessageSize
));
199 void SadTabView::OnPaint(gfx::Canvas
* canvas
) {
201 // User actually saw the error, keep track for user experience stats.
202 // TODO(jamescook): Remove this after R20 stable. Keep it for now so we can
203 // compare R20 to earlier versions.
204 UMA_HISTOGRAM_COUNTS("SadTab.Displayed", kind_
);
206 // These stats should use the same counting approach and bucket size used
207 // for tab discard events in chromeos::OomPriorityManager so they
208 // can be directly compared.
210 case chrome::SAD_TAB_KIND_CRASHED
: {
211 static int crashed
= 0;
212 UMA_HISTOGRAM_CUSTOM_COUNTS(
213 "Tabs.SadTab.CrashDisplayed", ++crashed
, 1, 1000, 50);
216 case chrome::SAD_TAB_KIND_KILLED
: {
217 static int killed
= 0;
218 UMA_HISTOGRAM_CUSTOM_COUNTS(
219 "Tabs.SadTab.KillDisplayed", ++killed
, 1, 1000, 50);
227 View::OnPaint(canvas
);
230 void SadTabView::Show() {
231 views::Widget::InitParams
sad_tab_params(
232 views::Widget::InitParams::TYPE_CONTROL
);
234 // It is not possible to create a native_widget_win that has no parent in
235 // and later re-parent it.
236 // TODO(avi): This is a cheat. Can this be made cleaner?
237 sad_tab_params
.parent
= web_contents_
->GetView()->GetNativeView();
239 set_owned_by_client();
241 views::Widget
* sad_tab
= new views::Widget
;
242 sad_tab
->Init(sad_tab_params
);
243 sad_tab
->SetContentsView(this);
245 views::Widget::ReparentNativeView(sad_tab
->GetNativeView(),
246 web_contents_
->GetView()->GetNativeView());
248 web_contents_
->GetView()->GetContainerBounds(&bounds
);
249 sad_tab
->SetBounds(gfx::Rect(bounds
.size()));
252 void SadTabView::Close() {
254 GetWidget()->Close();
257 views::Label
* SadTabView::CreateLabel(const base::string16
& text
) {
258 views::Label
* label
= new views::Label(text
);
259 label
->SetBackgroundColor(background()->get_color());
260 label
->SetEnabledColor(kTextColor
);
264 views::Link
* SadTabView::CreateLink(const base::string16
& text
) {
265 views::Link
* link
= new views::Link(text
);
266 link
->SetBackgroundColor(background()->get_color());
267 link
->SetEnabledColor(kTextColor
);
268 link
->set_listener(this);
274 SadTab
* SadTab::Create(content::WebContents
* web_contents
,
276 return new SadTabView(web_contents
, kind
);
279 } // namespace chrome