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 #ifndef CONTENT_BROWSER_SPEECH_INPUT_TAG_SPEECH_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_SPEECH_INPUT_TAG_SPEECH_DISPATCHER_HOST_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/common/content_export.h"
12 #include "content/public/browser/browser_message_filter.h"
13 #include "content/public/browser/speech_recognition_event_listener.h"
14 #include "content/public/common/speech_recognition_result.h"
15 #include "net/url_request/url_request_context_getter.h"
17 struct InputTagSpeechHostMsg_StartRecognition_Params;
21 class SpeechRecognitionManager;
23 // InputTagSpeechDispatcherHost is a delegate for Speech API messages used by
24 // RenderMessageFilter. Basically it acts as a proxy, relaying the events coming
25 // from the SpeechRecognitionManager to IPC messages (and vice versa).
26 // It's the complement of SpeechRecognitionDispatcher (owned by RenderView).
27 class CONTENT_EXPORT InputTagSpeechDispatcherHost
28 : public BrowserMessageFilter,
29 public SpeechRecognitionEventListener {
31 InputTagSpeechDispatcherHost(
33 int render_process_id,
34 net::URLRequestContextGetter* url_request_context_getter);
36 base::WeakPtr<InputTagSpeechDispatcherHost> AsWeakPtr();
38 // SpeechRecognitionEventListener methods.
39 virtual void OnRecognitionStart(int session_id) OVERRIDE;
40 virtual void OnAudioStart(int session_id) OVERRIDE;
41 virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE;
42 virtual void OnSoundStart(int session_id) OVERRIDE;
43 virtual void OnSoundEnd(int session_id) OVERRIDE;
44 virtual void OnAudioEnd(int session_id) OVERRIDE;
45 virtual void OnRecognitionEnd(int session_id) OVERRIDE;
46 virtual void OnRecognitionResults(
48 const SpeechRecognitionResults& results) OVERRIDE;
49 virtual void OnRecognitionError(
51 const SpeechRecognitionError& error) OVERRIDE;
52 virtual void OnAudioLevelsChange(int session_id,
54 float noise_volume) OVERRIDE;
56 // BrowserMessageFilter implementation.
57 virtual bool OnMessageReceived(const IPC::Message& message,
58 bool* message_was_ok) OVERRIDE;
59 virtual void OverrideThreadForMessage(
60 const IPC::Message& message,
61 BrowserThread::ID* thread) OVERRIDE;
63 virtual void OnChannelClosing() OVERRIDE;
66 virtual ~InputTagSpeechDispatcherHost();
68 void OnStartRecognition(
69 const InputTagSpeechHostMsg_StartRecognition_Params& params);
70 void OnCancelRecognition(int render_view_id, int request_id);
71 void OnStopRecording(int render_view_id, int request_id);
73 void StartRecognitionOnIO(
74 int embedder_render_process_id,
75 int embedder_render_view_id,
76 const InputTagSpeechHostMsg_StartRecognition_Params& params,
77 bool filter_profanities);
80 int render_process_id_;
81 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
83 // Used for posting asynchronous tasks (on the IO thread) without worrying
84 // about this class being destroyed in the meanwhile (due to browser shutdown)
85 // since tasks pending on a destroyed WeakPtr are automatically discarded.
86 base::WeakPtrFactory<InputTagSpeechDispatcherHost> weak_factory_;
88 DISALLOW_COPY_AND_ASSIGN(InputTagSpeechDispatcherHost);
91 } // namespace content
93 #endif // CONTENT_BROWSER_SPEECH_INPUT_TAG_SPEECH_DISPATCHER_HOST_H_