OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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/views/recovery_component_bubble_view.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/component_updater/recovery_component_installer.h" |
| 10 #include "chrome/browser/ui/views/elevation_icon_setter.h" |
| 11 #include "chrome/browser/upgrade_detector.h" |
| 12 #include "content/public/browser/user_metrics.h" |
| 13 #include "grit/chromium_strings.h" |
| 14 #include "grit/generated_resources.h" |
| 15 #include "grit/theme_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/views/controls/button/label_button.h" |
| 19 #include "ui/views/controls/image_view.h" |
| 20 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/layout/grid_layout.h" |
| 22 #include "ui/views/layout/layout_constants.h" |
| 23 #include "ui/views/widget/widget.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Fixed width of the column holding the description label of the bubble. |
| 28 const int kWidthOfDescriptionText = 350; |
| 29 |
| 30 // We subtract 2 to account for the natural button padding, and |
| 31 // to bring the separation visually in line with the row separation |
| 32 // height. |
| 33 const int kButtonPadding = views::kRelatedButtonHSpacing - 2; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 // RecoveryComponentBubbleView ------------------------------------------------- |
| 38 |
| 39 RecoveryComponentBubbleView* |
| 40 RecoveryComponentBubbleView::recovery_component_bubble_ = NULL; |
| 41 |
| 42 // static |
| 43 void RecoveryComponentBubbleView::ShowBubble(views::View* anchor_view) { |
| 44 if (IsShowing()) |
| 45 return; |
| 46 recovery_component_bubble_ = new RecoveryComponentBubbleView(anchor_view); |
| 47 views::BubbleDelegateView::CreateBubble(recovery_component_bubble_)->Show(); |
| 48 |
| 49 content::RecordAction( |
| 50 base::UserMetricsAction("RecoveryComponentBubble.Show")); |
| 51 } |
| 52 |
| 53 bool RecoveryComponentBubbleView::IsAvailable() { |
| 54 // Right now only Windows platform is supported. |
| 55 #if defined(OS_WIN) |
| 56 return true; |
| 57 #else |
| 58 return false; |
| 59 #endif |
| 60 } |
| 61 |
| 62 RecoveryComponentBubbleView::~RecoveryComponentBubbleView() { |
| 63 // Ensure |elevation_icon_setter_| is destroyed before |accept_button_|. |
| 64 elevation_icon_setter_.reset(); |
| 65 } |
| 66 |
| 67 views::View* RecoveryComponentBubbleView::GetInitiallyFocusedView() { |
| 68 return accept_button_; |
| 69 } |
| 70 |
| 71 void RecoveryComponentBubbleView::WindowClosing() { |
| 72 // Reset |recovery_component_bubble_| here, not in destructor, because |
| 73 // destruction is asynchronous and ShowBubble may be called before full |
| 74 // destruction and would attempt to show a bubble that is closing. |
| 75 DCHECK_EQ(recovery_component_bubble_, this); |
| 76 recovery_component_bubble_ = NULL; |
| 77 } |
| 78 |
| 79 void RecoveryComponentBubbleView::Init() { |
| 80 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 81 accept_button_ = new views::LabelButton( |
| 82 this, l10n_util::GetStringUTF16(IDS_RUN_RECOVERY)); |
| 83 accept_button_->SetStyle(views::Button::STYLE_BUTTON); |
| 84 accept_button_->SetIsDefault(true); |
| 85 accept_button_->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); |
| 86 elevation_icon_setter_.reset(new ElevationIconSetter(accept_button_)); |
| 87 |
| 88 decline_button_ = new views::LabelButton( |
| 89 this, l10n_util::GetStringUTF16(IDS_NO_THANKS)); |
| 90 decline_button_->SetStyle(views::Button::STYLE_BUTTON); |
| 91 |
| 92 views::Label* title_label = new views::Label( |
| 93 l10n_util::GetStringUTF16(IDS_RECOVERY_BUBBLE_TITLE)); |
| 94 title_label->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont)); |
| 95 title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 96 |
| 97 views::Label* text_label = new views::Label( |
| 98 l10n_util::GetStringUTF16(IDS_RECOVERY_BUBBLE_TEXT)); |
| 99 text_label->SetMultiLine(true); |
| 100 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 101 |
| 102 views::ImageView* image_view = new views::ImageView(); |
| 103 image_view->SetImage(rb.GetImageSkiaNamed(IDR_UPDATE_MENU_SEVERITY_HIGH)); |
| 104 |
| 105 views::GridLayout* layout = new views::GridLayout(this); |
| 106 SetLayoutManager(layout); |
| 107 |
| 108 const int kIconTitleColumnSetId = 0; |
| 109 views::ColumnSet* cs = layout->AddColumnSet(kIconTitleColumnSetId); |
| 110 |
| 111 // Top (icon-title) row. |
| 112 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 113 views::GridLayout::USE_PREF, 0, 0); |
| 114 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
| 115 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 0, |
| 116 views::GridLayout::USE_PREF, 0, 0); |
| 117 cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); |
| 118 |
| 119 // Middle (text) row. |
| 120 const int kTextColumnSetId = 1; |
| 121 cs = layout->AddColumnSet(kTextColumnSetId); |
| 122 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 123 views::GridLayout::FIXED, kWidthOfDescriptionText, 0); |
| 124 |
| 125 // Bottom (buttons) row. |
| 126 const int kButtonsColumnSetId = 2; |
| 127 cs = layout->AddColumnSet(kButtonsColumnSetId); |
| 128 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); |
| 129 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::TRAILING, 0, |
| 130 views::GridLayout::USE_PREF, 0, 0); |
| 131 cs->AddPaddingColumn(0, kButtonPadding); |
| 132 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::TRAILING, 0, |
| 133 views::GridLayout::USE_PREF, 0, 0); |
| 134 |
| 135 layout->StartRow(0, kIconTitleColumnSetId); |
| 136 layout->AddView(image_view); |
| 137 layout->AddView(title_label); |
| 138 |
| 139 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); |
| 140 layout->StartRow(0, kTextColumnSetId); |
| 141 layout->AddView(text_label); |
| 142 |
| 143 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 144 |
| 145 layout->StartRow(0, kButtonsColumnSetId); |
| 146 layout->AddView(accept_button_); |
| 147 layout->AddView(decline_button_); |
| 148 |
| 149 AddAccelerator(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); |
| 150 } |
| 151 |
| 152 RecoveryComponentBubbleView::RecoveryComponentBubbleView( |
| 153 views::View* anchor_view) |
| 154 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), |
| 155 accept_button_(NULL), |
| 156 decline_button_(NULL) { |
| 157 // Compensate for built-in vertical padding in the anchor view's image. |
| 158 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); |
| 159 } |
| 160 |
| 161 void RecoveryComponentBubbleView::ButtonPressed( |
| 162 views::Button* sender, const ui::Event& event) { |
| 163 if (event.IsMouseEvent() && |
| 164 !(static_cast<const ui::MouseEvent*>(&event))->IsOnlyLeftMouseButton()) { |
| 165 return; |
| 166 } |
| 167 HandleButtonPressed(sender); |
| 168 } |
| 169 |
| 170 void RecoveryComponentBubbleView::HandleButtonPressed(views::Button* sender) { |
| 171 if (sender == accept_button_ || sender == decline_button_) { |
| 172 const bool elevation_agreed = (sender == accept_button_); |
| 173 component_updater::StartElevatedRecoveryProcess( |
| 174 g_browser_process->local_state(), |
| 175 elevation_agreed, |
| 176 base::Bind(&UpgradeDetector::CheckForUpgrade, |
| 177 base::Unretained(UpgradeDetector::GetInstance()))); |
| 178 content::RecordAction(base::UserMetricsAction( |
| 179 elevation_agreed ? "RecoveryComponentBubble.ElevationAgreed" |
| 180 : "RecoveryComponentBubble.ElevationDeclined")); |
| 181 } else { |
| 182 content::RecordAction(base::UserMetricsAction( |
| 183 "RecoveryComponentBubble.Ignored")); |
| 184 } |
| 185 GetWidget()->Close(); |
| 186 } |
OLD | NEW |