OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/first_run/try_chrome_dialog_view.h" |
| 6 |
| 7 #include <shellapi.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string16.h" |
| 14 #include "chrome/browser/process_singleton.h" |
| 15 #include "chrome/grit/chromium_strings.h" |
| 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "chrome/grit/theme_resources.h" |
| 18 #include "chrome/install_static/install_util.h" |
| 19 #include "chrome/installer/util/user_experiment.h" |
| 20 #include "components/strings/grit/components_strings.h" |
| 21 #include "ui/aura/window.h" |
| 22 #include "ui/aura/window_tree_host.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/image/image.h" |
| 26 #include "ui/resources/grit/ui_resources.h" |
| 27 #include "ui/views/background.h" |
| 28 #include "ui/views/controls/button/checkbox.h" |
| 29 #include "ui/views/controls/button/image_button.h" |
| 30 #include "ui/views/controls/button/md_text_button.h" |
| 31 #include "ui/views/controls/button/radio_button.h" |
| 32 #include "ui/views/controls/image_view.h" |
| 33 #include "ui/views/controls/link.h" |
| 34 #include "ui/views/controls/separator.h" |
| 35 #include "ui/views/layout/grid_layout.h" |
| 36 #include "ui/views/layout/layout_constants.h" |
| 37 #include "ui/views/layout/layout_provider.h" |
| 38 #include "ui/views/widget/widget.h" |
| 39 |
| 40 namespace { |
| 41 |
| 42 const wchar_t kHelpCenterUrl[] = |
| 43 L"https://support.google.com/chrome/answer/150752"; |
| 44 |
| 45 enum ButtonTags { |
| 46 BT_NONE, |
| 47 BT_CLOSE_BUTTON, |
| 48 BT_OK_BUTTON, |
| 49 BT_TRY_IT_RADIO, |
| 50 BT_DONT_BUG_RADIO |
| 51 }; |
| 52 |
| 53 const int kRadioGroupID = 1; |
| 54 |
| 55 } // namespace |
| 56 |
| 57 // static |
| 58 TryChromeDialogView::Result TryChromeDialogView::Show( |
| 59 size_t flavor, |
| 60 const ActiveModalDialogListener& listener) { |
| 61 if (flavor > 10000) { |
| 62 // This is a test value. We want to make sure we exercise |
| 63 // returning this early. See TryChromeDialogBrowserTest test. |
| 64 return NOT_NOW; |
| 65 } |
| 66 TryChromeDialogView dialog(flavor); |
| 67 return dialog.ShowModal(listener); |
| 68 } |
| 69 |
| 70 TryChromeDialogView::TryChromeDialogView(size_t flavor) |
| 71 : flavor_(flavor), |
| 72 popup_(NULL), |
| 73 try_chrome_(NULL), |
| 74 kill_chrome_(NULL), |
| 75 dont_try_chrome_(NULL), |
| 76 make_default_(NULL), |
| 77 result_(COUNT) { |
| 78 } |
| 79 |
| 80 TryChromeDialogView::~TryChromeDialogView() { |
| 81 } |
| 82 |
| 83 TryChromeDialogView::Result TryChromeDialogView::ShowModal( |
| 84 const ActiveModalDialogListener& listener) { |
| 85 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 86 |
| 87 views::ImageView* icon = new views::ImageView(); |
| 88 icon->SetImage(rb.GetNativeImageNamed(IDR_PRODUCT_LOGO_32).ToImageSkia()); |
| 89 gfx::Size icon_size = icon->GetPreferredSize(); |
| 90 |
| 91 // An approximate window size. After Layout() we'll get better bounds. |
| 92 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 93 params.activatable = views::Widget::InitParams::ACTIVATABLE_YES; |
| 94 params.bounds = gfx::Rect(310, 200); |
| 95 popup_ = new views::Widget; |
| 96 popup_->Init(params); |
| 97 |
| 98 views::View* root_view = popup_->GetRootView(); |
| 99 // The window color is a tiny bit off-white. |
| 100 root_view->set_background( |
| 101 views::Background::CreateSolidBackground(0xfc, 0xfc, 0xfc)); |
| 102 |
| 103 views::GridLayout* layout = views::GridLayout::CreatePanel(root_view); |
| 104 views::ColumnSet* columns; |
| 105 |
| 106 // First row: [icon][pad][text][pad][button]. |
| 107 columns = layout->AddColumnSet(0); |
| 108 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 0, |
| 109 views::GridLayout::FIXED, icon_size.width(), |
| 110 icon_size.height()); |
| 111 columns->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
| 112 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 113 views::GridLayout::USE_PREF, 0, 0); |
| 114 columns->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing); |
| 115 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 1, |
| 116 views::GridLayout::USE_PREF, 0, 0); |
| 117 |
| 118 // Optional second row: [pad][pad][radio 1]. |
| 119 columns = layout->AddColumnSet(1); |
| 120 columns->AddPaddingColumn(0, icon_size.width()); |
| 121 columns->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
| 122 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| 123 views::GridLayout::USE_PREF, 0, 0); |
| 124 |
| 125 // Third row: [pad][pad][radio 2]. |
| 126 columns = layout->AddColumnSet(2); |
| 127 columns->AddPaddingColumn(0, icon_size.width()); |
| 128 columns->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
| 129 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| 130 views::GridLayout::USE_PREF, 0, 0); |
| 131 |
| 132 // Fourth row: [pad][pad][button][pad][button]. |
| 133 columns = layout->AddColumnSet(3); |
| 134 columns->AddPaddingColumn(0, icon_size.width()); |
| 135 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 0, |
| 136 views::GridLayout::USE_PREF, 0, 0); |
| 137 columns->AddPaddingColumn(0, views::LayoutProvider::Get()->GetDistanceMetric( |
| 138 views::DISTANCE_RELATED_BUTTON_HORIZONTAL)); |
| 139 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 0, |
| 140 views::GridLayout::USE_PREF, 0, 0); |
| 141 |
| 142 // Fifth row: [pad][pad][link]. |
| 143 columns = layout->AddColumnSet(4); |
| 144 columns->AddPaddingColumn(0, icon_size.width()); |
| 145 columns->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
| 146 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| 147 views::GridLayout::USE_PREF, 0, 0); |
| 148 |
| 149 // Optional fourth row: [button]. |
| 150 columns = layout->AddColumnSet(5); |
| 151 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1, |
| 152 views::GridLayout::USE_PREF, 0, 0); |
| 153 |
| 154 // Optional fourth row: [divider] |
| 155 columns = layout->AddColumnSet(6); |
| 156 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1, |
| 157 views::GridLayout::USE_PREF, 0, 0); |
| 158 |
| 159 // Optional fifth row [checkbox][pad][button] |
| 160 columns = layout->AddColumnSet(7); |
| 161 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 0, |
| 162 views::GridLayout::USE_PREF, 0, 0); |
| 163 columns->AddPaddingColumn(0, views::kUnrelatedControlHorizontalSpacing); |
| 164 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 1, |
| 165 views::GridLayout::USE_PREF, 0, 0); |
| 166 |
| 167 // First row. |
| 168 layout->StartRow(0, 0); |
| 169 layout->AddView(icon); |
| 170 |
| 171 // Find out what experiment we are conducting. |
| 172 installer::ExperimentDetails experiment; |
| 173 if (!install_static::SupportsRetentionExperiments() || |
| 174 !installer::CreateExperimentDetails(flavor_, &experiment) || |
| 175 !experiment.heading) { |
| 176 NOTREACHED() << "Cannot determine which headline to show."; |
| 177 return DIALOG_ERROR; |
| 178 } |
| 179 views::Label* label = |
| 180 new views::Label(l10n_util::GetStringUTF16(experiment.heading), |
| 181 views::style::CONTEXT_DIALOG_TITLE); |
| 182 label->SetMultiLine(true); |
| 183 label->SizeToFit(200); |
| 184 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 185 layout->AddView(label); |
| 186 // The close button is custom. |
| 187 views::ImageButton* close_button = new views::ImageButton(this); |
| 188 close_button->SetImage(views::CustomButton::STATE_NORMAL, |
| 189 rb.GetNativeImageNamed(IDR_CLOSE_2).ToImageSkia()); |
| 190 close_button->SetImage(views::CustomButton::STATE_HOVERED, |
| 191 rb.GetNativeImageNamed(IDR_CLOSE_2_H).ToImageSkia()); |
| 192 close_button->SetImage(views::CustomButton::STATE_PRESSED, |
| 193 rb.GetNativeImageNamed(IDR_CLOSE_2_P).ToImageSkia()); |
| 194 close_button->set_tag(BT_CLOSE_BUTTON); |
| 195 layout->AddView(close_button); |
| 196 |
| 197 // Second row. |
| 198 layout->StartRowWithPadding(0, 1, 0, 10); |
| 199 try_chrome_ = new views::RadioButton( |
| 200 l10n_util::GetStringUTF16(IDS_TRY_TOAST_TRY_OPT), kRadioGroupID); |
| 201 try_chrome_->SetChecked(true); |
| 202 try_chrome_->set_tag(BT_TRY_IT_RADIO); |
| 203 try_chrome_->set_listener(this); |
| 204 layout->AddView(try_chrome_); |
| 205 |
| 206 // Decide if the don't bug me is a button or a radio button. |
| 207 bool dont_bug_me_button = |
| 208 !!(experiment.flags & installer::kToastUiDontBugMeAsButton); |
| 209 |
| 210 // Optional third and fourth row. |
| 211 if (!dont_bug_me_button) { |
| 212 layout->StartRow(0, 1); |
| 213 dont_try_chrome_ = new views::RadioButton( |
| 214 l10n_util::GetStringUTF16(IDS_TRY_TOAST_CANCEL), kRadioGroupID); |
| 215 dont_try_chrome_->set_tag(BT_DONT_BUG_RADIO); |
| 216 dont_try_chrome_->set_listener(this); |
| 217 layout->AddView(dont_try_chrome_); |
| 218 } |
| 219 if (experiment.flags & installer::kToastUiUninstall) { |
| 220 layout->StartRow(0, 2); |
| 221 kill_chrome_ = new views::RadioButton( |
| 222 l10n_util::GetStringUTF16(IDS_UNINSTALL_CHROME), kRadioGroupID); |
| 223 layout->AddView(kill_chrome_); |
| 224 } |
| 225 |
| 226 views::LabelButton* accept_button = |
| 227 views::MdTextButton::CreateSecondaryUiButton( |
| 228 this, l10n_util::GetStringUTF16(IDS_OK)); |
| 229 accept_button->set_tag(BT_OK_BUTTON); |
| 230 |
| 231 views::Separator* separator = NULL; |
| 232 if (experiment.flags & installer::kToastUiMakeDefault) { |
| 233 // In this flavor we have some veritical space, then a separator line |
| 234 // and the 'make default' checkbox and the OK button on the same row. |
| 235 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 236 layout->StartRow(0, 6); |
| 237 separator = new views::Separator(); |
| 238 layout->AddView(separator); |
| 239 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 240 |
| 241 layout->StartRow(0, 7); |
| 242 make_default_ = new views::Checkbox( |
| 243 l10n_util::GetStringUTF16(IDS_TRY_TOAST_SET_DEFAULT)); |
| 244 make_default_->SetChecked(true); |
| 245 layout->AddView(make_default_); |
| 246 layout->AddView(accept_button); |
| 247 } else { |
| 248 // On this other flavor there is no checkbox, the OK button and possibly |
| 249 // the cancel button are in the same row. |
| 250 layout->StartRowWithPadding(0, dont_bug_me_button ? 3 : 5, 0, 10); |
| 251 layout->AddView(accept_button); |
| 252 if (dont_bug_me_button) { |
| 253 // The dialog needs a "Don't bug me" as a button or as a radio button, |
| 254 // this the button case. |
| 255 views::LabelButton* cancel_button = |
| 256 views::MdTextButton::CreateSecondaryUiButton( |
| 257 this, l10n_util::GetStringUTF16(IDS_TRY_TOAST_CANCEL)); |
| 258 cancel_button->set_tag(BT_CLOSE_BUTTON); |
| 259 layout->AddView(cancel_button); |
| 260 } |
| 261 } |
| 262 |
| 263 if (experiment.flags & installer::kToastUiWhyLink) { |
| 264 layout->StartRowWithPadding(0, 4, 0, 10); |
| 265 views::Link* link = new views::Link( |
| 266 l10n_util::GetStringUTF16(IDS_TRY_TOAST_WHY)); |
| 267 link->set_listener(this); |
| 268 layout->AddView(link); |
| 269 } |
| 270 |
| 271 // We resize the window according to the layout manager. This takes into |
| 272 // account the differences between XP and Vista fonts and buttons. |
| 273 layout->Layout(root_view); |
| 274 gfx::Size preferred = layout->GetPreferredSize(root_view); |
| 275 if (separator) { |
| 276 int separator_height = separator->GetPreferredSize().height(); |
| 277 separator->SetSize(gfx::Size(preferred.width(), separator_height)); |
| 278 } |
| 279 |
| 280 gfx::Rect pos = ComputeWindowPosition(preferred, base::i18n::IsRTL()); |
| 281 popup_->SetBounds(pos); |
| 282 |
| 283 // Carve the toast shape into the window. |
| 284 HWND toast_window; |
| 285 toast_window = popup_->GetNativeView()->GetHost()->GetAcceleratedWidget(); |
| 286 SetToastRegion(toast_window, preferred.width(), preferred.height()); |
| 287 |
| 288 // Time to show the window in a modal loop. |
| 289 popup_->Show(); |
| 290 if (!listener.is_null()) |
| 291 listener.Run(popup_->GetNativeView()); |
| 292 base::RunLoop().Run(); |
| 293 if (!listener.is_null()) |
| 294 listener.Run(NULL); |
| 295 return result_; |
| 296 } |
| 297 |
| 298 gfx::Rect TryChromeDialogView::ComputeWindowPosition(gfx::Size size, |
| 299 bool is_RTL) { |
| 300 // A best guess at a visible location in case all else fails. |
| 301 gfx::Point origin(20, 20); |
| 302 |
| 303 // The taskbar (the 'Shell_TrayWnd' window) is always on the primary monitor. |
| 304 constexpr POINT kOrigin = {}; |
| 305 MONITORINFO info = {sizeof(info)}; |
| 306 if (::GetMonitorInfo(::MonitorFromPoint(kOrigin, MONITOR_DEFAULTTOPRIMARY), |
| 307 &info)) { |
| 308 // |rcWork| is the work area, accounting for the visible taskbars. |
| 309 origin.set_x(is_RTL ? info.rcWork.left : info.rcWork.right - size.width()); |
| 310 origin.set_y(info.rcWork.bottom - size.height()); |
| 311 } |
| 312 |
| 313 return gfx::Rect(origin, size); |
| 314 } |
| 315 |
| 316 void TryChromeDialogView::SetToastRegion(HWND window, int w, int h) { |
| 317 static const POINT polygon[] = { |
| 318 {0, 4}, {1, 2}, {2, 1}, {4, 0}, // Left side. |
| 319 {w-4, 0}, {w-2, 1}, {w-1, 2}, {w, 4}, // Right side. |
| 320 {w, h}, {0, h} |
| 321 }; |
| 322 HRGN region = ::CreatePolygonRgn(polygon, arraysize(polygon), WINDING); |
| 323 ::SetWindowRgn(window, region, FALSE); |
| 324 } |
| 325 |
| 326 void TryChromeDialogView::ButtonPressed(views::Button* sender, |
| 327 const ui::Event& event) { |
| 328 if (sender->tag() == BT_DONT_BUG_RADIO) { |
| 329 if (make_default_) { |
| 330 make_default_->SetChecked(false); |
| 331 make_default_->SetVisible(false); |
| 332 } |
| 333 return; |
| 334 } else if (sender->tag() == BT_TRY_IT_RADIO) { |
| 335 if (make_default_) { |
| 336 make_default_->SetVisible(true); |
| 337 make_default_->SetChecked(true); |
| 338 } |
| 339 return; |
| 340 } else if (sender->tag() == BT_CLOSE_BUTTON) { |
| 341 // The user pressed cancel or the [x] button. |
| 342 result_ = NOT_NOW; |
| 343 } else if (!try_chrome_) { |
| 344 // We don't have radio buttons, the user pressed ok. |
| 345 result_ = TRY_CHROME; |
| 346 } else { |
| 347 // The outcome is according to the selected radio button. |
| 348 if (try_chrome_->checked()) |
| 349 result_ = TRY_CHROME; |
| 350 else if (dont_try_chrome_ && dont_try_chrome_->checked()) |
| 351 result_ = NOT_NOW; |
| 352 else if (kill_chrome_ && kill_chrome_->checked()) |
| 353 result_ = UNINSTALL_CHROME; |
| 354 else |
| 355 NOTREACHED() << "Unknown radio button selected"; |
| 356 } |
| 357 |
| 358 if (make_default_) { |
| 359 if ((result_ == TRY_CHROME) && make_default_->checked()) |
| 360 result_ = TRY_CHROME_AS_DEFAULT; |
| 361 } |
| 362 |
| 363 popup_->Close(); |
| 364 base::MessageLoop::current()->QuitWhenIdle(); |
| 365 } |
| 366 |
| 367 void TryChromeDialogView::LinkClicked(views::Link* source, int event_flags) { |
| 368 ::ShellExecuteW(NULL, L"open", kHelpCenterUrl, NULL, NULL, SW_SHOW); |
| 369 } |
OLD | NEW |