OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/sad_tab_view.h" | 5 #include "components/sad_tab/views/sad_tab_view.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/ui/browser.h" | 12 #include "components/sad_tab/sad_tab_client.h" |
13 #include "chrome/browser/ui/browser_finder.h" | 13 #include "components/sad_tab/url_constants.h" |
14 #include "chrome/browser/ui/chrome_pages.h" | |
15 #include "chrome/common/url_constants.h" | |
16 #include "chrome/grit/generated_resources.h" | |
17 #include "components/feedback/feedback_util.h" | |
18 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
19 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
20 #include "grit/components_strings.h" | 16 #include "grit/components_strings.h" |
21 #include "grit/theme_resources.h" | |
22 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
24 #include "ui/views/background.h" | 19 #include "ui/views/background.h" |
25 #include "ui/views/controls/button/label_button.h" | 20 #include "ui/views/controls/button/label_button.h" |
26 #include "ui/views/controls/button/label_button_border.h" | 21 #include "ui/views/controls/button/label_button_border.h" |
27 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
28 #include "ui/views/controls/label.h" | 23 #include "ui/views/controls/label.h" |
29 #include "ui/views/controls/link.h" | 24 #include "ui/views/controls/link.h" |
30 #include "ui/views/layout/grid_layout.h" | 25 #include "ui/views/layout/grid_layout.h" |
31 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
32 | 27 |
33 using content::OpenURLParams; | 28 using content::OpenURLParams; |
34 using content::WebContents; | 29 using content::WebContents; |
35 | 30 |
| 31 namespace sad_tab { |
| 32 |
36 namespace { | 33 namespace { |
37 | 34 |
38 const int kPadding = 20; | 35 const int kPadding = 20; |
39 const float kMessageSize = 0.65f; | 36 const float kMessageSize = 0.65f; |
40 const SkColor kTextColor = SK_ColorWHITE; | 37 const SkColor kTextColor = SK_ColorWHITE; |
41 const SkColor kCrashColor = SkColorSetRGB(35, 48, 64); | 38 const SkColor kCrashColor = SkColorSetRGB(35, 48, 64); |
42 const SkColor kKillColor = SkColorSetRGB(57, 48, 88); | 39 const SkColor kKillColor = SkColorSetRGB(57, 48, 88); |
43 | 40 |
44 const char kCategoryTagCrash[] = "Crash"; | 41 const char kCategoryTagCrash[] = "Crash"; |
45 | 42 |
46 } // namespace | 43 } // namespace |
47 | 44 |
48 SadTabView::SadTabView(WebContents* web_contents, chrome::SadTabKind kind) | 45 SadTabView::SadTabView(WebContents* web_contents, |
| 46 SadTabKind kind, |
| 47 SadTabClient* client) |
49 : web_contents_(web_contents), | 48 : web_contents_(web_contents), |
50 kind_(kind), | 49 kind_(kind), |
| 50 client_(client), |
51 painted_(false), | 51 painted_(false), |
52 message_(NULL), | 52 message_(NULL), |
53 help_link_(NULL), | 53 help_link_(NULL), |
54 feedback_link_(NULL), | 54 feedback_link_(NULL), |
55 reload_button_(NULL) { | 55 reload_button_(NULL) { |
56 DCHECK(web_contents); | 56 DCHECK(web_contents); |
57 | 57 |
58 // Sometimes the user will never see this tab, so keep track of the total | 58 // Sometimes the user will never see this tab, so keep track of the total |
59 // number of creation events to compare to display events. | 59 // number of creation events to compare to display events. |
60 // TODO(jamescook): Remove this after R20 stable. Keep it for now so we can | 60 // TODO(jamescook): Remove this after R20 stable. Keep it for now so we can |
61 // compare R20 to earlier versions. | 61 // compare R20 to earlier versions. |
62 UMA_HISTOGRAM_COUNTS("SadTab.Created", kind_); | 62 UMA_HISTOGRAM_COUNTS("SadTab.Created", kind_); |
63 | 63 |
64 // These stats should use the same counting approach and bucket size used for | 64 // These stats should use the same counting approach and bucket size used for |
65 // tab discard events in chromeos::OomPriorityManager so they can be | 65 // tab discard events in chromeos::OomPriorityManager so they can be |
66 // directly compared. | 66 // directly compared. |
67 // TODO(jamescook): Maybe track time between sad tabs? | 67 // TODO(jamescook): Maybe track time between sad tabs? |
68 switch (kind_) { | 68 switch (kind_) { |
69 case chrome::SAD_TAB_KIND_CRASHED: { | 69 case SAD_TAB_KIND_CRASHED: { |
70 static int crashed = 0; | 70 static int crashed = 0; |
71 crashed++; | 71 crashed++; |
72 UMA_HISTOGRAM_CUSTOM_COUNTS( | 72 UMA_HISTOGRAM_CUSTOM_COUNTS( |
73 "Tabs.SadTab.CrashCreated", crashed, 1, 1000, 50); | 73 "Tabs.SadTab.CrashCreated", crashed, 1, 1000, 50); |
74 break; | 74 break; |
75 } | 75 } |
76 case chrome::SAD_TAB_KIND_KILLED: { | 76 case SAD_TAB_KIND_KILLED: { |
77 static int killed = 0; | 77 static int killed = 0; |
78 killed++; | 78 killed++; |
79 UMA_HISTOGRAM_CUSTOM_COUNTS( | 79 UMA_HISTOGRAM_CUSTOM_COUNTS( |
80 "Tabs.SadTab.KillCreated", killed, 1, 1000, 50); | 80 "Tabs.SadTab.KillCreated", killed, 1, 1000, 50); |
81 break; | 81 break; |
82 } | 82 } |
83 default: | 83 default: |
84 NOTREACHED(); | 84 NOTREACHED(); |
85 } | 85 } |
86 | 86 |
87 // Set the background color. | 87 // Set the background color. |
88 set_background(views::Background::CreateSolidBackground( | 88 set_background(views::Background::CreateSolidBackground( |
89 (kind_ == chrome::SAD_TAB_KIND_CRASHED) ? kCrashColor : kKillColor)); | 89 (kind_ == SAD_TAB_KIND_CRASHED) ? kCrashColor : kKillColor)); |
90 | 90 |
91 views::GridLayout* layout = new views::GridLayout(this); | 91 views::GridLayout* layout = new views::GridLayout(this); |
92 SetLayoutManager(layout); | 92 SetLayoutManager(layout); |
93 | 93 |
94 const int column_set_id = 0; | 94 const int column_set_id = 0; |
95 views::ColumnSet* columns = layout->AddColumnSet(column_set_id); | 95 views::ColumnSet* columns = layout->AddColumnSet(column_set_id); |
96 columns->AddPaddingColumn(1, kPadding); | 96 columns->AddPaddingColumn(1, kPadding); |
97 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, | 97 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, |
98 0, views::GridLayout::USE_PREF, 0, 0); | 98 0, views::GridLayout::USE_PREF, 0, 0); |
99 columns->AddPaddingColumn(1, kPadding); | 99 columns->AddPaddingColumn(1, kPadding); |
100 | 100 |
101 views::ImageView* image = new views::ImageView(); | 101 views::ImageView* image = new views::ImageView(); |
102 image->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 102 image->SetImage(client_->GetImageForKind(kind_)); |
103 (kind_ == chrome::SAD_TAB_KIND_CRASHED) ? IDR_SAD_TAB : IDR_KILLED_TAB)); | |
104 layout->StartRowWithPadding(0, column_set_id, 1, kPadding); | 103 layout->StartRowWithPadding(0, column_set_id, 1, kPadding); |
105 layout->AddView(image); | 104 layout->AddView(image); |
106 | 105 |
107 views::Label* title = CreateLabel(l10n_util::GetStringUTF16( | 106 views::Label* title = CreateLabel(l10n_util::GetStringUTF16( |
108 (kind_ == chrome::SAD_TAB_KIND_CRASHED) ? | 107 (kind_ == SAD_TAB_KIND_CRASHED) ? |
109 IDS_SAD_TAB_TITLE : IDS_KILLED_TAB_TITLE)); | 108 IDS_SAD_TAB_TITLE : IDS_KILLED_TAB_TITLE)); |
110 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 109 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
111 title->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont)); | 110 title->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont)); |
112 layout->StartRowWithPadding(0, column_set_id, 0, kPadding); | 111 layout->StartRowWithPadding(0, column_set_id, 0, kPadding); |
113 layout->AddView(title); | 112 layout->AddView(title); |
114 | 113 |
115 message_ = CreateLabel(l10n_util::GetStringUTF16( | 114 message_ = CreateLabel(l10n_util::GetStringUTF16( |
116 (kind_ == chrome::SAD_TAB_KIND_CRASHED) ? | 115 (kind_ == SAD_TAB_KIND_CRASHED) ? |
117 IDS_SAD_TAB_MESSAGE : IDS_KILLED_TAB_MESSAGE)); | 116 IDS_SAD_TAB_MESSAGE : IDS_KILLED_TAB_MESSAGE)); |
118 message_->SetMultiLine(true); | 117 message_->SetMultiLine(true); |
119 layout->StartRowWithPadding(0, column_set_id, 0, kPadding); | 118 layout->StartRowWithPadding(0, column_set_id, 0, kPadding); |
120 layout->AddView(message_); | 119 layout->AddView(message_); |
121 | 120 |
122 if (web_contents_) { | 121 if (web_contents_) { |
123 layout->StartRowWithPadding(0, column_set_id, 0, kPadding); | 122 layout->StartRowWithPadding(0, column_set_id, 0, kPadding); |
124 reload_button_ = new views::LabelButton( | 123 reload_button_ = new views::LabelButton( |
125 this, | 124 this, |
126 l10n_util::GetStringUTF16(IDS_SAD_TAB_RELOAD_LABEL)); | 125 l10n_util::GetStringUTF16(IDS_SAD_TAB_RELOAD_LABEL)); |
127 reload_button_->SetStyle(views::Button::STYLE_BUTTON); | 126 reload_button_->SetStyle(views::Button::STYLE_BUTTON); |
128 // Always render the reload button with chrome style borders; never rely on | 127 // Always render the reload button with chrome style borders; never rely on |
129 // native styles. | 128 // native styles. |
130 reload_button_->SetBorder(scoped_ptr<views::Border>( | 129 reload_button_->SetBorder(scoped_ptr<views::Border>( |
131 new views::LabelButtonBorder(reload_button_->style()))); | 130 new views::LabelButtonBorder(reload_button_->style()))); |
132 layout->AddView(reload_button_); | 131 layout->AddView(reload_button_); |
133 | 132 |
134 help_link_ = CreateLink(l10n_util::GetStringUTF16( | 133 help_link_ = CreateLink(l10n_util::GetStringUTF16( |
135 (kind_ == chrome::SAD_TAB_KIND_CRASHED) ? | 134 (kind_ == SAD_TAB_KIND_CRASHED) ? |
136 IDS_SAD_TAB_HELP_LINK : IDS_LEARN_MORE)); | 135 IDS_SAD_TAB_HELP_LINK : IDS_LEARN_MORE)); |
137 | 136 |
138 if (kind_ == chrome::SAD_TAB_KIND_CRASHED) { | 137 if (kind_ == SAD_TAB_KIND_CRASHED) { |
139 size_t offset = 0; | 138 size_t offset = 0; |
140 base::string16 help_text( | 139 base::string16 help_text( |
141 l10n_util::GetStringFUTF16(IDS_SAD_TAB_HELP_MESSAGE, | 140 l10n_util::GetStringFUTF16(IDS_SAD_TAB_HELP_MESSAGE, |
142 base::string16(), &offset)); | 141 base::string16(), &offset)); |
143 views::Label* help_prefix = CreateLabel(help_text.substr(0, offset)); | 142 views::Label* help_prefix = CreateLabel(help_text.substr(0, offset)); |
144 views::Label* help_suffix = CreateLabel(help_text.substr(offset)); | 143 views::Label* help_suffix = CreateLabel(help_text.substr(offset)); |
145 | 144 |
146 const int help_column_set_id = 1; | 145 const int help_column_set_id = 1; |
147 views::ColumnSet* help_columns = layout->AddColumnSet(help_column_set_id); | 146 views::ColumnSet* help_columns = layout->AddColumnSet(help_column_set_id); |
148 help_columns->AddPaddingColumn(1, kPadding); | 147 help_columns->AddPaddingColumn(1, kPadding); |
(...skipping 18 matching lines...) Expand all Loading... |
167 } | 166 } |
168 } | 167 } |
169 layout->AddPaddingRow(1, kPadding); | 168 layout->AddPaddingRow(1, kPadding); |
170 } | 169 } |
171 | 170 |
172 SadTabView::~SadTabView() {} | 171 SadTabView::~SadTabView() {} |
173 | 172 |
174 void SadTabView::LinkClicked(views::Link* source, int event_flags) { | 173 void SadTabView::LinkClicked(views::Link* source, int event_flags) { |
175 DCHECK(web_contents_); | 174 DCHECK(web_contents_); |
176 if (source == help_link_) { | 175 if (source == help_link_) { |
177 GURL help_url((kind_ == chrome::SAD_TAB_KIND_CRASHED) ? | 176 GURL help_url((kind_ == SAD_TAB_KIND_CRASHED) ? |
178 chrome::kCrashReasonURL : chrome::kKillReasonURL); | 177 kCrashReasonURL : kKillReasonURL); |
179 OpenURLParams params( | 178 OpenURLParams params( |
180 help_url, content::Referrer(), CURRENT_TAB, | 179 help_url, content::Referrer(), CURRENT_TAB, |
181 content::PAGE_TRANSITION_LINK, false); | 180 content::PAGE_TRANSITION_LINK, false); |
182 web_contents_->OpenURL(params); | 181 web_contents_->OpenURL(params); |
183 } else if (source == feedback_link_) { | 182 } else if (source == feedback_link_) { |
184 chrome::ShowFeedbackPage( | 183 client_->ShowFeedbackPage( |
185 chrome::FindBrowserWithWebContents(web_contents_), | 184 web_contents_, |
186 l10n_util::GetStringUTF8(IDS_KILLED_TAB_FEEDBACK_MESSAGE), | 185 l10n_util::GetStringUTF8(IDS_KILLED_TAB_FEEDBACK_MESSAGE), |
187 std::string(kCategoryTagCrash)); | 186 std::string(kCategoryTagCrash)); |
188 } | 187 } |
189 } | 188 } |
190 | 189 |
191 void SadTabView::ButtonPressed(views::Button* sender, | 190 void SadTabView::ButtonPressed(views::Button* sender, |
192 const ui::Event& event) { | 191 const ui::Event& event) { |
193 DCHECK(web_contents_); | 192 DCHECK(web_contents_); |
194 DCHECK_EQ(reload_button_, sender); | 193 DCHECK_EQ(reload_button_, sender); |
195 web_contents_->GetController().Reload(true); | 194 web_contents_->GetController().Reload(true); |
196 } | 195 } |
197 | 196 |
198 void SadTabView::Layout() { | 197 void SadTabView::Layout() { |
199 // Specify the maximum message width explicitly. | 198 // Specify the maximum message width explicitly. |
200 message_->SizeToFit(static_cast<int>(width() * kMessageSize)); | 199 message_->SizeToFit(static_cast<int>(width() * kMessageSize)); |
201 View::Layout(); | 200 View::Layout(); |
202 } | 201 } |
203 | 202 |
204 void SadTabView::OnPaint(gfx::Canvas* canvas) { | 203 void SadTabView::OnPaint(gfx::Canvas* canvas) { |
205 if (!painted_) { | 204 if (!painted_) { |
206 // User actually saw the error, keep track for user experience stats. | 205 // User actually saw the error, keep track for user experience stats. |
207 // TODO(jamescook): Remove this after R20 stable. Keep it for now so we can | 206 // TODO(jamescook): Remove this after R20 stable. Keep it for now so we can |
208 // compare R20 to earlier versions. | 207 // compare R20 to earlier versions. |
209 UMA_HISTOGRAM_COUNTS("SadTab.Displayed", kind_); | 208 UMA_HISTOGRAM_COUNTS("SadTab.Displayed", kind_); |
210 | 209 |
211 // These stats should use the same counting approach and bucket size used | 210 // These stats should use the same counting approach and bucket size used |
212 // for tab discard events in chromeos::OomPriorityManager so they | 211 // for tab discard events in chromeos::OomPriorityManager so they |
213 // can be directly compared. | 212 // can be directly compared. |
214 switch (kind_) { | 213 switch (kind_) { |
215 case chrome::SAD_TAB_KIND_CRASHED: { | 214 case SAD_TAB_KIND_CRASHED: { |
216 static int crashed = 0; | 215 static int crashed = 0; |
217 UMA_HISTOGRAM_CUSTOM_COUNTS( | 216 UMA_HISTOGRAM_CUSTOM_COUNTS( |
218 "Tabs.SadTab.CrashDisplayed", ++crashed, 1, 1000, 50); | 217 "Tabs.SadTab.CrashDisplayed", ++crashed, 1, 1000, 50); |
219 break; | 218 break; |
220 } | 219 } |
221 case chrome::SAD_TAB_KIND_KILLED: { | 220 case SAD_TAB_KIND_KILLED: { |
222 static int killed = 0; | 221 static int killed = 0; |
223 UMA_HISTOGRAM_CUSTOM_COUNTS( | 222 UMA_HISTOGRAM_CUSTOM_COUNTS( |
224 "Tabs.SadTab.KillDisplayed", ++killed, 1, 1000, 50); | 223 "Tabs.SadTab.KillDisplayed", ++killed, 1, 1000, 50); |
225 break; | 224 break; |
226 } | 225 } |
227 default: | 226 default: |
228 NOTREACHED(); | 227 NOTREACHED(); |
229 } | 228 } |
230 painted_ = true; | 229 painted_ = true; |
231 } | 230 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 } | 265 } |
267 | 266 |
268 views::Link* SadTabView::CreateLink(const base::string16& text) { | 267 views::Link* SadTabView::CreateLink(const base::string16& text) { |
269 views::Link* link = new views::Link(text); | 268 views::Link* link = new views::Link(text); |
270 link->SetBackgroundColor(background()->get_color()); | 269 link->SetBackgroundColor(background()->get_color()); |
271 link->SetEnabledColor(kTextColor); | 270 link->SetEnabledColor(kTextColor); |
272 link->set_listener(this); | 271 link->set_listener(this); |
273 return link; | 272 return link; |
274 } | 273 } |
275 | 274 |
276 namespace chrome { | |
277 | |
278 SadTab* SadTab::Create(content::WebContents* web_contents, | 275 SadTab* SadTab::Create(content::WebContents* web_contents, |
279 SadTabKind kind) { | 276 SadTabKind kind, |
280 return new SadTabView(web_contents, kind); | 277 SadTabClient* client) { |
| 278 return new SadTabView(web_contents, kind, client); |
281 } | 279 } |
282 | 280 |
283 } // namespace chrome | 281 } // namespace sad_tab |
OLD | NEW |