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 "base/synchronization/lock.h"
11 #include "chrome/browser/speech/speech_recognition_bubble.h"
12 #include "ui/gfx/rect.h"
16 // This class handles the speech recognition popup UI on behalf of
17 // SpeechRecognitionManager, which invokes methods on the IO thread, processing
18 // those requests on the UI thread. At most one bubble can be active.
19 class SpeechRecognitionBubbleController
20 : public base::RefCountedThreadSafe
<SpeechRecognitionBubbleController
>,
21 public SpeechRecognitionBubbleDelegate
{
23 // All methods of this delegate are called on the IO thread.
26 // Invoked when the user clicks on a button in the speech recognition UI.
27 virtual void InfoBubbleButtonClicked(
28 int session_id
, SpeechRecognitionBubble::Button button
) = 0;
30 // Invoked when the user clicks outside the speech recognition info bubble
31 // causing it to close and input focus to change.
32 virtual void InfoBubbleFocusChanged(int session_id
) = 0;
35 virtual ~Delegate() {}
38 explicit SpeechRecognitionBubbleController(Delegate
* delegate
);
40 // Creates and shows a new speech recognition UI bubble in warmup mode.
41 void CreateBubble(int session_id
,
42 int render_process_id
,
44 const gfx::Rect
& element_rect
);
46 // Indicates to the user that audio recording is in progress.
47 void SetBubbleRecordingMode();
49 // Indicates to the user that recognition is in progress.
50 void SetBubbleRecognizingMode();
52 // Displays the given string with the 'Try again' and 'Cancel' buttons.
53 void SetBubbleMessage(const base::string16
& text
);
55 // Checks whether the bubble is active and is showing a message.
56 bool IsShowingMessage() const;
58 // Updates the current captured audio volume displayed on screen.
59 void SetBubbleInputVolume(float volume
, float noise_volume
);
64 // This is the only method that can be called from the UI thread.
65 void CloseBubbleForRenderViewOnUIThread(int render_process_id
,
68 // Retrieves the session ID associated to the active bubble (if any).
69 // Returns 0 if no bubble is currently shown.
70 int GetActiveSessionID();
72 // SpeechRecognitionBubble::Delegate methods.
73 virtual void InfoBubbleButtonClicked(
74 SpeechRecognitionBubble::Button button
) OVERRIDE
;
75 virtual void InfoBubbleFocusChanged() OVERRIDE
;
78 friend class base::RefCountedThreadSafe
<SpeechRecognitionBubbleController
>;
80 // The various calls received by this object and handled on the UI thread.
83 REQUEST_SET_RECORDING_MODE
,
84 REQUEST_SET_RECOGNIZING_MODE
,
86 REQUEST_SET_INPUT_VOLUME
,
92 base::string16 message
;
93 gfx::Rect element_rect
;
96 int render_process_id
;
99 explicit UIRequest(RequestType type_value
);
103 virtual ~SpeechRecognitionBubbleController();
105 void InvokeDelegateButtonClicked(SpeechRecognitionBubble::Button button
);
106 void InvokeDelegateFocusChanged();
107 void ProcessRequestInUiThread(const UIRequest
& request
);
109 // The following are accessed only on the IO thread.
111 RequestType last_request_issued_
;
113 // The following are accessed only on the UI thread.
114 scoped_ptr
<SpeechRecognitionBubble
> bubble_
;
116 // The following are accessed on both IO and UI threads.
119 // The session id for currently visible bubble.
120 int current_bubble_session_id_
;
122 // The render process and view ids for the currently visible bubble.
123 int current_bubble_render_process_id_
;
124 int current_bubble_render_view_id_
;
127 // This typedef is to workaround the issue with certain versions of
128 // Visual Studio where it gets confused between multiple Delegate
129 // classes and gives a C2500 error. (I saw this error on the try bots -
130 // the workaround was not needed for my machine).
131 typedef SpeechRecognitionBubbleController::Delegate
132 SpeechRecognitionBubbleControllerDelegate
;
134 } // namespace speech
136 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_