OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/signin/easy_unlock_notification_controller_chromeos.h" |
| 6 |
| 7 #include "ash/system/devicetype_utils.h" |
| 8 #include "base/guid.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/ui/chrome_pages.h" |
| 12 #include "chrome/grit/generated_resources.h" |
| 13 #include "chrome/grit/theme_resources.h" |
| 14 #include "components/proximity_auth/screenlock_bridge.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/message_center/message_center_types.h" |
| 18 #include "ui/message_center/notification_types.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kEasyUnlockChromebookAddedNotifierId[] = |
| 23 "easyunlock_notification_ids.chromebook_added"; |
| 24 |
| 25 const char kEasyUnlockPairingChangeNotifierId[] = |
| 26 "easyunlock_notification_ids.pairing_change"; |
| 27 |
| 28 const char kEasyUnlockPairingChangeAppliedNotifierId[] = |
| 29 "easyunlock_notification_ids.pairing_change_applied"; |
| 30 |
| 31 const char kLockScreenSettingsSubpage[] = "lockScreen"; |
| 32 |
| 33 // Convenience function for creating a Notification. |
| 34 std::unique_ptr<message_center::Notification> CreateNotification( |
| 35 const std::string& id, |
| 36 const base::string16& title, |
| 37 const base::string16& message, |
| 38 const gfx::Image& icon, |
| 39 const message_center::RichNotificationData& rich_notification_data, |
| 40 message_center::NotificationDelegate* delegate) { |
| 41 return base::MakeUnique<message_center::Notification>( |
| 42 message_center::NotificationType::NOTIFICATION_TYPE_SIMPLE, id, title, |
| 43 message, icon, base::string16() /* display_source */, |
| 44 GURL() /* origin_url */, |
| 45 message_center::NotifierId( |
| 46 message_center::NotifierId::NotifierType::SYSTEM_COMPONENT, id), |
| 47 rich_notification_data, delegate); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 EasyUnlockNotificationControllerChromeOS:: |
| 53 EasyUnlockNotificationControllerChromeOS( |
| 54 Profile* profile, |
| 55 message_center::MessageCenter* message_center) |
| 56 : profile_(profile), |
| 57 message_center_(message_center), |
| 58 weak_ptr_factory_(this) {} |
| 59 |
| 60 EasyUnlockNotificationControllerChromeOS:: |
| 61 ~EasyUnlockNotificationControllerChromeOS() {} |
| 62 |
| 63 void EasyUnlockNotificationControllerChromeOS:: |
| 64 ShowChromebookAddedNotification() { |
| 65 message_center::RichNotificationData rich_notification_data; |
| 66 rich_notification_data.buttons.push_back( |
| 67 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
| 68 IDS_EASY_UNLOCK_CHROMEBOOK_ADDED_NOTIFICATION_ABOUT_BUTTON))); |
| 69 |
| 70 ShowNotification(CreateNotification( |
| 71 kEasyUnlockChromebookAddedNotifierId, |
| 72 l10n_util::GetStringUTF16( |
| 73 IDS_EASY_UNLOCK_CHROMEBOOK_ADDED_NOTIFICATION_TITLE), |
| 74 l10n_util::GetStringFUTF16( |
| 75 IDS_EASY_UNLOCK_CHROMEBOOK_ADDED_NOTIFICATION_MESSAGE, |
| 76 ash::GetChromeOSDeviceName()), |
| 77 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 78 IDR_NOTIFICATION_EASYUNLOCK_ENABLED), |
| 79 rich_notification_data, |
| 80 new NotificationDelegate(kEasyUnlockChromebookAddedNotifierId, |
| 81 weak_ptr_factory_.GetWeakPtr()))); |
| 82 } |
| 83 |
| 84 void EasyUnlockNotificationControllerChromeOS::ShowPairingChangeNotification() { |
| 85 message_center::RichNotificationData rich_notification_data; |
| 86 rich_notification_data.buttons.push_back( |
| 87 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
| 88 IDS_EASY_UNLOCK_PAIRING_CHANGED_NOTIFICATION_UPDATE_BUTTON))); |
| 89 rich_notification_data.buttons.push_back( |
| 90 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
| 91 IDS_EASY_UNLOCK_CHROMEBOOK_ADDED_NOTIFICATION_ABOUT_BUTTON))); |
| 92 |
| 93 ShowNotification(CreateNotification( |
| 94 kEasyUnlockPairingChangeNotifierId, |
| 95 l10n_util::GetStringUTF16( |
| 96 IDS_EASY_UNLOCK_PAIRING_CHANGED_NOTIFICATION_TITLE), |
| 97 l10n_util::GetStringFUTF16( |
| 98 IDS_EASY_UNLOCK_PAIRING_CHANGED_NOTIFICATION_MESSAGE, |
| 99 ash::GetChromeOSDeviceName()), |
| 100 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 101 IDR_NOTIFICATION_EASYUNLOCK_ENABLED), |
| 102 rich_notification_data, |
| 103 new NotificationDelegate(kEasyUnlockPairingChangeNotifierId, |
| 104 weak_ptr_factory_.GetWeakPtr()))); |
| 105 } |
| 106 |
| 107 void EasyUnlockNotificationControllerChromeOS:: |
| 108 ShowPairingChangeAppliedNotification(const std::string& phone_name) { |
| 109 // Remove the pairing change notification if it is still being shown. |
| 110 message_center_->RemoveNotification(kEasyUnlockPairingChangeNotifierId, |
| 111 false /* by_user */); |
| 112 |
| 113 message_center::RichNotificationData rich_notification_data; |
| 114 rich_notification_data.buttons.push_back( |
| 115 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
| 116 IDS_EASY_UNLOCK_CHROMEBOOK_ADDED_NOTIFICATION_ABOUT_BUTTON))); |
| 117 |
| 118 ShowNotification(CreateNotification( |
| 119 kEasyUnlockPairingChangeAppliedNotifierId, |
| 120 l10n_util::GetStringUTF16( |
| 121 IDS_EASY_UNLOCK_PAIRING_CHANGE_APPLIED_NOTIFICATION_TITLE), |
| 122 l10n_util::GetStringFUTF16( |
| 123 IDS_EASY_UNLOCK_PAIRING_CHANGE_APPLIED_NOTIFICATION_MESSAGE, |
| 124 base::UTF8ToUTF16(phone_name), ash::GetChromeOSDeviceName()), |
| 125 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 126 IDR_NOTIFICATION_EASYUNLOCK_ENABLED), |
| 127 rich_notification_data, |
| 128 new NotificationDelegate(kEasyUnlockPairingChangeAppliedNotifierId, |
| 129 weak_ptr_factory_.GetWeakPtr()))); |
| 130 } |
| 131 |
| 132 void EasyUnlockNotificationControllerChromeOS::ShowNotification( |
| 133 std::unique_ptr<message_center::Notification> notification) { |
| 134 notification->SetSystemPriority(); |
| 135 std::string notification_id = notification->id(); |
| 136 if (message_center_->FindVisibleNotificationById(notification_id)) { |
| 137 message_center_->UpdateNotification(notification_id, |
| 138 std::move(notification)); |
| 139 } else { |
| 140 message_center_->AddNotification(std::move(notification)); |
| 141 } |
| 142 } |
| 143 |
| 144 void EasyUnlockNotificationControllerChromeOS::LaunchEasyUnlockSettings() { |
| 145 chrome::ShowSettingsSubPageForProfile(profile_, kLockScreenSettingsSubpage); |
| 146 } |
| 147 |
| 148 void EasyUnlockNotificationControllerChromeOS::LockScreen() { |
| 149 proximity_auth::ScreenlockBridge::Get()->Lock(); |
| 150 } |
| 151 |
| 152 EasyUnlockNotificationControllerChromeOS::NotificationDelegate:: |
| 153 NotificationDelegate( |
| 154 const std::string& notification_id, |
| 155 const base::WeakPtr<EasyUnlockNotificationControllerChromeOS>& |
| 156 notification_controller) |
| 157 : notification_id_(notification_id), |
| 158 notification_controller_(notification_controller) {} |
| 159 |
| 160 EasyUnlockNotificationControllerChromeOS::NotificationDelegate:: |
| 161 ~NotificationDelegate() {} |
| 162 |
| 163 void EasyUnlockNotificationControllerChromeOS::NotificationDelegate::Click() { |
| 164 if (!notification_controller_) |
| 165 return; |
| 166 |
| 167 if (notification_id_ == kEasyUnlockChromebookAddedNotifierId || |
| 168 notification_id_ == kEasyUnlockPairingChangeAppliedNotifierId) |
| 169 notification_controller_->LaunchEasyUnlockSettings(); |
| 170 } |
| 171 |
| 172 void EasyUnlockNotificationControllerChromeOS::NotificationDelegate:: |
| 173 ButtonClick(int button_index) { |
| 174 if (!notification_controller_) |
| 175 return; |
| 176 |
| 177 if (notification_id_ == kEasyUnlockChromebookAddedNotifierId || |
| 178 notification_id_ == kEasyUnlockPairingChangeAppliedNotifierId) { |
| 179 notification_controller_->LaunchEasyUnlockSettings(); |
| 180 } else if (notification_id_ == kEasyUnlockPairingChangeNotifierId) { |
| 181 if (button_index == 0) { |
| 182 notification_controller_->LockScreen(); |
| 183 } else if (button_index == 1) { |
| 184 notification_controller_->LaunchEasyUnlockSettings(); |
| 185 } |
| 186 } |
| 187 } |
OLD | NEW |