OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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/chromeos/login/quick_unlock/feature_notification_contro
ller.h" |
| 6 |
| 7 #include "ash/common/system/system_notifier.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 11 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 12 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser_navigator.h" |
| 15 #include "chrome/browser/ui/browser_navigator_params.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/common/url_constants.h" |
| 18 #include "components/prefs/pref_service.h" |
| 19 #include "components/user_manager/user.h" |
| 20 #include "components/user_manager/user_manager.h" |
| 21 #include "content/public/browser/notification_service.h" |
| 22 #include "grit/ash_strings.h" |
| 23 #include "grit/theme_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/strings/grit/ui_strings.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 const char kDelegateId[] = "quickunlock_delegate"; |
| 31 const char kNotificationId[] = "quickunlock_notification"; |
| 32 |
| 33 Profile* GetProfileFromAccountId(const AccountId& account_id) { |
| 34 const user_manager::User* user = |
| 35 user_manager::UserManager::Get()->FindUser(account_id); |
| 36 return chromeos::ProfileHelper::Get()->GetProfileByUser(user); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 namespace chromeos { |
| 42 namespace quickunlock { |
| 43 |
| 44 FeatureNotificationController::FeatureNotificationController( |
| 45 const AccountId& account_id) |
| 46 : profile_(GetProfileFromAccountId(account_id)), |
| 47 registrar_(new content::NotificationRegistrar) { |
| 48 registrar_->Add(this, chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, |
| 49 content::NotificationService::AllSources()); |
| 50 } |
| 51 |
| 52 FeatureNotificationController::~FeatureNotificationController() {} |
| 53 |
| 54 // static |
| 55 // TODO(http://crbug.com/291747): Add check for a policy that might disable |
| 56 // quick unlock. |
| 57 bool FeatureNotificationController::ShouldShowNotificationForAccountId( |
| 58 const AccountId& account_id) { |
| 59 Profile* profile = GetProfileFromAccountId(account_id); |
| 60 // Do not show notification if this is a guest session. |
| 61 if (profile->IsGuestSession()) |
| 62 return false; |
| 63 |
| 64 // Do not show notification to user if already displayed in the past. |
| 65 if (profile->GetPrefs()->GetBoolean( |
| 66 prefs::kQuickUnlockFeatureNotificationShown)) { |
| 67 return false; |
| 68 } |
| 69 return true; |
| 70 } |
| 71 |
| 72 // NotificationDelegate override: |
| 73 std::string FeatureNotificationController::id() const { |
| 74 return kDelegateId; |
| 75 } |
| 76 |
| 77 void FeatureNotificationController::Observe( |
| 78 int type, |
| 79 const content::NotificationSource& source, |
| 80 const content::NotificationDetails& details) { |
| 81 registrar_->Remove(this, chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, |
| 82 content::NotificationService::AllSources()); |
| 83 std::unique_ptr<Notification> notification(CreateNotification()); |
| 84 g_browser_process->notification_ui_manager()->Add(*notification, profile_); |
| 85 } |
| 86 |
| 87 // message_center::NotificationDelegate override: |
| 88 void FeatureNotificationController::Close(bool by_user) { |
| 89 if (by_user) |
| 90 UpdatePreferenceForProfile(profile_); |
| 91 } |
| 92 |
| 93 // message_center::NotificationDelegate override: |
| 94 void FeatureNotificationController::Click() { |
| 95 // TODO(http://crbug.com/291747): Redirect to specific checkbox location in |
| 96 // settings page. |
| 97 chrome::NavigateParams params(profile_, GURL(chrome::kChromeUISettingsURL), |
| 98 ui::PAGE_TRANSITION_LINK); |
| 99 params.disposition = NEW_FOREGROUND_TAB; |
| 100 params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 101 chrome::Navigate(¶ms); |
| 102 |
| 103 UpdatePreferenceForProfile(profile_); |
| 104 |
| 105 // Remove the notification from tray. |
| 106 g_browser_process->notification_ui_manager()->CancelById( |
| 107 id(), NotificationUIManager::GetProfileID(profile_)); |
| 108 } |
| 109 |
| 110 Notification* FeatureNotificationController::CreateNotification() { |
| 111 return new Notification( |
| 112 message_center::NOTIFICATION_TYPE_SIMPLE, |
| 113 l10n_util::GetStringUTF16(IDS_ASH_QUICK_UNLOCK_NOTIFICATION_TITLE), |
| 114 l10n_util::GetStringUTF16(IDS_ASH_QUICK_UNLOCK_NOTIFICATION_BODY), |
| 115 // TODO(http://crbug.com/291747): Change this to actual icon for |
| 116 // quick unlock feature notiifcation. |
| 117 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 118 IDR_SCREENSHOT_NOTIFICATION_ICON), |
| 119 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 120 ash::system_notifier::kNotifierQuickUnlock), |
| 121 l10n_util::GetStringUTF16( |
| 122 IDS_MESSAGE_CENTER_NOTIFIER_QUICK_UNLOCK_FEATURE_NAME), |
| 123 GURL(), kNotificationId, message_center::RichNotificationData(), this); |
| 124 } |
| 125 |
| 126 void FeatureNotificationController::UpdatePreferenceForProfile( |
| 127 Profile* profile) { |
| 128 PrefService* pref_service = profile->GetPrefs(); |
| 129 pref_service->SetBoolean(prefs::kQuickUnlockFeatureNotificationShown, true); |
| 130 } |
| 131 |
| 132 } // namespace quickunlock |
| 133 } // namespace chromeos |
OLD | NEW |