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