Chromium Code Reviews| 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 in the IO thread, processing |
| 29 // those requests in the UI thread. There could be multiple bubble objects alive | 22 // those requests in 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 in 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 | |
| 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 session_id, const string16& text); | |
| 76 | 58 |
| 77 // Updates the current captured audio volume displayed on screen. | 59 // Updates the current captured audio volume displayed on screen. |
| 78 void SetBubbleInputVolume(int session_id, float volume, float noise_volume); | 60 void SetBubbleInputVolume(float volume, float noise_volume); |
| 79 | 61 |
| 80 void CloseBubble(int session_id); | 62 void CloseBubble(); |
| 63 | |
| 64 // Retrieves the session ID associated to the active bubble (if any). | |
| 65 // Returns 0 if no bubble is currently shown. | |
| 66 int GetActiveSessionID(); | |
| 67 | |
| 68 // Checks whether a bubble is being shown on the renderer identified by the | |
| 69 // tuple {|render_process_id|,|render_view_id|} | |
| 70 bool IsShowingBubbleOn(int render_process_id, int render_view_id); | |
| 81 | 71 |
| 82 // SpeechRecognitionBubble::Delegate methods. | 72 // SpeechRecognitionBubble::Delegate methods. |
| 83 virtual void InfoBubbleButtonClicked( | 73 virtual void InfoBubbleButtonClicked( |
| 84 SpeechRecognitionBubble::Button button) OVERRIDE; | 74 SpeechRecognitionBubble::Button button) OVERRIDE; |
| 85 virtual void InfoBubbleFocusChanged() OVERRIDE; | 75 virtual void InfoBubbleFocusChanged() OVERRIDE; |
| 86 | 76 |
| 87 // content::NotificationObserver implementation. | |
| 88 virtual void Observe(int type, | |
| 89 const content::NotificationSource& source, | |
| 90 const content::NotificationDetails& details) OVERRIDE; | |
| 91 | |
| 92 private: | 77 private: |
| 93 friend class base::RefCountedThreadSafe<SpeechRecognitionBubbleController>; | 78 friend class base::RefCountedThreadSafe<SpeechRecognitionBubbleController>; |
| 94 | 79 |
| 95 // The various calls received by this object and handled in the UI thread. | 80 // The various calls received by this object and handled in the UI thread. |
| 96 enum RequestType { | 81 enum RequestType { |
| 97 REQUEST_SET_WARM_UP_MODE, | 82 REQUEST_CREATE, |
| 98 REQUEST_SET_RECORDING_MODE, | 83 REQUEST_SET_RECORDING_MODE, |
| 99 REQUEST_SET_RECOGNIZING_MODE, | 84 REQUEST_SET_RECOGNIZING_MODE, |
| 100 REQUEST_SET_MESSAGE, | 85 REQUEST_SET_MESSAGE, |
| 101 REQUEST_SET_INPUT_VOLUME, | 86 REQUEST_SET_INPUT_VOLUME, |
| 102 REQUEST_CLOSE, | 87 REQUEST_CLOSE, |
| 103 }; | 88 }; |
| 104 | 89 |
| 105 enum ManageSubscriptionAction { | 90 struct UIRequest { |
| 106 BUBBLE_ADDED, | 91 RequestType type; |
| 107 BUBBLE_REMOVED | 92 string16 message; |
| 93 gfx::Rect element_rect; | |
| 94 float volume; | |
| 95 float noise_volume; | |
| 96 int render_process_id; | |
| 97 int render_view_id; | |
| 98 | |
| 99 explicit UIRequest(RequestType type_value); | |
| 100 ~UIRequest(); | |
| 108 }; | 101 }; |
| 109 | 102 |
| 110 virtual ~SpeechRecognitionBubbleController(); | 103 virtual ~SpeechRecognitionBubbleController(); |
| 111 | 104 |
| 112 void InvokeDelegateButtonClicked(int session_id, | 105 void InvokeDelegateButtonClicked(SpeechRecognitionBubble::Button button); |
| 113 SpeechRecognitionBubble::Button button); | 106 void InvokeDelegateFocusChanged(); |
| 114 void InvokeDelegateFocusChanged(int session_id); | 107 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 | 108 |
| 121 // Called whenever a bubble was added to or removed from the list. If the | 109 // *** The following are accessed only in the IO thread. |
|
hans
2012/06/26 13:10:44
i prefer "on ... thread" rather than "in ... threa
Primiano Tucci (use gerrit)
2012/06/26 14:27:07
Fixed here and in the other comments.
| |
| 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_; | 110 Delegate* delegate_; |
| 131 | 111 |
| 132 // *** The following are accessed only in the UI thread. | 112 // 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_; | 113 int current_bubble_session_id_; |
| 137 | 114 |
| 138 // Map of session-ids to bubble objects. The bubbles are weak pointers owned | 115 // The render process and view ids for the currently visible bubble. |
| 139 // by this object and get destroyed by |CloseBubble|. | 116 int current_bubble_render_process_id_; |
| 140 typedef std::map<int, SpeechRecognitionBubble*> BubbleSessionIdMap; | 117 int current_bubble_render_view_id_; |
| 141 BubbleSessionIdMap bubbles_; | |
| 142 | 118 |
| 143 scoped_ptr<content::NotificationRegistrar> registrar_; | 119 // *** The following are accessed only in the UI thread. |
| 120 scoped_ptr<SpeechRecognitionBubble> bubble_; | |
| 144 }; | 121 }; |
| 145 | 122 |
| 146 // This typedef is to workaround the issue with certain versions of | 123 // This typedef is to workaround the issue with certain versions of |
| 147 // Visual Studio where it gets confused between multiple Delegate | 124 // Visual Studio where it gets confused between multiple Delegate |
| 148 // classes and gives a C2500 error. (I saw this error on the try bots - | 125 // classes and gives a C2500 error. (I saw this error on the try bots - |
| 149 // the workaround was not needed for my machine). | 126 // the workaround was not needed for my machine). |
| 150 typedef SpeechRecognitionBubbleController::Delegate | 127 typedef SpeechRecognitionBubbleController::Delegate |
| 151 SpeechRecognitionBubbleControllerDelegate; | 128 SpeechRecognitionBubbleControllerDelegate; |
| 152 | 129 |
| 153 } // namespace speech | 130 } // namespace speech |
| 154 | 131 |
| 155 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_ | 132 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_ |
| OLD | NEW |