OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_util.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/logging.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/extensions/extension_accessibility_api.h" | |
12 #include "chrome/browser/extensions/extension_tts_api_platform.h" | |
13 #include "chrome/browser/extensions/component_loader.h" | |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/extensions/file_reader.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/profiles/profile_manager.h" | |
18 #include "chrome/common/extensions/extension_messages.h" | |
19 #include "chrome/common/extensions/extension_resource.h" | |
20 #include "chrome/common/pref_names.h" | |
21 #include "content/browser/accessibility/browser_accessibility_state.h" | |
22 #include "content/browser/renderer_host/render_view_host.h" | |
23 #include "content/browser/tab_contents/tab_contents.h" | |
24 #include "content/browser/webui/web_ui.h" | |
25 #include "grit/browser_resources.h" | |
26 #include "grit/generated_resources.h" | |
27 #include "ui/base/l10n/l10n_util.h" | |
28 #include "ui/base/resource/resource_bundle.h" | |
29 | |
30 namespace chromeos { | |
31 namespace accessibility { | |
32 | |
33 // Helper class that directly loads an extension's content scripts into | |
34 // all of the frames corresponding to a given RenderViewHost. | |
35 class ContentScriptLoader { | |
36 public: | |
37 // Initialize the ContentScriptLoader with the ID of the extension | |
38 // and the RenderViewHost where the scripts should be loaded. | |
39 ContentScriptLoader(const std::string& extension_id, | |
40 RenderViewHost* render_view_host) | |
41 : extension_id_(extension_id), | |
42 render_view_host_(render_view_host) {} | |
43 | |
44 // Call this once with the ExtensionResource corresponding to each | |
45 // content script to be loaded. | |
46 void AppendScript(ExtensionResource resource) { | |
47 resources_.push(resource); | |
48 } | |
49 | |
50 // Fianlly, call this method once to fetch all of the resources and | |
51 // load them. This method will delete this object when done. | |
52 void Run() { | |
53 if (resources_.empty()) { | |
54 delete this; | |
55 return; | |
56 } | |
57 | |
58 ExtensionResource resource = resources_.front(); | |
59 resources_.pop(); | |
60 scoped_refptr<FileReader> reader(new FileReader(resource, base::Bind( | |
61 &ContentScriptLoader::OnFileLoaded, base::Unretained(this)))); | |
62 reader->Start(); | |
63 } | |
64 | |
65 private: | |
66 void OnFileLoaded(bool success, const std::string& data) { | |
67 if (success) { | |
68 ExtensionMsg_ExecuteCode_Params params; | |
69 params.request_id = 0; | |
70 params.extension_id = extension_id_; | |
71 params.is_javascript = true; | |
72 params.code = data; | |
73 params.all_frames = true; | |
74 params.in_main_world = false; | |
75 render_view_host_->Send(new ExtensionMsg_ExecuteCode( | |
76 render_view_host_->routing_id(), params)); | |
77 } | |
78 Run(); | |
79 } | |
80 | |
81 std::string extension_id_; | |
82 RenderViewHost* render_view_host_; | |
83 std::queue<ExtensionResource> resources_; | |
84 }; | |
85 | |
86 void EnableAccessibility(bool enabled, WebUI* login_web_ui) { | |
87 bool accessibility_enabled = g_browser_process && | |
88 g_browser_process->local_state()->GetBoolean( | |
89 prefs::kAccessibilityEnabled); | |
90 if (accessibility_enabled == enabled) { | |
91 LOG(INFO) << "Accessibility is already " << | |
92 (enabled ? "enabled" : "disabled") << ". Going to do nothing."; | |
93 return; | |
94 } | |
95 | |
96 g_browser_process->local_state()->SetBoolean( | |
97 prefs::kAccessibilityEnabled, enabled); | |
98 g_browser_process->local_state()->ScheduleSavePersistentPrefs(); | |
99 ExtensionAccessibilityEventRouter::GetInstance()-> | |
100 SetAccessibilityEnabled(enabled); | |
101 BrowserAccessibilityState::GetInstance()->OnAccessibilityEnabledManually(); | |
102 | |
103 Speak(enabled ? | |
104 l10n_util::GetStringUTF8(IDS_CHROMEOS_ACC_ACCESS_ENABLED).c_str() : | |
105 l10n_util::GetStringUTF8(IDS_CHROMEOS_ACC_ACCESS_DISABLED).c_str()); | |
106 | |
107 // Load/Unload ChromeVox | |
108 Profile* profile = ProfileManager::GetDefaultProfile(); | |
109 ExtensionService* extension_service = | |
110 profile->GetExtensionService(); | |
111 FilePath path = FilePath(extension_misc::kAccessExtensionPath) | |
112 .AppendASCII(extension_misc::kChromeVoxDirectoryName); | |
113 if (enabled) { // Load ChromeVox | |
114 const Extension* extension = | |
115 extension_service->component_loader()->Add(IDR_CHROMEVOX_MANIFEST, | |
116 path); | |
117 | |
118 if (login_web_ui) { | |
119 RenderViewHost* render_view_host = | |
120 login_web_ui->tab_contents()->render_view_host(); | |
121 ContentScriptLoader* loader = new ContentScriptLoader( | |
122 extension->id(), render_view_host); | |
123 | |
124 for (size_t i = 0; i < extension->content_scripts().size(); i++) { | |
125 const UserScript& script = extension->content_scripts()[i]; | |
126 for (size_t j = 0; j < script.js_scripts().size(); ++j) { | |
127 const UserScript::File &file = script.js_scripts()[j]; | |
128 ExtensionResource resource = extension->GetResource( | |
129 file.relative_path()); | |
130 loader->AppendScript(resource); | |
131 } | |
132 } | |
133 loader->Run(); // It cleans itself up when done. | |
134 } | |
135 | |
136 LOG(INFO) << "ChromeVox was Loaded."; | |
137 } else { // Unload ChromeVox | |
138 extension_service->component_loader()->Remove(path); | |
139 LOG(INFO) << "ChromeVox was Unloaded."; | |
140 } | |
141 } | |
142 | |
143 void ToggleAccessibility(WebUI* login_web_ui) { | |
144 bool accessibility_enabled = g_browser_process && | |
145 g_browser_process->local_state()->GetBoolean( | |
146 prefs::kAccessibilityEnabled); | |
147 accessibility_enabled = !accessibility_enabled; | |
148 EnableAccessibility(accessibility_enabled, login_web_ui); | |
149 }; | |
150 | |
151 void Speak(const char* utterance) { | |
152 UtteranceContinuousParameters params; | |
153 ExtensionTtsPlatformImpl::GetInstance()->Speak( | |
154 -1, // No utterance ID because we don't need a callback when it finishes. | |
155 utterance, | |
156 g_browser_process->GetApplicationLocale(), | |
157 params); | |
158 } | |
159 | |
160 bool IsAccessibilityEnabled() { | |
161 if (!g_browser_process) { | |
162 return false; | |
163 } | |
164 PrefService* prefs = g_browser_process->local_state(); | |
165 bool accessibility_enabled = prefs && | |
166 prefs->GetBoolean(prefs::kAccessibilityEnabled); | |
167 return accessibility_enabled; | |
168 } | |
169 | |
170 } // namespace accessibility | |
171 } // namespace chromeos | |
OLD | NEW |