Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/speech/speech_input_extension_manager.h

Issue 8386074: Add a tray notification UI for speech input recording in the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: requested file renames and api constants refactor. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
10 #include "chrome/browser/speech/speech_input_extension_notification_ui.h"
11 #include "content/browser/speech/speech_recognizer.h" 11 #include "content/browser/speech/speech_recognizer.h"
12 #include "content/common/speech_input_result.h" 12 #include "content/common/speech_input_result.h"
13 #include "content/public/browser/notification_observer.h" 13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h" 14 #include "content/public/browser/notification_registrar.h"
15 #include <string> 15 #include <string>
16 16
17 class Extension; 17 class Extension;
18 class Profile; 18 class Profile;
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 SpeechExtensionInterface {
Satish 2011/11/07 18:16:31 should be SpeechInputExtensionInterface ?
Leandro GraciĆ” Gil 2011/11/07 19:34:42 Done.
26 public: 26 public:
27 ExtensionSpeechInterface(); 27 SpeechExtensionInterface();
28 virtual ~ExtensionSpeechInterface(); 28 virtual ~SpeechExtensionInterface();
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 SpeechExtensionInterface {
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
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 SetSpeechExtensionInterface(SpeechExtensionInterface* interface);
130 ExtensionSpeechInterface* GetExtensionSpeechInterface(); 130 SpeechExtensionInterface* GetSpeechExtensionInterface();
131 131
132 private: 132 private:
133 // ExtensionSpeechInterface methods: 133 // SpeechExtensionInterface methods:
134 virtual bool IsRecordingInProcess() OVERRIDE; 134 virtual bool IsRecordingInProcess() OVERRIDE;
135 virtual bool HasAudioInputDevices() OVERRIDE; 135 virtual bool HasAudioInputDevices() OVERRIDE;
136 virtual bool HasValidRecognizer() OVERRIDE; 136 virtual bool HasValidRecognizer() OVERRIDE;
137 137
138 virtual void StartRecording( 138 virtual void StartRecording(
139 speech_input::SpeechRecognizerDelegate* delegate, 139 speech_input::SpeechRecognizerDelegate* delegate,
140 net::URLRequestContextGetter* context_getter, 140 net::URLRequestContextGetter* context_getter,
141 int caller_id, 141 int caller_id,
142 const std::string& language, 142 const std::string& language,
143 const std::string& grammar, 143 const std::string& grammar,
(...skipping 14 matching lines...) Expand all
158 const std::string& extension_id); 158 const std::string& extension_id);
159 void DidStartReceivingAudioOnUIThread(); 159 void DidStartReceivingAudioOnUIThread();
160 void StopSucceededOnUIThread(); 160 void StopSucceededOnUIThread();
161 161
162 void DispatchError(const std::string& error, bool dispatch_event); 162 void DispatchError(const std::string& error, bool dispatch_event);
163 void DispatchEventToExtension(const std::string& extension_id, 163 void DispatchEventToExtension(const std::string& extension_id,
164 const std::string& event, 164 const std::string& event,
165 const std::string& json_args); 165 const std::string& json_args);
166 void ExtensionUnloaded(const std::string& extension_id); 166 void ExtensionUnloaded(const std::string& extension_id);
167 167
168 void SetInputVolumeOnUIThread(float volume);
168 void ResetToIdleState(); 169 void ResetToIdleState();
169 170
170 virtual ~ExtensionSpeechInputManager(); 171 virtual ~SpeechInputExtensionManager();
171 172
172 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>; 173 friend class base::RefCountedThreadSafe<SpeechInputExtensionManager>;
173 class Factory; 174 class Factory;
174 175
175 // Lock used to allow exclusive access to the state variable and methods that 176 // 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 177 // 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. 178 // operates in the IO thread while the extension code uses the UI thread.
178 base::Lock state_lock_; 179 base::Lock state_lock_;
179 180
180 // Used in the UI thread but also its raw value as notification 181 // 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. 182 // source in the IO thread, guarded by the state lock and value.
182 Profile* profile_; 183 Profile* profile_;
183 184
184 // Used in both threads, guarded by the state lock. 185 // Used in both threads, guarded by the state lock.
185 State state_; 186 State state_;
186 std::string extension_id_in_use_; 187 std::string extension_id_in_use_;
187 188
188 // Used in the UI thread. 189 // Used in the UI thread.
189 content::NotificationRegistrar registrar_; 190 content::NotificationRegistrar registrar_;
190 ExtensionSpeechInterface* speech_interface_; 191 SpeechExtensionInterface* speech_interface_;
192 SpeechInputExtensionNotificationUI notification_;
191 }; 193 };
192 194
193 #endif // CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER _H_ 195 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_EXTENSION_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698