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) | |
63 ash::Shell::GetInstance()->magnification_controller()->SetEnabled(enabled); | |
64 #endif | |
65 } | |
66 | |
67 private: | |
68 void SetProfile(Profile* profile) { | |
69 if (profile) { | |
70 pref_change_registrar_.reset(new PrefChangeRegistrar); | |
71 pref_change_registrar_->Init(profile->GetPrefs()); | |
72 pref_change_registrar_->Add(prefs::kScreenMagnifierEnabled, this); | |
Zachary Kuznia
2012/10/05 04:42:21
Do you need to remove from the old profile?
yoshiki
2012/10/11 04:03:24
Done.
| |
73 } else { | |
74 pref_change_registrar_.reset(); | |
75 } | |
76 | |
77 profile_ = profile; | |
78 UpdateMagnifierStatus(); | |
79 } | |
80 | |
81 void UpdateMagnifierStatus() { | |
82 UserManager* manager = UserManager::Get(); | |
83 if (!profile_) { | |
84 SetEnabled(false); | |
85 } else if (manager && !manager->IsSessionStarted()) { | |
86 SetEnabled(true); | |
87 } else { | |
88 PrefService* pref_service = profile_->GetPrefs(); | |
89 bool enabled = pref_service->GetBoolean(prefs::kScreenMagnifierEnabled); | |
90 SetEnabled(enabled); | |
91 } | |
92 } | |
93 | |
94 // content::NotificationObserver implimentation: | |
95 virtual void Observe(int type, | |
96 const content::NotificationSource& source, | |
97 const content::NotificationDetails& details) OVERRIDE { | |
98 switch (type) { | |
99 case chrome::NOTIFICATION_PREF_CHANGED: { | |
100 std::string pref = *content::Details<std::string>(details).ptr(); | |
101 if (pref == prefs::kScreenMagnifierEnabled) | |
102 UpdateMagnifierStatus(); | |
103 break; | |
104 } | |
105 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: | |
106 case chrome::NOTIFICATION_SESSION_STARTED: { | |
107 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
108 SetProfile(profile); | |
109 break; | |
110 } | |
111 } | |
112 } | |
113 | |
114 static MagnificationManagerImpl* instance_; | |
115 | |
116 Profile* profile_; | |
117 content::NotificationRegistrar registrar_; | |
118 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; | |
119 | |
120 friend struct DefaultSingletonTraits<MagnificationManagerImpl>; | |
121 DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); | |
122 }; | |
123 | |
124 MagnificationManagerImpl* MagnificationManagerImpl::instance_ = NULL; | |
125 | |
126 MagnificationManager* MagnificationManager::GetInstance() { | |
127 return MagnificationManagerImpl::GetInstance(); | |
128 } | |
129 | |
130 MagnificationManager* MagnificationManager::CreateInstance() { | |
131 return new MagnificationManagerImpl(); | |
132 } | |
133 | |
134 } // namespace chromeos | |
OLD | NEW |