OLD | NEW |
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/accessibility_util.h" | 5 #include "chrome/browser/chromeos/accessibility/accessibility_util.h" |
6 | 6 |
7 #include <queue> | |
8 | |
9 #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" | |
13 #include "ash/shell_delegate.h" | |
14 #include "base/bind.h" | |
15 #include "base/bind_helpers.h" | |
16 #include "base/logging.h" | |
17 #include "base/metrics/histogram.h" | |
18 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
19 #include "chrome/browser/accessibility/accessibility_extension_api.h" | |
20 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/chromeos/accessibility/magnification_manager.h" | |
22 #include "chrome/browser/extensions/component_loader.h" | |
23 #include "chrome/browser/extensions/extension_service.h" | |
24 #include "chrome/browser/extensions/extension_system.h" | |
25 #include "chrome/browser/profiles/profile.h" | |
26 #include "chrome/browser/profiles/profile_manager.h" | |
27 #include "chrome/browser/speech/tts_controller.h" | |
28 #include "chrome/browser/ui/singleton_tabs.h" | 9 #include "chrome/browser/ui/singleton_tabs.h" |
29 #include "chrome/common/extensions/extension.h" | |
30 #include "chrome/common/extensions/extension_messages.h" | |
31 #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" | 10 #include "chrome/common/pref_names.h" |
34 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
35 #include "content/public/browser/browser_accessibility_state.h" | |
36 #include "content/public/browser/notification_service.h" | |
37 #include "content/public/browser/render_process_host.h" | |
38 #include "content/public/browser/render_view_host.h" | |
39 #include "content/public/browser/web_contents.h" | |
40 #include "content/public/browser/web_ui.h" | |
41 #include "extensions/browser/file_reader.h" | |
42 #include "extensions/common/extension_resource.h" | |
43 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
44 #include "grit/browser_resources.h" | |
45 #include "grit/generated_resources.h" | |
46 #include "ui/base/l10n/l10n_util.h" | |
47 #include "ui/base/resource/resource_bundle.h" | |
48 | 13 |
49 using content::RenderViewHost; | 14 // TODO(yoshiki): move the following method to accessibility_manager.cc and |
| 15 // remove this file. |
50 | 16 |
51 namespace chromeos { | 17 namespace chromeos { |
52 namespace accessibility { | 18 namespace accessibility { |
53 | 19 |
54 // Helper class that directly loads an extension's content scripts into | |
55 // all of the frames corresponding to a given RenderViewHost. | |
56 class ContentScriptLoader { | |
57 public: | |
58 // Initialize the ContentScriptLoader with the ID of the extension | |
59 // and the RenderViewHost where the scripts should be loaded. | |
60 ContentScriptLoader(const std::string& extension_id, | |
61 int render_process_id, | |
62 int render_view_id) | |
63 : extension_id_(extension_id), | |
64 render_process_id_(render_process_id), | |
65 render_view_id_(render_view_id) {} | |
66 | |
67 // Call this once with the ExtensionResource corresponding to each | |
68 // content script to be loaded. | |
69 void AppendScript(extensions::ExtensionResource resource) { | |
70 resources_.push(resource); | |
71 } | |
72 | |
73 // Fianlly, call this method once to fetch all of the resources and | |
74 // load them. This method will delete this object when done. | |
75 void Run() { | |
76 if (resources_.empty()) { | |
77 delete this; | |
78 return; | |
79 } | |
80 | |
81 extensions::ExtensionResource resource = resources_.front(); | |
82 resources_.pop(); | |
83 scoped_refptr<FileReader> reader(new FileReader(resource, base::Bind( | |
84 &ContentScriptLoader::OnFileLoaded, base::Unretained(this)))); | |
85 reader->Start(); | |
86 } | |
87 | |
88 private: | |
89 void OnFileLoaded(bool success, const std::string& data) { | |
90 if (success) { | |
91 ExtensionMsg_ExecuteCode_Params params; | |
92 params.request_id = 0; | |
93 params.extension_id = extension_id_; | |
94 params.is_javascript = true; | |
95 params.code = data; | |
96 params.run_at = extensions::UserScript::DOCUMENT_IDLE; | |
97 params.all_frames = true; | |
98 params.in_main_world = false; | |
99 | |
100 RenderViewHost* render_view_host = | |
101 RenderViewHost::FromID(render_process_id_, render_view_id_); | |
102 if (render_view_host) { | |
103 render_view_host->Send(new ExtensionMsg_ExecuteCode( | |
104 render_view_host->GetRoutingID(), params)); | |
105 } | |
106 } | |
107 Run(); | |
108 } | |
109 | |
110 std::string extension_id_; | |
111 int render_process_id_; | |
112 int render_view_id_; | |
113 std::queue<extensions::ExtensionResource> resources_; | |
114 }; | |
115 | |
116 void UpdateChromeOSAccessibilityHistograms() { | |
117 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback", | |
118 IsSpokenFeedbackEnabled()); | |
119 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast", | |
120 IsHighContrastEnabled()); | |
121 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard", | |
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 } | |
132 | |
133 void Initialize() { | |
134 content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback( | |
135 base::Bind(&UpdateChromeOSAccessibilityHistograms)); | |
136 } | |
137 | |
138 void EnableSpokenFeedback(bool enabled, | |
139 content::WebUI* login_web_ui, | |
140 ash::AccessibilityNotificationVisibility notify) { | |
141 bool spoken_feedback_enabled = g_browser_process && | |
142 g_browser_process->local_state()->GetBoolean( | |
143 prefs::kSpokenFeedbackEnabled); | |
144 if (spoken_feedback_enabled == enabled) { | |
145 DLOG(INFO) << "Spoken feedback is already " << | |
146 (enabled ? "enabled" : "disabled") << ". Going to do nothing."; | |
147 return; | |
148 } | |
149 | |
150 g_browser_process->local_state()->SetBoolean( | |
151 prefs::kSpokenFeedbackEnabled, enabled); | |
152 g_browser_process->local_state()->CommitPendingWrite(); | |
153 ExtensionAccessibilityEventRouter::GetInstance()-> | |
154 SetAccessibilityEnabled(enabled); | |
155 | |
156 AccessibilityStatusEventDetails details(enabled, notify); | |
157 content::NotificationService::current()->Notify( | |
158 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK, | |
159 content::NotificationService::AllSources(), | |
160 content::Details<AccessibilityStatusEventDetails>(&details)); | |
161 | |
162 Speak(l10n_util::GetStringUTF8( | |
163 enabled ? IDS_CHROMEOS_ACC_SPOKEN_FEEDBACK_ENABLED : | |
164 IDS_CHROMEOS_ACC_SPOKEN_FEEDBACK_DISABLED).c_str()); | |
165 | |
166 // Load/Unload ChromeVox | |
167 Profile* profile = login_web_ui ? | |
168 Profile::FromWebUI(login_web_ui) : | |
169 ProfileManager::GetDefaultProfile(); | |
170 ExtensionService* extension_service = | |
171 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
172 base::FilePath path = base::FilePath(extension_misc::kChromeVoxExtensionPath); | |
173 if (enabled) { // Load ChromeVox | |
174 std::string extension_id = | |
175 extension_service->component_loader()->Add(IDR_CHROMEVOX_MANIFEST, | |
176 path); | |
177 const extensions::Extension* extension = | |
178 extension_service->extensions()->GetByID(extension_id); | |
179 | |
180 if (login_web_ui) { | |
181 RenderViewHost* render_view_host = | |
182 login_web_ui->GetWebContents()->GetRenderViewHost(); | |
183 // Set a flag to tell ChromeVox that it's just been enabled, | |
184 // so that it won't interrupt our speech feedback enabled message. | |
185 ExtensionMsg_ExecuteCode_Params params; | |
186 params.request_id = 0; | |
187 params.extension_id = extension->id(); | |
188 params.is_javascript = true; | |
189 params.code = "window.INJECTED_AFTER_LOAD = true;"; | |
190 params.run_at = extensions::UserScript::DOCUMENT_IDLE; | |
191 params.all_frames = true; | |
192 params.in_main_world = false; | |
193 render_view_host->Send(new ExtensionMsg_ExecuteCode( | |
194 render_view_host->GetRoutingID(), params)); | |
195 | |
196 // Inject ChromeVox' content scripts. | |
197 ContentScriptLoader* loader = new ContentScriptLoader( | |
198 extension->id(), render_view_host->GetProcess()->GetID(), | |
199 render_view_host->GetRoutingID()); | |
200 | |
201 const extensions::UserScriptList& content_scripts = | |
202 extensions::ContentScriptsInfo::GetContentScripts(extension); | |
203 for (size_t i = 0; i < content_scripts.size(); i++) { | |
204 const extensions::UserScript& script = content_scripts[i]; | |
205 for (size_t j = 0; j < script.js_scripts().size(); ++j) { | |
206 const extensions::UserScript::File &file = script.js_scripts()[j]; | |
207 extensions::ExtensionResource resource = extension->GetResource( | |
208 file.relative_path()); | |
209 loader->AppendScript(resource); | |
210 } | |
211 } | |
212 loader->Run(); // It cleans itself up when done. | |
213 } | |
214 | |
215 DLOG(INFO) << "ChromeVox was Loaded."; | |
216 } else { // Unload ChromeVox | |
217 extension_service->component_loader()->Remove(path); | |
218 DLOG(INFO) << "ChromeVox was Unloaded."; | |
219 } | |
220 } | |
221 | |
222 void EnableHighContrast(bool enabled) { | |
223 PrefService* pref_service = g_browser_process->local_state(); | |
224 pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled); | |
225 pref_service->CommitPendingWrite(); | |
226 | |
227 AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE); | |
228 content::NotificationService::current()->Notify( | |
229 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE, | |
230 content::NotificationService::AllSources(), | |
231 content::Details<AccessibilityStatusEventDetails>(&detail)); | |
232 | |
233 #if defined(USE_ASH) | |
234 ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled); | |
235 #endif | |
236 } | |
237 | |
238 void EnableVirtualKeyboard(bool enabled) { | 20 void EnableVirtualKeyboard(bool enabled) { |
239 PrefService* pref_service = g_browser_process->local_state(); | 21 PrefService* pref_service = g_browser_process->local_state(); |
240 pref_service->SetBoolean(prefs::kVirtualKeyboardEnabled, enabled); | 22 pref_service->SetBoolean(prefs::kVirtualKeyboardEnabled, enabled); |
241 pref_service->CommitPendingWrite(); | 23 pref_service->CommitPendingWrite(); |
242 } | 24 } |
243 | 25 |
244 void ToggleSpokenFeedback(content::WebUI* login_web_ui, | |
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; | |
255 | |
256 Profile* profile = ProfileManager::GetDefaultProfile(); | |
257 Utterance* utterance = new Utterance(profile); | |
258 utterance->set_text(text); | |
259 utterance->set_lang(g_browser_process->GetApplicationLocale()); | |
260 utterance->set_continuous_parameters(params); | |
261 utterance->set_can_enqueue(false); | |
262 utterance->set_options(new DictionaryValue()); | |
263 | |
264 TtsController* controller = TtsController::GetInstance(); | |
265 controller->SpeakOrEnqueue(utterance); | |
266 } | |
267 | |
268 bool IsSpokenFeedbackEnabled() { | |
269 if (!g_browser_process) { | |
270 return false; | |
271 } | |
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 } | |
277 | |
278 bool IsHighContrastEnabled() { | |
279 if (!g_browser_process) { | |
280 return false; | |
281 } | |
282 PrefService* prefs = g_browser_process->local_state(); | |
283 bool high_contrast_enabled = prefs && | |
284 prefs->GetBoolean(prefs::kHighContrastEnabled); | |
285 return high_contrast_enabled; | |
286 } | |
287 | |
288 bool IsVirtualKeyboardEnabled() { | 26 bool IsVirtualKeyboardEnabled() { |
289 if (!g_browser_process) { | 27 if (!g_browser_process) { |
290 return false; | 28 return false; |
291 } | 29 } |
292 PrefService* prefs = g_browser_process->local_state(); | 30 PrefService* prefs = g_browser_process->local_state(); |
293 bool virtual_keyboard_enabled = prefs && | 31 bool virtual_keyboard_enabled = prefs && |
294 prefs->GetBoolean(prefs::kVirtualKeyboardEnabled); | 32 prefs->GetBoolean(prefs::kVirtualKeyboardEnabled); |
295 return virtual_keyboard_enabled; | 33 return virtual_keyboard_enabled; |
296 } | 34 } |
297 | 35 |
298 void MaybeSpeak(const std::string& utterance) { | |
299 if (IsSpokenFeedbackEnabled()) | |
300 Speak(utterance); | |
301 } | |
302 | |
303 void ShowAccessibilityHelp(Browser* browser) { | 36 void ShowAccessibilityHelp(Browser* browser) { |
304 chrome::ShowSingletonTab(browser, GURL(chrome::kChromeAccessibilityHelpURL)); | 37 chrome::ShowSingletonTab(browser, GURL(chrome::kChromeAccessibilityHelpURL)); |
305 } | 38 } |
306 | 39 |
307 } // namespace accessibility | 40 } // namespace accessibility |
308 } // namespace chromeos | 41 } // namespace chromeos |
OLD | NEW |