[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / speech / speech_recognition_manager_impl.h
blob39d49cceebcb828e94a48d3df751ece1dcc8df6a
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_SPEECH_RECOGNITION_MANAGER_IMPL_H_
6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/browser/renderer_host/media/media_stream_requester.h"
16 #include "content/public/browser/speech_recognition_event_listener.h"
17 #include "content/public/browser/speech_recognition_manager.h"
18 #include "content/public/browser/speech_recognition_session_config.h"
19 #include "content/public/browser/speech_recognition_session_context.h"
20 #include "content/public/common/speech_recognition_error.h"
22 namespace content {
23 class BrowserMainLoop;
24 class SpeechRecognitionManagerDelegate;
25 class SpeechRecognizer;
27 // This is the manager for speech recognition. It is a single instance in
28 // the browser process and can serve several requests. Each recognition request
29 // corresponds to a session, initiated via |CreateSession|.
31 // In any moment, the manager has a single session known as the primary session,
32 // |primary_session_id_|.
33 // This is the session that is capturing audio, waiting for user permission,
34 // etc. There may also be other, non-primary, sessions living in parallel that
35 // are waiting for results but not recording audio.
37 // The SpeechRecognitionManager has the following responsibilities:
38 // - Handles requests received from various render views and makes sure only
39 // one of them accesses the audio device at any given time.
40 // - Handles the instantiation of SpeechRecognitionEngine objects when
41 // requested by SpeechRecognitionSessions.
42 // - Relays recognition results/status/error events of each session to the
43 // corresponding listener (demuxing on the base of their session_id).
44 // - Relays also recognition results/status/error events of every session to
45 // the catch-all snoop listener (optionally) provided by the delegate.
46 class CONTENT_EXPORT SpeechRecognitionManagerImpl :
47 public NON_EXPORTED_BASE(SpeechRecognitionManager),
48 public SpeechRecognitionEventListener {
49 public:
50 // Returns the current SpeechRecognitionManagerImpl or NULL if the call is
51 // issued when it is not created yet or destroyed (by BrowserMainLoop).
52 static SpeechRecognitionManagerImpl* GetInstance();
54 // SpeechRecognitionManager implementation.
55 virtual int CreateSession(
56 const SpeechRecognitionSessionConfig& config) OVERRIDE;
57 virtual void StartSession(int session_id) OVERRIDE;
58 virtual void AbortSession(int session_id) OVERRIDE;
59 virtual void AbortAllSessionsForListener(
60 SpeechRecognitionEventListener* listener) OVERRIDE;
61 virtual void AbortAllSessionsForRenderView(int render_process_id,
62 int render_view_id) OVERRIDE;
63 virtual void StopAudioCaptureForSession(int session_id) OVERRIDE;
64 virtual const SpeechRecognitionSessionConfig& GetSessionConfig(
65 int session_id) const OVERRIDE;
66 virtual SpeechRecognitionSessionContext GetSessionContext(
67 int session_id) const OVERRIDE;
68 virtual int GetSession(int render_process_id,
69 int render_view_id,
70 int request_id) const OVERRIDE;
71 virtual bool HasAudioInputDevices() OVERRIDE;
72 virtual bool IsCapturingAudio() OVERRIDE;
73 virtual string16 GetAudioInputDeviceModel() OVERRIDE;
74 virtual void ShowAudioInputSettings() OVERRIDE;
76 // SpeechRecognitionEventListener methods.
77 virtual void OnRecognitionStart(int session_id) OVERRIDE;
78 virtual void OnAudioStart(int session_id) OVERRIDE;
79 virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE;
80 virtual void OnSoundStart(int session_id) OVERRIDE;
81 virtual void OnSoundEnd(int session_id) OVERRIDE;
82 virtual void OnAudioEnd(int session_id) OVERRIDE;
83 virtual void OnRecognitionEnd(int session_id) OVERRIDE;
84 virtual void OnRecognitionResults(
85 int session_id, const SpeechRecognitionResults& result) OVERRIDE;
86 virtual void OnRecognitionError(
87 int session_id, const SpeechRecognitionError& error) OVERRIDE;
88 virtual void OnAudioLevelsChange(int session_id, float volume,
89 float noise_volume) OVERRIDE;
91 protected:
92 // BrowserMainLoop is the only one allowed to istantiate and free us.
93 friend class BrowserMainLoop;
94 friend class scoped_ptr<SpeechRecognitionManagerImpl>; // Needed for dtor.
95 SpeechRecognitionManagerImpl();
96 virtual ~SpeechRecognitionManagerImpl();
98 private:
99 // Data types for the internal Finite State Machine (FSM).
100 enum FSMState {
101 SESSION_STATE_IDLE = 0,
102 SESSION_STATE_CAPTURING_AUDIO,
103 SESSION_STATE_WAITING_FOR_RESULT,
104 SESSION_STATE_MAX_VALUE = SESSION_STATE_WAITING_FOR_RESULT
107 enum FSMEvent {
108 EVENT_ABORT = 0,
109 EVENT_START,
110 EVENT_STOP_CAPTURE,
111 EVENT_AUDIO_ENDED,
112 EVENT_RECOGNITION_ENDED,
113 EVENT_MAX_VALUE = EVENT_RECOGNITION_ENDED
116 struct Session {
117 Session();
118 ~Session();
120 int id;
121 bool listener_is_active;
122 SpeechRecognitionSessionConfig config;
123 SpeechRecognitionSessionContext context;
124 scoped_refptr<SpeechRecognizer> recognizer;
127 // Callback issued by the SpeechRecognitionManagerDelegate for reporting
128 // asynchronously the result of the CheckRecognitionIsAllowed call.
129 void RecognitionAllowedCallback(int session_id,
130 bool ask_user,
131 bool is_allowed);
133 // Callback to get back the result of a media request. |label| is the string
134 // to identify the request; |devices| is an array of devices approved to be
135 // used for the request, |devices| is empty if the users deny the request.
136 void MediaRequestPermissionCallback(const std::string& label,
137 const MediaStreamDevices& devices);
139 // Entry point for pushing any external event into the session handling FSM.
140 void DispatchEvent(int session_id, FSMEvent event);
142 // Defines the behavior of the session handling FSM, selecting the appropriate
143 // transition according to the session, its current state and the event.
144 void ExecuteTransitionAndGetNextState(
145 const Session& session, FSMState session_state, FSMEvent event);
147 // Retrieves the state of the session, enquiring directly the recognizer.
148 FSMState GetSessionState(int session_id) const;
150 // The methods below handle transitions of the session handling FSM.
151 void SessionStart(const Session& session);
152 void SessionAbort(const Session& session);
153 void SessionStopAudioCapture(const Session& session);
154 void ResetCapturingSessionId(const Session& session);
155 void SessionDelete(const Session& session);
156 void NotFeasible(const Session& session, FSMEvent event);
158 bool SessionExists(int session_id) const;
159 const Session& GetSession(int session_id) const;
160 SpeechRecognitionEventListener* GetListener(int session_id) const;
161 SpeechRecognitionEventListener* GetDelegateListener() const;
162 int GetNextSessionID();
164 typedef std::map<int, Session> SessionsTable;
165 SessionsTable sessions_;
166 int primary_session_id_;
167 int last_session_id_;
168 bool is_dispatching_event_;
169 scoped_ptr<SpeechRecognitionManagerDelegate> delegate_;
171 // Used for posting asynchronous tasks (on the IO thread) without worrying
172 // about this class being destroyed in the meanwhile (due to browser shutdown)
173 // since tasks pending on a destroyed WeakPtr are automatically discarded.
174 base::WeakPtrFactory<SpeechRecognitionManagerImpl> weak_factory_;
177 } // namespace content
179 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_MANAGER_IMPL_H_