locale_update_controller_impl.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "ash/system/locale/locale_update_controller_impl.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "ash/constants/notifier_catalogs.h"
  9. #include "ash/public/cpp/notification_utils.h"
  10. #include "ash/session/session_controller_impl.h"
  11. #include "ash/shell.h"
  12. #include "ash/strings/grit/ash_strings.h"
  13. #include "ash/system/tray/system_tray_notifier.h"
  14. #include "components/session_manager/session_manager_types.h"
  15. #include "components/vector_icons/vector_icons.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/message_center/message_center.h"
  18. #include "ui/message_center/public/cpp/notification.h"
  19. #include "ui/message_center/public/cpp/notification_delegate.h"
  20. #include "ui/message_center/public/cpp/notification_types.h"
  21. using message_center::Notification;
  22. using session_manager::SessionState;
  23. namespace ash {
  24. namespace {
  25. const char kLocaleChangeNotificationId[] = "chrome://settings/locale";
  26. const char kNotifierLocale[] = "ash.locale";
  27. class LocaleNotificationDelegate : public message_center::NotificationDelegate {
  28. public:
  29. explicit LocaleNotificationDelegate(
  30. base::OnceCallback<void(LocaleNotificationResult)> callback);
  31. LocaleNotificationDelegate(const LocaleNotificationDelegate&) = delete;
  32. LocaleNotificationDelegate& operator=(const LocaleNotificationDelegate&) =
  33. delete;
  34. protected:
  35. ~LocaleNotificationDelegate() override;
  36. // message_center::NotificationDelegate overrides:
  37. void Close(bool by_user) override;
  38. void Click(const absl::optional<int>& button_index,
  39. const absl::optional<std::u16string>& reply) override;
  40. private:
  41. base::OnceCallback<void(LocaleNotificationResult)> callback_;
  42. };
  43. LocaleNotificationDelegate::LocaleNotificationDelegate(
  44. base::OnceCallback<void(LocaleNotificationResult)> callback)
  45. : callback_(std::move(callback)) {}
  46. LocaleNotificationDelegate::~LocaleNotificationDelegate() {
  47. if (callback_) {
  48. // We're being destroyed but the user didn't click on anything. Run the
  49. // callback so that we don't crash.
  50. std::move(callback_).Run(LocaleNotificationResult::kAccept);
  51. }
  52. }
  53. void LocaleNotificationDelegate::Close(bool by_user) {
  54. if (callback_) {
  55. std::move(callback_).Run(LocaleNotificationResult::kAccept);
  56. }
  57. }
  58. void LocaleNotificationDelegate::Click(
  59. const absl::optional<int>& button_index,
  60. const absl::optional<std::u16string>& reply) {
  61. if (!callback_)
  62. return;
  63. std::move(callback_).Run(button_index ? LocaleNotificationResult::kRevert
  64. : LocaleNotificationResult::kAccept);
  65. message_center::MessageCenter::Get()->RemoveNotification(
  66. kLocaleChangeNotificationId, true /* by_user */);
  67. }
  68. } // namespace
  69. LocaleUpdateControllerImpl::LocaleUpdateControllerImpl() = default;
  70. LocaleUpdateControllerImpl::~LocaleUpdateControllerImpl() = default;
  71. void LocaleUpdateControllerImpl::OnLocaleChanged() {
  72. for (auto& observer : observers_)
  73. observer.OnLocaleChanged();
  74. }
  75. void LocaleUpdateControllerImpl::ConfirmLocaleChange(
  76. const std::string& current_locale,
  77. const std::string& from_locale,
  78. const std::string& to_locale,
  79. LocaleChangeConfirmationCallback callback) {
  80. DCHECK(Shell::Get()->session_controller()->IsActiveUserSessionStarted());
  81. std::u16string from_locale_name =
  82. l10n_util::GetDisplayNameForLocale(from_locale, current_locale, true);
  83. std::u16string to_locale_name =
  84. l10n_util::GetDisplayNameForLocale(to_locale, current_locale, true);
  85. message_center::RichNotificationData optional;
  86. optional.buttons.push_back(
  87. message_center::ButtonInfo(l10n_util::GetStringFUTF16(
  88. IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from_locale_name)));
  89. optional.never_timeout = true;
  90. for (auto& observer : observers_)
  91. observer.OnLocaleChanged();
  92. std::unique_ptr<Notification> notification = CreateSystemNotification(
  93. message_center::NOTIFICATION_TYPE_SIMPLE, kLocaleChangeNotificationId,
  94. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_TITLE),
  95. l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE,
  96. from_locale_name, to_locale_name),
  97. std::u16string() /* display_source */, GURL(),
  98. message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
  99. kNotifierLocale,
  100. NotificationCatalogName::kLocaleUpdate),
  101. optional, new LocaleNotificationDelegate(std::move(callback)),
  102. vector_icons::kSettingsIcon,
  103. message_center::SystemNotificationWarningLevel::NORMAL);
  104. message_center::MessageCenter::Get()->AddNotification(
  105. std::move(notification));
  106. }
  107. void LocaleUpdateControllerImpl::AddObserver(LocaleChangeObserver* observer) {
  108. observers_.AddObserver(observer);
  109. }
  110. void LocaleUpdateControllerImpl::RemoveObserver(
  111. LocaleChangeObserver* observer) {
  112. observers_.RemoveObserver(observer);
  113. }
  114. } // namespace ash