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.
5 #ifndef CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_
6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "chrome/browser/speech/speech_recognition_bubble.h"
11 #include "ui/gfx/rect.h"
15 // This class handles the speech recognition popup UI on behalf of
16 // SpeechRecognitionManager, which invokes methods on the IO thread, processing
17 // those requests on the UI thread. At most one bubble can be active.
18 class SpeechRecognitionBubbleController
19 : public base::RefCountedThreadSafe
<SpeechRecognitionBubbleController
>,
20 public SpeechRecognitionBubbleDelegate
{
22 // All methods of this delegate are called on the IO thread.
25 // Invoked when the user clicks on a button in the speech recognition UI.
26 virtual void InfoBubbleButtonClicked(
27 int session_id
, SpeechRecognitionBubble::Button button
) = 0;
29 // Invoked when the user clicks outside the speech recognition info bubble
30 // causing it to close and input focus to change.
31 virtual void InfoBubbleFocusChanged(int session_id
) = 0;
34 virtual ~Delegate() {}
37 explicit SpeechRecognitionBubbleController(Delegate
* delegate
);
39 // Creates and shows a new speech recognition UI bubble in warmup mode.
40 void CreateBubble(int session_id
,
41 int render_process_id
,
43 const gfx::Rect
& element_rect
);
45 // Indicates to the user that audio recording is in progress.
46 void SetBubbleRecordingMode();
48 // Indicates to the user that recognition is in progress.
49 void SetBubbleRecognizingMode();
51 // Displays the given string with the 'Try again' and 'Cancel' buttons.
52 void SetBubbleMessage(const string16
& text
);
54 // Checks whether the bubble is active and is showing a message.
55 bool IsShowingMessage() const;
57 // Updates the current captured audio volume displayed on screen.
58 void SetBubbleInputVolume(float volume
, float noise_volume
);
62 // Retrieves the session ID associated to the active bubble (if any).
63 // Returns 0 if no bubble is currently shown.
64 int GetActiveSessionID() const;
66 // Checks whether a bubble is being shown on the RenderView (tab/extension)
67 // identified by the tuple {|render_process_id|,|render_view_id|}
68 bool IsShowingBubbleForRenderView(int render_process_id
, int render_view_id
);
70 // SpeechRecognitionBubble::Delegate methods.
71 virtual void InfoBubbleButtonClicked(
72 SpeechRecognitionBubble::Button button
) OVERRIDE
;
73 virtual void InfoBubbleFocusChanged() OVERRIDE
;
76 friend class base::RefCountedThreadSafe
<SpeechRecognitionBubbleController
>;
78 // The various calls received by this object and handled on the UI thread.
81 REQUEST_SET_RECORDING_MODE
,
82 REQUEST_SET_RECOGNIZING_MODE
,
84 REQUEST_SET_INPUT_VOLUME
,
91 gfx::Rect element_rect
;
94 int render_process_id
;
97 explicit UIRequest(RequestType type_value
);
101 virtual ~SpeechRecognitionBubbleController();
103 void InvokeDelegateButtonClicked(SpeechRecognitionBubble::Button button
);
104 void InvokeDelegateFocusChanged();
105 void ProcessRequestInUiThread(const UIRequest
& request
);
107 // *** The following are accessed only on the IO thread.
110 // The session id for currently visible bubble.
111 int current_bubble_session_id_
;
113 // The render process and view ids for the currently visible bubble.
114 int current_bubble_render_process_id_
;
115 int current_bubble_render_view_id_
;
117 RequestType last_request_issued_
;
119 // *** The following are accessed only on the UI thread.
120 scoped_ptr
<SpeechRecognitionBubble
> bubble_
;
123 // This typedef is to workaround the issue with certain versions of
124 // Visual Studio where it gets confused between multiple Delegate
125 // classes and gives a C2500 error. (I saw this error on the try bots -
126 // the workaround was not needed for my machine).
127 typedef SpeechRecognitionBubbleController::Delegate
128 SpeechRecognitionBubbleControllerDelegate
;
130 } // namespace speech
132 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_