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