| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ |
| 6 #define CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ | 6 #define CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/string16.h" | |
| 15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 16 #include "content/public/browser/notification_observer.h" | 15 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/speech_recognition_event_listener.h" | 16 #include "content/public/browser/speech_recognition_event_listener.h" |
| 18 | 17 |
| 19 class Profile; | 18 class Profile; |
| 20 | 19 |
| 21 namespace content { | 20 namespace content { |
| 22 class NotificationRegistrar; | 21 class NotificationRegistrar; |
| 23 struct SpeechRecognitionError; | 22 struct SpeechRecognitionError; |
| 24 class SpeechRecognitionManager; | 23 class SpeechRecognitionManager; |
| 25 struct SpeechRecognitionResult; | 24 struct SpeechRecognitionResult; |
| 26 } | 25 } |
| 27 | 26 |
| 28 namespace net { | 27 namespace net { |
| 29 class URLRequestContextGetter; | 28 class URLRequestContextGetter; |
| 30 } | 29 } |
| 31 | 30 |
| 32 // Used for API tests. | 31 // Used for API tests. |
| 33 class SpeechInputExtensionInterface { | 32 class SpeechInputExtensionInterface { |
| 34 public: | 33 public: |
| 35 SpeechInputExtensionInterface(); | 34 SpeechInputExtensionInterface(); |
| 36 virtual ~SpeechInputExtensionInterface(); | 35 virtual ~SpeechInputExtensionInterface(); |
| 37 | 36 |
| 38 // Called from the IO thread. | 37 // Called from the IO thread. |
| 39 virtual void StartRecording( | 38 virtual void StartRecording( |
| 40 content::SpeechRecognitionEventListener* listener, | 39 content::SpeechRecognitionEventListener* listener, |
| 41 net::URLRequestContextGetter* context_getter, | 40 net::URLRequestContextGetter* context_getter, |
| 42 const string16& extension_name, | 41 const std::string& extension_name, |
| 43 const std::string& language, | 42 const std::string& language, |
| 44 const std::string& grammar, | 43 const std::string& grammar, |
| 45 bool filter_profanities, | 44 bool filter_profanities, |
| 46 bool show_notification) = 0; | 45 int render_process_id) = 0; |
| 47 | 46 |
| 48 virtual void StopRecording(bool recognition_failed) = 0; | 47 virtual void StopRecording(bool recognition_failed) = 0; |
| 49 virtual bool HasAudioInputDevices() = 0; | 48 virtual bool HasAudioInputDevices() = 0; |
| 50 virtual bool IsCapturingAudio() = 0; | 49 virtual bool IsCapturingAudio() = 0; |
| 51 | 50 |
| 52 // Called from the UI thread. | 51 // Called from the UI thread. |
| 53 virtual bool HasValidRecognizer() = 0; | 52 virtual bool HasValidRecognizer() = 0; |
| 54 }; | 53 }; |
| 55 | 54 |
| 56 // Manages the speech input requests and responses from the extensions | 55 // Manages the speech input requests and responses from the extensions |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 virtual void OnRecognitionResult( | 125 virtual void OnRecognitionResult( |
| 127 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE; | 126 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE; |
| 128 virtual void OnRecognitionError( | 127 virtual void OnRecognitionError( |
| 129 int session_id, const content::SpeechRecognitionError& error) OVERRIDE; | 128 int session_id, const content::SpeechRecognitionError& error) OVERRIDE; |
| 130 virtual void OnAudioLevelsChange(int session_id, float volume, | 129 virtual void OnAudioLevelsChange(int session_id, float volume, |
| 131 float noise_volume) OVERRIDE; | 130 float noise_volume) OVERRIDE; |
| 132 virtual void OnRecognitionEnd(int session_id) OVERRIDE; | 131 virtual void OnRecognitionEnd(int session_id) OVERRIDE; |
| 133 | 132 |
| 134 // Methods for API testing. | 133 // Methods for API testing. |
| 135 void SetSpeechInputExtensionInterface( | 134 void SetSpeechInputExtensionInterface( |
| 136 SpeechInputExtensionInterface* interface); | 135 SpeechInputExtensionInterface* speech_interface); |
| 136 |
| 137 SpeechInputExtensionInterface* GetSpeechInputExtensionInterface(); | 137 SpeechInputExtensionInterface* GetSpeechInputExtensionInterface(); |
| 138 | 138 |
| 139 private: | 139 private: |
| 140 // SpeechInputExtensionInterface methods: | 140 // SpeechInputExtensionInterface methods: |
| 141 virtual bool IsCapturingAudio() OVERRIDE; | 141 virtual bool IsCapturingAudio() OVERRIDE; |
| 142 virtual bool HasAudioInputDevices() OVERRIDE; | 142 virtual bool HasAudioInputDevices() OVERRIDE; |
| 143 virtual bool HasValidRecognizer() OVERRIDE; | 143 virtual bool HasValidRecognizer() OVERRIDE; |
| 144 virtual void StartRecording( | 144 virtual void StartRecording( |
| 145 content::SpeechRecognitionEventListener* listener, | 145 content::SpeechRecognitionEventListener* listener, |
| 146 net::URLRequestContextGetter* context_getter, | 146 net::URLRequestContextGetter* context_getter, |
| 147 const string16& extension_name, | 147 const std::string& extension_name, |
| 148 const std::string& language, | 148 const std::string& language, |
| 149 const std::string& grammar, | 149 const std::string& grammar, |
| 150 bool filter_profanities, | 150 bool filter_profanities, |
| 151 bool show_notification) OVERRIDE; | 151 int render_process_id) OVERRIDE; |
| 152 | 152 |
| 153 virtual void StopRecording(bool recognition_failed) OVERRIDE; | 153 virtual void StopRecording(bool recognition_failed) OVERRIDE; |
| 154 | 154 |
| 155 // Internal methods. | 155 // Internal methods. |
| 156 void StartOnIOThread( | 156 void StartOnIOThread( |
| 157 scoped_refptr<net::URLRequestContextGetter> context_getter, | 157 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 158 const string16& extension_name, | 158 const std::string& extension_name, |
| 159 const std::string& language, | 159 const std::string& language, |
| 160 const std::string& grammar, | 160 const std::string& grammar, |
| 161 bool filter_profanities, | 161 bool filter_profanities, |
| 162 bool show_notification); | 162 int render_process_id); |
| 163 void ForceStopOnIOThread(); | 163 void ForceStopOnIOThread(); |
| 164 void IsRecordingOnIOThread(const IsRecordingCallback& callback); | 164 void IsRecordingOnIOThread(const IsRecordingCallback& callback); |
| 165 | 165 |
| 166 void SetRecognitionResultOnUIThread( | 166 void SetRecognitionResultOnUIThread( |
| 167 const content::SpeechRecognitionResult& result, | 167 const content::SpeechRecognitionResult& result, |
| 168 const std::string& extension_id); | 168 const std::string& extension_id); |
| 169 void DidStartReceivingAudioOnUIThread(); | 169 void DidStartReceivingAudioOnUIThread(); |
| 170 void StopSucceededOnUIThread(); | 170 void StopSucceededOnUIThread(); |
| 171 void IsRecordingOnUIThread(const IsRecordingCallback& callback, bool result); | 171 void IsRecordingOnUIThread(const IsRecordingCallback& callback, bool result); |
| 172 | 172 |
| 173 void DispatchError(const std::string& error, bool dispatch_event); | 173 void DispatchError(const std::string& error, bool dispatch_event); |
| 174 void DispatchEventToExtension(const std::string& extension_id, | 174 void DispatchEventToExtension(const std::string& extension_id, |
| 175 const std::string& event, | 175 const std::string& event, |
| 176 const std::string& json_args); | 176 const std::string& json_args); |
| 177 void ExtensionUnloaded(const std::string& extension_id); | 177 void ExtensionUnloaded(const std::string& extension_id); |
| 178 | 178 |
| 179 void ResetToIdleState(); | 179 void ResetToIdleState(); |
| 180 | 180 |
| 181 void AbortAllSessionsOnIOThread(); | 181 void AbortAllSessionsOnIOThread(); |
| 182 | 182 |
| 183 int GetRenderProcessIDForExtension(const std::string& extension_id) const; |
| 184 |
| 183 virtual ~SpeechInputExtensionManager(); | 185 virtual ~SpeechInputExtensionManager(); |
| 184 | 186 |
| 185 friend class base::RefCountedThreadSafe<SpeechInputExtensionManager>; | 187 friend class base::RefCountedThreadSafe<SpeechInputExtensionManager>; |
| 186 class Factory; | 188 class Factory; |
| 187 | 189 |
| 188 // Lock used to allow exclusive access to the state variable and methods that | 190 // Lock used to allow exclusive access to the state variable and methods that |
| 189 // either read or write on it. This is required since the speech code | 191 // either read or write on it. This is required since the speech code |
| 190 // operates in the IO thread while the extension code uses the UI thread. | 192 // operates in the IO thread while the extension code uses the UI thread. |
| 191 base::Lock state_lock_; | 193 base::Lock state_lock_; |
| 192 | 194 |
| 193 // Used in the UI thread but also its raw value as notification | 195 // Used in the UI thread but also its raw value as notification |
| 194 // source in the IO thread, guarded by the state lock and value. | 196 // source in the IO thread, guarded by the state lock and value. |
| 195 Profile* profile_; | 197 Profile* profile_; |
| 196 | 198 |
| 197 // Used in both threads, guarded by the state lock. | 199 // Used in both threads, guarded by the state lock. |
| 198 State state_; | 200 State state_; |
| 199 std::string extension_id_in_use_; | 201 std::string extension_id_in_use_; |
| 200 | 202 |
| 201 // Used in the UI thread. | 203 // Used in the UI thread. |
| 202 scoped_ptr<content::NotificationRegistrar> registrar_; | 204 scoped_ptr<content::NotificationRegistrar> registrar_; |
| 203 SpeechInputExtensionInterface* speech_interface_; | 205 SpeechInputExtensionInterface* speech_interface_; |
| 204 | 206 |
| 205 // Used in the IO thread. | 207 // Used in the IO thread. |
| 206 bool is_recognition_in_progress_; | 208 bool is_recognition_in_progress_; |
| 207 int speech_recognition_session_id_; | 209 int speech_recognition_session_id_; |
| 208 }; | 210 }; |
| 209 | 211 |
| 210 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ | 212 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ |
| OLD | NEW |