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_H_
6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string16.h"
13 #include "ui/base/layout.h"
14 #include "ui/gfx/image/image_skia.h"
28 // SpeechRecognitionBubble displays a popup info bubble during speech
29 // recognition, points to the html element which requested speech recognition
30 // and shows progress events. The popup is closed by the user clicking anywhere
31 // outside the popup window, or by the caller destroying this object.
32 class SpeechRecognitionBubble {
34 // The various buttons which may be part of the bubble.
40 // Informs listeners of user actions in the bubble.
43 // Invoked when the user selects a button in the info bubble. The InfoBubble
44 // is still active and the caller should close it if necessary.
45 virtual void InfoBubbleButtonClicked(Button button) = 0;
47 // Invoked when the user clicks outside the InfoBubble causing it to close.
48 // The InfoBubble window is no longer visible on screen and the caller can
49 // free the InfoBubble instance. This callback is not issued if the bubble
50 // got closed because the object was destroyed by the caller.
51 virtual void InfoBubbleFocusChanged() = 0;
58 // Factory method to create new instances.
59 // Creates the bubble, call |Show| to display it on screen.
60 // |render_process_id| and |render_view_id| is used to extract the
61 // correct WebContents.
62 // |element_rect| is the display bounds of the html element requesting speech
63 // recognition (in page coordinates).
64 static SpeechRecognitionBubble* Create(
65 int render_process_id,
68 const gfx::Rect& element_rect);
70 // This is implemented by platform specific code to create the underlying
71 // bubble window. Not to be called directly by users of this class.
72 static SpeechRecognitionBubble* CreateNativeBubble(
73 int render_process_id,
76 const gfx::Rect& element_rect);
78 // |Create| uses the currently registered FactoryMethod to create the
79 // SpeechRecognitionBubble instances. FactoryMethod is intended for testing.
80 typedef SpeechRecognitionBubble* (*FactoryMethod)(content::WebContents*,
83 // Sets the factory used by the static method Create. SpeechRecognitionBubble
84 // does not take ownership of |factory|. A value of NULL results in a
85 // SpeechRecognitionBubble being created directly.
86 #if defined(UNIT_TEST)
87 static void set_factory(FactoryMethod factory) { factory_ = factory; }
90 virtual ~SpeechRecognitionBubble() {}
92 // Indicates to the user that audio hardware is initializing. If the bubble is
93 // hidden, |Show| must be called to make it appear on screen.
94 virtual void SetWarmUpMode() = 0;
96 // Indicates to the user that audio recording is in progress. If the bubble is
97 // hidden, |Show| must be called to make it appear on screen.
98 virtual void SetRecordingMode() = 0;
100 // Indicates to the user that recognition is in progress. If the bubble is
101 // hidden, |Show| must be called to make it appear on screen.
102 virtual void SetRecognizingMode() = 0;
104 // Displays the given string with the 'Try again' and 'Cancel' buttons. If the
105 // bubble is hidden, |Show| must be called to make it appear on screen.
106 virtual void SetMessage(const base::string16& text) = 0;
108 // Brings up the bubble on screen.
109 virtual void Show() = 0;
111 // Hides the info bubble, resulting in a call to
112 // |Delegate::InfoBubbleFocusChanged| as well.
113 virtual void Hide() = 0;
115 // Updates and draws the current captured audio volume displayed on screen.
116 virtual void SetInputVolume(float volume, float noise_volume) = 0;
118 // Returns the WebContents for which this bubble gets displayed.
119 virtual content::WebContents* GetWebContents() = 0;
121 // The horizontal distance between the start of the html widget and the speech
123 static const int kBubbleTargetOffsetX;
126 static FactoryMethod factory_;
129 // Base class for the platform specific bubble implementations, this contains
130 // the platform independent code for SpeechRecognitionBubble.
131 class SpeechRecognitionBubbleBase : public SpeechRecognitionBubble {
133 // The current display mode of the bubble, useful only for the platform
134 // specific implementation.
136 DISPLAY_MODE_WARM_UP,
137 DISPLAY_MODE_RECORDING,
138 DISPLAY_MODE_RECOGNIZING,
142 SpeechRecognitionBubbleBase(int render_process_id, int render_view_id);
143 virtual ~SpeechRecognitionBubbleBase();
145 // SpeechRecognitionBubble methods
146 virtual void SetWarmUpMode() OVERRIDE;
147 virtual void SetRecordingMode() OVERRIDE;
148 virtual void SetRecognizingMode() OVERRIDE;
149 virtual void SetMessage(const base::string16& text) OVERRIDE;
150 virtual void SetInputVolume(float volume, float noise_volume) OVERRIDE;
151 virtual content::WebContents* GetWebContents() OVERRIDE;
154 // Updates the platform specific UI layout for the current display mode.
155 virtual void UpdateLayout() = 0;
157 // Overridden by subclasses to copy |icon_image()| to the screen.
158 virtual void UpdateImage() = 0;
160 DisplayMode display_mode() const { return display_mode_; }
162 const base::string16& message_text() const { return message_text_; }
164 gfx::ImageSkia icon_image();
167 void DoRecognizingAnimationStep();
168 void DoWarmingUpAnimationStep();
169 void SetImage(const gfx::ImageSkia& image);
171 void DrawVolumeOverlay(SkCanvas* canvas,
172 const gfx::ImageSkia& image_skia,
175 // Task factory used for animation timer.
176 base::WeakPtrFactory<SpeechRecognitionBubbleBase> weak_factory_;
177 int animation_step_; // Current index/step of the animation.
179 DisplayMode display_mode_;
180 base::string16 message_text_; // Text displayed in DISPLAY_MODE_MESSAGE
182 // The current microphone image with volume level indication.
183 scoped_ptr<SkBitmap> mic_image_;
184 // A temporary buffer image used in creating the above mic image.
185 scoped_ptr<SkBitmap> buffer_image_;
187 // Content in which this bubble gets displayed.
188 int render_process_id_;
191 // The current image displayed in the bubble's icon widget.
192 gfx::ImageSkia icon_image_;
193 // The scale factor used for the web-contents.
196 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleBase);
199 // This typedef is to workaround the issue with certain versions of
200 // Visual Studio where it gets confused between multiple Delegate
201 // classes and gives a C2500 error. (I saw this error on the try bots -
202 // the workaround was not needed for my machine).
203 typedef SpeechRecognitionBubble::Delegate SpeechRecognitionBubbleDelegate;
205 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_