Home | History | Annotate | Download | only in media
      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_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_
      6 #define CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <utility>
     11 #include <vector>
     12 
     13 #include "base/callback_forward.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "chrome/browser/status_icons/status_icon_menu_model.h"
     16 #include "content/public/common/media_stream_request.h"
     17 
     18 namespace content {
     19 class WebContents;
     20 }  // namespace content
     21 
     22 namespace gfx {
     23 class ImageSkia;
     24 }  // namespace gfx
     25 
     26 class StatusIcon;
     27 class StatusTray;
     28 
     29 // This indicator is owned by MediaCaptureDevicesDispatcher
     30 // (MediaCaptureDevicesDispatcher is a singleton).
     31 class MediaStreamCaptureIndicator
     32     : public base::RefCountedThreadSafe<MediaStreamCaptureIndicator>,
     33       public StatusIconMenuModel::Delegate {
     34  public:
     35   MediaStreamCaptureIndicator();
     36 
     37   // Registers a new media stream for |web_contents| and returns UI object
     38   // that's used by the content layer to notify about state of the stream.
     39   scoped_ptr<content::MediaStreamUI> RegisterMediaStream(
     40       content::WebContents* web_contents,
     41       const content::MediaStreamDevices& devices);
     42 
     43   // Overrides from StatusIconMenuModel::Delegate implementation.
     44   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
     45 
     46   // Returns true if the |web_contents| is capturing user media (e.g., webcam or
     47   // microphone input).
     48   bool IsCapturingUserMedia(content::WebContents* web_contents) const;
     49 
     50   // Returns true if the |web_contents| is capturing video (e.g., webcam).
     51   bool IsCapturingVideo(content::WebContents* web_contents) const;
     52 
     53   // Returns true if the |web_contents| is capturing audio (e.g., microphone).
     54   bool IsCapturingAudio(content::WebContents* web_contents) const;
     55 
     56   // Returns true if the |web_contents| itself is being mirrored (e.g., a source
     57   // of media for remote broadcast).
     58   bool IsBeingMirrored(content::WebContents* web_contents) const;
     59 
     60  private:
     61   class UIDelegate;
     62   class WebContentsDeviceUsage;
     63   friend class WebContentsDeviceUsage;
     64 
     65   friend class base::RefCountedThreadSafe<MediaStreamCaptureIndicator>;
     66   virtual ~MediaStreamCaptureIndicator();
     67 
     68   // Following functions/variables are executed/accessed only on UI thread.
     69 
     70   // Called by WebContentsDeviceUsage when it's about to destroy itself, i.e.
     71   // when WebContents is being destroyed.
     72   void UnregisterWebContents(content::WebContents* web_contents);
     73 
     74   // Updates the status tray menu. Called by WebContentsDeviceUsage.
     75   void UpdateNotificationUserInterface();
     76 
     77   // Helpers to create and destroy status tray icon. Called from
     78   // UpdateNotificationUserInterface().
     79   void EnsureStatusTrayIconResources();
     80   void MaybeCreateStatusTrayIcon(bool audio, bool video);
     81   void MaybeDestroyStatusTrayIcon();
     82 
     83   // Gets the status icon image and the string to use as the tooltip.
     84   void GetStatusTrayIconInfo(bool audio,
     85                              bool video,
     86                              gfx::ImageSkia* image,
     87                              base::string16* tool_tip);
     88 
     89   // Reference to our status icon - owned by the StatusTray. If null,
     90   // the platform doesn't support status icons.
     91   StatusIcon* status_icon_;
     92 
     93   // These images are owned by ResourceBundle and need not be destroyed.
     94   gfx::ImageSkia* mic_image_;
     95   gfx::ImageSkia* camera_image_;
     96 
     97   // A map that contains the usage counts of the opened capture devices for each
     98   // WebContents instance.
     99   typedef std::map<content::WebContents*, WebContentsDeviceUsage*> UsageMap;
    100   UsageMap usage_map_;
    101 
    102   // A vector which maps command IDs to their associated WebContents
    103   // instance. This is rebuilt each time the status tray icon context menu is
    104   // updated.
    105   typedef std::vector<content::WebContents*> CommandTargets;
    106   CommandTargets command_targets_;
    107 };
    108 
    109 #endif  // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_
    110