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 CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_
6 #define CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/speech_recognition_event_listener.h"
24 class NotificationRegistrar
;
25 struct SpeechRecognitionError
;
26 class SpeechRecognitionManager
;
27 struct SpeechRecognitionResult
;
31 class URLRequestContextGetter
;
34 // Used for API tests.
35 class SpeechInputExtensionInterface
{
37 SpeechInputExtensionInterface();
38 virtual ~SpeechInputExtensionInterface();
40 // Called from the IO thread.
41 virtual void StartRecording(
42 content::SpeechRecognitionEventListener
* listener
,
43 net::URLRequestContextGetter
* context_getter
,
44 const std::string
& extension_name
,
45 const std::string
& language
,
46 const std::string
& grammar
,
47 bool filter_profanities
,
48 int render_process_id
) = 0;
50 virtual void StopRecording(bool recognition_failed
) = 0;
51 virtual bool HasAudioInputDevices() = 0;
52 virtual bool IsCapturingAudio() = 0;
54 // Called from the UI thread.
55 virtual bool HasValidRecognizer() = 0;
58 // Manages the speech input requests and responses from the extensions
59 // associated to the given profile.
60 class SpeechInputExtensionManager
61 : public base::RefCountedThreadSafe
<SpeechInputExtensionManager
>,
62 public content::SpeechRecognitionEventListener
,
63 public content::NotificationObserver
,
64 private SpeechInputExtensionInterface
{
71 kShutdown
// Internal sink state when the profile is destroyed on shutdown.
74 // Structure containing the details of the speech input failed notification.
75 struct ExtensionError
{
76 std::string extension_id_
;
79 ExtensionError(const std::string
& extension_id
, const std::string
& error
)
80 : extension_id_(extension_id
), error_(error
) {}
83 typedef base::Callback
<void(bool)> IsRecordingCallback
;
85 // Should not be used directly. Managed by a ProfileKeyedServiceFactory.
86 explicit SpeechInputExtensionManager(Profile
* profile
);
88 // Returns the corresponding manager for the given profile, creating
89 // a new one if required.
90 static SpeechInputExtensionManager
* GetForProfile(Profile
* profile
);
92 // Initialize the ProfileKeyedServiceFactory.
93 static void InitializeFactory();
95 // Request to start speech recognition for the provided extension.
96 bool Start(const std::string
& extension_id
,
97 const std::string
& language
,
98 const std::string
& grammar
,
99 bool filter_profanities
,
102 // Request to stop an ongoing speech recognition.
103 bool Stop(const std::string
& extension_id
, std::string
* error
);
105 // Retrieve the actual state of the API manager.
106 State
state() const { return state_
; }
108 // Check if recording is currently ongoing in Chrome.
109 // This method is expected to be called from the UI thread.
110 // The callback will be invoked with the result on this same thread.
111 void IsRecording(const IsRecordingCallback
& callback
);
113 // Called by internal ProfileKeyedService class.
114 void ShutdownOnUIThread();
116 // Methods from content::NotificationObserver.
117 virtual void Observe(int type
,
118 const content::NotificationSource
& source
,
119 const content::NotificationDetails
& details
) OVERRIDE
;
121 // Methods from SpeechRecognitionEventListener.
122 virtual void OnRecognitionStart(int session_id
) OVERRIDE
;
123 virtual void OnAudioStart(int session_id
) OVERRIDE
;
124 virtual void OnEnvironmentEstimationComplete(int session_id
) OVERRIDE
;
125 virtual void OnSoundStart(int session_id
) OVERRIDE
;
126 virtual void OnSoundEnd(int session_id
) OVERRIDE
;
127 virtual void OnAudioEnd(int session_id
) OVERRIDE
;
128 virtual void OnRecognitionResult(
129 int session_id
, const content::SpeechRecognitionResult
& result
) OVERRIDE
;
130 virtual void OnRecognitionError(
131 int session_id
, const content::SpeechRecognitionError
& error
) OVERRIDE
;
132 virtual void OnAudioLevelsChange(int session_id
, float volume
,
133 float noise_volume
) OVERRIDE
;
134 virtual void OnRecognitionEnd(int session_id
) OVERRIDE
;
136 // Methods for API testing.
137 void SetSpeechInputExtensionInterface(
138 SpeechInputExtensionInterface
* speech_interface
);
140 SpeechInputExtensionInterface
* GetSpeechInputExtensionInterface();
143 // SpeechInputExtensionInterface methods:
144 virtual bool IsCapturingAudio() OVERRIDE
;
145 virtual bool HasAudioInputDevices() OVERRIDE
;
146 virtual bool HasValidRecognizer() OVERRIDE
;
147 virtual void StartRecording(
148 content::SpeechRecognitionEventListener
* listener
,
149 net::URLRequestContextGetter
* context_getter
,
150 const std::string
& extension_name
,
151 const std::string
& language
,
152 const std::string
& grammar
,
153 bool filter_profanities
,
154 int render_process_id
) OVERRIDE
;
156 virtual void StopRecording(bool recognition_failed
) OVERRIDE
;
159 void StartOnIOThread(
160 scoped_refptr
<net::URLRequestContextGetter
> context_getter
,
161 const std::string
& extension_name
,
162 const std::string
& language
,
163 const std::string
& grammar
,
164 bool filter_profanities
,
165 int render_process_id
);
166 void ForceStopOnIOThread();
167 void IsRecordingOnIOThread(const IsRecordingCallback
& callback
);
169 void SetRecognitionResultOnUIThread(
170 const content::SpeechRecognitionResult
& result
,
171 const std::string
& extension_id
);
172 void DidStartReceivingAudioOnUIThread();
173 void StopSucceededOnUIThread();
174 void IsRecordingOnUIThread(const IsRecordingCallback
& callback
, bool result
);
176 void DispatchError(const std::string
& error
, bool dispatch_event
);
177 void DispatchEventToExtension(const std::string
& extension_id
,
178 const std::string
& event
,
179 scoped_ptr
<base::ListValue
> event_args
);
180 void ExtensionUnloaded(const std::string
& extension_id
);
182 void ResetToIdleState();
184 void AbortAllSessionsOnIOThread();
186 int GetRenderProcessIDForExtension(const std::string
& extension_id
) const;
188 virtual ~SpeechInputExtensionManager();
190 friend class base::RefCountedThreadSafe
<SpeechInputExtensionManager
>;
193 // Lock used to allow exclusive access to the state variable and methods that
194 // either read or write on it. This is required since the speech code
195 // operates in the IO thread while the extension code uses the UI thread.
196 base::Lock state_lock_
;
198 // Used in the UI thread but also its raw value as notification
199 // source in the IO thread, guarded by the state lock and value.
202 // Used in both threads, guarded by the state lock.
204 std::string extension_id_in_use_
;
206 // Used in the UI thread.
207 scoped_ptr
<content::NotificationRegistrar
> registrar_
;
208 SpeechInputExtensionInterface
* speech_interface_
;
210 // Used in the IO thread.
211 bool is_recognition_in_progress_
;
212 int speech_recognition_session_id_
;
215 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_