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