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