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::MAGNIFIER_OFF) { | 37 type_(ash::MAGNIFIER_DEFAULT), |
| 38 enabled_(false) { |
38 registrar_.Add(this, | 39 registrar_.Add(this, |
39 chrome::NOTIFICATION_SESSION_STARTED, | 40 chrome::NOTIFICATION_SESSION_STARTED, |
40 content::NotificationService::AllSources()); | 41 content::NotificationService::AllSources()); |
41 registrar_.Add(this, | 42 registrar_.Add(this, |
42 chrome::NOTIFICATION_PROFILE_DESTROYED, | 43 chrome::NOTIFICATION_PROFILE_DESTROYED, |
43 content::NotificationService::AllSources()); | 44 content::NotificationService::AllSources()); |
44 registrar_.Add(this, | 45 registrar_.Add(this, |
45 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, | 46 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
46 content::NotificationService::AllSources()); | 47 content::NotificationService::AllSources()); |
47 } | 48 } |
48 | 49 |
49 virtual ~MagnificationManagerImpl() { | 50 virtual ~MagnificationManagerImpl() { |
50 CHECK(this == g_magnification_manager); | 51 CHECK(this == g_magnification_manager); |
51 } | 52 } |
52 | 53 |
53 // MagnificationManager implimentation: | 54 // MagnificationManager implimentation: |
54 ash::MagnifierType GetMagnifierType() OVERRIDE { | 55 bool IsMagnifierEnabled() const OVERRIDE { |
| 56 return enabled_; |
| 57 } |
| 58 |
| 59 ash::MagnifierType GetMagnifierType() const OVERRIDE { |
55 return type_; | 60 return type_; |
56 } | 61 } |
57 | 62 |
58 void SetMagnifier(ash::MagnifierType type) OVERRIDE { | 63 void SetMagnifier(bool enabled, ash::MagnifierType type) OVERRIDE { |
59 if (type == type_ && type == ash::MAGNIFIER_OFF) | 64 if (type == ash::MAGNIFIER_TYPE_UNCHANGE) |
| 65 type = type_; |
| 66 |
| 67 if (enabled_ == enabled && type_ == type && !enabled) |
60 return; | 68 return; |
61 | 69 |
| 70 DCHECK(type == ash::MAGNIFIER_FULL || type == ash::MAGNIFIER_PARTIAL); |
| 71 |
| 72 enabled_ = enabled; |
62 type_ = type; | 73 type_ = type; |
63 | 74 |
64 if (profile_) { | 75 if (profile_) { |
65 PrefService* prefs = profile_->GetPrefs(); | 76 PrefService* prefs = profile_->GetPrefs(); |
66 if (prefs) { | 77 DCHECK(prefs); |
67 bool enabled = (type != ash::MAGNIFIER_OFF); | 78 bool changed = false; |
68 if (enabled != prefs->GetBoolean(prefs::kScreenMagnifierEnabled)) { | 79 |
69 prefs->SetBoolean(prefs::kScreenMagnifierEnabled, enabled); | 80 if (enabled != prefs->GetBoolean(prefs::kScreenMagnifierEnabled)) { |
70 prefs->CommitPendingWrite(); | 81 prefs->SetBoolean(prefs::kScreenMagnifierEnabled, enabled); |
71 } | 82 changed = true; |
72 } | 83 } |
| 84 |
| 85 if (type != prefs->GetInteger(prefs::kScreenMagnifierType)) { |
| 86 prefs->SetInteger(prefs::kScreenMagnifierType, type); |
| 87 changed = true; |
| 88 } |
| 89 |
| 90 if (changed) |
| 91 prefs->CommitPendingWrite(); |
73 } | 92 } |
74 | 93 |
75 accessibility::AccessibilityStatusEventDetails details( | 94 accessibility::AccessibilityStatusEventDetails details( |
76 type != ash::MAGNIFIER_OFF, ash::A11Y_NOTIFICATION_NONE); | 95 enabled, type, ash::A11Y_NOTIFICATION_NONE); |
77 content::NotificationService::current()->Notify( | 96 content::NotificationService::current()->Notify( |
78 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER, | 97 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER, |
79 content::NotificationService::AllSources(), | 98 content::NotificationService::AllSources(), |
80 content::Details<accessibility::AccessibilityStatusEventDetails>( | 99 content::Details<accessibility::AccessibilityStatusEventDetails>( |
81 &details)); | 100 &details)); |
82 | 101 |
83 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( | 102 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( |
84 type == ash::MAGNIFIER_FULL); | 103 enabled && type == ash::MAGNIFIER_FULL); |
85 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( | 104 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( |
86 type == ash::MAGNIFIER_PARTIAL); | 105 enabled && type == ash::MAGNIFIER_PARTIAL); |
87 } | 106 } |
88 | 107 |
89 void SaveScreenMagnifierScale(double scale) OVERRIDE { | 108 void SaveScreenMagnifierScale(double scale) OVERRIDE { |
90 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 109 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 110 DCHECK(profile->GetPrefs()); |
91 profile->GetPrefs()->SetDouble(prefs::kScreenMagnifierScale, scale); | 111 profile->GetPrefs()->SetDouble(prefs::kScreenMagnifierScale, scale); |
92 } | 112 } |
93 | 113 |
94 double GetSavedScreenMagnifierScale() OVERRIDE { | 114 double GetSavedScreenMagnifierScale() const OVERRIDE { |
95 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 115 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 116 DCHECK(profile->GetPrefs()); |
96 if (profile->GetPrefs()->HasPrefPath(prefs::kScreenMagnifierScale)) | 117 if (profile->GetPrefs()->HasPrefPath(prefs::kScreenMagnifierScale)) |
97 return profile->GetPrefs()->GetDouble(prefs::kScreenMagnifierScale); | 118 return profile->GetPrefs()->GetDouble(prefs::kScreenMagnifierScale); |
98 return std::numeric_limits<double>::min(); | 119 return std::numeric_limits<double>::min(); |
99 } | 120 } |
100 | 121 |
101 private: | 122 private: |
| 123 bool IsMagnifierEnabledFromPref() { |
| 124 if (!profile_) |
| 125 return false; |
| 126 |
| 127 DCHECK(profile_->GetPrefs()); |
| 128 return profile_->GetPrefs()->GetBoolean(prefs::kScreenMagnifierEnabled); |
| 129 } |
| 130 |
102 ash::MagnifierType GetMagnifierTypeFromPref() { | 131 ash::MagnifierType GetMagnifierTypeFromPref() { |
103 if (!profile_) | 132 if (!profile_) |
104 return ash::MAGNIFIER_OFF; | 133 return ash::MAGNIFIER_DEFAULT; |
105 | 134 |
106 PrefService* prefs = profile_->GetPrefs(); | 135 DCHECK(profile_->GetPrefs()); |
107 if (!prefs) | 136 ash::MagnifierType type = static_cast<ash::MagnifierType>( |
108 return ash::MAGNIFIER_OFF; | 137 profile_->GetPrefs()->GetInteger(prefs::kScreenMagnifierType)); |
109 | 138 |
110 return prefs->GetBoolean(prefs::kScreenMagnifierEnabled) ? | 139 if (type == ash::MAGNIFIER_FULL || type == ash::MAGNIFIER_PARTIAL) |
111 ash::MAGNIFIER_FULL : ash::MAGNIFIER_OFF; | 140 return type; |
| 141 |
| 142 return ash::MAGNIFIER_DEFAULT; |
112 } | 143 } |
113 | 144 |
114 void SetProfile(Profile* profile) { | 145 void SetProfile(Profile* profile) { |
115 if (pref_change_registrar_) { | 146 if (pref_change_registrar_) { |
116 pref_change_registrar_.reset(); | 147 pref_change_registrar_.reset(); |
117 } | 148 } |
118 | 149 |
119 if (profile) { | 150 if (profile) { |
120 pref_change_registrar_.reset(new PrefChangeRegistrar); | 151 pref_change_registrar_.reset(new PrefChangeRegistrar); |
121 pref_change_registrar_->Init(profile->GetPrefs()); | 152 pref_change_registrar_->Init(profile->GetPrefs()); |
122 pref_change_registrar_->Add( | 153 pref_change_registrar_->Add( |
123 prefs::kScreenMagnifierEnabled, | 154 prefs::kScreenMagnifierEnabled, |
124 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, | 155 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, |
125 base::Unretained(this))); | 156 base::Unretained(this))); |
| 157 pref_change_registrar_->Add( |
| 158 prefs::kScreenMagnifierType, |
| 159 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, |
| 160 base::Unretained(this))); |
126 } | 161 } |
127 | 162 |
128 profile_ = profile; | 163 profile_ = profile; |
129 UpdateMagnifierStatus(); | 164 UpdateMagnifierStatus(); |
130 } | 165 } |
131 | 166 |
132 void UpdateMagnifierStatus() { | 167 void UpdateMagnifierStatus() { |
133 ash::MagnifierType type = GetMagnifierTypeFromPref(); | 168 SetMagnifier(IsMagnifierEnabledFromPref(), GetMagnifierTypeFromPref()); |
134 SetMagnifier(type); | |
135 } | 169 } |
136 | 170 |
137 // content::NotificationObserver implimentation: | 171 // content::NotificationObserver implimentation: |
138 virtual void Observe(int type, | 172 virtual void Observe(int type, |
139 const content::NotificationSource& source, | 173 const content::NotificationSource& source, |
140 const content::NotificationDetails& details) OVERRIDE { | 174 const content::NotificationDetails& details) OVERRIDE { |
141 switch (type) { | 175 switch (type) { |
142 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: | 176 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: |
143 case chrome::NOTIFICATION_SESSION_STARTED: { | 177 case chrome::NOTIFICATION_SESSION_STARTED: { |
144 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 178 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
145 SetProfile(profile); | 179 SetProfile(profile); |
146 break; | 180 break; |
147 } | 181 } |
148 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 182 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
149 SetProfile(NULL); | 183 SetProfile(NULL); |
150 break; | 184 break; |
151 } | 185 } |
152 } | 186 } |
153 } | 187 } |
154 | 188 |
155 bool first_time_update_; | 189 bool first_time_update_; |
156 Profile* profile_; | 190 Profile* profile_; |
157 ash::MagnifierType type_; | 191 ash::MagnifierType type_; |
| 192 bool enabled_; |
158 content::NotificationRegistrar registrar_; | 193 content::NotificationRegistrar registrar_; |
159 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | 194 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; |
160 | 195 |
161 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); | 196 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); |
162 }; | 197 }; |
163 | 198 |
164 // static | 199 // static |
165 void MagnificationManager::Initialize() { | 200 void MagnificationManager::Initialize() { |
166 CHECK(g_magnification_manager == NULL); | 201 CHECK(g_magnification_manager == NULL); |
167 g_magnification_manager = new MagnificationManagerImpl(); | 202 g_magnification_manager = new MagnificationManagerImpl(); |
168 } | 203 } |
169 | 204 |
170 // static | 205 // static |
171 void MagnificationManager::Shutdown() { | 206 void MagnificationManager::Shutdown() { |
172 CHECK(g_magnification_manager); | 207 CHECK(g_magnification_manager); |
173 delete g_magnification_manager; | 208 delete g_magnification_manager; |
174 g_magnification_manager = NULL; | 209 g_magnification_manager = NULL; |
175 } | 210 } |
176 | 211 |
177 // static | 212 // static |
178 MagnificationManager* MagnificationManager::Get() { | 213 MagnificationManager* MagnificationManager::Get() { |
179 return g_magnification_manager; | 214 return g_magnification_manager; |
180 } | 215 } |
181 | 216 |
182 } // namespace chromeos | 217 } // namespace chromeos |
OLD | NEW |