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/accelerators/accelerator_controller_delegate_aura.h" | 7 #include "ash/accelerators/accelerator_controller_delegate_aura.h" |
8 #include "ash/magnifier/partial_magnification_controller.h" | 8 #include "ash/magnifier/partial_magnification_controller.h" |
9 #include "ash/screenshot_delegate.h" | 9 #include "ash/screenshot_delegate.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "ash/utility/screenshot_controller.h" | 11 #include "ash/utility/screenshot_controller.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/chromeos/note_taking_app_utils.h" | 13 #include "chrome/browser/chromeos/note_taking_app_utils.h" |
| 14 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "components/prefs/pref_change_registrar.h" |
15 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 20 #include "components/user_manager/user_manager.h" |
| 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/notification_source.h" |
16 #include "ui/events/devices/input_device_manager.h" | 23 #include "ui/events/devices/input_device_manager.h" |
17 | 24 |
18 namespace chromeos { | 25 namespace chromeos { |
19 namespace { | |
20 | |
21 Profile* GetProfile() { | |
22 return ProfileManager::GetActiveUserProfile(); | |
23 } | |
24 | |
25 } // namespace | |
26 | 26 |
27 PaletteDelegateChromeOS::PaletteDelegateChromeOS() { | 27 PaletteDelegateChromeOS::PaletteDelegateChromeOS() { |
| 28 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
| 29 content::NotificationService::AllSources()); |
| 30 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 31 content::NotificationService::AllSources()); |
| 32 |
28 ui::InputDeviceManager::GetInstance()->AddObserver(this); | 33 ui::InputDeviceManager::GetInstance()->AddObserver(this); |
29 } | 34 } |
30 | 35 |
31 PaletteDelegateChromeOS::~PaletteDelegateChromeOS() { | 36 PaletteDelegateChromeOS::~PaletteDelegateChromeOS() { |
32 ui::InputDeviceManager::GetInstance()->RemoveObserver(this); | 37 ui::InputDeviceManager::GetInstance()->RemoveObserver(this); |
33 } | 38 } |
34 | 39 |
| 40 std::unique_ptr<PaletteDelegateChromeOS::EnableListenerSubscription> |
| 41 PaletteDelegateChromeOS::AddPaletteEnableListener( |
| 42 const EnableListener& on_state_changed) { |
| 43 auto subscription = palette_enabled_callback_list_.Add(on_state_changed); |
| 44 OnPaletteEnabledPrefChanged(); |
| 45 return subscription; |
| 46 } |
| 47 |
35 void PaletteDelegateChromeOS::CreateNote() { | 48 void PaletteDelegateChromeOS::CreateNote() { |
36 chromeos::LaunchNoteTakingAppForNewNote(GetProfile(), base::FilePath()); | 49 if (!profile_) |
| 50 return; |
| 51 |
| 52 chromeos::LaunchNoteTakingAppForNewNote(profile_, base::FilePath()); |
37 } | 53 } |
38 | 54 |
39 bool PaletteDelegateChromeOS::HasNoteApp() { | 55 bool PaletteDelegateChromeOS::HasNoteApp() { |
40 return chromeos::IsNoteTakingAppAvailable(GetProfile()); | 56 if (!profile_) |
| 57 return false; |
| 58 |
| 59 return chromeos::IsNoteTakingAppAvailable(profile_); |
| 60 } |
| 61 |
| 62 void PaletteDelegateChromeOS::ActiveUserChanged(const AccountId& account_id) { |
| 63 const user_manager::User* user = |
| 64 user_manager::UserManager::Get()->FindUser(account_id); |
| 65 Profile* profile = ProfileHelper::Get()->GetProfileByUser(user); |
| 66 SetProfile(profile); |
| 67 } |
| 68 |
| 69 void PaletteDelegateChromeOS::Observe( |
| 70 int type, |
| 71 const content::NotificationSource& source, |
| 72 const content::NotificationDetails& details) { |
| 73 switch (type) { |
| 74 case chrome::NOTIFICATION_SESSION_STARTED: |
| 75 // Update |profile_| when entering a session. |
| 76 SetProfile(ProfileManager::GetActiveUserProfile()); |
| 77 |
| 78 // Add a session state observer to be able to monitor session changes. |
| 79 if (!session_state_observer_.get() && ash::Shell::HasInstance()) { |
| 80 session_state_observer_.reset( |
| 81 new ash::ScopedSessionStateObserver(this)); |
| 82 } |
| 83 break; |
| 84 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 85 // Update |profile_| when exiting a session or shutting down. |
| 86 Profile* profile = content::Source<Profile>(source).ptr(); |
| 87 if (profile_ == profile) |
| 88 SetProfile(nullptr); |
| 89 break; |
| 90 } |
| 91 } |
| 92 } |
| 93 |
| 94 void PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged() { |
| 95 if (profile_) { |
| 96 palette_enabled_callback_list_.Notify( |
| 97 profile_->GetPrefs()->GetBoolean(prefs::kEnableStylusTools)); |
| 98 } |
| 99 } |
| 100 |
| 101 void PaletteDelegateChromeOS::SetProfile(Profile* profile) { |
| 102 profile_ = profile; |
| 103 pref_change_registrar_.reset(); |
| 104 if (!profile_) |
| 105 return; |
| 106 |
| 107 PrefService* prefs = profile_->GetPrefs(); |
| 108 pref_change_registrar_.reset(new PrefChangeRegistrar); |
| 109 pref_change_registrar_->Init(prefs); |
| 110 pref_change_registrar_->Add( |
| 111 prefs::kEnableStylusTools, |
| 112 base::Bind(&PaletteDelegateChromeOS::OnPaletteEnabledPrefChanged, |
| 113 base::Unretained(this))); |
| 114 |
| 115 // Run listener with new pref value, if any. |
| 116 OnPaletteEnabledPrefChanged(); |
41 } | 117 } |
42 | 118 |
43 void PaletteDelegateChromeOS::SetPartialMagnifierState(bool enabled) { | 119 void PaletteDelegateChromeOS::SetPartialMagnifierState(bool enabled) { |
44 ash::PartialMagnificationController* controller = | 120 ash::PartialMagnificationController* controller = |
45 ash::Shell::GetInstance()->partial_magnification_controller(); | 121 ash::Shell::GetInstance()->partial_magnification_controller(); |
46 controller->SetEnabled(enabled); | 122 controller->SetEnabled(enabled); |
47 } | 123 } |
48 | 124 |
49 void PaletteDelegateChromeOS::SetStylusStateChangedCallback( | 125 void PaletteDelegateChromeOS::SetStylusStateChangedCallback( |
50 const OnStylusStateChangedCallback& on_stylus_state_changed) { | 126 const OnStylusStateChangedCallback& on_stylus_state_changed) { |
51 on_stylus_state_changed_ = on_stylus_state_changed; | 127 on_stylus_state_changed_ = on_stylus_state_changed; |
52 } | 128 } |
53 | 129 |
54 bool PaletteDelegateChromeOS::ShouldAutoOpenPalette() { | 130 bool PaletteDelegateChromeOS::ShouldAutoOpenPalette() { |
55 return GetProfile()->GetPrefs()->GetBoolean( | 131 if (!profile_) |
56 prefs::kLaunchPaletteOnEjectEvent); | 132 return false; |
| 133 |
| 134 return profile_->GetPrefs()->GetBoolean(prefs::kLaunchPaletteOnEjectEvent); |
57 } | 135 } |
58 | 136 |
59 void PaletteDelegateChromeOS::TakeScreenshot() { | 137 void PaletteDelegateChromeOS::TakeScreenshot() { |
60 auto* screenshot_delegate = ash::Shell::GetInstance() | 138 auto* screenshot_delegate = ash::Shell::GetInstance() |
61 ->accelerator_controller_delegate() | 139 ->accelerator_controller_delegate() |
62 ->screenshot_delegate(); | 140 ->screenshot_delegate(); |
63 screenshot_delegate->HandleTakeScreenshotForAllRootWindows(); | 141 screenshot_delegate->HandleTakeScreenshotForAllRootWindows(); |
64 } | 142 } |
65 | 143 |
66 void PaletteDelegateChromeOS::TakePartialScreenshot() { | 144 void PaletteDelegateChromeOS::TakePartialScreenshot() { |
67 auto* screenshot_controller = | 145 auto* screenshot_controller = |
68 ash::Shell::GetInstance()->screenshot_controller(); | 146 ash::Shell::GetInstance()->screenshot_controller(); |
69 auto* screenshot_delegate = ash::Shell::GetInstance() | 147 auto* screenshot_delegate = ash::Shell::GetInstance() |
70 ->accelerator_controller_delegate() | 148 ->accelerator_controller_delegate() |
71 ->screenshot_delegate(); | 149 ->screenshot_delegate(); |
72 | 150 |
73 screenshot_controller->set_pen_events_only(true); | 151 screenshot_controller->set_pen_events_only(true); |
74 screenshot_controller->StartPartialScreenshotSession( | 152 screenshot_controller->StartPartialScreenshotSession( |
75 screenshot_delegate, false /* draw_overlay_immediately */); | 153 screenshot_delegate, false /* draw_overlay_immediately */); |
76 } | 154 } |
77 | 155 |
78 void PaletteDelegateChromeOS::OnStylusStateChanged(ui::StylusState state) { | 156 void PaletteDelegateChromeOS::OnStylusStateChanged(ui::StylusState state) { |
79 on_stylus_state_changed_.Run(state); | 157 on_stylus_state_changed_.Run(state); |
80 } | 158 } |
81 | 159 |
82 } // namespace chromeos | 160 } // namespace chromeos |
OLD | NEW |