OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/accessibility/magnification_manager.h" | |
6 | |
7 #include "ash/magnifier/magnification_controller.h" | |
8 #include "ash/shell.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "chrome/browser/api/prefs/pref_change_registrar.h" | |
12 #include "chrome/browser/api/prefs/pref_member.h" | |
13 #include "chrome/browser/chromeos/login/user_manager.h" | |
14 #include "chrome/browser/prefs/pref_service.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/profiles/profile_manager.h" | |
17 #include "chrome/common/chrome_notification_types.h" | |
18 #include "chrome/common/pref_names.h" | |
19 #include "content/public/browser/notification_observer.h" | |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 | |
24 namespace chromeos { | |
25 | |
26 class MagnificationManagerImpl : public MagnificationManager, | |
27 public content::NotificationObserver { | |
28 public: | |
29 MagnificationManagerImpl() { | |
30 DCHECK(!instance_); | |
31 instance_ = this; | |
32 | |
33 registrar_.Add(this, | |
34 chrome::NOTIFICATION_SESSION_STARTED, | |
35 content::NotificationService::AllSources()); | |
36 registrar_.Add(this, | |
37 chrome::NOTIFICATION_PROFILE_CREATED, | |
38 content::NotificationService::AllSources()); | |
39 registrar_.Add(this, | |
40 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, | |
41 content::NotificationService::AllSources()); | |
42 | |
43 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
44 SetProfile(profile); | |
45 } | |
46 virtual ~MagnificationManagerImpl() {} | |
47 | |
48 static MagnificationManagerImpl* GetInstance() { | |
49 return instance_; | |
50 } | |
51 | |
52 // MagnificationManager implimentation: | |
53 bool IsEnabled() OVERRIDE { | |
54 #if defined(USE_ASH) | |
55 return ash::Shell::GetInstance()->magnification_controller()->IsEnabled(); | |
56 #else | |
57 return false; | |
58 #endif | |
59 } | |
60 | |
61 void SetEnabled(bool enabled) OVERRIDE { | |
62 #if defined(USE_ASH) | |
DaveMoore
2012/10/11 20:09:27
Should there be a check here that it's not called
yoshiki
2012/10/12 15:00:55
Done.
| |
63 ash::Shell::GetInstance()->magnification_controller()->SetEnabled(enabled); | |
64 #endif | |
65 } | |
66 | |
67 private: | |
68 void SetProfile(Profile* profile) { | |
69 if (pref_change_registrar_) { | |
70 pref_change_registrar_->Remove(prefs::kScreenMagnifierEnabled, this); | |
71 pref_change_registrar_.reset(); | |
72 } | |
73 | |
74 if (profile) { | |
75 pref_change_registrar_.reset(new PrefChangeRegistrar); | |
76 pref_change_registrar_->Init(profile->GetPrefs()); | |
77 pref_change_registrar_->Add(prefs::kScreenMagnifierEnabled, this); | |
78 } | |
79 | |
80 profile_ = profile; | |
81 UpdateMagnifierStatus(); | |
82 } | |
83 | |
84 void UpdateMagnifierStatus() { | |
85 UserManager* manager = UserManager::Get(); | |
86 if (!profile_) { | |
87 SetEnabled(false); | |
88 } else if (manager && !manager->IsSessionStarted()) { | |
89 SetEnabled(true); | |
90 } else { | |
91 PrefService* pref_service = profile_->GetPrefs(); | |
92 bool enabled = pref_service->GetBoolean(prefs::kScreenMagnifierEnabled); | |
93 SetEnabled(enabled); | |
94 } | |
95 } | |
96 | |
97 // content::NotificationObserver implimentation: | |
98 virtual void Observe(int type, | |
99 const content::NotificationSource& source, | |
100 const content::NotificationDetails& details) OVERRIDE { | |
101 switch (type) { | |
102 case chrome::NOTIFICATION_PREF_CHANGED: { | |
103 std::string pref = *content::Details<std::string>(details).ptr(); | |
104 if (pref == prefs::kScreenMagnifierEnabled) | |
105 UpdateMagnifierStatus(); | |
106 break; | |
107 } | |
108 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: | |
109 case chrome::NOTIFICATION_SESSION_STARTED: { | |
110 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
111 SetProfile(profile); | |
112 break; | |
113 } | |
114 } | |
115 } | |
116 | |
117 static MagnificationManagerImpl* instance_; | |
118 | |
119 Profile* profile_; | |
120 content::NotificationRegistrar registrar_; | |
121 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | |
122 | |
123 friend struct DefaultSingletonTraits<MagnificationManagerImpl>; | |
124 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); | |
125 }; | |
126 | |
127 MagnificationManagerImpl* MagnificationManagerImpl::instance_ = NULL; | |
128 | |
129 MagnificationManager* MagnificationManager::GetInstance() { | |
130 return MagnificationManagerImpl::GetInstance(); | |
131 } | |
132 | |
133 MagnificationManager* MagnificationManager::CreateInstance() { | |
DaveMoore
2012/10/11 20:09:27
Please add a check that this isn't called more tha
yoshiki
2012/10/12 15:00:55
Done.
| |
134 return new MagnificationManagerImpl(); | |
135 } | |
136 | |
137 } // namespace chromeos | |
OLD | NEW |