OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/speech/speech_recognition_dispatcher_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" |
| 10 #include "content/common/speech_recognition_messages.h" |
| 11 #include "content/public/browser/speech_recognition_manager.h" |
| 12 #include "content/public/browser/speech_recognition_preferences.h" |
| 13 #include "content/public/browser/speech_recognition_session_config.h" |
| 14 #include "content/public/browser/speech_recognition_session_context.h" |
| 15 #include "content/public/common/content_switches.h" |
| 16 |
| 17 using content::SpeechRecognitionManager; |
| 18 using content::SpeechRecognitionSessionConfig; |
| 19 using content::SpeechRecognitionSessionContext; |
| 20 |
| 21 namespace speech { |
| 22 SpeechRecognitionManager* SpeechRecognitionDispatcherHost::manager_for_tests_; |
| 23 |
| 24 void SpeechRecognitionDispatcherHost::SetManagerForTests( |
| 25 SpeechRecognitionManager* manager) { |
| 26 manager_for_tests_ = manager; |
| 27 } |
| 28 |
| 29 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost( |
| 30 int render_process_id, |
| 31 net::URLRequestContextGetter* context_getter, |
| 32 content::SpeechRecognitionPreferences* recognition_preferences) |
| 33 : render_process_id_(render_process_id), |
| 34 context_getter_(context_getter), |
| 35 recognition_preferences_(recognition_preferences) { |
| 36 // Do not add any non-trivial initialization here, instead do it lazily when |
| 37 // required (e.g. see the method |manager()|) or add an Init() method. |
| 38 } |
| 39 |
| 40 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() { |
| 41 if (SpeechRecognitionManager* sr_manager = manager()) |
| 42 sr_manager->AbortAllSessionsForListener(this); |
| 43 } |
| 44 |
| 45 SpeechRecognitionManager* SpeechRecognitionDispatcherHost::manager() { |
| 46 if (manager_for_tests_) |
| 47 return manager_for_tests_; |
| 48 |
| 49 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 50 if (command_line.HasSwitch(switches::kEnableScriptedSpeech)) |
| 51 return SpeechRecognitionManager::GetInstance(); |
| 52 |
| 53 return NULL; |
| 54 } |
| 55 |
| 56 bool SpeechRecognitionDispatcherHost::OnMessageReceived( |
| 57 const IPC::Message& message, bool* message_was_ok) { |
| 58 bool handled = true; |
| 59 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message, |
| 60 *message_was_ok) |
| 61 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest, |
| 62 OnStartRequest) |
| 63 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest, |
| 64 OnAbortRequest) |
| 65 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest, |
| 66 OnStopCaptureRequest) |
| 67 IPC_MESSAGE_UNHANDLED(handled = false) |
| 68 IPC_END_MESSAGE_MAP() |
| 69 return handled; |
| 70 } |
| 71 |
| 72 void SpeechRecognitionDispatcherHost::OnStartRequest( |
| 73 const SpeechRecognitionHostMsg_StartRequest_Params& params) { |
| 74 |
| 75 SpeechRecognitionSessionContext context; |
| 76 context.render_process_id = render_process_id_; |
| 77 context.render_view_id = params.render_view_id; |
| 78 context.request_id = params.request_id; |
| 79 |
| 80 SpeechRecognitionSessionConfig config; |
| 81 config.is_one_shot = params.is_one_shot; |
| 82 config.language = params.language; |
| 83 config.grammars = params.grammars; |
| 84 config.origin_url = params.origin_url; |
| 85 config.initial_context = context; |
| 86 config.url_request_context_getter = context_getter_.get(); |
| 87 config.filter_profanities = recognition_preferences_->FilterProfanities(); |
| 88 config.event_listener = this; |
| 89 |
| 90 int session_id = manager()->CreateSession(config); |
| 91 DCHECK_NE(session_id, content::SpeechRecognitionManager::kSessionIDInvalid); |
| 92 manager()->StartSession(session_id); |
| 93 } |
| 94 |
| 95 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id, |
| 96 int request_id) { |
| 97 int session_id = manager()->GetSession(render_process_id_, |
| 98 render_view_id, |
| 99 request_id); |
| 100 if (session_id != content::SpeechRecognitionManager::kSessionIDInvalid) |
| 101 manager()->AbortSession(session_id); |
| 102 } |
| 103 |
| 104 void SpeechRecognitionDispatcherHost::OnStopCaptureRequest( |
| 105 int render_view_id, int request_id) { |
| 106 int session_id = manager()->GetSession(render_process_id_, |
| 107 render_view_id, |
| 108 request_id); |
| 109 if (session_id != content::SpeechRecognitionManager::kSessionIDInvalid) |
| 110 manager()->StopAudioCaptureForSession(session_id); |
| 111 } |
| 112 |
| 113 // -------- SpeechRecognitionEventListener interface implementation ----------- |
| 114 |
| 115 void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) { |
| 116 const SpeechRecognitionSessionContext& context = |
| 117 manager()->GetSessionContext(session_id); |
| 118 Send(new SpeechRecognitionMsg_Started(context.render_view_id, |
| 119 context.request_id)); |
| 120 } |
| 121 |
| 122 void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) { |
| 123 const SpeechRecognitionSessionContext& context = |
| 124 manager()->GetSessionContext(session_id); |
| 125 Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id, |
| 126 context.request_id)); |
| 127 } |
| 128 |
| 129 void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) { |
| 130 const SpeechRecognitionSessionContext& context = |
| 131 manager()->GetSessionContext(session_id); |
| 132 Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id, |
| 133 context.request_id)); |
| 134 } |
| 135 |
| 136 void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) { |
| 137 const SpeechRecognitionSessionContext& context = |
| 138 manager()->GetSessionContext(session_id); |
| 139 Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id, |
| 140 context.request_id)); |
| 141 } |
| 142 |
| 143 void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) { |
| 144 const SpeechRecognitionSessionContext& context = |
| 145 manager()->GetSessionContext(session_id); |
| 146 Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id, |
| 147 context.request_id)); |
| 148 } |
| 149 |
| 150 void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) { |
| 151 const SpeechRecognitionSessionContext& context = |
| 152 manager()->GetSessionContext(session_id); |
| 153 Send(new SpeechRecognitionMsg_Ended(context.render_view_id, |
| 154 context.request_id)); |
| 155 } |
| 156 |
| 157 void SpeechRecognitionDispatcherHost::OnRecognitionResult( |
| 158 int session_id, const content::SpeechRecognitionResult& result) { |
| 159 const SpeechRecognitionSessionContext& context = |
| 160 manager()->GetSessionContext(session_id); |
| 161 Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id, |
| 162 context.request_id, |
| 163 result)); |
| 164 } |
| 165 |
| 166 void SpeechRecognitionDispatcherHost::OnRecognitionError( |
| 167 int session_id, const content::SpeechRecognitionError& error) { |
| 168 const SpeechRecognitionSessionContext& context = |
| 169 manager()->GetSessionContext(session_id); |
| 170 Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id, |
| 171 context.request_id, |
| 172 error)); |
| 173 } |
| 174 |
| 175 // The events below are currently not used by speech JS APIs implementation. |
| 176 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange( |
| 177 int session_id, float volume, float noise_volume) {} |
| 178 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete( |
| 179 int session_id) {} |
| 180 |
| 181 } // namespace speech |
OLD | NEW |