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