OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_CONTROLLER_H_ | |
6 #define CHROME_BROWSER_SPEECH_SPEECH_INPUT_BUBBLE_CONTROLLER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "chrome/browser/speech/speech_input_bubble.h" | |
14 #include "content/public/browser/notification_observer.h" | |
15 | |
16 namespace gfx { | |
17 class Rect; | |
18 } | |
19 | |
20 namespace content { | |
21 class NotificationRegistrar; | |
22 } | |
23 | |
24 namespace speech_input { | |
25 | |
26 // This class handles the speech input popup UI on behalf of SpeechInputManager. | |
27 // SpeechInputManager invokes methods in the IO thread and this class processes | |
28 // those requests in the UI thread. There could be multiple bubble objects alive | |
29 // at the same time but only one of them is visible to the user. User actions on | |
30 // that bubble are reported to the delegate. | |
31 class SpeechInputBubbleController | |
32 : public base::RefCountedThreadSafe<SpeechInputBubbleController>, | |
33 public SpeechInputBubbleDelegate, | |
34 public content::NotificationObserver { | |
35 public: | |
36 // All methods of this delegate are called in the IO thread. | |
37 class Delegate { | |
38 public: | |
39 // Invoked when the user clicks on a button in the speech input UI. | |
40 virtual void InfoBubbleButtonClicked(int caller_id, | |
41 SpeechInputBubble::Button button) = 0; | |
42 | |
43 // Invoked when the user clicks outside the speech input info bubble causing | |
44 // it to close and input focus to change. | |
45 virtual void InfoBubbleFocusChanged(int caller_id) = 0; | |
46 | |
47 protected: | |
48 virtual ~Delegate() {} | |
49 }; | |
50 | |
51 explicit SpeechInputBubbleController(Delegate* delegate); | |
52 virtual ~SpeechInputBubbleController(); | |
53 | |
54 // Creates a new speech input UI bubble. One of the SetXxxx methods below need | |
55 // to be called to specify what to display. | |
56 void CreateBubble(int caller_id, | |
57 int render_process_id, | |
58 int render_view_id, | |
59 const gfx::Rect& element_rect); | |
60 | |
61 // Indicates to the user that audio hardware is warming up. This also makes | |
62 // the bubble visible if not already visible. | |
63 void SetBubbleWarmUpMode(int caller_id); | |
64 | |
65 // Indicates to the user that audio recording is in progress. This also makes | |
66 // the bubble visible if not already visible. | |
67 void SetBubbleRecordingMode(int caller_id); | |
68 | |
69 // Indicates to the user that recognition is in progress. If the bubble is | |
70 // hidden, |Show| must be called to make it appear on screen. | |
71 void SetBubbleRecognizingMode(int caller_id); | |
72 | |
73 // Displays the given string with the 'Try again' and 'Cancel' buttons. If the | |
74 // bubble is hidden, |Show| must be called to make it appear on screen. | |
75 void SetBubbleMessage(int caller_id, const string16& text); | |
76 | |
77 // Updates the current captured audio volume displayed on screen. | |
78 void SetBubbleInputVolume(int caller_id, float volume, float noise_volume); | |
79 | |
80 void CloseBubble(int caller_id); | |
81 | |
82 // SpeechInputBubble::Delegate methods. | |
83 virtual void InfoBubbleButtonClicked( | |
84 SpeechInputBubble::Button button) OVERRIDE; | |
85 virtual void InfoBubbleFocusChanged() OVERRIDE; | |
86 | |
87 // content::NotificationObserver implementation. | |
88 virtual void Observe(int type, | |
89 const content::NotificationSource& source, | |
90 const content::NotificationDetails& details) OVERRIDE; | |
91 | |
92 private: | |
93 // The various calls received by this object and handled in the UI thread. | |
94 enum RequestType { | |
95 REQUEST_SET_WARM_UP_MODE, | |
96 REQUEST_SET_RECORDING_MODE, | |
97 REQUEST_SET_RECOGNIZING_MODE, | |
98 REQUEST_SET_MESSAGE, | |
99 REQUEST_SET_INPUT_VOLUME, | |
100 REQUEST_CLOSE, | |
101 }; | |
102 | |
103 enum ManageSubscriptionAction { | |
104 BUBBLE_ADDED, | |
105 BUBBLE_REMOVED | |
106 }; | |
107 | |
108 void InvokeDelegateButtonClicked(int caller_id, | |
109 SpeechInputBubble::Button button); | |
110 void InvokeDelegateFocusChanged(int caller_id); | |
111 void ProcessRequestInUiThread(int caller_id, | |
112 RequestType type, | |
113 const string16& text, | |
114 float volume, | |
115 float noise_volume); | |
116 | |
117 // Called whenever a bubble was added to or removed from the list. If the | |
118 // bubble was being added, this method registers for close notifications with | |
119 // the TabContents if this was the first bubble for the tab. Similarly if the | |
120 // bubble was being removed, this method unregisters from TabContents if this | |
121 // was the last bubble associated with that tab. | |
122 void UpdateTabContentsSubscription(int caller_id, | |
123 ManageSubscriptionAction action); | |
124 | |
125 // Only accessed in the IO thread. | |
126 Delegate* delegate_; | |
127 | |
128 // *** The following are accessed only in the UI thread. | |
129 | |
130 // The caller id for currently visible bubble (since only one bubble is | |
131 // visible at any time). | |
132 int current_bubble_caller_id_; | |
133 | |
134 // Map of caller-ids to bubble objects. The bubbles are weak pointers owned by | |
135 // this object and get destroyed by |CloseBubble|. | |
136 typedef std::map<int, SpeechInputBubble*> BubbleCallerIdMap; | |
137 BubbleCallerIdMap bubbles_; | |
138 | |
139 scoped_ptr<content::NotificationRegistrar> registrar_; | |
140 }; | |
141 | |
142 // This typedef is to workaround the issue with certain versions of | |
143 // Visual Studio where it gets confused between multiple Delegate | |
144 // classes and gives a C2500 error. (I saw this error on the try bots - | |
145 // the workaround was not needed for my machine). | |
146 typedef SpeechInputBubbleController::Delegate | |
147 SpeechInputBubbleControllerDelegate; | |
148 | |
149 } // namespace speech_input | |
150 | |
151 #endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_BUBBLE_CONTROLLER_H_ | |
OLD | NEW |