Home | History | Annotate | Download | only in speech
      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_H_
      6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/strings/string16.h"
     13 #include "ui/base/layout.h"
     14 #include "ui/gfx/image/image_skia.h"
     15 
     16 class SkBitmap;
     17 class SkCanvas;
     18 
     19 namespace content {
     20 class WebContents;
     21 }
     22 
     23 namespace gfx {
     24 class Canvas;
     25 class Rect;
     26 }
     27 
     28 // SpeechRecognitionBubble displays a popup info bubble during speech
     29 // recognition, points to the html element which requested speech recognition
     30 // and shows progress events. The popup is closed by the user clicking anywhere
     31 // outside the popup window, or by the caller destroying this object.
     32 class SpeechRecognitionBubble {
     33  public:
     34   // The various buttons which may be part of the bubble.
     35   enum Button {
     36     BUTTON_TRY_AGAIN,
     37     BUTTON_CANCEL
     38   };
     39 
     40   // Informs listeners of user actions in the bubble.
     41   class Delegate {
     42    public:
     43     // Invoked when the user selects a button in the info bubble. The InfoBubble
     44     // is still active and the caller should close it if necessary.
     45     virtual void InfoBubbleButtonClicked(Button button) = 0;
     46 
     47     // Invoked when the user clicks outside the InfoBubble causing it to close.
     48     // The InfoBubble window is no longer visible on screen and the caller can
     49     // free the InfoBubble instance. This callback is not issued if the bubble
     50     // got closed because the object was destroyed by the caller.
     51     virtual void InfoBubbleFocusChanged() = 0;
     52 
     53    protected:
     54     virtual ~Delegate() {
     55     }
     56   };
     57 
     58   // Factory method to create new instances.
     59   // Creates the bubble, call |Show| to display it on screen.
     60   // |web_contents| is the WebContents hosting the page.
     61   // |element_rect| is the display bounds of the html element requesting speech
     62   // recognition (in page coordinates).
     63   static SpeechRecognitionBubble* Create(content::WebContents* web_contents,
     64                                          Delegate* delegate,
     65                                          const gfx::Rect& element_rect);
     66 
     67   // This is implemented by platform specific code to create the underlying
     68   // bubble window. Not to be called directly by users of this class.
     69   static SpeechRecognitionBubble* CreateNativeBubble(
     70       content::WebContents* web_contents,
     71       Delegate* delegate,
     72       const gfx::Rect& element_rect);
     73 
     74   // |Create| uses the currently registered FactoryMethod to create the
     75   // SpeechRecognitionBubble instances. FactoryMethod is intended for testing.
     76   typedef SpeechRecognitionBubble* (*FactoryMethod)(content::WebContents*,
     77                                                     Delegate*,
     78                                                     const gfx::Rect&);
     79   // Sets the factory used by the static method Create. SpeechRecognitionBubble
     80   // does not take ownership of |factory|. A value of NULL results in a
     81   // SpeechRecognitionBubble being created directly.
     82 #if defined(UNIT_TEST)
     83   static void set_factory(FactoryMethod factory) { factory_ = factory; }
     84 #endif
     85 
     86   virtual ~SpeechRecognitionBubble() {}
     87 
     88   // Indicates to the user that audio hardware is initializing. If the bubble is
     89   // hidden, |Show| must be called to make it appear on screen.
     90   virtual void SetWarmUpMode() = 0;
     91 
     92   // Indicates to the user that audio recording is in progress. If the bubble is
     93   // hidden, |Show| must be called to make it appear on screen.
     94   virtual void SetRecordingMode() = 0;
     95 
     96   // Indicates to the user that recognition is in progress. If the bubble is
     97   // hidden, |Show| must be called to make it appear on screen.
     98   virtual void SetRecognizingMode() = 0;
     99 
    100   // Displays the given string with the 'Try again' and 'Cancel' buttons. If the
    101   // bubble is hidden, |Show| must be called to make it appear on screen.
    102   virtual void SetMessage(const string16& text) = 0;
    103 
    104   // Brings up the bubble on screen.
    105   virtual void Show() = 0;
    106 
    107   // Hides the info bubble, resulting in a call to
    108   // |Delegate::InfoBubbleFocusChanged| as well.
    109   virtual void Hide() = 0;
    110 
    111   // Updates and draws the current captured audio volume displayed on screen.
    112   virtual void SetInputVolume(float volume, float noise_volume) = 0;
    113 
    114   // Returns the WebContents for which this bubble gets displayed.
    115   virtual content::WebContents* GetWebContents() = 0;
    116 
    117   // The horizontal distance between the start of the html widget and the speech
    118   // bubble's arrow.
    119   static const int kBubbleTargetOffsetX;
    120 
    121  private:
    122   static FactoryMethod factory_;
    123 };
    124 
    125 // Base class for the platform specific bubble implementations, this contains
    126 // the platform independent code for SpeechRecognitionBubble.
    127 class SpeechRecognitionBubbleBase : public SpeechRecognitionBubble {
    128  public:
    129   // The current display mode of the bubble, useful only for the platform
    130   // specific implementation.
    131   enum DisplayMode {
    132     DISPLAY_MODE_WARM_UP,
    133     DISPLAY_MODE_RECORDING,
    134     DISPLAY_MODE_RECOGNIZING,
    135     DISPLAY_MODE_MESSAGE
    136   };
    137 
    138   explicit SpeechRecognitionBubbleBase(content::WebContents* web_contents);
    139   virtual ~SpeechRecognitionBubbleBase();
    140 
    141   // SpeechRecognitionBubble methods
    142   virtual void SetWarmUpMode() OVERRIDE;
    143   virtual void SetRecordingMode() OVERRIDE;
    144   virtual void SetRecognizingMode() OVERRIDE;
    145   virtual void SetMessage(const string16& text) OVERRIDE;
    146   virtual void SetInputVolume(float volume, float noise_volume) OVERRIDE;
    147   virtual content::WebContents* GetWebContents() OVERRIDE;
    148 
    149  protected:
    150   // Updates the platform specific UI layout for the current display mode.
    151   virtual void UpdateLayout() = 0;
    152 
    153   // Overridden by subclasses to copy |icon_image()| to the screen.
    154   virtual void UpdateImage() = 0;
    155 
    156   DisplayMode display_mode() const { return display_mode_; }
    157 
    158   const string16& message_text() const { return message_text_; }
    159 
    160   gfx::ImageSkia icon_image();
    161 
    162  private:
    163   void DoRecognizingAnimationStep();
    164   void DoWarmingUpAnimationStep();
    165   void SetImage(const gfx::ImageSkia& image);
    166 
    167   void DrawVolumeOverlay(SkCanvas* canvas,
    168                          const gfx::ImageSkia& image_skia,
    169                          float volume);
    170 
    171   // Task factory used for animation timer.
    172   base::WeakPtrFactory<SpeechRecognitionBubbleBase> weak_factory_;
    173   int animation_step_;  // Current index/step of the animation.
    174 
    175   DisplayMode display_mode_;
    176   string16 message_text_;  // Text displayed in DISPLAY_MODE_MESSAGE
    177 
    178   // The current microphone image with volume level indication.
    179   scoped_ptr<SkBitmap> mic_image_;
    180   // A temporary buffer image used in creating the above mic image.
    181   scoped_ptr<SkBitmap> buffer_image_;
    182   // WebContents in which this this bubble gets displayed.
    183   content::WebContents* web_contents_;
    184   // The current image displayed in the bubble's icon widget.
    185   gfx::ImageSkia icon_image_;
    186   // The scale factor used for the web-contents.
    187   ui::ScaleFactor scale_factor_;
    188 
    189   DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleBase);
    190 };
    191 
    192 // This typedef is to workaround the issue with certain versions of
    193 // Visual Studio where it gets confused between multiple Delegate
    194 // classes and gives a C2500 error. (I saw this error on the try bots -
    195 // the workaround was not needed for my machine).
    196 typedef SpeechRecognitionBubble::Delegate SpeechRecognitionBubbleDelegate;
    197 
    198 #endif  // CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_BUBBLE_H_
    199