Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: chrome/browser/chromeos/accessibility/magnification_manager.cc

Issue 11280287: Magnifier: Prevent useless operation in enabling/disabling magnifier. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: review fix (#2 & #3) Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 namespace {
28 static MagnificationManager* g_magnification_manager = NULL;
29 }
30
27 class MagnificationManagerImpl : public MagnificationManager, 31 class MagnificationManagerImpl : public MagnificationManager,
28 public content::NotificationObserver { 32 public content::NotificationObserver {
29 public: 33 public:
30 MagnificationManagerImpl() { 34 MagnificationManagerImpl() : profile_(NULL),
31 DCHECK(!instance_); 35 type_(ash::MAGNIFIER_OFF) {
32 instance_ = this;
33
34 registrar_.Add(this, 36 registrar_.Add(this,
35 chrome::NOTIFICATION_SESSION_STARTED, 37 chrome::NOTIFICATION_SESSION_STARTED,
36 content::NotificationService::AllSources()); 38 content::NotificationService::AllSources());
37 registrar_.Add(this, 39 registrar_.Add(this,
38 chrome::NOTIFICATION_PROFILE_CREATED, 40 chrome::NOTIFICATION_PROFILE_DESTROYED,
39 content::NotificationService::AllSources()); 41 content::NotificationService::AllSources());
40 registrar_.Add(this, 42 registrar_.Add(this,
41 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, 43 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE,
42 content::NotificationService::AllSources()); 44 content::NotificationService::AllSources());
45 }
43 46
44 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 47 virtual ~MagnificationManagerImpl() {
45 SetProfile(profile); 48 CHECK(this == g_magnification_manager);
46 }
47 virtual ~MagnificationManagerImpl() {}
48
49 static MagnificationManagerImpl* GetInstance() {
50 return instance_;
51 } 49 }
52 50
53 // MagnificationManager implimentation: 51 // MagnificationManager implimentation:
54 ash::MagnifierType GetMagnifierType() OVERRIDE { 52 ash::MagnifierType GetMagnifierType() OVERRIDE {
55 if (!profile_) 53 return type_;
56 return ash::MAGNIFIER_OFF;
57
58 PrefService* prefs = profile_->GetPrefs();
59 if (!prefs)
60 return ash::MAGNIFIER_OFF;
61
62 return accessibility::MagnifierTypeFromName(
63 prefs->GetString(prefs::kMagnifierType).c_str());
64 } 54 }
65 55
66 void SetMagnifier(ash::MagnifierType type) OVERRIDE { 56 void SetMagnifier(ash::MagnifierType type) OVERRIDE {
57 if (type == type_)
58 return;
59
60 type_ = type;
61
67 PrefService* prefs = profile_->GetPrefs(); 62 PrefService* prefs = profile_->GetPrefs();
68 if (prefs) { 63 if (prefs) {
69 std::string typeString = accessibility::ScreenMagnifierNameFromType(type); 64 std::string typeString = accessibility::ScreenMagnifierNameFromType(type);
70 if (typeString != prefs->GetString(prefs::kMagnifierType)) { 65 if (typeString != prefs->GetString(prefs::kMagnifierType)) {
71 prefs->SetString(prefs::kMagnifierType, typeString); 66 prefs->SetString(prefs::kMagnifierType, typeString);
72 prefs->CommitPendingWrite(); 67 prefs->CommitPendingWrite();
73 } 68 }
74 } 69 }
75 70
76 ash::Shell::GetInstance()->system_tray_notifier()->
77 NotifyAccessibilityModeChanged();
78
79 ash::Shell::GetInstance()->magnification_controller()->SetEnabled( 71 ash::Shell::GetInstance()->magnification_controller()->SetEnabled(
80 type == ash::MAGNIFIER_FULL); 72 type == ash::MAGNIFIER_FULL);
81 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled( 73 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled(
82 type == ash::MAGNIFIER_PARTIAL); 74 type == ash::MAGNIFIER_PARTIAL);
75
76 NotifyMagnifierTypeChanged(type);
77 }
78
79 void AddObserver(MagnificationObserver* observer) OVERRIDE {
80 observers_.AddObserver(observer);
81 }
82
83 void RemoveObserver(MagnificationObserver* observer) OVERRIDE {
84 observers_.RemoveObserver(observer);
83 } 85 }
84 86
85 private: 87 private:
88 void NotifyMagnifierTypeChanged(ash::MagnifierType new_type) {
89 FOR_EACH_OBSERVER(MagnificationObserver, observers_,
90 OnMagnifierTypeChanged(new_type));
91 }
92
93 ash::MagnifierType GetMagnifierTypeFromPref() {
94 if (!profile_)
95 return ash::MAGNIFIER_OFF;
96
97 PrefService* prefs = profile_->GetPrefs();
98 if (!prefs)
99 return ash::MAGNIFIER_OFF;
100
101 return accessibility::MagnifierTypeFromName(
102 prefs->GetString(prefs::kMagnifierType).c_str());
103 }
104
86 void SetProfile(Profile* profile) { 105 void SetProfile(Profile* profile) {
87 if (pref_change_registrar_) { 106 if (pref_change_registrar_) {
88 pref_change_registrar_.reset(); 107 pref_change_registrar_.reset();
89 } 108 }
90 109
91 if (profile) { 110 if (profile) {
92 pref_change_registrar_.reset(new PrefChangeRegistrar); 111 pref_change_registrar_.reset(new PrefChangeRegistrar);
93 pref_change_registrar_->Init(profile->GetPrefs()); 112 pref_change_registrar_->Init(profile->GetPrefs());
94 pref_change_registrar_->Add( 113 pref_change_registrar_->Add(
95 prefs::kMagnifierType, 114 prefs::kMagnifierType,
96 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, 115 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus,
97 base::Unretained(this))); 116 base::Unretained(this)));
98 } 117 }
99 118
100 profile_ = profile; 119 profile_ = profile;
101 UpdateMagnifierStatus(); 120 UpdateMagnifierStatus();
102 } 121 }
103 122
104 void UpdateMagnifierStatus() { 123 void UpdateMagnifierStatus() {
105 UserManager* manager = UserManager::Get(); 124 UserManager* manager = UserManager::Get();
106 if (!profile_) { 125 if (!profile_) {
107 SetMagnifier(ash::MAGNIFIER_OFF); 126 SetMagnifier(ash::MAGNIFIER_OFF);
108 } else if (manager && !manager->IsSessionStarted()) { 127 } else if (manager && !manager->IsSessionStarted()) {
109 SetMagnifier(ash::MAGNIFIER_FULL); 128 SetMagnifier(ash::MAGNIFIER_FULL);
110 } else { 129 } else {
111 ash::MagnifierType type = GetMagnifierType(); 130 ash::MagnifierType type = GetMagnifierTypeFromPref();
112 SetMagnifier(type); 131 SetMagnifier(type);
113 } 132 }
114 } 133 }
115 134
135 // content::NotificationObserver implimentation:
116 virtual void Observe(int type, 136 virtual void Observe(int type,
117 const content::NotificationSource& source, 137 const content::NotificationSource& source,
118 const content::NotificationDetails& details) OVERRIDE { 138 const content::NotificationDetails& details) OVERRIDE {
119 switch (type) { 139 switch (type) {
120 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: 140 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE:
121 case chrome::NOTIFICATION_SESSION_STARTED: { 141 case chrome::NOTIFICATION_SESSION_STARTED: {
122 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 142 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
123 SetProfile(profile); 143 SetProfile(profile);
124 break; 144 break;
125 } 145 }
146 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
147 SetProfile(NULL);
148 break;
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
135 friend struct DefaultSingletonTraits<MagnificationManagerImpl>; 158 ObserverList<MagnificationObserver> observers_;
136 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); 159 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl);
137 }; 160 };
138 161
139 MagnificationManagerImpl* MagnificationManagerImpl::instance_ = NULL; 162 // static
140 163 void MagnificationManager::Initialize() {
141 MagnificationManager* MagnificationManager::GetInstance() { 164 CHECK(g_magnification_manager == NULL);
142 return MagnificationManagerImpl::GetInstance(); 165 g_magnification_manager = new MagnificationManagerImpl();
143 } 166 }
144 167
145 MagnificationManager* MagnificationManager::CreateInstance() { 168 // static
146 // Makes sure that this is not called more than once. 169 void MagnificationManager::Shutdown() {
147 CHECK(!GetInstance()); 170 CHECK(g_magnification_manager);
171 delete g_magnification_manager;
172 g_magnification_manager = NULL;
173 }
148 174
149 return new MagnificationManagerImpl(); 175 // static
176 MagnificationManager* MagnificationManager::Get() {
177 return g_magnification_manager;
150 } 178 }
151 179
152 } // namespace chromeos 180 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698