| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromeos/accessibility/magnification_manager.h" | 5 #include "chrome/browser/chromeos/accessibility/magnification_manager.h" |
| 6 | 6 |
| 7 #include "ash/magnifier/magnification_controller.h" | 7 #include "ash/magnifier/magnification_controller.h" |
| 8 #include "ash/magnifier/partial_magnification_controller.h" | 8 #include "ash/magnifier/partial_magnification_controller.h" |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "ash/system/tray/system_tray_notifier.h" | 10 #include "ash/system/tray/system_tray_notifier.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 namespace { | 27 namespace { |
| 28 const double kInitialMagnifiedScale = 2.0; | 28 const double kInitialMagnifiedScale = 2.0; |
| 29 static MagnificationManager* g_magnification_manager = NULL; | 29 static MagnificationManager* g_magnification_manager = NULL; |
| 30 } | 30 } |
| 31 | 31 |
| 32 class MagnificationManagerImpl : public MagnificationManager, | 32 class MagnificationManagerImpl : public MagnificationManager, |
| 33 public content::NotificationObserver { | 33 public content::NotificationObserver { |
| 34 public: | 34 public: |
| 35 MagnificationManagerImpl() : first_time_update_(true), | 35 MagnificationManagerImpl() : first_time_update_(true), |
| 36 profile_(NULL), | 36 profile_(NULL), |
| 37 type_(ash::kDefaultMagnifierType), | 37 type_(ash::MAGNIFIER_OFF) { |
| 38 enabled_(false) { | |
| 39 registrar_.Add(this, | 38 registrar_.Add(this, |
| 40 chrome::NOTIFICATION_PROFILE_CREATED, | 39 chrome::NOTIFICATION_PROFILE_CREATED, |
| 41 content::NotificationService::AllSources()); | 40 content::NotificationService::AllSources()); |
| 42 registrar_.Add(this, | 41 registrar_.Add(this, |
| 43 chrome::NOTIFICATION_SESSION_STARTED, | 42 chrome::NOTIFICATION_SESSION_STARTED, |
| 44 content::NotificationService::AllSources()); | 43 content::NotificationService::AllSources()); |
| 45 registrar_.Add(this, | 44 registrar_.Add(this, |
| 46 chrome::NOTIFICATION_PROFILE_DESTROYED, | 45 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 47 content::NotificationService::AllSources()); | 46 content::NotificationService::AllSources()); |
| 48 registrar_.Add(this, | 47 registrar_.Add(this, |
| 49 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, | 48 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
| 50 content::NotificationService::AllSources()); | 49 content::NotificationService::AllSources()); |
| 51 } | 50 } |
| 52 | 51 |
| 53 virtual ~MagnificationManagerImpl() { | 52 virtual ~MagnificationManagerImpl() { |
| 54 CHECK(this == g_magnification_manager); | 53 CHECK(this == g_magnification_manager); |
| 55 } | 54 } |
| 56 | 55 |
| 57 // MagnificationManager implimentation: | 56 // MagnificationManager implimentation: |
| 58 bool IsMagnifierEnabled() const OVERRIDE { | 57 ash::MagnifierType GetMagnifierType() OVERRIDE { |
| 59 return enabled_; | |
| 60 } | |
| 61 | |
| 62 ash::MagnifierType GetMagnifierType() const OVERRIDE { | |
| 63 return type_; | 58 return type_; |
| 64 } | 59 } |
| 65 | 60 |
| 66 void SetMagnifierEnabled(bool enabled) OVERRIDE { | 61 void SetMagnifier(ash::MagnifierType type) OVERRIDE { |
| 67 // This method may be invoked even when the other magnifier settings (e.g. | 62 if (type == type_ && type == ash::MAGNIFIER_OFF) |
| 68 // type or scale) are changed, so we need to call magnification controller | |
| 69 // even if |enabled| is unchanged. Only if |enabled| is false and the | |
| 70 // magnifier is already disabled, we are sure that we don't need to reflect | |
| 71 // the new settings right now because the magnifier keeps disabled. | |
| 72 if (!enabled && !enabled_) | |
| 73 return; | 63 return; |
| 74 | 64 |
| 75 enabled_ = enabled; | |
| 76 | |
| 77 if (profile_) { | |
| 78 PrefService* prefs = profile_->GetPrefs(); | |
| 79 DCHECK(prefs); | |
| 80 if (enabled != prefs->GetBoolean(prefs::kScreenMagnifierEnabled)) { | |
| 81 prefs->SetBoolean(prefs::kScreenMagnifierEnabled, enabled); | |
| 82 prefs->CommitPendingWrite(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 NotifyMagnifierChanged(); | |
| 87 | |
| 88 if (type_ == ash::MAGNIFIER_FULL) { | |
| 89 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( | |
| 90 enabled_); | |
| 91 } else { | |
| 92 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( | |
| 93 enabled_); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void SetMagnifierType(ash::MagnifierType type) OVERRIDE { | |
| 98 if (type_ == type) | |
| 99 return; | |
| 100 | |
| 101 DCHECK(type == ash::MAGNIFIER_FULL || type == ash::MAGNIFIER_PARTIAL); | |
| 102 type_ = type; | 65 type_ = type; |
| 103 | 66 |
| 104 if (profile_) { | 67 if (profile_) { |
| 105 PrefService* prefs = profile_->GetPrefs(); | 68 PrefService* prefs = profile_->GetPrefs(); |
| 106 DCHECK(prefs); | 69 if (prefs) { |
| 107 prefs->SetInteger(prefs::kScreenMagnifierType, type); | 70 bool enabled = (type != ash::MAGNIFIER_OFF); |
| 108 prefs->CommitPendingWrite(); | 71 if (enabled != prefs->GetBoolean(prefs::kScreenMagnifierEnabled)) { |
| 72 prefs->SetBoolean(prefs::kScreenMagnifierEnabled, enabled); |
| 73 prefs->CommitPendingWrite(); |
| 74 } |
| 75 } |
| 109 } | 76 } |
| 110 | 77 |
| 111 NotifyMagnifierChanged(); | 78 accessibility::AccessibilityStatusEventDetails details( |
| 79 type != ash::MAGNIFIER_OFF, ash::A11Y_NOTIFICATION_NONE); |
| 80 content::NotificationService::current()->Notify( |
| 81 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER, |
| 82 content::NotificationService::AllSources(), |
| 83 content::Details<accessibility::AccessibilityStatusEventDetails>( |
| 84 &details)); |
| 112 | 85 |
| 113 if (enabled_) { | 86 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( |
| 114 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( | 87 type == ash::MAGNIFIER_FULL); |
| 115 type_ == ash::MAGNIFIER_FULL); | 88 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( |
| 116 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( | 89 type == ash::MAGNIFIER_PARTIAL); |
| 117 type_ == ash::MAGNIFIER_PARTIAL); | |
| 118 } | |
| 119 } | 90 } |
| 120 | 91 |
| 121 void SaveScreenMagnifierScale(double scale) OVERRIDE { | 92 void SaveScreenMagnifierScale(double scale) OVERRIDE { |
| 122 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 93 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 123 DCHECK(profile->GetPrefs()); | |
| 124 profile->GetPrefs()->SetDouble(prefs::kScreenMagnifierScale, scale); | 94 profile->GetPrefs()->SetDouble(prefs::kScreenMagnifierScale, scale); |
| 125 } | 95 } |
| 126 | 96 |
| 127 double GetSavedScreenMagnifierScale() const OVERRIDE { | 97 double GetSavedScreenMagnifierScale() OVERRIDE { |
| 128 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 98 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 129 DCHECK(profile->GetPrefs()); | |
| 130 if (profile->GetPrefs()->HasPrefPath(prefs::kScreenMagnifierScale)) | 99 if (profile->GetPrefs()->HasPrefPath(prefs::kScreenMagnifierScale)) |
| 131 return profile->GetPrefs()->GetDouble(prefs::kScreenMagnifierScale); | 100 return profile->GetPrefs()->GetDouble(prefs::kScreenMagnifierScale); |
| 132 return std::numeric_limits<double>::min(); | 101 return std::numeric_limits<double>::min(); |
| 133 } | 102 } |
| 134 | 103 |
| 135 private: | 104 private: |
| 136 void NotifyMagnifierChanged() { | |
| 137 accessibility::AccessibilityStatusEventDetails details( | |
| 138 enabled_, type_, ash::A11Y_NOTIFICATION_NONE); | |
| 139 content::NotificationService::current()->Notify( | |
| 140 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER, | |
| 141 content::NotificationService::AllSources(), | |
| 142 content::Details<accessibility::AccessibilityStatusEventDetails>( | |
| 143 &details)); | |
| 144 } | |
| 145 | |
| 146 bool IsMagnifierEnabledFromPref() { | |
| 147 if (!profile_) | |
| 148 return false; | |
| 149 | |
| 150 DCHECK(profile_->GetPrefs()); | |
| 151 return profile_->GetPrefs()->GetBoolean(prefs::kScreenMagnifierEnabled); | |
| 152 } | |
| 153 | |
| 154 ash::MagnifierType GetMagnifierTypeFromPref() { | 105 ash::MagnifierType GetMagnifierTypeFromPref() { |
| 155 if (!profile_) | 106 if (!profile_) |
| 156 return ash::kDefaultMagnifierType; | 107 return ash::MAGNIFIER_OFF; |
| 157 | 108 |
| 158 DCHECK(profile_->GetPrefs()); | 109 PrefService* prefs = profile_->GetPrefs(); |
| 159 ash::MagnifierType type = static_cast<ash::MagnifierType>( | 110 if (!prefs) |
| 160 profile_->GetPrefs()->GetInteger(prefs::kScreenMagnifierType)); | 111 return ash::MAGNIFIER_OFF; |
| 161 | 112 |
| 162 if (type == ash::MAGNIFIER_FULL || type == ash::MAGNIFIER_PARTIAL) | 113 return prefs->GetBoolean(prefs::kScreenMagnifierEnabled) ? |
| 163 return type; | 114 ash::MAGNIFIER_FULL : ash::MAGNIFIER_OFF; |
| 164 | |
| 165 return ash::kDefaultMagnifierType; | |
| 166 } | 115 } |
| 167 | 116 |
| 168 void SetProfile(Profile* profile) { | 117 void SetProfile(Profile* profile) { |
| 169 if (pref_change_registrar_) { | 118 if (pref_change_registrar_) { |
| 170 pref_change_registrar_.reset(); | 119 pref_change_registrar_.reset(); |
| 171 } | 120 } |
| 172 | 121 |
| 173 if (profile) { | 122 if (profile) { |
| 174 pref_change_registrar_.reset(new PrefChangeRegistrar); | 123 pref_change_registrar_.reset(new PrefChangeRegistrar); |
| 175 pref_change_registrar_->Init(profile->GetPrefs()); | 124 pref_change_registrar_->Init(profile->GetPrefs()); |
| 176 pref_change_registrar_->Add( | 125 pref_change_registrar_->Add( |
| 177 prefs::kScreenMagnifierEnabled, | 126 prefs::kScreenMagnifierEnabled, |
| 178 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatusFromPref, | 127 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, |
| 179 base::Unretained(this))); | |
| 180 pref_change_registrar_->Add( | |
| 181 prefs::kScreenMagnifierType, | |
| 182 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatusFromPref, | |
| 183 base::Unretained(this))); | 128 base::Unretained(this))); |
| 184 } | 129 } |
| 185 | 130 |
| 186 profile_ = profile; | 131 profile_ = profile; |
| 187 UpdateMagnifierStatusFromPref(); | 132 UpdateMagnifierStatus(); |
| 188 } | 133 } |
| 189 | 134 |
| 190 void UpdateMagnifierStatusFromPref() { | 135 void UpdateMagnifierStatus() { |
| 191 bool enabled = IsMagnifierEnabledFromPref(); | 136 ash::MagnifierType type = GetMagnifierTypeFromPref(); |
| 192 if (!enabled) { | 137 SetMagnifier(type); |
| 193 SetMagnifierEnabled(enabled); | |
| 194 SetMagnifierType(GetMagnifierTypeFromPref()); | |
| 195 } else { | |
| 196 SetMagnifierType(GetMagnifierTypeFromPref()); | |
| 197 SetMagnifierEnabled(enabled); | |
| 198 } | |
| 199 } | 138 } |
| 200 | 139 |
| 201 // content::NotificationObserver implimentation: | 140 // content::NotificationObserver implimentation: |
| 202 virtual void Observe(int type, | 141 virtual void Observe(int type, |
| 203 const content::NotificationSource& source, | 142 const content::NotificationSource& source, |
| 204 const content::NotificationDetails& details) OVERRIDE { | 143 const content::NotificationDetails& details) OVERRIDE { |
| 205 switch (type) { | 144 switch (type) { |
| 206 // When entering the login screen or non-guest desktop. | 145 // When entering the login screen or non-guest desktop. |
| 207 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: | 146 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: |
| 208 case chrome::NOTIFICATION_SESSION_STARTED: { | 147 case chrome::NOTIFICATION_SESSION_STARTED: { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 222 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 161 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 223 SetProfile(NULL); | 162 SetProfile(NULL); |
| 224 break; | 163 break; |
| 225 } | 164 } |
| 226 } | 165 } |
| 227 } | 166 } |
| 228 | 167 |
| 229 bool first_time_update_; | 168 bool first_time_update_; |
| 230 Profile* profile_; | 169 Profile* profile_; |
| 231 ash::MagnifierType type_; | 170 ash::MagnifierType type_; |
| 232 bool enabled_; | |
| 233 content::NotificationRegistrar registrar_; | 171 content::NotificationRegistrar registrar_; |
| 234 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | 172 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; |
| 235 | 173 |
| 236 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); | 174 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); |
| 237 }; | 175 }; |
| 238 | 176 |
| 239 // static | 177 // static |
| 240 void MagnificationManager::Initialize() { | 178 void MagnificationManager::Initialize() { |
| 241 CHECK(g_magnification_manager == NULL); | 179 CHECK(g_magnification_manager == NULL); |
| 242 g_magnification_manager = new MagnificationManagerImpl(); | 180 g_magnification_manager = new MagnificationManagerImpl(); |
| 243 } | 181 } |
| 244 | 182 |
| 245 // static | 183 // static |
| 246 void MagnificationManager::Shutdown() { | 184 void MagnificationManager::Shutdown() { |
| 247 CHECK(g_magnification_manager); | 185 CHECK(g_magnification_manager); |
| 248 delete g_magnification_manager; | 186 delete g_magnification_manager; |
| 249 g_magnification_manager = NULL; | 187 g_magnification_manager = NULL; |
| 250 } | 188 } |
| 251 | 189 |
| 252 // static | 190 // static |
| 253 MagnificationManager* MagnificationManager::Get() { | 191 MagnificationManager* MagnificationManager::Get() { |
| 254 return g_magnification_manager; | 192 return g_magnification_manager; |
| 255 } | 193 } |
| 256 | 194 |
| 257 } // namespace chromeos | 195 } // namespace chromeos |
| OLD | NEW |