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

Side by Side Diff: chrome/browser/extensions/extension_speech_input_manager.h

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

Powered by Google App Engine
This is Rietveld 408576698