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

Side by Side Diff: chrome/browser/extensions/speech_input/extension_speech_input_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: creating the notification object dynamically on first use. 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
(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/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 ExtensionSpeechInterface {
26 public:
27 ExtensionSpeechInterface();
28 virtual ~ExtensionSpeechInterface();
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 ExtensionSpeechInputManager
55 : public base::RefCountedThreadSafe<ExtensionSpeechInputManager>,
56 public speech_input::SpeechRecognizerDelegate,
57 public content::NotificationObserver,
58 private ExtensionSpeechInterface {
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 ExtensionSpeechInputManager(Profile* profile);
79
80 // Returns the corresponding manager for the given profile, creating
81 // a new one if required.
82 static ExtensionSpeechInputManager* 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 SetExtensionSpeechInterface(ExtensionSpeechInterface* interface);
130 ExtensionSpeechInterface* GetExtensionSpeechInterface();
131
132 private:
133 // ExtensionSpeechInterface methods:
134 virtual bool IsRecordingInProcess() OVERRIDE;
135 virtual bool HasAudioInputDevices() OVERRIDE;
136 virtual bool HasValidRecognizer() OVERRIDE;
137
138 virtual void StartRecording(
139 speech_input::SpeechRecognizerDelegate* delegate,
140 net::URLRequestContextGetter* context_getter,
141 int caller_id,
142 const std::string& language,
143 const std::string& grammar,
144 bool filter_profanities) OVERRIDE;
145
146 virtual void StopRecording(bool recognition_failed) OVERRIDE;
147
148 // Internal methods.
149 void StartOnIOThread(
150 net::URLRequestContextGetter* context_getter,
151 const std::string& language,
152 const std::string& grammar,
153 bool filter_profanities);
154 void ForceStopOnIOThread();
155
156 void SetRecognitionResultOnUIThread(
157 const speech_input::SpeechInputResult& result,
158 const std::string& extension_id);
159 void DidStartReceivingAudioOnUIThread();
160 void StopSucceededOnUIThread();
161
162 void DispatchError(const std::string& error, bool dispatch_event);
163 void DispatchEventToExtension(const std::string& extension_id,
164 const std::string& event,
165 const std::string& json_args);
166 void ExtensionUnloaded(const std::string& extension_id);
167
168 void ResetToIdleState();
169
170 virtual ~ExtensionSpeechInputManager();
171
172 friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>;
173 class Factory;
174
175 // 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 // operates in the IO thread while the extension code uses the UI thread.
178 base::Lock state_lock_;
179
180 // 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 Profile* profile_;
183
184 // Used in both threads, guarded by the state lock.
185 State state_;
186 std::string extension_id_in_use_;
187
188 // Used in the UI thread.
189 content::NotificationRegistrar registrar_;
190 ExtensionSpeechInterface* speech_interface_;
191 };
192
193 #endif // CHROME_BROWSER_EXTENSIONS_SPEECH_INPUT_EXTENSION_SPEECH_INPUT_MANAGER _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698