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/speech/extension_api/tts_engine_extension_api.h" | 5 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/event_router.h" | 11 #include "chrome/browser/extensions/event_router.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/speech/extension_api/tts_extension_api_constants.h" | 15 #include "chrome/browser/speech/extension_api/tts_extension_api_constants.h" |
15 #include "chrome/browser/speech/extension_api/tts_extension_api_controller.h" | 16 #include "chrome/browser/speech/extension_api/tts_extension_api_controller.h" |
16 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
17 | 18 |
18 using extensions::Extension; | 19 using extensions::Extension; |
19 | 20 |
20 namespace constants = tts_extension_api_constants; | 21 namespace constants = tts_extension_api_constants; |
21 | 22 |
22 namespace events { | 23 namespace events { |
23 const char kOnSpeak[] = "ttsEngine.onSpeak"; | 24 const char kOnSpeak[] = "ttsEngine.onSpeak"; |
24 const char kOnStop[] = "ttsEngine.onStop"; | 25 const char kOnStop[] = "ttsEngine.onStop"; |
25 }; // namespace events | 26 }; // namespace events |
26 | 27 |
27 namespace { | 28 namespace { |
28 // Given a language/region code of the form 'fr-FR', returns just the basic | 29 // Given a language/region code of the form 'fr-FR', returns just the basic |
29 // language portion, e.g. 'fr'. | 30 // language portion, e.g. 'fr'. |
30 std::string TrimLanguageCode(std::string lang) { | 31 std::string TrimLanguageCode(std::string lang) { |
31 if (lang.size() >= 5 && lang[2] == '-') | 32 if (lang.size() >= 5 && lang[2] == '-') |
32 return lang.substr(0, 2); | 33 return lang.substr(0, 2); |
33 else | 34 else |
34 return lang; | 35 return lang; |
35 } | 36 } |
36 } | 37 } |
37 | 38 |
38 void GetExtensionVoices(Profile* profile, ListValue* result_voices) { | 39 void GetExtensionVoices(Profile* profile, ListValue* result_voices) { |
39 ExtensionService* service = profile->GetExtensionService(); | 40 ExtensionService* service = profile->GetExtensionService(); |
40 DCHECK(service); | 41 DCHECK(service); |
41 extensions::EventRouter* event_router = profile->GetExtensionEventRouter(); | 42 extensions::EventRouter* event_router = |
| 43 extensions::ExtensionSystem::Get(profile)->event_router(); |
42 DCHECK(event_router); | 44 DCHECK(event_router); |
43 | 45 |
44 const ExtensionSet* extensions = service->extensions(); | 46 const ExtensionSet* extensions = service->extensions(); |
45 ExtensionSet::const_iterator iter; | 47 ExtensionSet::const_iterator iter; |
46 for (iter = extensions->begin(); iter != extensions->end(); ++iter) { | 48 for (iter = extensions->begin(); iter != extensions->end(); ++iter) { |
47 const Extension* extension = *iter; | 49 const Extension* extension = *iter; |
48 | 50 |
49 if (!event_router->ExtensionHasEventListener( | 51 if (!event_router->ExtensionHasEventListener( |
50 extension->id(), events::kOnSpeak) || | 52 extension->id(), events::kOnSpeak) || |
51 !event_router->ExtensionHasEventListener( | 53 !event_router->ExtensionHasEventListener( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return false; | 101 return false; |
100 | 102 |
101 ExtensionService* service = utterance->profile()->GetExtensionService(); | 103 ExtensionService* service = utterance->profile()->GetExtensionService(); |
102 | 104 |
103 // If speech is generated when Chrome OS first starts up, it's possible | 105 // If speech is generated when Chrome OS first starts up, it's possible |
104 // the extension service isn't even available. | 106 // the extension service isn't even available. |
105 if (!service) | 107 if (!service) |
106 return false; | 108 return false; |
107 | 109 |
108 extensions::EventRouter* event_router = | 110 extensions::EventRouter* event_router = |
109 utterance->profile()->GetExtensionEventRouter(); | 111 extensions::ExtensionSystem::Get(utterance->profile())->event_router(); |
110 DCHECK(event_router); | 112 DCHECK(event_router); |
111 | 113 |
112 *matching_extension = NULL; | 114 *matching_extension = NULL; |
113 *voice_index = -1; | 115 *voice_index = -1; |
114 const ExtensionSet* extensions = service->extensions(); | 116 const ExtensionSet* extensions = service->extensions(); |
115 ExtensionSet::const_iterator iter; | 117 ExtensionSet::const_iterator iter; |
116 | 118 |
117 // Make two passes: the first time, do strict language matching | 119 // Make two passes: the first time, do strict language matching |
118 // ('fr-FR' does not match 'fr-CA'). The second time, do prefix | 120 // ('fr-FR' does not match 'fr-CA'). The second time, do prefix |
119 // language matching ('fr-FR' matches 'fr' and 'fr-CA') | 121 // language matching ('fr-FR' matches 'fr' and 'fr-CA') |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 if (options->HasKey(constants::kSrcIdKey)) | 211 if (options->HasKey(constants::kSrcIdKey)) |
210 options->Remove(constants::kSrcIdKey, NULL); | 212 options->Remove(constants::kSrcIdKey, NULL); |
211 if (options->HasKey(constants::kIsFinalEventKey)) | 213 if (options->HasKey(constants::kIsFinalEventKey)) |
212 options->Remove(constants::kIsFinalEventKey, NULL); | 214 options->Remove(constants::kIsFinalEventKey, NULL); |
213 if (options->HasKey(constants::kOnEventKey)) | 215 if (options->HasKey(constants::kOnEventKey)) |
214 options->Remove(constants::kOnEventKey, NULL); | 216 options->Remove(constants::kOnEventKey, NULL); |
215 | 217 |
216 args->Set(1, options); | 218 args->Set(1, options); |
217 args->Set(2, Value::CreateIntegerValue(utterance->id())); | 219 args->Set(2, Value::CreateIntegerValue(utterance->id())); |
218 | 220 |
219 utterance->profile()->GetExtensionEventRouter()->DispatchEventToExtension( | 221 extensions::ExtensionSystem::Get(utterance->profile())->event_router()-> |
220 extension->id(), | 222 DispatchEventToExtension( |
221 events::kOnSpeak, | 223 extension->id(), |
222 args.Pass(), | 224 events::kOnSpeak, |
223 utterance->profile(), | 225 args.Pass(), |
224 GURL()); | 226 utterance->profile(), |
| 227 GURL()); |
225 } | 228 } |
226 | 229 |
227 void ExtensionTtsEngineStop(Utterance* utterance) { | 230 void ExtensionTtsEngineStop(Utterance* utterance) { |
228 scoped_ptr<ListValue> args(new ListValue()); | 231 scoped_ptr<ListValue> args(new ListValue()); |
229 utterance->profile()->GetExtensionEventRouter()->DispatchEventToExtension( | 232 extensions::ExtensionSystem::Get(utterance->profile())->event_router()-> |
230 utterance->extension_id(), | 233 DispatchEventToExtension( |
231 events::kOnStop, | 234 utterance->extension_id(), |
232 args.Pass(), | 235 events::kOnStop, |
233 utterance->profile(), | 236 args.Pass(), |
234 GURL()); | 237 utterance->profile(), |
| 238 GURL()); |
235 } | 239 } |
236 | 240 |
237 bool ExtensionTtsEngineSendTtsEventFunction::RunImpl() { | 241 bool ExtensionTtsEngineSendTtsEventFunction::RunImpl() { |
238 int utterance_id; | 242 int utterance_id; |
239 std::string error_message; | 243 std::string error_message; |
240 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &utterance_id)); | 244 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &utterance_id)); |
241 | 245 |
242 DictionaryValue* event; | 246 DictionaryValue* event; |
243 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &event)); | 247 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &event)); |
244 | 248 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 std::string error_message; | 291 std::string error_message; |
288 event->GetString(constants::kErrorMessageKey, &error_message); | 292 event->GetString(constants::kErrorMessageKey, &error_message); |
289 controller->OnTtsEvent( | 293 controller->OnTtsEvent( |
290 utterance_id, TTS_EVENT_ERROR, char_index, error_message); | 294 utterance_id, TTS_EVENT_ERROR, char_index, error_message); |
291 } else { | 295 } else { |
292 EXTENSION_FUNCTION_VALIDATE(false); | 296 EXTENSION_FUNCTION_VALIDATE(false); |
293 } | 297 } |
294 | 298 |
295 return true; | 299 return true; |
296 } | 300 } |
OLD | NEW |