| OLD | NEW |
| (Empty) |
| 1 // Copyright 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. | |
| 4 | |
| 5 #include "ash/system/locale/locale_notification_controller.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "ash/common/wm_shell.h" | |
| 11 #include "ash/resources/grit/ash_resources.h" | |
| 12 #include "ash/strings/grit/ash_strings.h" | |
| 13 #include "ash/system/system_notifier.h" | |
| 14 #include "ash/system/tray/system_tray_notifier.h" | |
| 15 #include "base/strings/string16.h" | |
| 16 #include "ui/base/l10n/l10n_util.h" | |
| 17 #include "ui/base/resource/resource_bundle.h" | |
| 18 #include "ui/message_center/message_center.h" | |
| 19 #include "ui/message_center/notification.h" | |
| 20 #include "ui/message_center/notification_delegate.h" | |
| 21 #include "ui/message_center/notification_types.h" | |
| 22 | |
| 23 using message_center::Notification; | |
| 24 | |
| 25 namespace ash { | |
| 26 namespace { | |
| 27 | |
| 28 const char kLocaleChangeNotificationId[] = "chrome://settings/locale"; | |
| 29 | |
| 30 class LocaleNotificationDelegate : public message_center::NotificationDelegate { | |
| 31 public: | |
| 32 explicit LocaleNotificationDelegate( | |
| 33 const base::Callback<void(ash::mojom::LocaleNotificationResult)>& | |
| 34 callback); | |
| 35 | |
| 36 protected: | |
| 37 ~LocaleNotificationDelegate() override; | |
| 38 | |
| 39 // message_center::NotificationDelegate overrides: | |
| 40 void Close(bool by_user) override; | |
| 41 bool HasClickedListener() override; | |
| 42 void Click() override; | |
| 43 void ButtonClick(int button_index) override; | |
| 44 | |
| 45 private: | |
| 46 base::Callback<void(ash::mojom::LocaleNotificationResult)> callback_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(LocaleNotificationDelegate); | |
| 49 }; | |
| 50 | |
| 51 LocaleNotificationDelegate::LocaleNotificationDelegate( | |
| 52 const base::Callback<void(ash::mojom::LocaleNotificationResult)>& callback) | |
| 53 : callback_(callback) {} | |
| 54 | |
| 55 LocaleNotificationDelegate::~LocaleNotificationDelegate() { | |
| 56 if (callback_) { | |
| 57 // We're being destroyed but the user didn't click on anything. Run the | |
| 58 // callback so that we don't crash. | |
| 59 callback_.Run(ash::mojom::LocaleNotificationResult::ACCEPT); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void LocaleNotificationDelegate::Close(bool by_user) { | |
| 64 if (callback_) { | |
| 65 callback_.Run(ash::mojom::LocaleNotificationResult::ACCEPT); | |
| 66 callback_.Reset(); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 bool LocaleNotificationDelegate::HasClickedListener() { | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 void LocaleNotificationDelegate::Click() { | |
| 75 if (callback_) { | |
| 76 callback_.Run(ash::mojom::LocaleNotificationResult::ACCEPT); | |
| 77 callback_.Reset(); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void LocaleNotificationDelegate::ButtonClick(int button_index) { | |
| 82 DCHECK_EQ(0, button_index); | |
| 83 | |
| 84 if (callback_) { | |
| 85 callback_.Run(ash::mojom::LocaleNotificationResult::REVERT); | |
| 86 callback_.Reset(); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 LocaleNotificationController::LocaleNotificationController() {} | |
| 93 | |
| 94 LocaleNotificationController::~LocaleNotificationController() {} | |
| 95 | |
| 96 void LocaleNotificationController::BindRequest( | |
| 97 mojom::LocaleNotificationControllerRequest request) { | |
| 98 bindings_.AddBinding(this, std::move(request)); | |
| 99 } | |
| 100 | |
| 101 void LocaleNotificationController::OnLocaleChanged( | |
| 102 const std::string& cur_locale, | |
| 103 const std::string& from_locale, | |
| 104 const std::string& to_locale, | |
| 105 const OnLocaleChangedCallback& callback) { | |
| 106 base::string16 from = | |
| 107 l10n_util::GetDisplayNameForLocale(from_locale, cur_locale, true); | |
| 108 base::string16 to = | |
| 109 l10n_util::GetDisplayNameForLocale(to_locale, cur_locale, true); | |
| 110 | |
| 111 message_center::RichNotificationData optional; | |
| 112 optional.buttons.push_back( | |
| 113 message_center::ButtonInfo(l10n_util::GetStringFUTF16( | |
| 114 IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from))); | |
| 115 optional.never_timeout = true; | |
| 116 | |
| 117 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 118 std::unique_ptr<Notification> notification(new Notification( | |
| 119 message_center::NOTIFICATION_TYPE_SIMPLE, kLocaleChangeNotificationId, | |
| 120 base::string16() /* title */, | |
| 121 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, | |
| 122 from, to), | |
| 123 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE), | |
| 124 base::string16() /* display_source */, GURL(), | |
| 125 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 126 system_notifier::kNotifierLocale), | |
| 127 optional, new LocaleNotificationDelegate(callback))); | |
| 128 message_center::MessageCenter::Get()->AddNotification( | |
| 129 std::move(notification)); | |
| 130 } | |
| 131 | |
| 132 } // namespace ash | |
| OLD | NEW |