OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_ | |
6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_ | |
7 | |
8 #include <vector> | |
9 | |
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" | |
15 | |
16 class SkBitmap; | |
17 class SkCanvas; | |
18 | |
19 namespace content { | |
20 class WebContents; | |
21 } | |
22 | |
23 namespace gfx { | |
24 class Canvas; | |
25 class Rect; | |
26 } | |
27 | |
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 { | |
33 public: | |
34 // The various buttons which may be part of the bubble. | |
35 enum Button { | |
36 BUTTON_TRY_AGAIN, | |
37 BUTTON_CANCEL | |
38 }; | |
39 | |
40 // Informs listeners of user actions in the bubble. | |
41 class Delegate { | |
42 public: | |
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; | |
46 | |
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; | |
52 | |
53 protected: | |
54 virtual ~Delegate() { | |
55 } | |
56 }; | |
57 | |
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, | |
66 int render_view_id, | |
67 Delegate* delegate, | |
68 const gfx::Rect& element_rect); | |
69 | |
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, | |
74 int render_view_id, | |
75 Delegate* delegate, | |
76 const gfx::Rect& element_rect); | |
77 | |
78 // |Create| uses the currently registered FactoryMethod to create the | |
79 // SpeechRecognitionBubble instances. FactoryMethod is intended for testing. | |
80 typedef SpeechRecognitionBubble* (*FactoryMethod)(content::WebContents*, | |
81 Delegate*, | |
82 const gfx::Rect&); | |
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; } | |
88 #endif | |
89 | |
90 virtual ~SpeechRecognitionBubble() {} | |
91 | |
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; | |
95 | |
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; | |
99 | |
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; | |
103 | |
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; | |
107 | |
108 // Brings up the bubble on screen. | |
109 virtual void Show() = 0; | |
110 | |
111 // Hides the info bubble, resulting in a call to | |
112 // |Delegate::InfoBubbleFocusChanged| as well. | |
113 virtual void Hide() = 0; | |
114 | |
115 // Updates and draws the current captured audio volume displayed on screen. | |
116 virtual void SetInputVolume(float volume, float noise_volume) = 0; | |
117 | |
118 // Returns the WebContents for which this bubble gets displayed. | |
119 virtual content::WebContents* GetWebContents() = 0; | |
120 | |
121 // The horizontal distance between the start of the html widget and the speech | |
122 // bubble's arrow. | |
123 static const int kBubbleTargetOffsetX; | |
124 | |
125 private: | |
126 static FactoryMethod factory_; | |
127 }; | |
128 | |
129 // Base class for the platform specific bubble implementations, this contains | |
130 // the platform independent code for SpeechRecognitionBubble. | |
131 class SpeechRecognitionBubbleBase : public SpeechRecognitionBubble { | |
132 public: | |
133 // The current display mode of the bubble, useful only for the platform | |
134 // specific implementation. | |
135 enum DisplayMode { | |
136 DISPLAY_MODE_WARM_UP, | |
137 DISPLAY_MODE_RECORDING, | |
138 DISPLAY_MODE_RECOGNIZING, | |
139 DISPLAY_MODE_MESSAGE | |
140 }; | |
141 | |
142 SpeechRecognitionBubbleBase(int render_process_id, int render_view_id); | |
143 virtual ~SpeechRecognitionBubbleBase(); | |
144 | |
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; | |
152 | |
153 protected: | |
154 // Updates the platform specific UI layout for the current display mode. | |
155 virtual void UpdateLayout() = 0; | |
156 | |
157 // Overridden by subclasses to copy |icon_image()| to the screen. | |
158 virtual void UpdateImage() = 0; | |
159 | |
160 DisplayMode display_mode() const { return display_mode_; } | |
161 | |
162 const base::string16& message_text() const { return message_text_; } | |
163 | |
164 gfx::ImageSkia icon_image(); | |
165 | |
166 private: | |
167 void DoRecognizingAnimationStep(); | |
168 void DoWarmingUpAnimationStep(); | |
169 void SetImage(const gfx::ImageSkia& image); | |
170 | |
171 void DrawVolumeOverlay(SkCanvas* canvas, | |
172 const gfx::ImageSkia& image_skia, | |
173 float volume); | |
174 | |
175 // Task factory used for animation timer. | |
176 base::WeakPtrFactory<SpeechRecognitionBubbleBase> weak_factory_; | |
177 int animation_step_; // Current index/step of the animation. | |
178 | |
179 DisplayMode display_mode_; | |
180 base::string16 message_text_; // Text displayed in DISPLAY_MODE_MESSAGE | |
181 | |
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_; | |
186 | |
187 // Content in which this bubble gets displayed. | |
188 int render_process_id_; | |
189 int render_view_id_; | |
190 | |
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. | |
194 float scale_; | |
195 | |
196 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleBase); | |
197 }; | |
198 | |
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; | |
204 | |
205 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_ | |
OLD | NEW |