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

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: fix test failure (MagnificationManagerTest.ChangeMagnifierType) 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 {
53 return type_;
54 }
55
56 void SetMagnifier(ash::MagnifierType type) OVERRIDE {
57 if (type == type_)
58 return;
59
60 type_ = type;
61
62 if (profile_) {
63 PrefService* prefs = profile_->GetPrefs();
64 if (prefs) {
65 std::string typeString =
66 accessibility::ScreenMagnifierNameFromType(type);
67 if (typeString != prefs->GetString(prefs::kMagnifierType)) {
68 prefs->SetString(prefs::kMagnifierType, typeString);
69 prefs->CommitPendingWrite();
70 }
71 }
72 }
73
74 ash::Shell::GetInstance()->magnification_controller()->SetEnabled(
75 type == ash::MAGNIFIER_FULL);
76 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled(
77 type == ash::MAGNIFIER_PARTIAL);
78
79 NotifyMagnifierTypeChanged(type);
80 }
81
82 void AddObserver(MagnificationObserver* observer) OVERRIDE {
83 observers_.AddObserver(observer);
84 }
85
86 void RemoveObserver(MagnificationObserver* observer) OVERRIDE {
87 observers_.RemoveObserver(observer);
88 }
89
90 private:
91 void NotifyMagnifierTypeChanged(ash::MagnifierType new_type) {
92 FOR_EACH_OBSERVER(MagnificationObserver, observers_,
93 OnMagnifierTypeChanged(new_type));
94 }
95
96 ash::MagnifierType GetMagnifierTypeFromPref() {
55 if (!profile_) 97 if (!profile_)
56 return ash::MAGNIFIER_OFF; 98 return ash::MAGNIFIER_OFF;
57 99
58 PrefService* prefs = profile_->GetPrefs(); 100 PrefService* prefs = profile_->GetPrefs();
59 if (!prefs) 101 if (!prefs)
60 return ash::MAGNIFIER_OFF; 102 return ash::MAGNIFIER_OFF;
61 103
62 return accessibility::MagnifierTypeFromName( 104 return accessibility::MagnifierTypeFromName(
63 prefs->GetString(prefs::kMagnifierType).c_str()); 105 prefs->GetString(prefs::kMagnifierType).c_str());
64 } 106 }
65 107
66 void SetMagnifier(ash::MagnifierType type) OVERRIDE {
67 PrefService* prefs = profile_->GetPrefs();
68 if (prefs) {
69 std::string typeString = accessibility::ScreenMagnifierNameFromType(type);
70 if (typeString != prefs->GetString(prefs::kMagnifierType)) {
71 prefs->SetString(prefs::kMagnifierType, typeString);
72 prefs->CommitPendingWrite();
73 }
74 }
75
76 ash::Shell::GetInstance()->system_tray_notifier()->
77 NotifyAccessibilityModeChanged();
78
79 ash::Shell::GetInstance()->magnification_controller()->SetEnabled(
80 type == ash::MAGNIFIER_FULL);
81 ash::Shell::GetInstance()->partial_magnification_controller()->SetEnabled(
82 type == ash::MAGNIFIER_PARTIAL);
83 }
84
85 private:
86 void SetProfile(Profile* profile) { 108 void SetProfile(Profile* profile) {
87 if (pref_change_registrar_) { 109 if (pref_change_registrar_) {
88 pref_change_registrar_.reset(); 110 pref_change_registrar_.reset();
89 } 111 }
90 112
91 if (profile) { 113 if (profile) {
92 pref_change_registrar_.reset(new PrefChangeRegistrar); 114 pref_change_registrar_.reset(new PrefChangeRegistrar);
93 pref_change_registrar_->Init(profile->GetPrefs()); 115 pref_change_registrar_->Init(profile->GetPrefs());
94 pref_change_registrar_->Add( 116 pref_change_registrar_->Add(
95 prefs::kMagnifierType, 117 prefs::kMagnifierType,
96 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus, 118 base::Bind(&MagnificationManagerImpl::UpdateMagnifierStatus,
97 base::Unretained(this))); 119 base::Unretained(this)));
98 } 120 }
99 121
100 profile_ = profile; 122 profile_ = profile;
101 UpdateMagnifierStatus(); 123 UpdateMagnifierStatus();
102 } 124 }
103 125
104 void UpdateMagnifierStatus() { 126 void UpdateMagnifierStatus() {
105 ash::MagnifierType type = GetMagnifierType(); 127 ash::MagnifierType type = GetMagnifierTypeFromPref();
106 SetMagnifier(type); 128 SetMagnifier(type);
107 } 129 }
108 130
131 // content::NotificationObserver implimentation:
109 virtual void Observe(int type, 132 virtual void Observe(int type,
110 const content::NotificationSource& source, 133 const content::NotificationSource& source,
111 const content::NotificationDetails& details) OVERRIDE { 134 const content::NotificationDetails& details) OVERRIDE {
112 switch (type) { 135 switch (type) {
113 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: 136 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE:
114 case chrome::NOTIFICATION_SESSION_STARTED: { 137 case chrome::NOTIFICATION_SESSION_STARTED: {
115 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 138 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
116 SetProfile(profile); 139 SetProfile(profile);
117 break; 140 break;
118 } 141 }
142 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
143 SetProfile(NULL);
144 break;
145 }
119 } 146 }
120 } 147 }
121 148
122 static MagnificationManagerImpl* instance_;
123
124 Profile* profile_; 149 Profile* profile_;
150 ash::MagnifierType type_;
125 content::NotificationRegistrar registrar_; 151 content::NotificationRegistrar registrar_;
126 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; 152 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
127 153
128 friend struct DefaultSingletonTraits<MagnificationManagerImpl>; 154 ObserverList<MagnificationObserver> observers_;
129 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); 155 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl);
130 }; 156 };
131 157
132 MagnificationManagerImpl* MagnificationManagerImpl::instance_ = NULL; 158 // static
133 159 void MagnificationManager::Initialize() {
134 MagnificationManager* MagnificationManager::GetInstance() { 160 CHECK(g_magnification_manager == NULL);
135 return MagnificationManagerImpl::GetInstance(); 161 g_magnification_manager = new MagnificationManagerImpl();
136 } 162 }
137 163
138 MagnificationManager* MagnificationManager::CreateInstance() { 164 // static
139 // Makes sure that this is not called more than once. 165 void MagnificationManager::Shutdown() {
140 CHECK(!GetInstance()); 166 CHECK(g_magnification_manager);
167 delete g_magnification_manager;
168 g_magnification_manager = NULL;
169 }
141 170
142 return new MagnificationManagerImpl(); 171 // static
172 MagnificationManager* MagnificationManager::Get() {
173 return g_magnification_manager;
143 } 174 }
144 175
145 } // namespace chromeos 176 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698