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 6bc49908f8b0851d779590bbe12861f1806b216b..3192f8b4bfe9ba1cef05121b3f5c968977d1497f 100644 |
--- a/chrome/browser/ui/ash/palette_delegate_chromeos.cc |
+++ b/chrome/browser/ui/ash/palette_delegate_chromeos.cc |
@@ -9,22 +9,27 @@ |
#include "ash/screenshot_delegate.h" |
#include "ash/shell.h" |
#include "ash/utility/screenshot_controller.h" |
+#include "chrome/browser/chrome_notification_types.h" |
#include "chrome/browser/chromeos/note_taking_app_utils.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" |
#include "ui/events/devices/input_device_manager.h" |
namespace chromeos { |
-namespace { |
- |
-Profile* GetProfile() { |
- return ProfileManager::GetActiveUserProfile(); |
-} |
- |
-} // namespace |
PaletteDelegateChromeOS::PaletteDelegateChromeOS() { |
+ registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
+ content::NotificationService::AllSources()); |
+ registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
+ content::NotificationService::AllSources()); |
+ |
ui::InputDeviceManager::GetInstance()->AddObserver(this); |
} |
@@ -32,12 +37,83 @@ PaletteDelegateChromeOS::~PaletteDelegateChromeOS() { |
ui::InputDeviceManager::GetInstance()->RemoveObserver(this); |
} |
+std::unique_ptr<PaletteDelegateChromeOS::EnableListenerSubscription> |
+PaletteDelegateChromeOS::AddPaletteEnableListener( |
+ const EnableListener& on_state_changed) { |
+ auto subscription = palette_enabled_callback_list_.Add(on_state_changed); |
+ OnPaletteEnabledPrefChanged(); |
+ return subscription; |
+} |
+ |
void PaletteDelegateChromeOS::CreateNote() { |
- chromeos::LaunchNoteTakingAppForNewNote(GetProfile(), base::FilePath()); |
+ if (!profile_) |
+ return; |
+ |
+ chromeos::LaunchNoteTakingAppForNewNote(profile_, base::FilePath()); |
} |
bool PaletteDelegateChromeOS::HasNoteApp() { |
- return chromeos::IsNoteTakingAppAvailable(GetProfile()); |
+ if (!profile_) |
+ return false; |
+ |
+ return chromeos::IsNoteTakingAppAvailable(profile_); |
+} |
+ |
+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_) { |
+ palette_enabled_callback_list_.Notify( |
+ profile_->GetPrefs()->GetBoolean(prefs::kEnableStylusTools)); |
+ } |
+} |
+ |
+void PaletteDelegateChromeOS::SetProfile(Profile* profile) { |
+ profile_ = profile; |
+ pref_change_registrar_.reset(); |
+ if (!profile_) |
+ return; |
+ |
+ PrefService* prefs = profile_->GetPrefs(); |
+ pref_change_registrar_.reset(new PrefChangeRegistrar); |
+ pref_change_registrar_->Init(prefs); |
+ pref_change_registrar_->Add( |
+ prefs::kEnableStylusTools, |
+ base::Bind(&PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged, |
+ base::Unretained(this))); |
+ |
+ // Run listener with new pref value, if any. |
+ OnPaletteEnabledPrefChanged(); |
} |
void PaletteDelegateChromeOS::SetPartialMagnifierState(bool enabled) { |
@@ -52,8 +128,10 @@ void PaletteDelegateChromeOS::SetStylusStateChangedCallback( |
} |
bool PaletteDelegateChromeOS::ShouldAutoOpenPalette() { |
- return GetProfile()->GetPrefs()->GetBoolean( |
- prefs::kLaunchPaletteOnEjectEvent); |
+ if (!profile_) |
+ return false; |
+ |
+ return profile_->GetPrefs()->GetBoolean(prefs::kLaunchPaletteOnEjectEvent); |
} |
void PaletteDelegateChromeOS::TakeScreenshot() { |