Chromium Code Reviews| 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_); | |
| 52 } | |
| 53 | 54 |
| 54 LocaleNotificationDelegate::~LocaleNotificationDelegate() {} | 55 LocaleNotificationDelegate::~LocaleNotificationDelegate() {} |
| 55 | 56 |
| 56 void LocaleNotificationDelegate::Close(bool by_user) { | 57 void LocaleNotificationDelegate::Close(bool by_user) { |
| 57 delegate_->AcceptLocaleChange(); | 58 callback_.Run(ash::mojom::LocaleNotificationResult::ACCEPT); |
|
sky
2016/10/18 16:58:03
I'm not familiar with the code in this class. Are
Elliot Glaysher
2016/10/18 19:41:57
Done. (Added check in dtor, too.)
| |
| 58 } | 59 } |
| 59 | 60 |
| 60 bool LocaleNotificationDelegate::HasClickedListener() { | 61 bool LocaleNotificationDelegate::HasClickedListener() { |
| 61 return true; | 62 return true; |
| 62 } | 63 } |
| 63 | 64 |
| 64 void LocaleNotificationDelegate::Click() { | 65 void LocaleNotificationDelegate::Click() { |
| 65 delegate_->AcceptLocaleChange(); | 66 callback_.Run(ash::mojom::LocaleNotificationResult::ACCEPT); |
| 66 } | 67 } |
| 67 | 68 |
| 68 void LocaleNotificationDelegate::ButtonClick(int button_index) { | 69 void LocaleNotificationDelegate::ButtonClick(int button_index) { |
| 69 DCHECK_EQ(0, button_index); | 70 DCHECK_EQ(0, button_index); |
| 70 delegate_->RevertLocaleChange(); | 71 callback_.Run(ash::mojom::LocaleNotificationResult::REVERT); |
| 71 } | 72 } |
| 72 | 73 |
| 73 } // namespace | 74 } // namespace |
| 74 | 75 |
| 75 LocaleNotificationController::LocaleNotificationController() { | 76 LocaleNotificationController::LocaleNotificationController() {} |
| 76 WmShell::Get()->system_tray_notifier()->AddLocaleObserver(this); | |
| 77 } | |
| 78 | 77 |
| 79 LocaleNotificationController::~LocaleNotificationController() { | 78 LocaleNotificationController::~LocaleNotificationController() {} |
| 80 WmShell::Get()->system_tray_notifier()->RemoveLocaleObserver(this); | 79 |
| 80 void LocaleNotificationController::BindRequest( | |
| 81 mojom::LocaleNotificationControllerRequest request) { | |
| 82 bindings_.AddBinding(this, std::move(request)); | |
| 81 } | 83 } |
| 82 | 84 |
| 83 void LocaleNotificationController::OnLocaleChanged( | 85 void LocaleNotificationController::OnLocaleChanged( |
| 84 LocaleObserver::Delegate* delegate, | |
| 85 const std::string& cur_locale, | 86 const std::string& cur_locale, |
| 86 const std::string& from_locale, | 87 const std::string& from_locale, |
| 87 const std::string& to_locale) { | 88 const std::string& to_locale, |
| 88 if (!delegate) | 89 const OnLocaleChangedCallback& callback) { |
| 89 return; | |
| 90 | |
| 91 base::string16 from = | 90 base::string16 from = |
| 92 l10n_util::GetDisplayNameForLocale(from_locale, cur_locale, true); | 91 l10n_util::GetDisplayNameForLocale(from_locale, cur_locale, true); |
| 93 base::string16 to = | 92 base::string16 to = |
| 94 l10n_util::GetDisplayNameForLocale(to_locale, cur_locale, true); | 93 l10n_util::GetDisplayNameForLocale(to_locale, cur_locale, true); |
| 95 | 94 |
| 96 message_center::RichNotificationData optional; | 95 message_center::RichNotificationData optional; |
| 97 optional.buttons.push_back( | 96 optional.buttons.push_back( |
| 98 message_center::ButtonInfo(l10n_util::GetStringFUTF16( | 97 message_center::ButtonInfo(l10n_util::GetStringFUTF16( |
| 99 IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from))); | 98 IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from))); |
| 100 optional.never_timeout = true; | 99 optional.never_timeout = true; |
| 101 | 100 |
| 102 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 101 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 103 std::unique_ptr<Notification> notification(new Notification( | 102 std::unique_ptr<Notification> notification(new Notification( |
| 104 message_center::NOTIFICATION_TYPE_SIMPLE, kLocaleChangeNotificationId, | 103 message_center::NOTIFICATION_TYPE_SIMPLE, kLocaleChangeNotificationId, |
| 105 base::string16() /* title */, | 104 base::string16() /* title */, |
| 106 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, | 105 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, |
| 107 from, to), | 106 from, to), |
| 108 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE), | 107 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE), |
| 109 base::string16() /* display_source */, GURL(), | 108 base::string16() /* display_source */, GURL(), |
| 110 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | 109 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 111 system_notifier::kNotifierLocale), | 110 system_notifier::kNotifierLocale), |
| 112 optional, new LocaleNotificationDelegate(delegate))); | 111 optional, new LocaleNotificationDelegate(callback))); |
| 113 message_center::MessageCenter::Get()->AddNotification( | 112 message_center::MessageCenter::Get()->AddNotification( |
| 114 std::move(notification)); | 113 std::move(notification)); |
| 115 } | 114 } |
| 116 | 115 |
| 117 } // namespace ash | 116 } // namespace ash |
| OLD | NEW |