| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/synchronization/lock.h" |
| 11 #include "content/browser/speech/speech_recognizer.h" |
| 12 #include "content/common/notification_observer.h" |
| 13 #include "content/common/notification_registrar.h" |
| 14 #include "content/common/speech_input_result.h" |
| 15 #include <string> |
| 16 |
| 17 class Extension; |
| 18 class NotificationDetails; |
| 19 class NotificationSource; |
| 20 class Profile; |
| 21 |
| 22 namespace net { |
| 23 class URLRequestContextGetter; |
| 24 } |
| 25 |
| 26 // Used for API tests. All methods run on the IO thread. |
| 27 class ExtensionSpeechInterface { |
| 28 public: |
| 29 ExtensionSpeechInterface(); |
| 30 virtual ~ExtensionSpeechInterface(); |
| 31 |
| 32 virtual bool CheckForAvailableDevices() = 0; |
| 33 virtual void StartRecording( |
| 34 speech_input::SpeechRecognizerDelegate* delegate, |
| 35 net::URLRequestContextGetter* context_getter, |
| 36 int caller_id, |
| 37 const std::string& language, |
| 38 const std::string& grammar, |
| 39 bool filter_profanities) = 0; |
| 40 |
| 41 virtual void StopRecording(bool recognition_failed) = 0; |
| 42 virtual bool HasValidRecognizer() const = 0; |
| 43 |
| 44 protected: |
| 45 scoped_refptr<speech_input::SpeechRecognizer> recognizer_; |
| 46 }; |
| 47 |
| 48 // Manages the speech input requests and responses from the extensions |
| 49 // associated to the given profile. |
| 50 class ExtensionSpeechInputManager : public NotificationObserver, |
| 51 public base::RefCountedThreadSafe<ExtensionSpeechInputManager>, |
| 52 public speech_input::SpeechRecognizerDelegate, |
| 53 private ExtensionSpeechInterface { |
| 54 public: |
| 55 enum State { |
| 56 kIdle = 0, |
| 57 kStarting, |
| 58 kRecording, |
| 59 kStopping, |
| 60 kShutdown // Internal sink state when the profile is destroyed on shutdown. |
| 61 }; |
| 62 |
| 63 // Structure containing the details of the speech input failed notification. |
| 64 struct ExtensionError { |
| 65 std::string extension_id_; |
| 66 std::string error_; |
| 67 |
| 68 ExtensionError(const std::string& extension_id, const std::string& error) |
| 69 : extension_id_(extension_id), error_(error) {} |
| 70 }; |
| 71 |
| 72 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. |
| 73 explicit ExtensionSpeechInputManager(Profile* profile); |
| 74 |
| 75 // Returns the corresponding manager for the given profile, creating |
| 76 // a new one if required. |
| 77 static ExtensionSpeechInputManager* GetForProfile(Profile* profile); |
| 78 |
| 79 // Initialize the ProfileKeyedServiceFactory. |
| 80 static void InitializeFactory(); |
| 81 |
| 82 // Request to start speech recognition for the provided extension. |
| 83 bool Start(const std::string& extension_id, |
| 84 const std::string& language, |
| 85 const std::string& grammar, |
| 86 bool filter_profanities, |
| 87 std::string* error); |
| 88 |
| 89 // Request to stop an ongoing speech recognition. |
| 90 bool Stop(const std::string& extension_id, std::string* error); |
| 91 |
| 92 // Retrieve the actual state of the API manager. |
| 93 State state() const { return state_; } |
| 94 |
| 95 // Called by internal ProfileKeyedService class. |
| 96 void ShutdownOnUIThread(); |
| 97 |
| 98 // Methods from NotificationObserver. |
| 99 virtual void Observe(int type, |
| 100 const NotificationSource& source, |
| 101 const NotificationDetails& details) OVERRIDE; |
| 102 |
| 103 // Methods from SpeechRecognizerDelegate. |
| 104 virtual void SetRecognitionResult( |
| 105 int caller_id, |
| 106 const speech_input::SpeechInputResult& result) OVERRIDE; |
| 107 |
| 108 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE; |
| 109 virtual void DidCompleteRecording(int caller_id) OVERRIDE; |
| 110 virtual void DidCompleteRecognition(int caller_id) OVERRIDE; |
| 111 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE; |
| 112 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE; |
| 113 virtual void OnRecognizerError(int caller_id, |
| 114 speech_input::SpeechInputError error) |
| 115 OVERRIDE; |
| 116 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE; |
| 117 virtual void SetInputVolume(int caller_id, float volume, |
| 118 float noise_volume) OVERRIDE {} |
| 119 |
| 120 // Methods for API testing. |
| 121 void SetExtensionSpeechInterface(ExtensionSpeechInterface* interface); |
| 122 ExtensionSpeechInterface* GetExtensionSpeechInterface(); |
| 123 |
| 124 private: |
| 125 // ExtensionSpeechInterface methods: |
| 126 virtual bool CheckForAvailableDevices() OVERRIDE; |
| 127 virtual bool HasValidRecognizer() const OVERRIDE; |
| 128 |
| 129 virtual void StartRecording( |
| 130 speech_input::SpeechRecognizerDelegate* delegate, |
| 131 net::URLRequestContextGetter* context_getter, |
| 132 int caller_id, |
| 133 const std::string& language, |
| 134 const std::string& grammar, |
| 135 bool filter_profanities) OVERRIDE; |
| 136 |
| 137 virtual void StopRecording(bool recognition_failed) OVERRIDE; |
| 138 |
| 139 // Internal methods. |
| 140 void StartOnIOThread( |
| 141 net::URLRequestContextGetter* context_getter, |
| 142 const std::string& language, |
| 143 const std::string& grammar, |
| 144 bool filter_profanities); |
| 145 void ForceStopOnIOThread(); |
| 146 |
| 147 void SetRecognitionResultOnUIThread( |
| 148 const speech_input::SpeechInputResult& result, |
| 149 const std::string& extension_id); |
| 150 void DidStartReceivingAudioOnUIThread(); |
| 151 void StopSucceededOnUIThread(); |
| 152 |
| 153 void DispatchError(const std::string& error, bool dispatch_event); |
| 154 void DispatchEventToExtension(const std::string& extension_id, |
| 155 const std::string& event, |
| 156 const std::string& json_args); |
| 157 void ExtensionUnloaded(const std::string& extension_id); |
| 158 |
| 159 void ResetToIdleState(); |
| 160 |
| 161 virtual ~ExtensionSpeechInputManager(); |
| 162 |
| 163 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; |
| 164 class Factory; |
| 165 |
| 166 // Lock used to allow exclusive access to the state variable and methods that |
| 167 // either read or write on it. This is required since the speech code |
| 168 // operates in the IO thread while the extension code uses the UI thread. |
| 169 base::Lock state_lock_; |
| 170 |
| 171 // Used in the UI thread but also its raw value as notification |
| 172 // source in the IO thread, guarded by the state lock and value. |
| 173 Profile* profile_; |
| 174 |
| 175 // Used in both threads, guarded by the state lock. |
| 176 State state_; |
| 177 std::string extension_id_in_use_; |
| 178 |
| 179 // Used in the UI thread. |
| 180 NotificationRegistrar registrar_; |
| 181 ExtensionSpeechInterface* speech_interface_; |
| 182 }; |
| 183 |
| 184 #endif // CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER
_H_ |
| OLD | NEW |