summaryrefslogtreecommitdiffstats
path: root/chrome/browser/speech/speech_recognition_bubble_controller.h
blob: e5b18a5aded734c062aef46d30dfeddedd2986ab (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
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
// 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_RECOGNITION_BUBBLE_CONTROLLER_H_
#define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_
#pragma once

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/speech/speech_recognition_bubble.h"
#include "ui/gfx/rect.h"

namespace speech {

// This class handles the speech recognition popup UI on behalf of
// SpeechRecognitionManager, which invokes methods on the IO thread, processing
// those requests on the UI thread. At most one bubble can be active.
class SpeechRecognitionBubbleController
    : public base::RefCountedThreadSafe<SpeechRecognitionBubbleController>,
      public SpeechRecognitionBubbleDelegate {
 public:
  // All methods of this delegate are called on the IO thread.
  class Delegate {
   public:
    // Invoked when the user clicks on a button in the speech recognition UI.
    virtual void InfoBubbleButtonClicked(
        int session_id, SpeechRecognitionBubble::Button button) = 0;

    // Invoked when the user clicks outside the speech recognition info bubble
    // causing it to close and input focus to change.
    virtual void InfoBubbleFocusChanged(int session_id) = 0;

   protected:
    virtual ~Delegate() {}
  };

  explicit SpeechRecognitionBubbleController(Delegate* delegate);

  // Creates and shows a new speech recognition UI bubble in warmup mode.
  void CreateBubble(int session_id,
                    int render_process_id,
                    int render_view_id,
                    const gfx::Rect& element_rect);

  // Indicates to the user that audio recording is in progress.
  void SetBubbleRecordingMode();

  // Indicates to the user that recognition is in progress.
  void SetBubbleRecognizingMode();

  // Displays the given string with the 'Try again' and 'Cancel' buttons.
  void SetBubbleMessage(const string16& text);

  // Checks whether the bubble is active and is showing a message.
  bool IsShowingMessage() const;

  // Updates the current captured audio volume displayed on screen.
  void SetBubbleInputVolume(float volume, float noise_volume);

  void CloseBubble();

  // Retrieves the session ID associated to the active bubble (if any).
  // Returns 0 if no bubble is currently shown.
  int GetActiveSessionID() const;

  // Checks whether a bubble is being shown on the RenderView (tab/extension)
  // identified by the tuple {|render_process_id|,|render_view_id|}
  bool IsShowingBubbleForRenderView(int render_process_id, int render_view_id);

  // SpeechRecognitionBubble::Delegate methods.
  virtual void InfoBubbleButtonClicked(
      SpeechRecognitionBubble::Button button) OVERRIDE;
  virtual void InfoBubbleFocusChanged() OVERRIDE;

 private:
  friend class base::RefCountedThreadSafe<SpeechRecognitionBubbleController>;

  // The various calls received by this object and handled on the UI thread.
  enum RequestType {
    REQUEST_CREATE,
    REQUEST_SET_RECORDING_MODE,
    REQUEST_SET_RECOGNIZING_MODE,
    REQUEST_SET_MESSAGE,
    REQUEST_SET_INPUT_VOLUME,
    REQUEST_CLOSE,
  };

  struct UIRequest {
    RequestType type;
    string16 message;
    gfx::Rect element_rect;
    float volume;
    float noise_volume;
    int render_process_id;
    int render_view_id;

    explicit UIRequest(RequestType type_value);
    ~UIRequest();
  };

  virtual ~SpeechRecognitionBubbleController();

  void InvokeDelegateButtonClicked(SpeechRecognitionBubble::Button button);
  void InvokeDelegateFocusChanged();
  void ProcessRequestInUiThread(const UIRequest& request);

  // *** The following are accessed only on the IO thread.
  Delegate* delegate_;

  // The session id for currently visible bubble.
  int current_bubble_session_id_;

  // The render process and view ids for the currently visible bubble.
  int current_bubble_render_process_id_;
  int current_bubble_render_view_id_;

  RequestType last_request_issued_;

  // *** The following are accessed only on the UI thread.
  scoped_ptr<SpeechRecognitionBubble> bubble_;
};

// 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 SpeechRecognitionBubbleController::Delegate
    SpeechRecognitionBubbleControllerDelegate;

}  // namespace speech

#endif  // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_