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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/extension_speech_input_manager.h
diff --git a/chrome/browser/extensions/extension_speech_input_manager.h b/chrome/browser/extensions/extension_speech_input_manager.h
index 4d8ef00c6bd898c2881e8148a01f5bc8b0124103..e01345549fe348cdb0747c92f2b21c62cfe136a7 100644
--- a/chrome/browser/extensions/extension_speech_input_manager.h
+++ b/chrome/browser/extensions/extension_speech_input_manager.h
@@ -7,9 +7,11 @@
#pragma once
#include "base/memory/ref_counted.h"
-#include "base/memory/singleton.h"
+#include "base/synchronization/lock.h"
+#include "content/browser/speech/speech_recognizer.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
+#include "content/common/speech_input_result.h"
#include <string>
class Extension;
@@ -17,11 +19,53 @@ class NotificationDetails;
class NotificationSource;
class Profile;
+namespace net {
+class URLRequestContextGetter;
+}
+
+// Used for API tests. All methods run on the IO thread.
+class ExtensionSpeechInterface {
+ public:
+ virtual bool CheckForAvailableDevices() = 0;
+ virtual void StartRecording(
+ speech_input::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 HasValidRecognizer() const = 0;
+
+ protected:
+ scoped_refptr<speech_input::SpeechRecognizer> recognizer_;
+};
+
// Manages the speech input requests and responses from the extensions
// associated to the given profile.
class ExtensionSpeechInputManager : public NotificationObserver,
- public base::RefCountedThreadSafe<ExtensionSpeechInputManager> {
+ public base::RefCountedThreadSafe<ExtensionSpeechInputManager>,
+ public speech_input::SpeechRecognizerDelegate,
+ private ExtensionSpeechInterface {
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) {}
+ };
+
// Should not be used directly. Managed by a ProfileKeyedServiceFactory.
explicit ExtensionSpeechInputManager(Profile* profile);
@@ -32,31 +76,104 @@ class ExtensionSpeechInputManager : public NotificationObserver,
// Initialize the ProfileKeyedServiceFactory.
static void InitializeFactory();
- // Methods from NotificationObserver.
- virtual void Observe(int type,
- const NotificationSource& source,
- const NotificationDetails& details);
-
// Request to start speech recognition for the provided extension.
- void Start(const Extension *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.
- void Stop(const Extension *extension);
+ bool Stop(const std::string& extension_id, std::string* error);
+
+ // Retrieve the actual state of the API manager.
+ State state() const { return state_; }
// Called by internal ProfileKeyedService class.
void ShutdownOnUIThread();
+ // Methods from NotificationObserver.
+ virtual void Observe(int type,
+ const NotificationSource& source,
+ const NotificationDetails& details) OVERRIDE;
+
+ // Methods from SpeechRecognizerDelegate.
+ virtual void SetRecognitionResult(
+ int caller_id,
+ const speech_input::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,
+ speech_input::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 SetExtensionSpeechInterface(ExtensionSpeechInterface* interface);
+ ExtensionSpeechInterface* GetExtensionSpeechInterface();
+
private:
- void ExtensionUnloaded(const std::string& id);
+ // ExtensionSpeechInterface methods:
+ virtual bool CheckForAvailableDevices() OVERRIDE;
+ virtual bool HasValidRecognizer() const OVERRIDE;
+
+ virtual void StartRecording(
+ speech_input::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 SetRecognitionResultOnUIThread(
+ const speech_input::SpeechInputResult& result,
+ const std::string& extension_id);
+ void DidStartReceivingAudioOnUIThread();
+ void StopSucceededOnUIThread();
+
+ 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);
+
+ virtual ~ExtensionSpeechInputManager();
friend class base::RefCountedThreadSafe<ExtensionSpeechInputManager>;
class Factory;
- virtual ~ExtensionSpeechInputManager();
+ // 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_;
- scoped_refptr<Extension> extension_;
+
+ // Used in both threads, guarded by the state lock.
+ State state_;
+ std::string extension_id_in_use_;
+
+ // Used in the UI thread.
NotificationRegistrar registrar_;
+ ExtensionSpeechInterface* speech_interface_;
};
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SPEECH_INPUT_MANAGER_H_

Powered by Google App Engine
This is Rietveld 408576698