Chromium Code Reviews| 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_EXTENSION_SPEECH_INPUT_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/synchronization/lock.h" | |
| 12 #include "content/browser/speech/speech_recognizer.h" | |
| 11 #include "content/common/notification_observer.h" | 13 #include "content/common/notification_observer.h" |
| 12 #include "content/common/notification_registrar.h" | 14 #include "content/common/notification_registrar.h" |
| 15 #include "content/common/speech_input_result.h" | |
| 13 #include <string> | 16 #include <string> |
| 14 | 17 |
| 15 class Extension; | 18 class Extension; |
| 16 class NotificationDetails; | 19 class NotificationDetails; |
| 17 class NotificationSource; | 20 class NotificationSource; |
| 18 class Profile; | 21 class Profile; |
| 19 | 22 |
| 23 namespace net { | |
| 24 class URLRequestContextGetter; | |
| 25 } | |
| 26 | |
| 20 // Manages the speech input requests and responses from the extensions | 27 // Manages the speech input requests and responses from the extensions |
| 21 // associated to the given profile. | 28 // associated to the given profile. |
| 22 class ExtensionSpeechInputManager : public NotificationObserver, | 29 class ExtensionSpeechInputManager : public NotificationObserver, |
| 23 public base::RefCountedThreadSafe<ExtensionSpeechInputManager> { | 30 public base::RefCountedThreadSafe<ExtensionSpeechInputManager>, |
| 31 public speech_input::SpeechRecognizerDelegate { | |
| 24 public: | 32 public: |
| 33 enum State { | |
| 34 kIdle = 0, | |
| 35 kStarting, | |
| 36 kRecording, | |
| 37 kStopping, | |
| 38 kShutdown // Internal sink state when the profile is destroyed on shutdown. | |
| 39 }; | |
| 40 | |
| 41 // Structure containing the details of the speech input failed notification. | |
| 42 struct ExtensionError { | |
| 43 const std::string& extension_id_; | |
|
Satish
2011/10/11 13:37:16
these shouldn't be a reference as they'll point to
Leandro Graciá Gil
2011/10/11 18:08:00
Used references because the notification is synchr
| |
| 44 const std::string& error_; | |
| 45 | |
| 46 ExtensionError(const std::string& extension_id, const std::string& error) | |
| 47 : extension_id_(extension_id), error_(error) {} | |
| 48 }; | |
| 49 | |
| 25 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. | 50 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. |
| 26 explicit ExtensionSpeechInputManager(Profile* profile); | 51 explicit ExtensionSpeechInputManager(Profile* profile); |
| 27 | 52 |
| 28 // Returns the corresponding manager for the given profile, creating | 53 // Returns the corresponding manager for the given profile, creating |
| 29 // a new one if required. | 54 // a new one if required. |
| 30 static ExtensionSpeechInputManager* GetForProfile(Profile* profile); | 55 static ExtensionSpeechInputManager* GetForProfile(Profile* profile); |
| 31 | 56 |
| 32 // Initialize the ProfileKeyedServiceFactory. | 57 // Initialize the ProfileKeyedServiceFactory. |
| 33 static void InitializeFactory(); | 58 static void InitializeFactory(); |
| 34 | 59 |
| 60 // Request to start speech recognition for the provided extension. | |
| 61 bool Start(const std::string& extension_id, | |
| 62 const std::string& language, | |
| 63 const std::string& grammar, | |
| 64 bool filter_profanities, | |
| 65 std::string* error); | |
| 66 | |
| 67 // Request to stop an ongoing speech recognition. | |
| 68 bool Stop(const std::string& extension_id, std::string* error); | |
| 69 | |
| 70 // Retrieve the actual state of the API manager. | |
| 71 State state() const { return state_; } | |
| 72 | |
| 73 // Called by internal ProfileKeyedService class. | |
| 74 void ShutdownOnUIThread(); | |
| 75 | |
| 35 // Methods from NotificationObserver. | 76 // Methods from NotificationObserver. |
| 36 virtual void Observe(int type, | 77 virtual void Observe(int type, |
| 37 const NotificationSource& source, | 78 const NotificationSource& source, |
| 38 const NotificationDetails& details); | 79 const NotificationDetails& details) OVERRIDE; |
| 39 | 80 |
| 40 // Request to start speech recognition for the provided extension. | 81 // Methods from SpeechRecognizerDelegate. |
| 41 void Start(const Extension *extension); | 82 virtual void SetRecognitionResult( |
| 83 int caller_id, | |
| 84 const speech_input::SpeechInputResult& result) OVERRIDE; | |
| 42 | 85 |
| 43 // Request to stop an ongoing speech recognition. | 86 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE; |
| 44 void Stop(const Extension *extension); | 87 virtual void DidCompleteRecording(int caller_id) OVERRIDE; |
| 45 | 88 virtual void DidCompleteRecognition(int caller_id) OVERRIDE; |
| 46 // Called by internal ProfileKeyedService class. | 89 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE; |
| 47 void ShutdownOnUIThread(); | 90 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE; |
| 91 virtual void OnRecognizerError(int caller_id, | |
| 92 speech_input::SpeechInputError error) | |
| 93 OVERRIDE; | |
| 94 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE; | |
| 95 virtual void SetInputVolume(int caller_id, float volume, | |
| 96 float noise_volume) OVERRIDE {} | |
| 48 | 97 |
| 49 private: | 98 private: |
| 50 void ExtensionUnloaded(const std::string& id); | 99 void StartOnIOThread( |
| 100 net::URLRequestContextGetter* context_getter, | |
| 101 const std::string& language, | |
| 102 const std::string& grammar, | |
| 103 bool filter_profanities); | |
| 104 void ForceStopOnIOThread(); | |
| 105 | |
| 106 void SetRecognitionResultOnUIThread( | |
| 107 const speech_input::SpeechInputResult& result); | |
| 108 void DidStartReceivingAudioOnUIThread(); | |
| 109 void StopSucceededOnUIThread(); | |
| 110 | |
| 111 void DispatchError(const std::string& error); | |
| 112 void DispatchEventToExtension(const std::string& event, | |
| 113 const std::string& json_args); | |
| 114 void ExtensionUnloaded(const std::string& extension_id); | |
| 115 | |
| 116 virtual ~ExtensionSpeechInputManager(); | |
| 51 | 117 |
| 52 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; | 118 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; |
| 53 class Factory; | 119 class Factory; |
| 54 | 120 |
| 55 virtual ~ExtensionSpeechInputManager(); | 121 // Since only one extension can be recording on the same time a constant |
| 122 // value is enough as id. | |
| 123 static const int kSpeechCallerId = 1; | |
|
Satish
2011/10/11 13:37:16
suggest moving this to the anonymous namespace in
Leandro Graciá Gil
2011/10/11 18:08:00
Done.
| |
| 56 | 124 |
| 125 // Lock used to allow exclusive access to the state variable and methods that | |
| 126 // either read or write on it. This is required since the speech code | |
| 127 // operates in the IO thread while the extension code uses the UI thread. | |
| 128 base::Lock state_lock_; | |
| 129 | |
| 130 // Used in the UI thread but also its raw value as notification | |
| 131 // source in the IO thread, guarded by the state lock and value. | |
| 57 Profile* profile_; | 132 Profile* profile_; |
| 58 scoped_refptr<Extension> extension_; | 133 |
| 134 // Used in both threads, guarded by the state lock. | |
| 135 State state_; | |
| 136 std::string extension_id_in_use_; | |
| 137 | |
| 138 // Used in the UI thread. | |
| 59 NotificationRegistrar registrar_; | 139 NotificationRegistrar registrar_; |
| 140 | |
| 141 // Used in the IO thread. Checked on the UI thread under the state lock. | |
| 142 scoped_refptr<speech_input::SpeechRecognizer> recognizer_; | |
| 60 }; | 143 }; |
| 61 | 144 |
| 62 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ | 145 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ |
| OLD | NEW |