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