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

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

Issue 14200048: Introduce AcccessibilityManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/accessibility_util.h" 5 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
6
7 #include <queue>
8 6
9 #include "ash/high_contrast/high_contrast_controller.h" 7 #include "ash/high_contrast/high_contrast_controller.h"
10 #include "ash/magnifier/magnification_controller.h"
11 #include "ash/magnifier/partial_magnification_controller.h"
12 #include "ash/shell.h" 8 #include "ash/shell.h"
13 #include "ash/shell_delegate.h" 9 #include "ash/system/tray/system_tray_notifier.h"
14 #include "base/bind.h" 10 #include "base/memory/scoped_ptr.h"
15 #include "base/bind_helpers.h" 11 #include "base/memory/singleton.h"
16 #include "base/logging.h"
17 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/prefs/pref_member.h"
18 #include "base/prefs/pref_service.h" 14 #include "base/prefs/pref_service.h"
19 #include "chrome/browser/accessibility/accessibility_extension_api.h" 15 #include "chrome/browser/accessibility/accessibility_extension_api.h"
20 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chromeos/accessibility/magnification_manager.h" 17 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
18 #include "chrome/browser/chromeos/login/user_manager.h"
22 #include "chrome/browser/extensions/component_loader.h" 19 #include "chrome/browser/extensions/component_loader.h"
23 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/extensions/extension_system.h" 21 #include "chrome/browser/extensions/extension_system.h"
25 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_manager.h" 23 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/speech/tts_controller.h" 24 #include "chrome/browser/speech/tts_controller.h"
28 #include "chrome/browser/ui/singleton_tabs.h" 25 #include "chrome/common/chrome_notification_types.h"
29 #include "chrome/common/extensions/extension.h" 26 #include "chrome/common/extensions/extension.h"
30 #include "chrome/common/extensions/extension_messages.h" 27 #include "chrome/common/extensions/extension_messages.h"
31 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" 28 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
32 #include "chrome/common/extensions/user_script.h"
33 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h"
35 #include "content/public/browser/browser_accessibility_state.h" 30 #include "content/public/browser/browser_accessibility_state.h"
31 #include "content/public/browser/notification_observer.h"
32 #include "content/public/browser/notification_registrar.h"
36 #include "content/public/browser/notification_service.h" 33 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/render_process_host.h" 34 #include "content/public/browser/render_process_host.h"
38 #include "content/public/browser/render_view_host.h" 35 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
40 #include "content/public/browser/web_ui.h" 37 #include "content/public/browser/web_ui.h"
41 #include "extensions/browser/file_reader.h" 38 #include "extensions/browser/file_reader.h"
42 #include "extensions/common/extension_resource.h" 39 #include "extensions/common/extension_resource.h"
43 #include "googleurl/src/gurl.h"
44 #include "grit/browser_resources.h" 40 #include "grit/browser_resources.h"
45 #include "grit/generated_resources.h" 41 #include "grit/generated_resources.h"
46 #include "ui/base/l10n/l10n_util.h" 42 #include "ui/base/l10n/l10n_util.h"
47 #include "ui/base/resource/resource_bundle.h" 43 #include "ui/base/resource/resource_bundle.h"
48 44
49 using content::RenderViewHost; 45 using content::RenderViewHost;
50 46
51 namespace chromeos { 47 namespace chromeos {
52 namespace accessibility { 48
49 namespace {
50
51 static chromeos::AccessibilityManager* g_accessibility_manager = NULL;
53 52
54 // Helper class that directly loads an extension's content scripts into 53 // Helper class that directly loads an extension's content scripts into
55 // all of the frames corresponding to a given RenderViewHost. 54 // all of the frames corresponding to a given RenderViewHost.
56 class ContentScriptLoader { 55 class ContentScriptLoader {
57 public: 56 public:
58 // Initialize the ContentScriptLoader with the ID of the extension 57 // Initialize the ContentScriptLoader with the ID of the extension
59 // and the RenderViewHost where the scripts should be loaded. 58 // and the RenderViewHost where the scripts should be loaded.
60 ContentScriptLoader(const std::string& extension_id, 59 ContentScriptLoader(const std::string& extension_id,
61 int render_process_id, 60 int render_process_id,
62 int render_view_id) 61 int render_view_id)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 } 105 }
107 Run(); 106 Run();
108 } 107 }
109 108
110 std::string extension_id_; 109 std::string extension_id_;
111 int render_process_id_; 110 int render_process_id_;
112 int render_view_id_; 111 int render_view_id_;
113 std::queue<extensions::ExtensionResource> resources_; 112 std::queue<extensions::ExtensionResource> resources_;
114 }; 113 };
115 114
116 void UpdateChromeOSAccessibilityHistograms() { 115 } // namespace
117 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback", 116
118 IsSpokenFeedbackEnabled()); 117 // static
119 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast", 118 void AccessibilityManager::Initialize() {
120 IsHighContrastEnabled()); 119 CHECK(g_accessibility_manager == NULL);
121 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard", 120 g_accessibility_manager = new AccessibilityManager();
122 IsVirtualKeyboardEnabled());
123 if (MagnificationManager::Get()) {
124 uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ?
125 MagnificationManager::Get()->GetMagnifierType() : 0;
126 // '0' means magnifier is disabled.
127 UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier",
128 type,
129 ash::kMaxMagnifierType + 1);
130 }
131 } 121 }
132 122
133 void Initialize() { 123 // static
134 content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback( 124 void AccessibilityManager::Shutdown() {
135 base::Bind(&UpdateChromeOSAccessibilityHistograms)); 125 CHECK(g_accessibility_manager);
126 delete g_accessibility_manager;
127 g_accessibility_manager = NULL;
136 } 128 }
137 129
138 void EnableSpokenFeedback(bool enabled, 130 // static
131 AccessibilityManager* AccessibilityManager::Get() {
132 CHECK(g_accessibility_manager);
133 return g_accessibility_manager;
134 }
135
136 AccessibilityManager::AccessibilityManager() : profile_(NULL),
137 spoken_feedback_enabled_(false),
138 high_contrast_enabled_(false) {
139 registrar_.Add(this,
140 chrome::NOTIFICATION_SESSION_STARTED,
141 content::NotificationService::AllSources());
142 registrar_.Add(this,
143 chrome::NOTIFICATION_PROFILE_CREATED,
144 content::NotificationService::AllSources());
145 registrar_.Add(this,
146 chrome::NOTIFICATION_PROFILE_DESTROYED,
147 content::NotificationService::AllSources());
148 registrar_.Add(this,
149 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE,
150 content::NotificationService::AllSources());
151 }
152
153 AccessibilityManager::~AccessibilityManager() {
154 CHECK(this == g_accessibility_manager);
155 }
156
157 void AccessibilityManager::EnableSpokenFeedback(bool enabled,
139 content::WebUI* login_web_ui, 158 content::WebUI* login_web_ui,
Zachary Kuznia 2013/04/24 04:21:03 Fix indent,.
yoshiki 2013/04/24 06:00:52 Done.
140 ash::AccessibilityNotificationVisibility notify) { 159 ash::AccessibilityNotificationVisibility notify) {
141 bool spoken_feedback_enabled = g_browser_process && 160 if (!profile_)
142 g_browser_process->local_state()->GetBoolean( 161 return;
143 prefs::kSpokenFeedbackEnabled); 162
144 if (spoken_feedback_enabled == enabled) { 163 if (spoken_feedback_enabled_ == enabled) {
145 DLOG(INFO) << "Spoken feedback is already " << 164 DLOG(INFO) << "Spoken feedback is already " <<
146 (enabled ? "enabled" : "disabled") << ". Going to do nothing."; 165 (enabled ? "enabled" : "disabled") << ". Going to do nothing.";
147 return; 166 return;
148 } 167 }
149 168
150 g_browser_process->local_state()->SetBoolean( 169 spoken_feedback_enabled_ = enabled;
170
171 PrefService* pref_service = profile_->GetPrefs();
172 pref_service->SetBoolean(
151 prefs::kSpokenFeedbackEnabled, enabled); 173 prefs::kSpokenFeedbackEnabled, enabled);
152 g_browser_process->local_state()->CommitPendingWrite(); 174 pref_service->CommitPendingWrite();
153 ExtensionAccessibilityEventRouter::GetInstance()-> 175 ExtensionAccessibilityEventRouter::GetInstance()->
154 SetAccessibilityEnabled(enabled); 176 SetAccessibilityEnabled(enabled);
155 177
156 AccessibilityStatusEventDetails details(enabled, notify); 178 AccessibilityStatusEventDetails details(enabled, notify);
157 content::NotificationService::current()->Notify( 179 content::NotificationService::current()->Notify(
158 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK, 180 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK,
159 content::NotificationService::AllSources(), 181 content::NotificationService::AllSources(),
160 content::Details<AccessibilityStatusEventDetails>(&details)); 182 content::Details<AccessibilityStatusEventDetails>(&details));
161 183
162 Speak(l10n_util::GetStringUTF8( 184 Speak(l10n_util::GetStringUTF8(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 loader->Run(); // It cleans itself up when done. 234 loader->Run(); // It cleans itself up when done.
213 } 235 }
214 236
215 DLOG(INFO) << "ChromeVox was Loaded."; 237 DLOG(INFO) << "ChromeVox was Loaded.";
216 } else { // Unload ChromeVox 238 } else { // Unload ChromeVox
217 extension_service->component_loader()->Remove(path); 239 extension_service->component_loader()->Remove(path);
218 DLOG(INFO) << "ChromeVox was Unloaded."; 240 DLOG(INFO) << "ChromeVox was Unloaded.";
219 } 241 }
220 } 242 }
221 243
222 void EnableHighContrast(bool enabled) { 244 bool AccessibilityManager::IsSpokenFeedbackEnabled() {
223 PrefService* pref_service = g_browser_process->local_state(); 245 if (!profile_)
224 pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled); 246 return false;
225 pref_service->CommitPendingWrite();
226 247
227 AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE); 248 PrefService* pref_service = profile_->GetPrefs();
228 content::NotificationService::current()->Notify( 249 bool spoken_feedback_enabled =
229 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE, 250 pref_service->GetBoolean(prefs::kSpokenFeedbackEnabled);
230 content::NotificationService::AllSources(), 251 return spoken_feedback_enabled;
231 content::Details<AccessibilityStatusEventDetails>(&detail));
232
233 #if defined(USE_ASH)
234 ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled);
235 #endif
236 } 252 }
237 253
238 void EnableVirtualKeyboard(bool enabled) { 254 void AccessibilityManager::ToggleSpokenFeedback(content::WebUI* login_web_ui,
239 PrefService* pref_service = g_browser_process->local_state(); 255 ash::AccessibilityNotificationVisibility notify) {
Zachary Kuznia 2013/04/24 04:21:03 Fix indent, or newline after (
yoshiki 2013/04/24 06:00:52 Done.
240 pref_service->SetBoolean(prefs::kVirtualKeyboardEnabled, enabled); 256 bool spoken_feedback_enabled = IsSpokenFeedbackEnabled();
241 pref_service->CommitPendingWrite(); 257 spoken_feedback_enabled = !spoken_feedback_enabled;
258 EnableSpokenFeedback(spoken_feedback_enabled, login_web_ui, notify);
242 } 259 }
243 260
244 void ToggleSpokenFeedback(content::WebUI* login_web_ui, 261 void AccessibilityManager::Speak(const std::string& text) {
245 ash::AccessibilityNotificationVisibility notify) {
246 bool spoken_feedback_enabled = g_browser_process &&
247 g_browser_process->local_state()->GetBoolean(
248 prefs::kSpokenFeedbackEnabled);
249 spoken_feedback_enabled = !spoken_feedback_enabled;
250 EnableSpokenFeedback(spoken_feedback_enabled, login_web_ui, notify);
251 };
252
253 void Speak(const std::string& text) {
254 UtteranceContinuousParameters params; 262 UtteranceContinuousParameters params;
255 263
256 Profile* profile = ProfileManager::GetDefaultProfile(); 264 Utterance* utterance = new Utterance(profile_);
257 Utterance* utterance = new Utterance(profile);
258 utterance->set_text(text); 265 utterance->set_text(text);
259 utterance->set_lang(g_browser_process->GetApplicationLocale()); 266 utterance->set_lang(g_browser_process->GetApplicationLocale());
260 utterance->set_continuous_parameters(params); 267 utterance->set_continuous_parameters(params);
261 utterance->set_can_enqueue(false); 268 utterance->set_can_enqueue(false);
262 utterance->set_options(new DictionaryValue()); 269 utterance->set_options(new DictionaryValue());
263 270
264 TtsController* controller = TtsController::GetInstance(); 271 TtsController* controller = TtsController::GetInstance();
265 controller->SpeakOrEnqueue(utterance); 272 controller->SpeakOrEnqueue(utterance);
266 } 273 }
267 274
268 bool IsSpokenFeedbackEnabled() { 275 void AccessibilityManager::MaybeSpeak(const std::string& utterance) {
269 if (!g_browser_process) { 276 if (AccessibilityManager::Get() &&
270 return false; 277 AccessibilityManager::Get()->IsSpokenFeedbackEnabled())
Zachary Kuznia 2013/04/24 04:21:03 Fix indent
yoshiki 2013/04/24 06:00:52 Done.
271 } 278 Speak(utterance);
272 PrefService* prefs = g_browser_process->local_state();
273 bool spoken_feedback_enabled = prefs &&
274 prefs->GetBoolean(prefs::kSpokenFeedbackEnabled);
275 return spoken_feedback_enabled;
276 } 279 }
277 280
278 bool IsHighContrastEnabled() { 281 void AccessibilityManager::EnableHighContrast(bool enabled) {
279 if (!g_browser_process) { 282 if (!profile_)
283 return;
284
285 if (high_contrast_enabled_ == enabled)
286 return;
287
288 high_contrast_enabled_ = enabled;
289
290 PrefService* pref_service = profile_->GetPrefs();
291 pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled);
292 pref_service->CommitPendingWrite();
293
294 AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE);
295 content::NotificationService::current()->Notify(
296 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE,
297 content::NotificationService::AllSources(),
298 content::Details<AccessibilityStatusEventDetails>(&detail));
299
300 #if defined(USE_ASH)
301 ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled);
302 #endif
303 }
304
305 bool AccessibilityManager::IsHighContrastEnabled() {
306 if (!profile_)
280 return false; 307 return false;
281 } 308
282 PrefService* prefs = g_browser_process->local_state(); 309 PrefService* pref_service = profile_->GetPrefs();
283 bool high_contrast_enabled = prefs && 310 bool high_contrast_enabled =
284 prefs->GetBoolean(prefs::kHighContrastEnabled); 311 pref_service->GetBoolean(prefs::kHighContrastEnabled);
285 return high_contrast_enabled; 312 return high_contrast_enabled;
286 } 313 }
287 314
288 bool IsVirtualKeyboardEnabled() { 315 void AccessibilityManager::UpdateSpokenFeedbackStatus() {
289 if (!g_browser_process) { 316 EnableSpokenFeedback(
Zachary Kuznia 2013/04/24 04:21:03 Add a comment explaining this.
yoshiki 2013/04/24 06:00:52 Changes the method name to indicate the operation.
290 return false; 317 IsSpokenFeedbackEnabled(), NULL, ash::A11Y_NOTIFICATION_NONE);
291 }
292 PrefService* prefs = g_browser_process->local_state();
293 bool virtual_keyboard_enabled = prefs &&
294 prefs->GetBoolean(prefs::kVirtualKeyboardEnabled);
295 return virtual_keyboard_enabled;
296 } 318 }
297 319
298 void MaybeSpeak(const std::string& utterance) { 320 void AccessibilityManager::UpdateHighContrastStatus() {
299 if (IsSpokenFeedbackEnabled()) 321 EnableHighContrast(IsHighContrastEnabled());
Zachary Kuznia 2013/04/24 04:21:03 Add a comment explaining this line.
yoshiki 2013/04/24 06:00:52 Done.
300 Speak(utterance);
301 } 322 }
302 323
303 void ShowAccessibilityHelp(Browser* browser) { 324 void AccessibilityManager::SetProfile(Profile* profile) {
304 chrome::ShowSingletonTab(browser, GURL(chrome::kChromeAccessibilityHelpURL)); 325 if (pref_change_registrar_) {
Zachary Kuznia 2013/04/24 04:21:03 No {} for one line if clause. Also, you can just
yoshiki 2013/04/24 06:00:52 Done.
326 pref_change_registrar_.reset();
327 }
328
329 if (profile) {
330 pref_change_registrar_.reset(new PrefChangeRegistrar);
331 pref_change_registrar_->Init(profile->GetPrefs());
332 pref_change_registrar_->Add(
333 prefs::kSpokenFeedbackEnabled,
334 base::Bind(&AccessibilityManager::UpdateSpokenFeedbackStatus,
335 base::Unretained(this)));
336 pref_change_registrar_->Add(
337 prefs::kHighContrastEnabled,
338 base::Bind(&AccessibilityManager::UpdateHighContrastStatus,
339 base::Unretained(this)));
340
341 content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback(
342 base::Bind(
343 &AccessibilityManager::UpdateChromeOSAccessibilityHistograms,
344 base::Unretained(this)));
345 }
346
347 profile_ = profile;
348 UpdateSpokenFeedbackStatus();
349 UpdateHighContrastStatus();
305 } 350 }
306 351
307 } // namespace accessibility 352 void AccessibilityManager::SetProfileForTest(Profile* profile) {
353 SetProfile(profile);
354 }
355
356 void AccessibilityManager::UpdateChromeOSAccessibilityHistograms() {
357 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback",
358 IsSpokenFeedbackEnabled());
359 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast",
360 IsHighContrastEnabled());
361 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard",
362 accessibility::IsVirtualKeyboardEnabled());
363 if (MagnificationManager::Get()) {
364 uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ?
365 MagnificationManager::Get()->GetMagnifierType() : 0;
366 // '0' means magnifier is disabled.
367 UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier",
368 type,
369 ash::kMaxMagnifierType + 1);
370 }
371 }
372
373 void AccessibilityManager::Observe(
374 int type,
375 const content::NotificationSource& source,
376 const content::NotificationDetails& details) {
377 switch (type) {
378 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE:
379 case chrome::NOTIFICATION_SESSION_STARTED: {
380 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
381 SetProfile(profile);
382 break;
383 }
384 case chrome::NOTIFICATION_PROFILE_CREATED: {
385 Profile* profile = content::Source<Profile>(source).ptr();
386 SetProfile(profile);
387 break;
388 }
389 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
390 SetProfile(NULL);
391 break;
392 }
393 }
394 }
395
308 } // namespace chromeos 396 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698