1// Copyright (c) 2012 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 CONTENT_PUBLIC_BROWSER_SPEECH_RECOGNITION_MANAGER_H_
6#define CONTENT_PUBLIC_BROWSER_SPEECH_RECOGNITION_MANAGER_H_
7
8#include "base/callback.h"
9#include "base/strings/string16.h"
10#include "content/common/content_export.h"
11#include "content/public/common/speech_recognition_result.h"
12
13namespace content {
14
15class SpeechRecognitionEventListener;
16struct SpeechRecognitionSessionConfig;
17struct SpeechRecognitionSessionContext;
18
19// The SpeechRecognitionManager (SRM) is a singleton class that handles SR
20// functionalities within Chrome. Everyone that needs to perform SR should
21// interface exclusively with the SRM, receiving events through the callback
22// interface SpeechRecognitionEventListener.
23// Since many different sources can use SR in different times (some overlapping
24// is allowed while waiting for results), the SRM has the further responsibility
25// of handling separately and reliably (taking into account also call sequences
26// that might not make sense, e.g., two subsequent AbortSession calls).
27// In this sense a session, within the SRM, models the ongoing evolution of a
28// SR request from the viewpoint of the end-user, abstracting all the concrete
29// operations that must be carried out, that will be handled by inner classes.
30class SpeechRecognitionManager {
31 public:
32  enum { kSessionIDInvalid = 0 };
33
34  // Returns the singleton instance.
35  static CONTENT_EXPORT SpeechRecognitionManager* GetInstance();
36
37  // Singleton manager setter useful for tests.
38  static void CONTENT_EXPORT SetManagerForTests(
39      SpeechRecognitionManager* manager);
40
41  // Creates a new recognition session.
42  virtual int CreateSession(const SpeechRecognitionSessionConfig& config) = 0;
43
44  // Starts/restarts recognition for an existing session, after performing a
45  // premilinary check on the delegate (CheckRecognitionIsAllowed).
46  virtual void StartSession(int session_id) = 0;
47
48  // Aborts recognition for an existing session, without providing any result.
49  virtual void AbortSession(int session_id) = 0;
50
51  // Aborts all sessions for a given listener, without providing any result.
52  virtual void AbortAllSessionsForListener(
53      SpeechRecognitionEventListener* listener) = 0;
54
55  // Aborts all sessions for a given RenderView, without providing any result.
56  virtual void AbortAllSessionsForRenderView(int render_process_id,
57                                             int render_view_id) = 0;
58
59  // Stops audio capture for an existing session. The audio captured before the
60  // call will be processed, possibly ending up with a result.
61  virtual void StopAudioCaptureForSession(int session_id) = 0;
62
63  // Retrieves the configuration of a session, as provided by the caller
64  // upon CreateSession.
65  virtual const SpeechRecognitionSessionConfig& GetSessionConfig(int session_id)
66      const = 0;
67
68  // Retrieves the context associated to a session.
69  virtual SpeechRecognitionSessionContext GetSessionContext(
70      int session_id) const = 0;
71
72  // Looks-up an existing session from the context tuple
73  // {render_view_id, render_view_id, request_id}.
74  virtual int GetSession(int render_process_id,
75                         int render_view_id,
76                         int request_id) const = 0;
77
78  // Returns true if the OS reports existence of audio recording devices.
79  virtual bool HasAudioInputDevices() = 0;
80
81  // Returns a human readable string for the model/make of the active audio
82  // input device for this computer.
83  virtual string16 GetAudioInputDeviceModel() = 0;
84
85  // Invokes the platform provided microphone settings UI in a non-blocking way,
86  // via the BrowserThread::FILE thread.
87  virtual void ShowAudioInputSettings() = 0;
88
89 protected:
90  virtual ~SpeechRecognitionManager() {}
91
92 private:
93  static SpeechRecognitionManager* manager_for_tests_;
94};
95
96}  // namespace content
97
98#endif  // CONTENT_PUBLIC_BROWSER_SPEECH_RECOGNITION_MANAGER_H_
99