Chromium Code Reviews| Index: chrome/browser/ui/ash/palette_delegate_chromeos.cc |
| diff --git a/chrome/browser/ui/ash/palette_delegate_chromeos.cc b/chrome/browser/ui/ash/palette_delegate_chromeos.cc |
| index 0342a30c08b6f48a81a9f6cb3efed1d5a9b91b31..a68c7b34032ab3430d617cb710a7bb1a809378ad 100644 |
| --- a/chrome/browser/ui/ash/palette_delegate_chromeos.cc |
| +++ b/chrome/browser/ui/ash/palette_delegate_chromeos.cc |
| @@ -4,10 +4,92 @@ |
| #include "chrome/browser/ui/ash/palette_delegate_chromeos.h" |
| +#include "ash/shell.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/chromeos/profiles/profile_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "components/prefs/pref_change_registrar.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/user_manager/user_manager.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| + |
| namespace chromeos { |
| -PaletteDelegateChromeOS::PaletteDelegateChromeOS() {} |
| +PaletteDelegateChromeOS::PaletteDelegateChromeOS() { |
| + registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
| + content::NotificationService::AllSources()); |
| + registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| + content::NotificationService::AllSources()); |
| +} |
| PaletteDelegateChromeOS::~PaletteDelegateChromeOS() {} |
| +std::unique_ptr<PaletteDelegateChromeOS::EnableListenerSubscription> |
| +PaletteDelegateChromeOS::AddPaletteEnableListener( |
| + const EnableListener& on_state_changed) { |
| + auto subscription = callback_list_.Add(on_state_changed); |
| + OnPaletteEnabledPrefChanged(); |
| + return subscription; |
| +} |
| + |
| +void PaletteDelegateChromeOS::ActiveUserChanged(const AccountId& account_id) { |
| + const user_manager::User* user = |
| + user_manager::UserManager::Get()->FindUser(account_id); |
| + Profile* profile = ProfileHelper::Get()->GetProfileByUser(user); |
| + SetProfile(profile); |
| +} |
| + |
| +void PaletteDelegateChromeOS::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + switch (type) { |
| + case chrome::NOTIFICATION_SESSION_STARTED: |
| + // Update |profile_| when entering a session. |
| + SetProfile(ProfileManager::GetActiveUserProfile()); |
| + |
| + // Add a session state observer to be able to monitor session changes. |
| + if (!session_state_observer_.get() && ash::Shell::HasInstance()) { |
| + session_state_observer_.reset( |
| + new ash::ScopedSessionStateObserver(this)); |
| + } |
| + break; |
| + case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| + // Update |profile_| when exiting a session or shutting down. |
| + Profile* profile = content::Source<Profile>(source).ptr(); |
| + if (profile_ == profile) |
| + SetProfile(nullptr); |
| + break; |
| + } |
| + } |
| +} |
| + |
| +void PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged() { |
| + if (profile_) { |
| + callback_list_.Notify( |
| + profile_->GetPrefs()->GetBoolean(prefs::kEnableAshPalette)); |
| + } |
| +} |
| + |
| +void PaletteDelegateChromeOS::SetProfile(Profile* profile) { |
| + profile_ = profile; |
| + pref_change_registrar_.reset(); |
| + |
| + if (profile_) { |
|
stevenjb
2016/08/23 17:36:41
if (!profile_) return;
jdufault
2016/08/23 22:49:56
Done.
|
| + PrefService* prefs = profile_->GetPrefs(); |
| + pref_change_registrar_.reset(new PrefChangeRegistrar); |
| + pref_change_registrar_->Init(prefs); |
| + pref_change_registrar_->Add( |
| + prefs::kEnableAshPalette, |
| + base::Bind(&PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged, |
| + base::Unretained(this))); |
| + |
| + // Run listener with new pref value, if any. |
| + OnPaletteEnabledPrefChanged(); |
| + } |
| +} |
| + |
| } // namespace chromeos |