Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1048)

Unified Diff: content/browser/speech/speech_recognition_dispatcher_host.cc

Issue 10273006: Introduced SpeechRecognitionDispatcher(Host) classes, handling dispatch of IPC messages for continu… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/speech/speech_recognition_dispatcher_host.cc
diff --git a/content/browser/speech/speech_recognition_dispatcher_host.cc b/content/browser/speech/speech_recognition_dispatcher_host.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e1162b1de3cc429f371a25760e662ed5b4030224
--- /dev/null
+++ b/content/browser/speech/speech_recognition_dispatcher_host.cc
@@ -0,0 +1,219 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/speech/speech_recognition_dispatcher_host.h"
+
+#include "base/bind.h"
+#include "base/command_line.h"
+#include "base/lazy_instance.h"
+#include "content/browser/speech/speech_recognition_manager_impl.h"
+#include "content/common/speech_recognition_messages.h"
+#include "content/public/browser/speech_recognition_preferences.h"
+#include "content/public/browser/speech_recognition_session_config.h"
+#include "content/public/browser/speech_recognition_session_context.h"
+#include "content/public/common/content_switches.h"
+
+using content::BrowserThread;
+using content::SpeechRecognitionSessionConfig;
+using content::SpeechRecognitionSessionContext;
+
+namespace {
+bool IsSameContext(int render_process_id,
hans 2012/05/11 16:56:32 i would personally move this function closer to wh
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Done.
+ int render_view_id,
+ int js_handle_id,
+ const SpeechRecognitionSessionContext& context) {
+ return context.render_process_id == render_process_id &&
+ context.render_view_id == render_view_id &&
+ context.js_handle_id == js_handle_id;
+}
+} // namespace
+
+namespace speech {
+SpeechRecognitionManagerImpl* SpeechRecognitionDispatcherHost::manager_;
+
+void SpeechRecognitionDispatcherHost::set_manager(
+ SpeechRecognitionManagerImpl* manager) {
+ manager_ = manager;
+}
+
+SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost(
+ int render_process_id,
+ net::URLRequestContextGetter* context_getter,
+ content::SpeechRecognitionPreferences* recognition_preferences)
+ : render_process_id_(render_process_id),
+ may_have_pending_requests_(false),
+ context_getter_(context_getter),
+ recognition_preferences_(recognition_preferences) {
+ // This is initialized by Browser. Do not add any non-trivial
+ // initialization here, instead do it lazily when required (e.g. see the
+ // method |manager()|) or add an Init() method.
+}
+
+SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() {
+ // If the renderer crashed for some reason or if we didn't receive a proper
+ // Cancel/Stop call for an existing session, cancel such active sessions now.
+ // We first check if this dispatcher received any speech IPC requst so that
hans 2012/05/11 16:56:32 s/requst/request/
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Done.
+ // we don't end up creating the speech input manager for web pages which don't
+ // use speech input.
+ if (may_have_pending_requests_)
hans 2012/05/11 16:56:32 couldn't we just do "if (manager_) ..." ? That wou
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 2 Observations: 1) Depending on CL1.10 it might be
hans 2012/05/14 14:04:52 Ah, thanks for clarifying.
+ manager()->AbortAllSessionsForListener(this);
+}
+
+SpeechRecognitionManagerImpl* SpeechRecognitionDispatcherHost::manager() {
+ if (manager_)
+ return manager_;
+
+ const CommandLine& command_line = *CommandLine::ForCurrentProcess();
+ if (command_line.HasSwitch(switches::kEnableScriptedSpeech))
+ return SpeechRecognitionManagerImpl::GetInstance();
+
+ return NULL;
+}
+
+bool SpeechRecognitionDispatcherHost::OnMessageReceived(
+ const IPC::Message& message, bool* message_was_ok) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message,
+ *message_was_ok)
+ IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest,
+ OnStartRequest)
+ IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest,
+ OnAbortRequest)
+ IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest,
+ OnStopCaptureRequest)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ if (handled)
+ may_have_pending_requests_ = true;
+ return handled;
+}
+
+void SpeechRecognitionDispatcherHost::OnStartRequest(
+ const SpeechRecognitionHostMsg_StartRequest_Params &params) {
hans 2012/05/11 16:56:32 the & is misplaced
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 I see that is a copy/paste "heritage". Fixed also
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ SpeechRecognitionSessionContext context;
+ context.render_process_id = render_process_id_;
+ context.render_view_id = params.render_view_id;
+ context.js_handle_id = params.js_handle_id;
+
+ SpeechRecognitionSessionConfig config;
+ config.is_one_shot = params.is_one_shot;
+ config.language = params.language;
+ config.grammars = params.grammars;
+ config.origin_url = params.origin_url;
+ config.initial_context = context;
+ config.url_request_context_getter = context_getter_.get();
+ config.filter_profanities = recognition_preferences_->FilterProfanities();
+
+ int session_id = manager()->CreateSession(config, this);
+
+ if (session_id == content::SpeechRecognitionManager::kSessionIDInvalid)
hans 2012/05/11 16:56:32 when does this happen?
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Hmm I remember that I turned this into a DCHECK in
+ return;
+
+ manager()->StartSession(session_id);
+}
+
+void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id,
+ int js_handle_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
hans 2012/05/11 16:56:32 indent +1
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Done.
+ int session_id = manager()->LookupSessionByContext(
+ base::Bind(&IsSameContext,
+ render_process_id_,
+ render_view_id,
+ js_handle_id));
+ if (session_id != 0)
hans 2012/05/11 16:56:32 should 0 be kSessionIDInvalid?
Primiano Tucci (use gerrit) 2012/05/14 12:58:22 Right.
+ manager()->AbortSession(session_id);
+}
+
+void SpeechRecognitionDispatcherHost::OnStopCaptureRequest(
+ int render_view_id, int js_handle_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ int session_id = manager()->LookupSessionByContext(
+ base::Bind(&IsSameContext,
+ render_process_id_,
+ render_view_id,
+ js_handle_id));
+ if (session_id != 0)
+ manager()->StopAudioCaptureForSession(session_id);
+}
+
+// -------- SpeechRecognitionEventListener interface implementation -----------
+
+void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_Started(context.render_view_id,
+ context.js_handle_id));
+}
+
+void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id,
+ context.js_handle_id));
+}
+
+void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id,
+ context.js_handle_id));
+}
+
+void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id,
+ context.js_handle_id));
+}
+
+void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id,
+ context.js_handle_id));
+}
+
+void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_Ended(context.render_view_id,
+ context.js_handle_id));
+}
+
+void SpeechRecognitionDispatcherHost::OnRecognitionResult(
+ int session_id, const content::SpeechRecognitionResult& result) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id,
+ context.js_handle_id,
+ result));
+}
+
+void SpeechRecognitionDispatcherHost::OnRecognitionError(
+ int session_id, const content::SpeechRecognitionError& error) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ manager()->GetSessionContext(session_id);
+ Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id,
+ context.js_handle_id,
+ error));
+}
+
+// The events below are currently not used by speech JS APIs implementation.
+void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(
+ int session_id, float volume, float noise_volume) {}
+void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete(
+ int session_id) {}
+
+} // namespace speech

Powered by Google App Engine
This is Rietveld 408576698