Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / speech / speech_recognition_dispatcher_host.cc
blob7f20ca4efa301ecdd81c393bf25661b8bcba6efc
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.
5 #include "content/browser/speech/speech_recognition_dispatcher_host.h"
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"
17 namespace content {
18 SpeechRecognitionManager* SpeechRecognitionDispatcherHost::manager_for_tests_;
20 void SpeechRecognitionDispatcherHost::SetManagerForTests(
21 SpeechRecognitionManager* manager) {
22 manager_for_tests_ = manager;
25 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost(
26 int render_process_id,
27 net::URLRequestContextGetter* context_getter,
28 SpeechRecognitionPreferences* recognition_preferences)
29 : render_process_id_(render_process_id),
30 context_getter_(context_getter),
31 recognition_preferences_(recognition_preferences) {
32 // Do not add any non-trivial initialization here, instead do it lazily when
33 // required (e.g. see the method |manager()|) or add an Init() method.
36 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() {
37 if (SpeechRecognitionManager* sr_manager = manager())
38 sr_manager->AbortAllSessionsForListener(this);
41 SpeechRecognitionManager* SpeechRecognitionDispatcherHost::manager() {
42 if (manager_for_tests_)
43 return manager_for_tests_;
45 return SpeechRecognitionManager::GetInstance();
48 bool SpeechRecognitionDispatcherHost::OnMessageReceived(
49 const IPC::Message& message, bool* message_was_ok) {
50 bool handled = true;
51 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message,
52 *message_was_ok)
53 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest,
54 OnStartRequest)
55 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest,
56 OnAbortRequest)
57 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest,
58 OnStopCaptureRequest)
59 IPC_MESSAGE_UNHANDLED(handled = false)
60 IPC_END_MESSAGE_MAP()
61 return handled;
64 void SpeechRecognitionDispatcherHost::OnStartRequest(
65 const SpeechRecognitionHostMsg_StartRequest_Params& params) {
67 SpeechRecognitionSessionContext context;
68 context.context_name = params.origin_url;
69 context.render_process_id = render_process_id_;
70 context.render_view_id = params.render_view_id;
71 context.request_id = params.request_id;
72 context.requested_by_page_element = false;
74 SpeechRecognitionSessionConfig config;
75 config.is_legacy_api = false;
76 config.language = params.language;
77 config.grammars = params.grammars;
78 config.max_hypotheses = params.max_hypotheses;
79 config.origin_url = params.origin_url;
80 config.initial_context = context;
81 config.url_request_context_getter = context_getter_.get();
82 if (recognition_preferences_.get()) {
83 config.filter_profanities = recognition_preferences_->FilterProfanities();
84 } else {
85 config.filter_profanities = false;
87 config.continuous = params.continuous;
88 config.interim_results = params.interim_results;
89 config.event_listener = this;
91 int session_id = manager()->CreateSession(config);
92 DCHECK_NE(session_id, SpeechRecognitionManager::kSessionIDInvalid);
93 manager()->StartSession(session_id);
96 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id,
97 int request_id) {
98 int session_id = manager()->GetSession(render_process_id_,
99 render_view_id,
100 request_id);
102 // The renderer might provide an invalid |request_id| if the session was not
103 // started as expected, e.g., due to unsatisfied security requirements.
104 if (session_id != SpeechRecognitionManager::kSessionIDInvalid)
105 manager()->AbortSession(session_id);
108 void SpeechRecognitionDispatcherHost::OnStopCaptureRequest(
109 int render_view_id, int request_id) {
110 int session_id = manager()->GetSession(render_process_id_,
111 render_view_id,
112 request_id);
114 // The renderer might provide an invalid |request_id| if the session was not
115 // started as expected, e.g., due to unsatisfied security requirements.
116 if (session_id != SpeechRecognitionManager::kSessionIDInvalid)
117 manager()->StopAudioCaptureForSession(session_id);
120 // -------- SpeechRecognitionEventListener interface implementation -----------
122 void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) {
123 const SpeechRecognitionSessionContext& context =
124 manager()->GetSessionContext(session_id);
125 Send(new SpeechRecognitionMsg_Started(context.render_view_id,
126 context.request_id));
129 void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) {
130 const SpeechRecognitionSessionContext& context =
131 manager()->GetSessionContext(session_id);
132 Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id,
133 context.request_id));
136 void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) {
137 const SpeechRecognitionSessionContext& context =
138 manager()->GetSessionContext(session_id);
139 Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id,
140 context.request_id));
143 void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) {
144 const SpeechRecognitionSessionContext& context =
145 manager()->GetSessionContext(session_id);
146 Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id,
147 context.request_id));
150 void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) {
151 const SpeechRecognitionSessionContext& context =
152 manager()->GetSessionContext(session_id);
153 Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id,
154 context.request_id));
157 void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) {
158 const SpeechRecognitionSessionContext& context =
159 manager()->GetSessionContext(session_id);
160 Send(new SpeechRecognitionMsg_Ended(context.render_view_id,
161 context.request_id));
164 void SpeechRecognitionDispatcherHost::OnRecognitionResults(
165 int session_id,
166 const SpeechRecognitionResults& results) {
167 const SpeechRecognitionSessionContext& context =
168 manager()->GetSessionContext(session_id);
169 Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id,
170 context.request_id,
171 results));
174 void SpeechRecognitionDispatcherHost::OnRecognitionError(
175 int session_id,
176 const SpeechRecognitionError& error) {
177 const SpeechRecognitionSessionContext& context =
178 manager()->GetSessionContext(session_id);
179 Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id,
180 context.request_id,
181 error));
184 // The events below are currently not used by speech JS APIs implementation.
185 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(int session_id,
186 float volume,
187 float noise_volume) {
190 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete(
191 int session_id) {
194 } // namespace content