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_INPUT_BUBBLE_H_ | |
6 #define CHROME_BROWSER_SPEECH_SPEECH_INPUT_BUBBLE_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/string16.h" | |
14 | |
15 class SkBitmap; | |
16 class SkCanvas; | |
17 | |
18 namespace content { | |
19 class WebContents; | |
20 } | |
21 | |
22 namespace gfx { | |
23 class Canvas; | |
24 class Rect; | |
25 } | |
26 | |
27 // SpeechInputBubble displays a popup info bubble during speech recognition, | |
28 // points to the html element which requested speech input and shows recognition | |
29 // progress events. The popup is closed by the user clicking anywhere outside | |
30 // the popup window, or by the caller destroying this object. | |
31 class SpeechInputBubble { | |
32 public: | |
33 // The various buttons which may be part of the bubble. | |
34 enum Button { | |
35 BUTTON_TRY_AGAIN, | |
36 BUTTON_CANCEL | |
37 }; | |
38 | |
39 // Informs listeners of user actions in the bubble. | |
40 class Delegate { | |
41 public: | |
42 // Invoked when the user selects a button in the info bubble. The InfoBubble | |
43 // is still active and the caller should close it if necessary. | |
44 virtual void InfoBubbleButtonClicked(Button button) = 0; | |
45 | |
46 // Invoked when the user clicks outside the InfoBubble causing it to close. | |
47 // The InfoBubble window is no longer visible on screen and the caller can | |
48 // free the InfoBubble instance. This callback is not issued if the bubble | |
49 // got closed because the object was destroyed by the caller. | |
50 virtual void InfoBubbleFocusChanged() = 0; | |
51 | |
52 protected: | |
53 virtual ~Delegate() { | |
54 } | |
55 }; | |
56 | |
57 // Factory method to create new instances. | |
58 // Creates the bubble, call |Show| to display it on screen. | |
59 // |web_contents| is the WebContents hosting the page. | |
60 // |element_rect| is the display bounds of the html element requesting speech | |
61 // input (in page coordinates). | |
62 static SpeechInputBubble* Create(content::WebContents* web_contents, | |
63 Delegate* delegate, | |
64 const gfx::Rect& element_rect); | |
65 | |
66 // This is implemented by platform specific code to create the underlying | |
67 // bubble window. Not to be called directly by users of this class. | |
68 static SpeechInputBubble* CreateNativeBubble( | |
69 content::WebContents* web_contents, | |
70 Delegate* delegate, | |
71 const gfx::Rect& element_rect); | |
72 | |
73 // |Create| uses the currently registered FactoryMethod to create the | |
74 // SpeechInputBubble instances. FactoryMethod is intended for testing. | |
75 typedef SpeechInputBubble* (*FactoryMethod)(content::WebContents*, | |
76 Delegate*, | |
77 const gfx::Rect&); | |
78 // Sets the factory used by the static method Create. SpeechInputBubble does | |
79 // not take ownership of |factory|. A value of NULL results in a | |
80 // SpeechInputBubble being created directly. | |
81 #if defined(UNIT_TEST) | |
82 static void set_factory(FactoryMethod factory) { factory_ = factory; } | |
83 #endif | |
84 | |
85 virtual ~SpeechInputBubble() {} | |
86 | |
87 // Indicates to the user that audio hardware is initializing. If the bubble is | |
88 // hidden, |Show| must be called to make it appear on screen. | |
89 virtual void SetWarmUpMode() = 0; | |
90 | |
91 // Indicates to the user that audio recording is in progress. If the bubble is | |
92 // hidden, |Show| must be called to make it appear on screen. | |
93 virtual void SetRecordingMode() = 0; | |
94 | |
95 // Indicates to the user that recognition is in progress. If the bubble is | |
96 // hidden, |Show| must be called to make it appear on screen. | |
97 virtual void SetRecognizingMode() = 0; | |
98 | |
99 // Displays the given string with the 'Try again' and 'Cancel' buttons. If the | |
100 // bubble is hidden, |Show| must be called to make it appear on screen. | |
101 virtual void SetMessage(const string16& text) = 0; | |
102 | |
103 // Brings up the bubble on screen. | |
104 virtual void Show() = 0; | |
105 | |
106 // Hides the info bubble, resulting in a call to | |
107 // |Delegate::InfoBubbleFocusChanged| as well. | |
108 virtual void Hide() = 0; | |
109 | |
110 // Updates and draws the current captured audio volume displayed on screen. | |
111 virtual void SetInputVolume(float volume, float noise_volume) = 0; | |
112 | |
113 // Returns the WebContents for which this bubble gets displayed. | |
114 virtual content::WebContents* web_contents() = 0; | |
115 | |
116 // The horizontal distance between the start of the html widget and the speech | |
117 // bubble's arrow. | |
118 static const int kBubbleTargetOffsetX; | |
119 | |
120 private: | |
121 static FactoryMethod factory_; | |
122 }; | |
123 | |
124 // Base class for the platform specific bubble implementations, this contains | |
125 // the platform independent code for SpeechInputBubble. | |
126 class SpeechInputBubbleBase : public SpeechInputBubble { | |
127 public: | |
128 // The current display mode of the bubble, useful only for the platform | |
129 // specific implementation. | |
130 enum DisplayMode { | |
131 DISPLAY_MODE_WARM_UP, | |
132 DISPLAY_MODE_RECORDING, | |
133 DISPLAY_MODE_RECOGNIZING, | |
134 DISPLAY_MODE_MESSAGE | |
135 }; | |
136 | |
137 explicit SpeechInputBubbleBase(content::WebContents* web_contents); | |
138 virtual ~SpeechInputBubbleBase(); | |
139 | |
140 // SpeechInputBubble methods | |
141 virtual void SetWarmUpMode() OVERRIDE; | |
142 virtual void SetRecordingMode() OVERRIDE; | |
143 virtual void SetRecognizingMode() OVERRIDE; | |
144 virtual void SetMessage(const string16& text) OVERRIDE; | |
145 virtual void SetInputVolume(float volume, float noise_volume) OVERRIDE; | |
146 virtual content::WebContents* web_contents() OVERRIDE; | |
147 | |
148 protected: | |
149 // Updates the platform specific UI layout for the current display mode. | |
150 virtual void UpdateLayout() = 0; | |
151 | |
152 // Overridden by subclasses to copy |icon_image()| to the screen. | |
153 virtual void UpdateImage() = 0; | |
154 | |
155 DisplayMode display_mode() const { return display_mode_; } | |
156 | |
157 const string16& message_text() const { return message_text_; } | |
158 | |
159 SkBitmap icon_image(); | |
160 | |
161 private: | |
162 void DoRecognizingAnimationStep(); | |
163 void DoWarmingUpAnimationStep(); | |
164 void SetImage(const SkBitmap& image); | |
165 | |
166 void DrawVolumeOverlay(SkCanvas* canvas, | |
167 const SkBitmap& bitmap, | |
168 float volume); | |
169 | |
170 // Task factory used for animation timer. | |
171 base::WeakPtrFactory<SpeechInputBubbleBase> weak_factory_; | |
172 int animation_step_; // Current index/step of the animation. | |
173 std::vector<SkBitmap> animation_frames_; | |
174 std::vector<SkBitmap> warming_up_frames_; | |
175 | |
176 DisplayMode display_mode_; | |
177 string16 message_text_; // Text displayed in DISPLAY_MODE_MESSAGE | |
178 // The current microphone image with volume level indication. | |
179 scoped_ptr<SkBitmap> mic_image_; | |
180 // A temporary buffer image used in creating the above mic image. | |
181 scoped_ptr<SkBitmap> buffer_image_; | |
182 // WebContents in which this this bubble gets displayed. | |
183 content::WebContents* web_contents_; | |
184 // The current image displayed in the bubble's icon widget. | |
185 scoped_ptr<SkBitmap> icon_image_; | |
186 }; | |
187 | |
188 // This typedef is to workaround the issue with certain versions of | |
189 // Visual Studio where it gets confused between multiple Delegate | |
190 // classes and gives a C2500 error. (I saw this error on the try bots - | |
191 // the workaround was not needed for my machine). | |
192 typedef SpeechInputBubble::Delegate SpeechInputBubbleDelegate; | |
193 | |
194 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_BUBBLE_H_ | |
OLD | NEW |