Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/ui/ash/palette_delegate_chromeos.h" | 5 #include "chrome/browser/ui/ash/palette_delegate_chromeos.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | |
| 8 #include "chrome/browser/chrome_notification_types.h" | |
| 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/profiles/profile_manager.h" | |
| 12 #include "chrome/common/pref_names.h" | |
| 13 #include "components/prefs/pref_change_registrar.h" | |
| 14 #include "components/prefs/pref_service.h" | |
| 15 #include "components/user_manager/user_manager.h" | |
| 16 #include "content/public/browser/notification_service.h" | |
| 17 #include "content/public/browser/notification_source.h" | |
| 18 | |
| 7 namespace chromeos { | 19 namespace chromeos { |
| 8 | 20 |
| 9 PaletteDelegateChromeOS::PaletteDelegateChromeOS() {} | 21 PaletteDelegateChromeOS::PaletteDelegateChromeOS() { |
| 22 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, | |
| 23 content::NotificationService::AllSources()); | |
| 24 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
| 25 content::NotificationService::AllSources()); | |
| 26 } | |
| 10 | 27 |
| 11 PaletteDelegateChromeOS::~PaletteDelegateChromeOS() {} | 28 PaletteDelegateChromeOS::~PaletteDelegateChromeOS() {} |
| 12 | 29 |
| 30 std::unique_ptr<PaletteDelegateChromeOS::EnableListenerSubscription> | |
| 31 PaletteDelegateChromeOS::AddPaletteEnableListener( | |
| 32 const EnableListener& on_state_changed) { | |
| 33 auto subscription = callback_list_.Add(on_state_changed); | |
| 34 OnPaletteEnabledPrefChanged(); | |
| 35 return subscription; | |
| 36 } | |
| 37 | |
| 38 void PaletteDelegateChromeOS::ActiveUserChanged(const AccountId& account_id) { | |
| 39 const user_manager::User* user = | |
| 40 user_manager::UserManager::Get()->FindUser(account_id); | |
| 41 Profile* profile = ProfileHelper::Get()->GetProfileByUser(user); | |
| 42 SetProfile(profile); | |
| 43 } | |
| 44 | |
| 45 void PaletteDelegateChromeOS::Observe( | |
| 46 int type, | |
| 47 const content::NotificationSource& source, | |
| 48 const content::NotificationDetails& details) { | |
| 49 switch (type) { | |
| 50 case chrome::NOTIFICATION_SESSION_STARTED: | |
| 51 // Update |profile_| when entering a session. | |
| 52 SetProfile(ProfileManager::GetActiveUserProfile()); | |
| 53 | |
| 54 // Add a session state observer to be able to monitor session changes. | |
| 55 if (!session_state_observer_.get() && ash::Shell::HasInstance()) { | |
| 56 session_state_observer_.reset( | |
| 57 new ash::ScopedSessionStateObserver(this)); | |
| 58 } | |
| 59 break; | |
| 60 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | |
| 61 // Update |profile_| when exiting a session or shutting down. | |
| 62 Profile* profile = content::Source<Profile>(source).ptr(); | |
| 63 if (profile_ == profile) | |
| 64 SetProfile(nullptr); | |
| 65 break; | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged() { | |
| 71 if (profile_) { | |
| 72 callback_list_.Notify( | |
| 73 profile_->GetPrefs()->GetBoolean(prefs::kEnableAshPalette)); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void PaletteDelegateChromeOS::SetProfile(Profile* profile) { | |
| 78 profile_ = profile; | |
| 79 pref_change_registrar_.reset(); | |
| 80 | |
| 81 if (profile_) { | |
|
stevenjb
2016/08/23 17:36:41
if (!profile_) return;
jdufault
2016/08/23 22:49:56
Done.
| |
| 82 PrefService* prefs = profile_->GetPrefs(); | |
| 83 pref_change_registrar_.reset(new PrefChangeRegistrar); | |
| 84 pref_change_registrar_->Init(prefs); | |
| 85 pref_change_registrar_->Add( | |
| 86 prefs::kEnableAshPalette, | |
| 87 base::Bind(&PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged, | |
| 88 base::Unretained(this))); | |
| 89 | |
| 90 // Run listener with new pref value, if any. | |
| 91 OnPaletteEnabledPrefChanged(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 13 } // namespace chromeos | 95 } // namespace chromeos |
| OLD | NEW |