| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER_H_ | 6 #define CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
| 11 #include "content/browser/speech/speech_recognizer.h" | 10 #include "content/browser/speech/speech_recognizer.h" |
| 12 #include "content/common/speech_input_result.h" | 11 #include "content/common/speech_input_result.h" |
| 13 #include "content/public/browser/notification_observer.h" | 12 #include "content/public/browser/notification_observer.h" |
| 14 #include "content/public/browser/notification_registrar.h" | 13 #include "content/public/browser/notification_registrar.h" |
| 15 #include <string> | 14 #include <string> |
| 16 | 15 |
| 17 class Extension; | 16 class Extension; |
| 18 class Profile; | 17 class Profile; |
| 18 class SpeechInputExtensionNotification; |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 class URLRequestContextGetter; | 21 class URLRequestContextGetter; |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Used for API tests. | 24 // Used for API tests. |
| 25 class ExtensionSpeechInterface { | 25 class SpeechInputExtensionInterface { |
| 26 public: | 26 public: |
| 27 ExtensionSpeechInterface(); | 27 SpeechInputExtensionInterface(); |
| 28 virtual ~ExtensionSpeechInterface(); | 28 virtual ~SpeechInputExtensionInterface(); |
| 29 | 29 |
| 30 // Called from the IO thread. | 30 // Called from the IO thread. |
| 31 virtual void StartRecording( | 31 virtual void StartRecording( |
| 32 speech_input::SpeechRecognizerDelegate* delegate, | 32 speech_input::SpeechRecognizerDelegate* delegate, |
| 33 net::URLRequestContextGetter* context_getter, | 33 net::URLRequestContextGetter* context_getter, |
| 34 int caller_id, | 34 int caller_id, |
| 35 const std::string& language, | 35 const std::string& language, |
| 36 const std::string& grammar, | 36 const std::string& grammar, |
| 37 bool filter_profanities) = 0; | 37 bool filter_profanities) = 0; |
| 38 | 38 |
| 39 virtual void StopRecording(bool recognition_failed) = 0; | 39 virtual void StopRecording(bool recognition_failed) = 0; |
| 40 virtual bool HasAudioInputDevices() = 0; | 40 virtual bool HasAudioInputDevices() = 0; |
| 41 | 41 |
| 42 // Called from the UI thread. | 42 // Called from the UI thread. |
| 43 virtual bool HasValidRecognizer() = 0; | 43 virtual bool HasValidRecognizer() = 0; |
| 44 | 44 |
| 45 // Called from both IO and UI threads. | 45 // Called from both IO and UI threads. |
| 46 virtual bool IsRecordingInProcess() = 0; | 46 virtual bool IsRecordingInProcess() = 0; |
| 47 | 47 |
| 48 protected: | 48 protected: |
| 49 scoped_refptr<speech_input::SpeechRecognizer> recognizer_; | 49 scoped_refptr<speech_input::SpeechRecognizer> recognizer_; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // Manages the speech input requests and responses from the extensions | 52 // Manages the speech input requests and responses from the extensions |
| 53 // associated to the given profile. | 53 // associated to the given profile. |
| 54 class ExtensionSpeechInputManager | 54 class SpeechInputExtensionManager |
| 55 : public base::RefCountedThreadSafe<ExtensionSpeechInputManager>, | 55 : public base::RefCountedThreadSafe<SpeechInputExtensionManager>, |
| 56 public speech_input::SpeechRecognizerDelegate, | 56 public speech_input::SpeechRecognizerDelegate, |
| 57 public content::NotificationObserver, | 57 public content::NotificationObserver, |
| 58 private ExtensionSpeechInterface { | 58 private SpeechInputExtensionInterface { |
| 59 public: | 59 public: |
| 60 enum State { | 60 enum State { |
| 61 kIdle = 0, | 61 kIdle = 0, |
| 62 kStarting, | 62 kStarting, |
| 63 kRecording, | 63 kRecording, |
| 64 kStopping, | 64 kStopping, |
| 65 kShutdown // Internal sink state when the profile is destroyed on shutdown. | 65 kShutdown // Internal sink state when the profile is destroyed on shutdown. |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Structure containing the details of the speech input failed notification. | 68 // Structure containing the details of the speech input failed notification. |
| 69 struct ExtensionError { | 69 struct ExtensionError { |
| 70 std::string extension_id_; | 70 std::string extension_id_; |
| 71 std::string error_; | 71 std::string error_; |
| 72 | 72 |
| 73 ExtensionError(const std::string& extension_id, const std::string& error) | 73 ExtensionError(const std::string& extension_id, const std::string& error) |
| 74 : extension_id_(extension_id), error_(error) {} | 74 : extension_id_(extension_id), error_(error) {} |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. | 77 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. |
| 78 explicit ExtensionSpeechInputManager(Profile* profile); | 78 explicit SpeechInputExtensionManager(Profile* profile); |
| 79 | 79 |
| 80 // Returns the corresponding manager for the given profile, creating | 80 // Returns the corresponding manager for the given profile, creating |
| 81 // a new one if required. | 81 // a new one if required. |
| 82 static ExtensionSpeechInputManager* GetForProfile(Profile* profile); | 82 static SpeechInputExtensionManager* GetForProfile(Profile* profile); |
| 83 | 83 |
| 84 // Initialize the ProfileKeyedServiceFactory. | 84 // Initialize the ProfileKeyedServiceFactory. |
| 85 static void InitializeFactory(); | 85 static void InitializeFactory(); |
| 86 | 86 |
| 87 // Request to start speech recognition for the provided extension. | 87 // Request to start speech recognition for the provided extension. |
| 88 bool Start(const std::string& extension_id, | 88 bool Start(const std::string& extension_id, |
| 89 const std::string& language, | 89 const std::string& language, |
| 90 const std::string& grammar, | 90 const std::string& grammar, |
| 91 bool filter_profanities, | 91 bool filter_profanities, |
| 92 std::string* error); | 92 std::string* error); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 116 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE; | 116 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE; |
| 117 virtual void DidCompleteRecording(int caller_id) OVERRIDE; | 117 virtual void DidCompleteRecording(int caller_id) OVERRIDE; |
| 118 virtual void DidCompleteRecognition(int caller_id) OVERRIDE; | 118 virtual void DidCompleteRecognition(int caller_id) OVERRIDE; |
| 119 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE; | 119 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE; |
| 120 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE; | 120 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE; |
| 121 virtual void OnRecognizerError(int caller_id, | 121 virtual void OnRecognizerError(int caller_id, |
| 122 speech_input::SpeechInputError error) | 122 speech_input::SpeechInputError error) |
| 123 OVERRIDE; | 123 OVERRIDE; |
| 124 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE; | 124 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE; |
| 125 virtual void SetInputVolume(int caller_id, float volume, | 125 virtual void SetInputVolume(int caller_id, float volume, |
| 126 float noise_volume) OVERRIDE {} | 126 float noise_volume) OVERRIDE; |
| 127 | 127 |
| 128 // Methods for API testing. | 128 // Methods for API testing. |
| 129 void SetExtensionSpeechInterface(ExtensionSpeechInterface* interface); | 129 void SetSpeechInputExtensionInterface( |
| 130 ExtensionSpeechInterface* GetExtensionSpeechInterface(); | 130 SpeechInputExtensionInterface* interface); |
| 131 SpeechInputExtensionInterface* GetSpeechInputExtensionInterface(); |
| 131 | 132 |
| 132 private: | 133 private: |
| 133 // ExtensionSpeechInterface methods: | 134 // SpeechInputExtensionInterface methods: |
| 134 virtual bool IsRecordingInProcess() OVERRIDE; | 135 virtual bool IsRecordingInProcess() OVERRIDE; |
| 135 virtual bool HasAudioInputDevices() OVERRIDE; | 136 virtual bool HasAudioInputDevices() OVERRIDE; |
| 136 virtual bool HasValidRecognizer() OVERRIDE; | 137 virtual bool HasValidRecognizer() OVERRIDE; |
| 137 | 138 |
| 138 virtual void StartRecording( | 139 virtual void StartRecording( |
| 139 speech_input::SpeechRecognizerDelegate* delegate, | 140 speech_input::SpeechRecognizerDelegate* delegate, |
| 140 net::URLRequestContextGetter* context_getter, | 141 net::URLRequestContextGetter* context_getter, |
| 141 int caller_id, | 142 int caller_id, |
| 142 const std::string& language, | 143 const std::string& language, |
| 143 const std::string& grammar, | 144 const std::string& grammar, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 158 const std::string& extension_id); | 159 const std::string& extension_id); |
| 159 void DidStartReceivingAudioOnUIThread(); | 160 void DidStartReceivingAudioOnUIThread(); |
| 160 void StopSucceededOnUIThread(); | 161 void StopSucceededOnUIThread(); |
| 161 | 162 |
| 162 void DispatchError(const std::string& error, bool dispatch_event); | 163 void DispatchError(const std::string& error, bool dispatch_event); |
| 163 void DispatchEventToExtension(const std::string& extension_id, | 164 void DispatchEventToExtension(const std::string& extension_id, |
| 164 const std::string& event, | 165 const std::string& event, |
| 165 const std::string& json_args); | 166 const std::string& json_args); |
| 166 void ExtensionUnloaded(const std::string& extension_id); | 167 void ExtensionUnloaded(const std::string& extension_id); |
| 167 | 168 |
| 169 void SetInputVolumeOnUIThread(float volume); |
| 168 void ResetToIdleState(); | 170 void ResetToIdleState(); |
| 169 | 171 |
| 170 virtual ~ExtensionSpeechInputManager(); | 172 virtual ~SpeechInputExtensionManager(); |
| 171 | 173 |
| 172 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; | 174 friend class base::RefCountedThreadSafe<SpeechInputExtensionManager>; |
| 173 class Factory; | 175 class Factory; |
| 174 | 176 |
| 175 // Lock used to allow exclusive access to the state variable and methods that | 177 // Lock used to allow exclusive access to the state variable and methods that |
| 176 // either read or write on it. This is required since the speech code | 178 // either read or write on it. This is required since the speech code |
| 177 // operates in the IO thread while the extension code uses the UI thread. | 179 // operates in the IO thread while the extension code uses the UI thread. |
| 178 base::Lock state_lock_; | 180 base::Lock state_lock_; |
| 179 | 181 |
| 180 // Used in the UI thread but also its raw value as notification | 182 // Used in the UI thread but also its raw value as notification |
| 181 // source in the IO thread, guarded by the state lock and value. | 183 // source in the IO thread, guarded by the state lock and value. |
| 182 Profile* profile_; | 184 Profile* profile_; |
| 183 | 185 |
| 184 // Used in both threads, guarded by the state lock. | 186 // Used in both threads, guarded by the state lock. |
| 185 State state_; | 187 State state_; |
| 186 std::string extension_id_in_use_; | 188 std::string extension_id_in_use_; |
| 187 | 189 |
| 188 // Used in the UI thread. | 190 // Used in the UI thread. |
| 189 content::NotificationRegistrar registrar_; | 191 content::NotificationRegistrar registrar_; |
| 190 ExtensionSpeechInterface* speech_interface_; | 192 SpeechInputExtensionInterface* speech_interface_; |
| 193 scoped_ptr<SpeechInputExtensionNotification> notification_; |
| 191 }; | 194 }; |
| 192 | 195 |
| 193 #endif // CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER
_H_ | 196 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_ |
| OLD | NEW |