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::DEFAULT_MAGNIFIER_TYPE), |
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 SetMagnifierEnabled(bool enabled) OVERRIDE { |
59 if (type == type_ && type == ash::MAGNIFIER_OFF) | 64 if (enabled_ == enabled && !enabled) |
Daniel Erat
2013/01/08 00:28:49
i'm a bit surprised by this -- you need to do the
yoshiki
2013/01/09 03:03:15
We need the rest of the work. Added a comment.
On
| |
60 return; | 65 return; |
61 | 66 |
67 enabled_ = enabled; | |
68 | |
69 if (profile_) { | |
70 PrefService* prefs = profile_->GetPrefs(); | |
71 DCHECK(prefs); | |
72 if (enabled != prefs->GetBoolean(prefs::kScreenMagnifierEnabled)) { | |
Daniel Erat
2013/01/08 00:28:49
shouldn't we already match the pref? why is this
yoshiki
2013/01/09 03:03:15
We need to keep it for the reason in the above com
| |
73 prefs->SetBoolean(prefs::kScreenMagnifierEnabled, enabled); | |
74 prefs->CommitPendingWrite(); | |
75 } | |
76 } | |
77 | |
78 NotifyMagnifierChanged(); | |
79 | |
80 if (type_ == ash::MAGNIFIER_FULL) { | |
81 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( | |
82 enabled_); | |
83 } else { | |
84 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( | |
85 enabled_); | |
86 } | |
87 } | |
88 | |
89 void SetMagnifierType(ash::MagnifierType type) OVERRIDE { | |
90 if (type_ == type) | |
91 return; | |
92 | |
93 DCHECK(type == ash::MAGNIFIER_FULL || type == ash::MAGNIFIER_PARTIAL); | |
62 type_ = type; | 94 type_ = type; |
63 | 95 |
64 if (profile_) { | 96 if (profile_) { |
65 PrefService* prefs = profile_->GetPrefs(); | 97 PrefService* prefs = profile_->GetPrefs(); |
66 if (prefs) { | 98 DCHECK(prefs); |
67 bool enabled = (type != ash::MAGNIFIER_OFF); | 99 if (type != prefs->GetInteger(prefs::kScreenMagnifierType)) { |
Daniel Erat
2013/01/08 00:28:49
ditto here
yoshiki
2013/01/09 03:03:15
Done. This can be removed.
| |
68 if (enabled != prefs->GetBoolean(prefs::kScreenMagnifierEnabled)) { | 100 prefs->SetInteger(prefs::kScreenMagnifierType, type); |
69 prefs->SetBoolean(prefs::kScreenMagnifierEnabled, enabled); | 101 prefs->CommitPendingWrite(); |
70 prefs->CommitPendingWrite(); | |
71 } | |
72 } | 102 } |
73 } | 103 } |
74 | 104 |
75 accessibility::AccessibilityStatusEventDetails details( | 105 NotifyMagnifierChanged(); |
76 type != ash::MAGNIFIER_OFF, ash::A11Y_NOTIFICATION_NONE); | |
77 content::NotificationService::current()->Notify( | |
78 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER, | |
79 content::NotificationService::AllSources(), | |
80 content::Details<accessibility::AccessibilityStatusEventDetails>( | |
81 &details)); | |
82 | 106 |
83 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( | 107 if (enabled_) { |
84 type == ash::MAGNIFIER_FULL); | 108 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( |
85 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( | 109 type_ == ash::MAGNIFIER_FULL); |
86 type == ash::MAGNIFIER_PARTIAL); | 110 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( |
111 type_ == ash::MAGNIFIER_PARTIAL); | |
112 } | |
87 } | 113 } |
88 | 114 |
89 void SaveScreenMagnifierScale(double scale) OVERRIDE { | 115 void SaveScreenMagnifierScale(double scale) OVERRIDE { |
90 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 116 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
117 DCHECK(profile->GetPrefs()); | |
91 profile->GetPrefs()->SetDouble(prefs::kScreenMagnifierScale, scale); | 118 profile->GetPrefs()->SetDouble(prefs::kScreenMagnifierScale, scale); |
92 } | 119 } |
93 | 120 |
94 double GetSavedScreenMagnifierScale() OVERRIDE { | 121 double GetSavedScreenMagnifierScale() const OVERRIDE { |
95 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 122 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
123 DCHECK(profile->GetPrefs()); | |
96 if (profile->GetPrefs()->HasPrefPath(prefs::kScreenMagnifierScale)) | 124 if (profile->GetPrefs()->HasPrefPath(prefs::kScreenMagnifierScale)) |
97 return profile->GetPrefs()->GetDouble(prefs::kScreenMagnifierScale); | 125 return profile->GetPrefs()->GetDouble(prefs::kScreenMagnifierScale); |
98 return std::numeric_limits<double>::min(); | 126 return std::numeric_limits<double>::min(); |
99 } | 127 } |
100 | 128 |
101 private: | 129 private: |
130 void NotifyMagnifierChanged() { | |
131 accessibility::AccessibilityStatusEventDetails details( | |
132 enabled_, type_, ash::A11Y_NOTIFICATION_NONE); | |
133 content::NotificationService::current()->Notify( | |
134 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER, | |
135 content::NotificationService::AllSources(), | |
136 content::Details<accessibility::AccessibilityStatusEventDetails>( | |
137 &details)); | |
138 } | |
139 | |
140 bool IsMagnifierEnabledFromPref() { | |
141 if (!profile_) | |
142 return false; | |
143 | |
144 DCHECK(profile_->GetPrefs()); | |
145 return profile_->GetPrefs()->GetBoolean(prefs::kScreenMagnifierEnabled); | |
146 } | |
147 | |
102 ash::MagnifierType GetMagnifierTypeFromPref() { | 148 ash::MagnifierType GetMagnifierTypeFromPref() { |
103 if (!profile_) | 149 if (!profile_) |
104 return ash::MAGNIFIER_OFF; | 150 return ash::DEFAULT_MAGNIFIER_TYPE; |
105 | 151 |
106 PrefService* prefs = profile_->GetPrefs(); | 152 DCHECK(profile_->GetPrefs()); |
107 if (!prefs) | 153 ash::MagnifierType type = static_cast<ash::MagnifierType>( |
108 return ash::MAGNIFIER_OFF; | 154 profile_->GetPrefs()->GetInteger(prefs::kScreenMagnifierType)); |
109 | 155 |
110 return prefs->GetBoolean(prefs::kScreenMagnifierEnabled) ? | 156 if (type == ash::MAGNIFIER_FULL || type == ash::MAGNIFIER_PARTIAL) |
111 ash::MAGNIFIER_FULL : ash::MAGNIFIER_OFF; | 157 return type; |
158 | |
159 return ash::DEFAULT_MAGNIFIER_TYPE; | |
112 } | 160 } |
113 | 161 |
114 void SetProfile(Profile* profile) { | 162 void SetProfile(Profile* profile) { |
115 if (pref_change_registrar_) { | 163 if (pref_change_registrar_) { |
116 pref_change_registrar_.reset(); | 164 pref_change_registrar_.reset(); |
117 } | 165 } |
118 | 166 |
119 if (profile) { | 167 if (profile) { |
120 pref_change_registrar_.reset(new PrefChangeRegistrar); | 168 pref_change_registrar_.reset(new PrefChangeRegistrar); |
121 pref_change_registrar_->Init(profile->GetPrefs()); | 169 pref_change_registrar_->Init(profile->GetPrefs()); |
122 pref_change_registrar_->Add( | 170 pref_change_registrar_->Add( |
123 prefs::kScreenMagnifierEnabled, | 171 prefs::kScreenMagnifierEnabled, |
124 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, | 172 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, |
125 base::Unretained(this))); | 173 base::Unretained(this))); |
174 pref_change_registrar_->Add( | |
175 prefs::kScreenMagnifierType, | |
176 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, | |
177 base::Unretained(this))); | |
126 } | 178 } |
127 | 179 |
128 profile_ = profile; | 180 profile_ = profile; |
129 UpdateMagnifierStatus(); | 181 UpdateMagnifierStatus(); |
130 } | 182 } |
131 | 183 |
132 void UpdateMagnifierStatus() { | 184 void UpdateMagnifierStatus() { |
Daniel Erat
2013/01/08 00:28:49
nit: maybe rename this to something like UpdateMag
yoshiki
2013/01/09 03:03:15
Done.
| |
133 ash::MagnifierType type = GetMagnifierTypeFromPref(); | 185 bool enabled = IsMagnifierEnabledFromPref(); |
134 SetMagnifier(type); | 186 if (!enabled) { |
187 SetMagnifierEnabled(enabled); | |
188 SetMagnifierType(GetMagnifierTypeFromPref()); | |
189 } else { | |
190 SetMagnifierType(GetMagnifierTypeFromPref()); | |
191 SetMagnifierEnabled(enabled); | |
192 } | |
135 } | 193 } |
136 | 194 |
137 // content::NotificationObserver implimentation: | 195 // content::NotificationObserver implimentation: |
138 virtual void Observe(int type, | 196 virtual void Observe(int type, |
139 const content::NotificationSource& source, | 197 const content::NotificationSource& source, |
140 const content::NotificationDetails& details) OVERRIDE { | 198 const content::NotificationDetails& details) OVERRIDE { |
141 switch (type) { | 199 switch (type) { |
142 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: | 200 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: |
143 case chrome::NOTIFICATION_SESSION_STARTED: { | 201 case chrome::NOTIFICATION_SESSION_STARTED: { |
144 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 202 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
145 SetProfile(profile); | 203 SetProfile(profile); |
146 break; | 204 break; |
147 } | 205 } |
148 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 206 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
149 SetProfile(NULL); | 207 SetProfile(NULL); |
150 break; | 208 break; |
151 } | 209 } |
152 } | 210 } |
153 } | 211 } |
154 | 212 |
155 bool first_time_update_; | 213 bool first_time_update_; |
156 Profile* profile_; | 214 Profile* profile_; |
157 ash::MagnifierType type_; | 215 ash::MagnifierType type_; |
216 bool enabled_; | |
158 content::NotificationRegistrar registrar_; | 217 content::NotificationRegistrar registrar_; |
159 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | 218 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; |
160 | 219 |
161 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); | 220 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); |
162 }; | 221 }; |
163 | 222 |
164 // static | 223 // static |
165 void MagnificationManager::Initialize() { | 224 void MagnificationManager::Initialize() { |
166 CHECK(g_magnification_manager == NULL); | 225 CHECK(g_magnification_manager == NULL); |
167 g_magnification_manager = new MagnificationManagerImpl(); | 226 g_magnification_manager = new MagnificationManagerImpl(); |
168 } | 227 } |
169 | 228 |
170 // static | 229 // static |
171 void MagnificationManager::Shutdown() { | 230 void MagnificationManager::Shutdown() { |
172 CHECK(g_magnification_manager); | 231 CHECK(g_magnification_manager); |
173 delete g_magnification_manager; | 232 delete g_magnification_manager; |
174 g_magnification_manager = NULL; | 233 g_magnification_manager = NULL; |
175 } | 234 } |
176 | 235 |
177 // static | 236 // static |
178 MagnificationManager* MagnificationManager::Get() { | 237 MagnificationManager* MagnificationManager::Get() { |
179 return g_magnification_manager; | 238 return g_magnification_manager; |
180 } | 239 } |
181 | 240 |
182 } // namespace chromeos | 241 } // namespace chromeos |
OLD | NEW |