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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_ 6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/synchronization/lock.h" 11 #include "chrome/browser/speech/speech_recognition_bubble.h" 12 #include "ui/gfx/rect.h" 13 14 namespace speech { 15 16 // This class handles the speech recognition popup UI on behalf of 17 // SpeechRecognitionManager, which invokes methods on the IO thread, processing 18 // those requests on the UI thread. At most one bubble can be active. 19 class SpeechRecognitionBubbleController 20 : public base::RefCountedThreadSafe<SpeechRecognitionBubbleController>, 21 public SpeechRecognitionBubbleDelegate { 22 public: 23 // All methods of this delegate are called on the IO thread. 24 class Delegate { 25 public: 26 // Invoked when the user clicks on a button in the speech recognition UI. 27 virtual void InfoBubbleButtonClicked( 28 int session_id, SpeechRecognitionBubble::Button button) = 0; 29 30 // Invoked when the user clicks outside the speech recognition info bubble 31 // causing it to close and input focus to change. 32 virtual void InfoBubbleFocusChanged(int session_id) = 0; 33 34 protected: 35 virtual ~Delegate() {} 36 }; 37 38 explicit SpeechRecognitionBubbleController(Delegate* delegate); 39 40 // Creates and shows a new speech recognition UI bubble in warmup mode. 41 void CreateBubble(int session_id, 42 int render_process_id, 43 int render_view_id, 44 const gfx::Rect& element_rect); 45 46 // Indicates to the user that audio recording is in progress. 47 void SetBubbleRecordingMode(); 48 49 // Indicates to the user that recognition is in progress. 50 void SetBubbleRecognizingMode(); 51 52 // Displays the given string with the 'Try again' and 'Cancel' buttons. 53 void SetBubbleMessage(const base::string16& text); 54 55 // Checks whether the bubble is active and is showing a message. 56 bool IsShowingMessage() const; 57 58 // Updates the current captured audio volume displayed on screen. 59 void SetBubbleInputVolume(float volume, float noise_volume); 60 61 // Closes the bubble. 62 void CloseBubble(); 63 64 // This is the only method that can be called from the UI thread. 65 void CloseBubbleForRenderViewOnUIThread(int render_process_id, 66 int render_view_id); 67 68 // Retrieves the session ID associated to the active bubble (if any). 69 // Returns 0 if no bubble is currently shown. 70 int GetActiveSessionID(); 71 72 // SpeechRecognitionBubble::Delegate methods. 73 virtual void InfoBubbleButtonClicked( 74 SpeechRecognitionBubble::Button button) OVERRIDE; 75 virtual void InfoBubbleFocusChanged() OVERRIDE; 76 77 private: 78 friend class base::RefCountedThreadSafe<SpeechRecognitionBubbleController>; 79 80 // The various calls received by this object and handled on the UI thread. 81 enum RequestType { 82 REQUEST_CREATE, 83 REQUEST_SET_RECORDING_MODE, 84 REQUEST_SET_RECOGNIZING_MODE, 85 REQUEST_SET_MESSAGE, 86 REQUEST_SET_INPUT_VOLUME, 87 REQUEST_CLOSE, 88 }; 89 90 struct UIRequest { 91 RequestType type; 92 base::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(); 101 }; 102 103 virtual ~SpeechRecognitionBubbleController(); 104 105 void InvokeDelegateButtonClicked(SpeechRecognitionBubble::Button button); 106 void InvokeDelegateFocusChanged(); 107 void ProcessRequestInUiThread(const UIRequest& request); 108 109 // The following are accessed only on the IO thread. 110 Delegate* delegate_; 111 RequestType last_request_issued_; 112 113 // The following are accessed only on the UI thread. 114 scoped_ptr<SpeechRecognitionBubble> bubble_; 115 116 // The following are accessed on both IO and UI threads. 117 base::Lock lock_; 118 119 // The session id for currently visible bubble. 120 int current_bubble_session_id_; 121 122 // The render process and view ids for the currently visible bubble. 123 int current_bubble_render_process_id_; 124 int current_bubble_render_view_id_; 125 }; 126 127 // This typedef is to workaround the issue with certain versions of 128 // Visual Studio where it gets confused between multiple Delegate 129 // classes and gives a C2500 error. (I saw this error on the try bots - 130 // the workaround was not needed for my machine). 131 typedef SpeechRecognitionBubbleController::Delegate 132 SpeechRecognitionBubbleControllerDelegate; 133 134 } // namespace speech 135 136 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_CONTROLLER_H_ 137