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" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
13 #include "chrome/browser/api/prefs/pref_member.h" | 13 #include "chrome/browser/api/prefs/pref_member.h" |
14 #include "chrome/browser/chromeos/login/user_manager.h" | 14 #include "chrome/browser/chromeos/login/user_manager.h" |
15 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
24 | 24 |
25 namespace chromeos { | 25 namespace chromeos { |
26 | 26 |
27 class MagnificationManagerImpl : public MagnificationManager, | 27 class MagnificationManagerImpl : public MagnificationManager, |
28 public content::NotificationObserver { | 28 public content::NotificationObserver { |
29 public: | 29 public: |
30 MagnificationManagerImpl() { | 30 MagnificationManagerImpl() : profile_(NULL), |
31 DCHECK(!instance_); | 31 type_(ash::MAGNIFIER_OFF) { |
32 instance_ = this; | |
33 | |
34 registrar_.Add(this, | 32 registrar_.Add(this, |
35 chrome::NOTIFICATION_SESSION_STARTED, | 33 chrome::NOTIFICATION_SESSION_STARTED, |
36 content::NotificationService::AllSources()); | 34 content::NotificationService::AllSources()); |
37 registrar_.Add(this, | 35 registrar_.Add(this, |
38 chrome::NOTIFICATION_PROFILE_CREATED, | 36 chrome::NOTIFICATION_PROFILE_CREATED, |
39 content::NotificationService::AllSources()); | 37 content::NotificationService::AllSources()); |
40 registrar_.Add(this, | 38 registrar_.Add(this, |
41 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, | 39 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
42 content::NotificationService::AllSources()); | 40 content::NotificationService::AllSources()); |
41 } | |
43 | 42 |
43 virtual void Initialize() OVERRIDE { | |
44 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 44 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
45 SetProfile(profile); | 45 SetProfile(profile); |
46 } | 46 } |
47 virtual ~MagnificationManagerImpl() {} | 47 |
48 virtual ~MagnificationManagerImpl() { | |
49 } | |
48 | 50 |
49 static MagnificationManagerImpl* GetInstance() { | 51 static MagnificationManagerImpl* GetInstance() { |
50 return instance_; | 52 return Singleton<MagnificationManagerImpl>::get(); |
Daniel Erat
2012/12/04 01:06:36
the Singleton template causes more trouble than it
stevenjb
2012/12/04 01:35:34
I would agree. Specifically, this class has a Prof
yoshiki
2012/12/04 08:30:20
Thanks, I removed Singleton and and use Initialize
| |
51 } | 53 } |
52 | 54 |
53 // MagnificationManager implimentation: | 55 // MagnificationManager implimentation: |
54 ash::MagnifierType GetMagnifierType() OVERRIDE { | 56 ash::MagnifierType GetMagnifierType() OVERRIDE { |
57 return type_; | |
58 } | |
59 | |
60 ash::MagnifierType GetMagnifierTypeFromPref() { | |
55 if (!profile_) | 61 if (!profile_) |
56 return ash::MAGNIFIER_OFF; | 62 return ash::MAGNIFIER_OFF; |
57 | 63 |
58 PrefService* prefs = profile_->GetPrefs(); | 64 PrefService* prefs = profile_->GetPrefs(); |
59 if (!prefs) | 65 if (!prefs) |
60 return ash::MAGNIFIER_OFF; | 66 return ash::MAGNIFIER_OFF; |
61 | 67 |
62 return accessibility::MagnifierTypeFromName( | 68 return accessibility::MagnifierTypeFromName( |
63 prefs->GetString(prefs::kMagnifierType).c_str()); | 69 prefs->GetString(prefs::kMagnifierType).c_str()); |
64 } | 70 } |
65 | 71 |
66 void SetMagnifier(ash::MagnifierType type) OVERRIDE { | 72 void SetMagnifier(ash::MagnifierType type) OVERRIDE { |
73 if (type_ == type) | |
74 return; | |
75 | |
76 type_ = type; | |
77 | |
67 PrefService* prefs = profile_->GetPrefs(); | 78 PrefService* prefs = profile_->GetPrefs(); |
68 if (prefs) { | 79 if (prefs) { |
69 std::string typeString = accessibility::ScreenMagnifierNameFromType(type); | 80 std::string typeString = accessibility::ScreenMagnifierNameFromType(type); |
70 if (typeString != prefs->GetString(prefs::kMagnifierType)) { | 81 if (typeString != prefs->GetString(prefs::kMagnifierType)) { |
71 prefs->SetString(prefs::kMagnifierType, typeString); | 82 prefs->SetString(prefs::kMagnifierType, typeString); |
72 prefs->CommitPendingWrite(); | 83 prefs->CommitPendingWrite(); |
73 } | 84 } |
74 } | 85 } |
75 | 86 |
76 ash::Shell::GetInstance()->system_tray_notifier()-> | |
77 NotifyAccessibilityModeChanged(); | |
78 | |
79 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( | 87 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( |
80 type == ash::MAGNIFIER_FULL); | 88 type == ash::MAGNIFIER_FULL); |
81 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( | 89 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( |
82 type == ash::MAGNIFIER_PARTIAL); | 90 type == ash::MAGNIFIER_PARTIAL); |
91 | |
92 NotifyMagnifierTypeChanged(type); | |
93 } | |
94 | |
95 void AddObserver(MagnificationObserver* observer) OVERRIDE { | |
96 observers_.AddObserver(observer); | |
97 } | |
98 | |
99 void RemoveObserver(MagnificationObserver* observer) OVERRIDE { | |
100 observers_.RemoveObserver(observer); | |
83 } | 101 } |
84 | 102 |
85 private: | 103 private: |
104 void NotifyMagnifierTypeChanged(ash::MagnifierType new_type) { | |
105 FOR_EACH_OBSERVER(MagnificationObserver, observers_, | |
106 OnMagnifierTypeChanged(new_type)); | |
107 } | |
108 | |
86 void SetProfile(Profile* profile) { | 109 void SetProfile(Profile* profile) { |
87 if (pref_change_registrar_) { | 110 if (pref_change_registrar_) { |
88 pref_change_registrar_.reset(); | 111 pref_change_registrar_.reset(); |
89 } | 112 } |
90 | 113 |
91 if (profile) { | 114 if (profile) { |
92 pref_change_registrar_.reset(new PrefChangeRegistrar); | 115 pref_change_registrar_.reset(new PrefChangeRegistrar); |
93 pref_change_registrar_->Init(profile->GetPrefs()); | 116 pref_change_registrar_->Init(profile->GetPrefs()); |
94 pref_change_registrar_->Add( | 117 pref_change_registrar_->Add( |
95 prefs::kMagnifierType, | 118 prefs::kMagnifierType, |
96 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, | 119 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, |
97 base::Unretained(this))); | 120 base::Unretained(this))); |
98 } | 121 } |
99 | 122 |
100 profile_ = profile; | 123 profile_ = profile; |
101 UpdateMagnifierStatus(); | 124 UpdateMagnifierStatus(); |
102 } | 125 } |
103 | 126 |
104 void UpdateMagnifierStatus() { | 127 void UpdateMagnifierStatus() { |
105 UserManager* manager = UserManager::Get(); | 128 UserManager* manager = UserManager::Get(); |
106 if (!profile_) { | 129 if (!profile_) { |
107 SetMagnifier(ash::MAGNIFIER_OFF); | 130 SetMagnifier(ash::MAGNIFIER_OFF); |
108 } else if (manager && !manager->IsSessionStarted()) { | 131 } else if (manager && !manager->IsSessionStarted()) { |
109 SetMagnifier(ash::MAGNIFIER_FULL); | 132 SetMagnifier(ash::MAGNIFIER_FULL); |
110 } else { | 133 } else { |
111 ash::MagnifierType type = GetMagnifierType(); | 134 ash::MagnifierType type = GetMagnifierTypeFromPref(); |
112 SetMagnifier(type); | 135 SetMagnifier(type); |
113 } | 136 } |
114 } | 137 } |
115 | 138 |
139 // content::NotificationObserver implimentation: | |
116 virtual void Observe(int type, | 140 virtual void Observe(int type, |
117 const content::NotificationSource& source, | 141 const content::NotificationSource& source, |
118 const content::NotificationDetails& details) OVERRIDE { | 142 const content::NotificationDetails& details) OVERRIDE { |
119 switch (type) { | 143 switch (type) { |
120 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: | 144 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: |
121 case chrome::NOTIFICATION_SESSION_STARTED: { | 145 case chrome::NOTIFICATION_SESSION_STARTED: { |
122 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 146 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
123 SetProfile(profile); | 147 SetProfile(profile); |
124 break; | 148 break; |
125 } | 149 } |
126 } | 150 } |
127 } | 151 } |
128 | 152 |
129 static MagnificationManagerImpl* instance_; | |
130 | |
131 Profile* profile_; | 153 Profile* profile_; |
154 ash::MagnifierType type_; | |
132 content::NotificationRegistrar registrar_; | 155 content::NotificationRegistrar registrar_; |
133 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | 156 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; |
134 | 157 |
158 ObserverList<MagnificationObserver> observers_; | |
159 | |
135 friend struct DefaultSingletonTraits<MagnificationManagerImpl>; | 160 friend struct DefaultSingletonTraits<MagnificationManagerImpl>; |
136 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); | 161 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); |
137 }; | 162 }; |
138 | 163 |
139 MagnificationManagerImpl* MagnificationManagerImpl::instance_ = NULL; | |
140 | |
141 MagnificationManager* MagnificationManager::GetInstance() { | 164 MagnificationManager* MagnificationManager::GetInstance() { |
142 return MagnificationManagerImpl::GetInstance(); | 165 return MagnificationManagerImpl::GetInstance(); |
143 } | 166 } |
144 | 167 |
145 MagnificationManager* MagnificationManager::CreateInstance() { | |
146 // Makes sure that this is not called more than once. | |
147 CHECK(!GetInstance()); | |
148 | |
149 return new MagnificationManagerImpl(); | |
150 } | |
151 | |
152 } // namespace chromeos | 168 } // namespace chromeos |
OLD | NEW |