Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / outdated_upgrade_bubble_view.cc
blob81972b0f9c4cadfad9d5350189f67f11a3428d7e
1 // Copyright (c) 2013 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/outdated_upgrade_bubble_view.h"
7 #include "base/metrics/histogram.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/ui/views/elevation_icon_setter.h"
11 #include "chrome/browser/upgrade_detector.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/grit/chromium_strings.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/page_navigator.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/views/controls/button/label_button.h"
22 #include "ui/views/controls/image_view.h"
23 #include "ui/views/controls/label.h"
24 #include "ui/views/layout/grid_layout.h"
25 #include "ui/views/layout/layout_constants.h"
26 #include "ui/views/widget/widget.h"
27 #include "url/gurl.h"
29 #if defined(OS_WIN)
30 #include "chrome/installer/util/google_update_util.h"
31 #endif
33 namespace {
35 // Fixed width of the column holding the description label of the bubble.
36 // TODO(mad): Make sure there is enough room for all languages.
37 const int kWidthOfDescriptionText = 330;
39 // We subtract 2 to account for the natural button padding, and
40 // to bring the separation visually in line with the row separation
41 // height.
42 const int kButtonPadding = views::kRelatedButtonHSpacing - 2;
44 // The URL to be used to re-install Chrome when auto-update failed for too long.
45 const char kDownloadChromeUrl[] = "https://www.google.com/chrome/?&brand=CHWL"
46 "&utm_campaign=en&utm_source=en-et-na-us-chrome-bubble&utm_medium=et";
48 // The maximum number of ignored bubble we track in the NumLaterPerReinstall
49 // histogram.
50 const int kMaxIgnored = 50;
51 // The number of buckets we want the NumLaterPerReinstall histogram to use.
52 const int kNumIgnoredBuckets = 5;
54 } // namespace
56 // OutdatedUpgradeBubbleView ---------------------------------------------------
58 OutdatedUpgradeBubbleView* OutdatedUpgradeBubbleView::upgrade_bubble_ = NULL;
59 int OutdatedUpgradeBubbleView::num_ignored_bubbles_ = 0;
61 // static
62 void OutdatedUpgradeBubbleView::ShowBubble(views::View* anchor_view,
63 content::PageNavigator* navigator,
64 bool auto_update_enabled) {
65 if (IsShowing())
66 return;
67 upgrade_bubble_ = new OutdatedUpgradeBubbleView(
68 anchor_view, navigator, auto_update_enabled);
69 views::BubbleDelegateView::CreateBubble(upgrade_bubble_)->Show();
70 content::RecordAction(auto_update_enabled ?
71 base::UserMetricsAction("OutdatedUpgradeBubble.Show") :
72 base::UserMetricsAction("OutdatedUpgradeBubble.ShowNoAU"));
75 bool OutdatedUpgradeBubbleView::IsAvailable() {
76 // This should only work on non-Chrome OS desktop platforms.
77 #if defined(OS_WIN) || defined(OS_MACOSX) || \
78 (defined(OS_LINUX) && !defined(OS_CHROMEOS))
79 return true;
80 #else
81 return false;
82 #endif
85 OutdatedUpgradeBubbleView::~OutdatedUpgradeBubbleView() {
86 if (!accepted_ && num_ignored_bubbles_ < kMaxIgnored)
87 ++num_ignored_bubbles_;
89 // Ensure |elevation_icon_setter_| is destroyed before |accept_button_|.
90 elevation_icon_setter_.reset();
93 views::View* OutdatedUpgradeBubbleView::GetInitiallyFocusedView() {
94 return accept_button_;
97 void OutdatedUpgradeBubbleView::WindowClosing() {
98 // Reset |upgrade_bubble_| here, not in destructor, because destruction is
99 // asynchronous and ShowBubble may be called before full destruction and
100 // would attempt to show a bubble that is closing.
101 DCHECK_EQ(upgrade_bubble_, this);
102 upgrade_bubble_ = NULL;
105 void OutdatedUpgradeBubbleView::Init() {
106 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
107 accept_button_ = new views::LabelButton(
108 this, l10n_util::GetStringUTF16(
109 auto_update_enabled_ ? IDS_REINSTALL_APP : IDS_REENABLE_UPDATES));
110 accept_button_->SetStyle(views::Button::STYLE_BUTTON);
111 accept_button_->SetIsDefault(true);
112 accept_button_->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont));
113 elevation_icon_setter_.reset(new ElevationIconSetter(
114 accept_button_,
115 base::Bind(&OutdatedUpgradeBubbleView::SizeToContents,
116 base::Unretained(this))));
118 later_button_ = new views::LabelButton(
119 this, l10n_util::GetStringUTF16(IDS_LATER));
120 later_button_->SetStyle(views::Button::STYLE_BUTTON);
122 views::Label* title_label = new views::Label(
123 l10n_util::GetStringUTF16(IDS_UPGRADE_BUBBLE_TITLE));
124 title_label->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
125 title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
127 views::Label* text_label = new views::Label(l10n_util::GetStringUTF16(
128 auto_update_enabled_ ? IDS_UPGRADE_BUBBLE_TEXT
129 : IDS_UPGRADE_BUBBLE_REENABLE_TEXT));
130 text_label->SetMultiLine(true);
131 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
133 views::ImageView* image_view = new views::ImageView();
134 image_view->SetImage(rb.GetImageSkiaNamed(IDR_UPDATE_MENU_SEVERITY_HIGH));
136 views::GridLayout* layout = new views::GridLayout(this);
137 SetLayoutManager(layout);
139 const int kIconTitleColumnSetId = 0;
140 views::ColumnSet* cs = layout->AddColumnSet(kIconTitleColumnSetId);
142 // Top (icon-title) row.
143 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
144 views::GridLayout::USE_PREF, 0, 0);
145 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
146 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 0,
147 views::GridLayout::USE_PREF, 0, 0);
148 cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing);
150 // Middle (text) row.
151 const int kTextColumnSetId = 1;
152 cs = layout->AddColumnSet(kTextColumnSetId);
153 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
154 views::GridLayout::FIXED, kWidthOfDescriptionText, 0);
156 // Bottom (buttons) row.
157 const int kButtonsColumnSetId = 2;
158 cs = layout->AddColumnSet(kButtonsColumnSetId);
159 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing);
160 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::TRAILING, 0,
161 views::GridLayout::USE_PREF, 0, 0);
162 cs->AddPaddingColumn(0, kButtonPadding);
163 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::TRAILING, 0,
164 views::GridLayout::USE_PREF, 0, 0);
166 layout->StartRow(0, kIconTitleColumnSetId);
167 layout->AddView(image_view);
168 layout->AddView(title_label);
170 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
171 layout->StartRow(0, kTextColumnSetId);
172 layout->AddView(text_label);
174 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
176 layout->StartRow(0, kButtonsColumnSetId);
177 layout->AddView(accept_button_);
178 layout->AddView(later_button_);
180 AddAccelerator(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
183 OutdatedUpgradeBubbleView::OutdatedUpgradeBubbleView(
184 views::View* anchor_view,
185 content::PageNavigator* navigator,
186 bool auto_update_enabled)
187 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
188 auto_update_enabled_(auto_update_enabled),
189 accepted_(false),
190 accept_button_(NULL),
191 later_button_(NULL),
192 navigator_(navigator) {
193 // Compensate for built-in vertical padding in the anchor view's image.
194 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
197 void OutdatedUpgradeBubbleView::ButtonPressed(
198 views::Button* sender, const ui::Event& event) {
199 if (event.IsMouseEvent() &&
200 !(static_cast<const ui::MouseEvent*>(&event))->IsOnlyLeftMouseButton()) {
201 return;
203 HandleButtonPressed(sender);
206 void OutdatedUpgradeBubbleView::HandleButtonPressed(views::Button* sender) {
207 if (sender == accept_button_) {
208 accepted_ = true;
209 if (auto_update_enabled_) {
210 DCHECK(UpgradeDetector::GetInstance()->is_outdated_install());
211 UMA_HISTOGRAM_CUSTOM_COUNTS(
212 "OutdatedUpgradeBubble.NumLaterPerReinstall", num_ignored_bubbles_,
213 0, kMaxIgnored, kNumIgnoredBuckets);
214 content::RecordAction(
215 base::UserMetricsAction("OutdatedUpgradeBubble.Reinstall"));
216 navigator_->OpenURL(content::OpenURLParams(GURL(kDownloadChromeUrl),
217 content::Referrer(),
218 NEW_FOREGROUND_TAB,
219 ui::PAGE_TRANSITION_LINK,
220 false));
221 #if defined(OS_WIN)
222 } else {
223 DCHECK(UpgradeDetector::GetInstance()->is_outdated_install_no_au());
224 UMA_HISTOGRAM_CUSTOM_COUNTS(
225 "OutdatedUpgradeBubble.NumLaterPerEnableAU", num_ignored_bubbles_,
226 0, kMaxIgnored, kNumIgnoredBuckets);
227 content::RecordAction(
228 base::UserMetricsAction("OutdatedUpgradeBubble.EnableAU"));
229 // Record that the autoupdate flavour of the dialog has been shown.
230 if (g_browser_process->local_state()) {
231 g_browser_process->local_state()->SetBoolean(
232 prefs::kAttemptedToEnableAutoupdate, true);
235 // Re-enable updates by shelling out to setup.exe in the blocking pool.
236 content::BrowserThread::PostBlockingPoolTask(
237 FROM_HERE,
238 base::Bind(&google_update::ElevateIfNeededToReenableUpdates));
239 #endif // defined(OS_WIN)
241 } else {
242 DCHECK_EQ(later_button_, sender);
243 content::RecordAction(
244 base::UserMetricsAction("OutdatedUpgradeBubble.Later"));
246 GetWidget()->Close();