summaryrefslogtreecommitdiffstats
path: root/chrome/browser/speech/speech_input_manager.h
blob: b16b76aa1c019428036244a1e96125a9fb401d47 (plain)
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
// Copyright (c) 2010 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_MANAGER_H_
#define CHROME_BROWSER_SPEECH_SPEECH_INPUT_MANAGER_H_

#include "base/basictypes.h"
#include "chrome/common/speech_input_result.h"
#include "gfx/rect.h"
#include "ipc/ipc_message.h"

namespace speech_input {

// This is the gatekeeper for speech recognition in the browser process. It
// handles requests received from various render views and makes sure only one
// of them can use speech recognition at a time. It also sends recognition
// results and status events to the render views when required.
// This class is a singleton and accessed via the Get method.
class SpeechInputManager {
 public:
  // Implemented by the dispatcher host to relay events to the render views.
  class Delegate {
   public:
    virtual void SetRecognitionResult(
        int caller_id,
        const SpeechInputResultArray& result) = 0;
    virtual void DidCompleteRecording(int caller_id) = 0;
    virtual void DidCompleteRecognition(int caller_id) = 0;

   protected:
    virtual ~Delegate() {}
  };

  // Whether the speech input feature is enabled, based on the browser channel
  // information and command line flags.
  static bool IsFeatureEnabled();

  // Factory method to access the singleton. We have this method here instead of
  // using Singleton<> directly in the calling code to aid tests in injection
  // mocks.
  static SpeechInputManager* Get();
  // Factory method definition useful for tests.
  typedef SpeechInputManager* (AccessorMethod)();

  virtual ~SpeechInputManager() {}

  // Handlers for requests from render views.

  // |delegate| is a weak pointer and should remain valid until
  // its |DidCompleteRecognition| method is called or recognition is cancelled.
  // |render_process_id| is the ID of the renderer process initiating the
  // request.
  // |element_rect| is the display bounds of the html element requesting speech
  // input (in page coordinates).
  virtual void StartRecognition(Delegate* delegate,
                                int caller_id,
                                int render_process_id,
                                int render_view_id,
                                const gfx::Rect& element_rect,
                                const std::string& language,
                                const std::string& grammar)  = 0;
  virtual void CancelRecognition(int caller_id) = 0;
  virtual void StopRecording(int caller_id) = 0;
};

// This typedef is to workaround the issue with certain versions of
// Visual Studio where it gets confused between multiple Delegate
// classes and gives a C2500 error. (I saw this error on the try bots -
// the workaround was not needed for my machine).
typedef SpeechInputManager::Delegate SpeechInputManagerDelegate;

}  // namespace speech_input

#endif  // CHROME_BROWSER_SPEECH_SPEECH_INPUT_MANAGER_H_