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