Index: chrome/browser/chromeos/accessibility/accessibility_manager.cc |
diff --git a/chrome/browser/chromeos/accessibility/accessibility_util.cc b/chrome/browser/chromeos/accessibility/accessibility_manager.cc |
similarity index 58% |
copy from chrome/browser/chromeos/accessibility/accessibility_util.cc |
copy to chrome/browser/chromeos/accessibility/accessibility_manager.cc |
index 2b3aee2d1b88564f317dc11cd93b1ee13cae6ccc..0ae929032547dfd4168ea044572b08b70c15f4b5 100644 |
--- a/chrome/browser/chromeos/accessibility/accessibility_util.cc |
+++ b/chrome/browser/chromeos/accessibility/accessibility_manager.cc |
@@ -1,38 +1,35 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/chromeos/accessibility/accessibility_util.h" |
- |
-#include <queue> |
+#include "chrome/browser/chromeos/accessibility/accessibility_manager.h" |
#include "ash/high_contrast/high_contrast_controller.h" |
-#include "ash/magnifier/magnification_controller.h" |
-#include "ash/magnifier/partial_magnification_controller.h" |
#include "ash/shell.h" |
-#include "ash/shell_delegate.h" |
-#include "base/bind.h" |
-#include "base/bind_helpers.h" |
-#include "base/logging.h" |
+#include "ash/system/tray/system_tray_notifier.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/singleton.h" |
#include "base/metrics/histogram.h" |
+#include "base/prefs/pref_member.h" |
#include "base/prefs/pref_service.h" |
#include "chrome/browser/accessibility/accessibility_extension_api.h" |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/chromeos/accessibility/magnification_manager.h" |
+#include "chrome/browser/chromeos/login/user_manager.h" |
#include "chrome/browser/extensions/component_loader.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/extensions/extension_system.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/profiles/profile_manager.h" |
#include "chrome/browser/speech/tts_controller.h" |
-#include "chrome/browser/ui/singleton_tabs.h" |
+#include "chrome/common/chrome_notification_types.h" |
#include "chrome/common/extensions/extension.h" |
#include "chrome/common/extensions/extension_messages.h" |
#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" |
-#include "chrome/common/extensions/user_script.h" |
#include "chrome/common/pref_names.h" |
-#include "chrome/common/url_constants.h" |
#include "content/public/browser/browser_accessibility_state.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
#include "content/public/browser/notification_service.h" |
#include "content/public/browser/render_process_host.h" |
#include "content/public/browser/render_view_host.h" |
@@ -40,7 +37,6 @@ |
#include "content/public/browser/web_ui.h" |
#include "extensions/browser/file_reader.h" |
#include "extensions/common/extension_resource.h" |
-#include "googleurl/src/gurl.h" |
#include "grit/browser_resources.h" |
#include "grit/generated_resources.h" |
#include "ui/base/l10n/l10n_util.h" |
@@ -49,7 +45,10 @@ |
using content::RenderViewHost; |
namespace chromeos { |
-namespace accessibility { |
+ |
+namespace { |
+ |
+static chromeos::AccessibilityManager* g_accessibility_manager = NULL; |
// Helper class that directly loads an extension's content scripts into |
// all of the frames corresponding to a given RenderViewHost. |
@@ -113,43 +112,89 @@ class ContentScriptLoader { |
std::queue<extensions::ExtensionResource> resources_; |
}; |
-void UpdateChromeOSAccessibilityHistograms() { |
- UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback", |
- IsSpokenFeedbackEnabled()); |
- UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast", |
- IsHighContrastEnabled()); |
- UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard", |
- IsVirtualKeyboardEnabled()); |
- if (MagnificationManager::Get()) { |
- uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ? |
- MagnificationManager::Get()->GetMagnifierType() : 0; |
- // '0' means magnifier is disabled. |
- UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier", |
- type, |
- ash::kMaxMagnifierType + 1); |
- } |
+} // namespace |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+// AccessibilityStatusEventDetails |
+ |
+AccessibilityStatusEventDetails::AccessibilityStatusEventDetails( |
+ bool enabled, |
+ ash::AccessibilityNotificationVisibility notify) |
+ : enabled(enabled), |
+ magnifier_type(ash::kDefaultMagnifierType), |
+ notify(notify) {} |
+ |
+AccessibilityStatusEventDetails::AccessibilityStatusEventDetails( |
+ bool enabled, |
+ ash::MagnifierType magnifier_type, |
+ ash::AccessibilityNotificationVisibility notify) |
+ : enabled(enabled), |
+ magnifier_type(magnifier_type), |
+ notify(notify) {} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+// |
+// AccessibilityManager |
+ |
+// static |
+void AccessibilityManager::Initialize() { |
+ CHECK(g_accessibility_manager == NULL); |
+ g_accessibility_manager = new AccessibilityManager(); |
+} |
+ |
+// static |
+void AccessibilityManager::Shutdown() { |
+ CHECK(g_accessibility_manager); |
+ delete g_accessibility_manager; |
+ g_accessibility_manager = NULL; |
} |
-void Initialize() { |
- content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback( |
- base::Bind(&UpdateChromeOSAccessibilityHistograms)); |
+// static |
+AccessibilityManager* AccessibilityManager::Get() { |
+ CHECK(g_accessibility_manager); |
+ return g_accessibility_manager; |
} |
-void EnableSpokenFeedback(bool enabled, |
- content::WebUI* login_web_ui, |
- ash::AccessibilityNotificationVisibility notify) { |
- bool spoken_feedback_enabled = g_browser_process && |
- g_browser_process->local_state()->GetBoolean( |
- prefs::kSpokenFeedbackEnabled); |
- if (spoken_feedback_enabled == enabled) { |
+AccessibilityManager::AccessibilityManager() : profile_(NULL), |
+ spoken_feedback_enabled_(false), |
+ high_contrast_enabled_(false) { |
+ notification_registrar_.Add(this, |
+ chrome::NOTIFICATION_SESSION_STARTED, |
+ content::NotificationService::AllSources()); |
+ notification_registrar_.Add(this, |
+ chrome::NOTIFICATION_PROFILE_CREATED, |
+ content::NotificationService::AllSources()); |
+ notification_registrar_.Add(this, |
+ chrome::NOTIFICATION_PROFILE_DESTROYED, |
+ content::NotificationService::AllSources()); |
+ notification_registrar_.Add(this, |
+ chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
+ content::NotificationService::AllSources()); |
+} |
+ |
+AccessibilityManager::~AccessibilityManager() { |
+ CHECK(this == g_accessibility_manager); |
+} |
+ |
+void AccessibilityManager::EnableSpokenFeedback( |
+ bool enabled, |
+ content::WebUI* login_web_ui, |
+ ash::AccessibilityNotificationVisibility notify) { |
+ if (!profile_) |
+ return; |
+ |
+ if (spoken_feedback_enabled_ == enabled) { |
DLOG(INFO) << "Spoken feedback is already " << |
(enabled ? "enabled" : "disabled") << ". Going to do nothing."; |
return; |
} |
- g_browser_process->local_state()->SetBoolean( |
+ spoken_feedback_enabled_ = enabled; |
+ |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ pref_service->SetBoolean( |
prefs::kSpokenFeedbackEnabled, enabled); |
- g_browser_process->local_state()->CommitPendingWrite(); |
+ pref_service->CommitPendingWrite(); |
ExtensionAccessibilityEventRouter::GetInstance()-> |
SetAccessibilityEnabled(enabled); |
@@ -219,42 +264,24 @@ void EnableSpokenFeedback(bool enabled, |
} |
} |
-void EnableHighContrast(bool enabled) { |
- PrefService* pref_service = g_browser_process->local_state(); |
- pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled); |
- pref_service->CommitPendingWrite(); |
- |
- AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE); |
- content::NotificationService::current()->Notify( |
- chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE, |
- content::NotificationService::AllSources(), |
- content::Details<AccessibilityStatusEventDetails>(&detail)); |
- |
-#if defined(USE_ASH) |
- ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled); |
-#endif |
-} |
- |
-void EnableVirtualKeyboard(bool enabled) { |
- PrefService* pref_service = g_browser_process->local_state(); |
- pref_service->SetBoolean(prefs::kVirtualKeyboardEnabled, enabled); |
- pref_service->CommitPendingWrite(); |
+bool AccessibilityManager::IsSpokenFeedbackEnabled() { |
+ if (!profile_) |
+ return false; |
+ return spoken_feedback_enabled_; |
} |
-void ToggleSpokenFeedback(content::WebUI* login_web_ui, |
+void AccessibilityManager::ToggleSpokenFeedback( |
+ content::WebUI* login_web_ui, |
ash::AccessibilityNotificationVisibility notify) { |
- bool spoken_feedback_enabled = g_browser_process && |
- g_browser_process->local_state()->GetBoolean( |
- prefs::kSpokenFeedbackEnabled); |
+ bool spoken_feedback_enabled = IsSpokenFeedbackEnabled(); |
spoken_feedback_enabled = !spoken_feedback_enabled; |
EnableSpokenFeedback(spoken_feedback_enabled, login_web_ui, notify); |
-}; |
+} |
-void Speak(const std::string& text) { |
+void AccessibilityManager::Speak(const std::string& text) { |
UtteranceContinuousParameters params; |
- Profile* profile = ProfileManager::GetDefaultProfile(); |
- Utterance* utterance = new Utterance(profile); |
+ Utterance* utterance = new Utterance(profile_); |
utterance->set_text(text); |
utterance->set_lang(g_browser_process->GetApplicationLocale()); |
utterance->set_continuous_parameters(params); |
@@ -265,44 +292,124 @@ void Speak(const std::string& text) { |
controller->SpeakOrEnqueue(utterance); |
} |
-bool IsSpokenFeedbackEnabled() { |
- if (!g_browser_process) { |
- return false; |
- } |
- PrefService* prefs = g_browser_process->local_state(); |
- bool spoken_feedback_enabled = prefs && |
- prefs->GetBoolean(prefs::kSpokenFeedbackEnabled); |
- return spoken_feedback_enabled; |
+void AccessibilityManager::MaybeSpeak(const std::string& utterance) { |
+ if (IsSpokenFeedbackEnabled()) |
+ Speak(utterance); |
} |
-bool IsHighContrastEnabled() { |
- if (!g_browser_process) { |
- return false; |
- } |
- PrefService* prefs = g_browser_process->local_state(); |
- bool high_contrast_enabled = prefs && |
- prefs->GetBoolean(prefs::kHighContrastEnabled); |
- return high_contrast_enabled; |
+void AccessibilityManager::EnableHighContrast(bool enabled) { |
+ if (!profile_) |
+ return; |
+ |
+ if (high_contrast_enabled_ == enabled) |
+ return; |
+ |
+ high_contrast_enabled_ = enabled; |
+ |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled); |
+ pref_service->CommitPendingWrite(); |
+ |
+ AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE); |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE, |
+ content::NotificationService::AllSources(), |
+ content::Details<AccessibilityStatusEventDetails>(&detail)); |
+ |
+#if defined(USE_ASH) |
+ ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled); |
+#endif |
} |
-bool IsVirtualKeyboardEnabled() { |
- if (!g_browser_process) { |
+bool AccessibilityManager::IsHighContrastEnabled() { |
+ if (!profile_) |
return false; |
+ return high_contrast_enabled_; |
+} |
+ |
+void AccessibilityManager::UpdateSpokenFeedbackStatusFromPref() { |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ bool spoken_feedback_enabled = |
+ pref_service->GetBoolean(prefs::kSpokenFeedbackEnabled); |
+ EnableSpokenFeedback( |
+ spoken_feedback_enabled, NULL, ash::A11Y_NOTIFICATION_NONE); |
+} |
+ |
+void AccessibilityManager::UpdateHighContrastStatusFromPref() { |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ bool high_contrast_enabled = |
+ pref_service->GetBoolean(prefs::kHighContrastEnabled); |
+ EnableHighContrast(high_contrast_enabled); |
+} |
+ |
+void AccessibilityManager::SetProfile(Profile* profile) { |
+ pref_change_registrar_.reset(); |
+ |
+ if (profile) { |
+ pref_change_registrar_.reset(new PrefChangeRegistrar); |
+ pref_change_registrar_->Init(profile->GetPrefs()); |
+ pref_change_registrar_->Add( |
+ prefs::kSpokenFeedbackEnabled, |
+ base::Bind(&AccessibilityManager::UpdateSpokenFeedbackStatusFromPref, |
+ base::Unretained(this))); |
+ pref_change_registrar_->Add( |
+ prefs::kHighContrastEnabled, |
+ base::Bind(&AccessibilityManager::UpdateHighContrastStatusFromPref, |
+ base::Unretained(this))); |
+ |
+ content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback( |
+ base::Bind( |
+ &AccessibilityManager::UpdateChromeOSAccessibilityHistograms, |
+ base::Unretained(this))); |
} |
- PrefService* prefs = g_browser_process->local_state(); |
- bool virtual_keyboard_enabled = prefs && |
- prefs->GetBoolean(prefs::kVirtualKeyboardEnabled); |
- return virtual_keyboard_enabled; |
+ |
+ profile_ = profile; |
+ UpdateSpokenFeedbackStatusFromPref(); |
+ UpdateHighContrastStatusFromPref(); |
} |
-void MaybeSpeak(const std::string& utterance) { |
- if (IsSpokenFeedbackEnabled()) |
- Speak(utterance); |
+void AccessibilityManager::SetProfileForTest(Profile* profile) { |
+ SetProfile(profile); |
+} |
+ |
+void AccessibilityManager::UpdateChromeOSAccessibilityHistograms() { |
+ UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback", |
+ IsSpokenFeedbackEnabled()); |
+ UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast", |
+ IsHighContrastEnabled()); |
+ UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard", |
+ accessibility::IsVirtualKeyboardEnabled()); |
+ if (MagnificationManager::Get()) { |
+ uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ? |
+ MagnificationManager::Get()->GetMagnifierType() : 0; |
+ // '0' means magnifier is disabled. |
+ UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier", |
+ type, |
+ ash::kMaxMagnifierType + 1); |
+ } |
} |
-void ShowAccessibilityHelp(Browser* browser) { |
- chrome::ShowSingletonTab(browser, GURL(chrome::kChromeAccessibilityHelpURL)); |
+void AccessibilityManager::Observe( |
+ int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+ switch (type) { |
+ case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: |
+ case chrome::NOTIFICATION_SESSION_STARTED: { |
+ Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
+ SetProfile(profile); |
+ break; |
+ } |
+ case chrome::NOTIFICATION_PROFILE_CREATED: { |
+ Profile* profile = content::Source<Profile>(source).ptr(); |
+ SetProfile(profile); |
+ break; |
+ } |
+ case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
+ SetProfile(NULL); |
+ break; |
+ } |
+ } |
} |
-} // namespace accessibility |
} // namespace chromeos |