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 | |
| 25 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. | 41 // Should not be used directly. Managed by a ProfileKeyedServiceFactory. |
| 26 explicit ExtensionSpeechInputManager(Profile* profile); | 42 ExtensionSpeechInputManager(Profile* profile, int speech_caller_id); |
| 27 | 43 |
| 28 // Returns the corresponding manager for the given profile, creating | 44 // Returns the corresponding manager for the given profile, creating |
| 29 // a new one if required. | 45 // a new one if required. |
| 30 static ExtensionSpeechInputManager* GetForProfile(Profile* profile); | 46 static ExtensionSpeechInputManager* GetForProfile(Profile* profile); |
| 31 | 47 |
| 32 // Initialize the ProfileKeyedServiceFactory. | 48 // Initialize the ProfileKeyedServiceFactory. |
| 33 static void InitializeFactory(); | 49 static void InitializeFactory(); |
| 34 | 50 |
| 51 // Request to start speech recognition for the provided extension. | |
| 52 void Start(const std::string& extension_id, | |
| 53 const std::string& language, | |
| 54 const std::string& grammar, | |
| 55 bool filter_profanities); | |
| 56 | |
| 57 // Request to stop an ongoing speech recognition. | |
| 58 void Stop(const std::string& extension_id); | |
| 59 | |
| 60 // Retrieve the actual state of the API manager. | |
| 61 State state() const { return state_; } | |
| 62 | |
| 63 // Called by internal ProfileKeyedService class. | |
| 64 void ShutdownOnUIThread(); | |
| 65 | |
| 35 // Methods from NotificationObserver. | 66 // Methods from NotificationObserver. |
| 36 virtual void Observe(int type, | 67 virtual void Observe(int type, |
| 37 const NotificationSource& source, | 68 const NotificationSource& source, |
| 38 const NotificationDetails& details); | 69 const NotificationDetails& details) OVERRIDE; |
| 39 | 70 |
| 40 // Request to start speech recognition for the provided extension. | 71 // Methods from SpeechRecognizerDelegate. |
| 41 void Start(const Extension *extension); | 72 virtual void SetRecognitionResult( |
| 73 int caller_id, | |
| 74 const speech_input::SpeechInputResult& result) OVERRIDE; | |
| 42 | 75 |
| 43 // Request to stop an ongoing speech recognition. | 76 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE; |
| 44 void Stop(const Extension *extension); | 77 virtual void DidCompleteRecording(int caller_id) OVERRIDE; |
| 45 | 78 virtual void DidCompleteRecognition(int caller_id) OVERRIDE; |
| 46 // Called by internal ProfileKeyedService class. | 79 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE; |
| 47 void ShutdownOnUIThread(); | 80 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE; |
| 81 virtual void OnRecognizerError(int caller_id, | |
| 82 speech_input::SpeechInputError error) | |
| 83 OVERRIDE; | |
| 84 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE; | |
| 85 virtual void SetInputVolume(int caller_id, float volume, | |
| 86 float noise_volume) OVERRIDE {} | |
| 48 | 87 |
| 49 private: | 88 private: |
| 89 void StartOnIOThread( | |
| 90 net::URLRequestContextGetter* context_getter, | |
| 91 const std::string& language, | |
| 92 const std::string& grammar, | |
| 93 bool filter_profanities); | |
| 94 | |
| 95 void ForceStopOnIOThread(); | |
| 96 | |
| 97 void SetRecognitionResultOnUIThread( | |
| 98 const speech_input::SpeechInputResult& result); | |
| 99 | |
| 100 void DidStartReceivingAudioOnUIThread(); | |
| 101 void DispatchError(const std::string& code); | |
| 102 | |
| 103 void DispatchEventToExtension(const std::string& event, | |
| 104 const std::string& json_args); | |
| 50 void ExtensionUnloaded(const std::string& id); | 105 void ExtensionUnloaded(const std::string& id); |
| 51 | 106 |
| 107 bool ShuttingDown(); | |
| 108 | |
| 109 virtual ~ExtensionSpeechInputManager(); | |
| 110 | |
| 52 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; | 111 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; |
| 53 class Factory; | 112 class Factory; |
| 54 | 113 |
| 55 virtual ~ExtensionSpeechInputManager(); | 114 Profile* profile_; |
| 115 std::string extension_id_; | |
| 116 NotificationRegistrar registrar_; | |
| 56 | 117 |
| 57 Profile* profile_; | 118 int speech_caller_id_; |
| 58 scoped_refptr<Extension> extension_; | 119 base::Lock state_lock_; |
|
Satish
2011/10/09 20:01:47
since you have a lock only for accessing/modifying
Leandro GraciĆ” Gil
2011/10/10 18:59:38
Done.
| |
| 59 NotificationRegistrar registrar_; | 120 State state_; |
| 121 scoped_refptr<speech_input::SpeechRecognizer> recognizer_; | |
| 60 }; | 122 }; |
| 61 | 123 |
| 62 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ | 124 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_ |
| OLD | NEW |