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 "content/public/common/media_stream_request.h" 16 #include "ui/base/models/simple_menu_model.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 ui::SimpleMenuModel::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 SimpleMenuModel::Delegate implementation. 44 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 45 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 46 virtual bool GetAcceleratorForCommandId( 47 int command_id, 48 ui::Accelerator* accelerator) OVERRIDE; 49 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 50 51 // Returns true if the |web_contents| is capturing user media (e.g., webcam or 52 // microphone input). 53 bool IsCapturingUserMedia(content::WebContents* web_contents) const; 54 55 // Returns true if the |web_contents| is capturing video (e.g., webcam). 56 bool IsCapturingVideo(content::WebContents* web_contents) const; 57 58 // Returns true if the |web_contents| is capturing audio (e.g., microphone). 59 bool IsCapturingAudio(content::WebContents* web_contents) const; 60 61 // Returns true if the |web_contents| itself is being mirrored (e.g., a source 62 // of media for remote broadcast). 63 bool IsBeingMirrored(content::WebContents* web_contents) const; 64 65 private: 66 class UIDelegate; 67 class WebContentsDeviceUsage; 68 friend class WebContentsDeviceUsage; 69 70 friend class base::RefCountedThreadSafe<MediaStreamCaptureIndicator>; 71 virtual ~MediaStreamCaptureIndicator(); 72 73 // Following functions/variables are executed/accessed only on UI thread. 74 75 // Called by WebContentsDeviceUsage when it's about to destroy itself, i.e. 76 // when WebContents is being destroyed. 77 void UnregisterWebContents(content::WebContents* web_contents); 78 79 // Updates the status tray menu. Called by WebContentsDeviceUsage. 80 void UpdateNotificationUserInterface(); 81 82 // Helpers to create and destroy status tray icon. Called from 83 // UpdateNotificationUserInterface(). 84 void EnsureStatusTrayIconResources(); 85 void MaybeCreateStatusTrayIcon(bool audio, bool video); 86 void MaybeDestroyStatusTrayIcon(); 87 88 // Gets the status icon image and the string to use as the tooltip. 89 void GetStatusTrayIconInfo(bool audio, 90 bool video, 91 gfx::ImageSkia* image, 92 string16* tool_tip); 93 94 // Reference to our status icon - owned by the StatusTray. If null, 95 // the platform doesn't support status icons. 96 StatusIcon* status_icon_; 97 98 // These images are owned by ResourceBundle and need not be destroyed. 99 gfx::ImageSkia* mic_image_; 100 gfx::ImageSkia* camera_image_; 101 102 // A map that contains the usage counts of the opened capture devices for each 103 // WebContents instance. 104 typedef std::map<content::WebContents*, WebContentsDeviceUsage*> UsageMap; 105 UsageMap usage_map_; 106 107 // A vector which maps command IDs to their associated WebContents 108 // instance. This is rebuilt each time the status tray icon context menu is 109 // updated. 110 typedef std::vector<content::WebContents*> CommandTargets; 111 CommandTargets command_targets_; 112 }; 113 114 #endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ 115