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