blob: c690fae09088fc35902e7de16dc0f068d01d2790 [file] [log] [blame]
[email protected]ebbbb9f2011-03-09 13:16:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]6a7746d2010-08-27 11:59:032// 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]210399f2010-09-10 22:46:528#include <map>
9
[email protected]6a7746d2010-08-27 11:59:0310#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
[email protected]6a7746d2010-08-27 11:59:0313#include "chrome/browser/speech/speech_input_bubble.h"
[email protected]ebbbb9f2011-03-09 13:16:1414#include "content/common/notification_observer.h"
[email protected]6a7746d2010-08-27 11:59:0315
16namespace gfx {
17class Rect;
18}
[email protected]061c7402011-01-07 12:14:4419class NotificationRegistrar;
[email protected]6a7746d2010-08-27 11:59:0320
21namespace 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]210399f2010-09-10 22:46:5225// 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]6a7746d2010-08-27 11:59:0328class SpeechInputBubbleController
29 : public base::RefCountedThreadSafe<SpeechInputBubbleController>,
[email protected]061c7402011-01-07 12:14:4430 public SpeechInputBubbleDelegate,
31 public NotificationObserver {
[email protected]6a7746d2010-08-27 11:59:0332 public:
33 // All methods of this delegate are called in the IO thread.
34 class Delegate {
35 public:
[email protected]210399f2010-09-10 22:46:5236 // 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]6a7746d2010-08-27 11:59:0339
40 // Invoked when the user clicks outside the speech input info bubble causing
41 // it to close and input focus to change.
[email protected]210399f2010-09-10 22:46:5242 virtual void InfoBubbleFocusChanged(int caller_id) = 0;
[email protected]6a7746d2010-08-27 11:59:0343
44 protected:
45 virtual ~Delegate() {}
46 };
47
48 explicit SpeechInputBubbleController(Delegate* delegate);
[email protected]210399f2010-09-10 22:46:5249 virtual ~SpeechInputBubbleController();
[email protected]6a7746d2010-08-27 11:59:0350
[email protected]210399f2010-09-10 22:46:5251 // 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]6a7746d2010-08-27 11:59:0353 void CreateBubble(int caller_id,
54 int render_process_id,
55 int render_view_id,
56 const gfx::Rect& element_rect);
57
[email protected]210399f2010-09-10 22:46:5258 // 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]6a7746d2010-08-27 11:59:0369
[email protected]fc89d8ae2010-09-16 12:07:5770 // Updates the current captured audio volume displayed on screen.
[email protected]3b283d62011-03-01 18:38:3671 void SetBubbleInputVolume(int caller_id, float volume, float noise_volume);
[email protected]fc89d8ae2010-09-16 12:07:5772
[email protected]6a7746d2010-08-27 11:59:0373 void CloseBubble(int caller_id);
74
75 // SpeechInputBubble::Delegate methods.
[email protected]210399f2010-09-10 22:46:5276 virtual void InfoBubbleButtonClicked(SpeechInputBubble::Button button);
77 virtual void InfoBubbleFocusChanged();
[email protected]6a7746d2010-08-27 11:59:0378
[email protected]061c7402011-01-07 12:14:4479 // NotificationObserver implementation.
80 virtual void Observe(NotificationType type,
81 const NotificationSource& source,
82 const NotificationDetails& details);
83
[email protected]6a7746d2010-08-27 11:59:0384 private:
[email protected]fc89d8ae2010-09-16 12:07:5785 // 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]061c7402011-01-07 12:14:4494 enum ManageSubscriptionAction {
95 BUBBLE_ADDED,
96 BUBBLE_REMOVED
97 };
98
[email protected]210399f2010-09-10 22:46:5299 void InvokeDelegateButtonClicked(int caller_id,
100 SpeechInputBubble::Button button);
[email protected]6a7746d2010-08-27 11:59:03101 void InvokeDelegateFocusChanged(int caller_id);
[email protected]fc89d8ae2010-09-16 12:07:57102 void ProcessRequestInUiThread(int caller_id,
103 RequestType type,
104 const string16& text,
[email protected]3b283d62011-03-01 18:38:36105 float volume,
106 float noise_volume);
[email protected]6a7746d2010-08-27 11:59:03107
[email protected]061c7402011-01-07 12:14:44108 // 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]6a7746d2010-08-27 11:59:03116 // Only accessed in the IO thread.
117 Delegate* delegate_;
118
[email protected]210399f2010-09-10 22:46:52119 //*** 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]6a7746d2010-08-27 11:59:03123 int current_bubble_caller_id_;
[email protected]210399f2010-09-10 22:46:52124
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]061c7402011-01-07 12:14:44127 typedef std::map<int, SpeechInputBubble*> BubbleCallerIdMap;
128 BubbleCallerIdMap bubbles_;
129
130 scoped_ptr<NotificationRegistrar> registrar_;
[email protected]6a7746d2010-08-27 11:59:03131};
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).
137typedef SpeechInputBubbleController::Delegate
138 SpeechInputBubbleControllerDelegate;
139
140} // namespace speech_input
141
142#endif // CHROME_BROWSER_SPEECH_SPEECH_INPUT_BUBBLE_CONTROLLER_H_