OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ |
6 #define CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ | 6 #define CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ |
7 | 7 |
8 #include <map> | |
8 #include <string> | 9 #include <string> |
10 #include <utility> | |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "content/public/common/media_stream_request.h" | 14 #include "content/public/common/media_stream_request.h" |
14 #include "ui/base/models/simple_menu_model.h" | 15 #include "ui/base/models/simple_menu_model.h" |
15 #include "ui/gfx/image/image_skia.h" | |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 class WebContents; | 18 class WebContents; |
19 } // namespace content | 19 } // namespace content |
20 | 20 |
21 namespace gfx { | |
22 class ImageSkia; | |
23 } // namespace gfx | |
24 | |
21 class StatusIcon; | 25 class StatusIcon; |
22 class StatusTray; | 26 class StatusTray; |
23 | 27 |
24 // This indicator is owned by MediaInternals and deleted when MediaInternals | 28 // This indicator is owned by MediaInternals and deleted when MediaInternals |
25 // is deleted. | 29 // is deleted. |
26 class MediaStreamCaptureIndicator | 30 class MediaStreamCaptureIndicator |
27 : public base::RefCountedThreadSafe<MediaStreamCaptureIndicator>, | 31 : public base::RefCountedThreadSafe<MediaStreamCaptureIndicator>, |
28 public ui::SimpleMenuModel::Delegate { | 32 public ui::SimpleMenuModel::Delegate { |
29 public: | 33 public: |
30 MediaStreamCaptureIndicator(); | 34 MediaStreamCaptureIndicator(); |
31 | 35 |
32 // Overrides from SimpleMenuModel::Delegate implementation. | 36 // Overrides from SimpleMenuModel::Delegate implementation. |
33 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 37 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
34 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 38 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
35 virtual bool GetAcceleratorForCommandId( | 39 virtual bool GetAcceleratorForCommandId( |
36 int command_id, | 40 int command_id, |
37 ui::Accelerator* accelerator) OVERRIDE; | 41 ui::Accelerator* accelerator) OVERRIDE; |
38 virtual void ExecuteCommand(int command_id) OVERRIDE; | 42 virtual void ExecuteCommand(int command_id) OVERRIDE; |
39 | 43 |
40 // Called on IO thread when MediaStream opens new capture devices. | 44 // Called on IO thread when MediaStream opens new capture devices. |
41 void CaptureDevicesOpened(int render_process_id, | 45 void CaptureDevicesOpened(int render_process_id, |
42 int render_view_id, | 46 int render_view_id, |
43 const content::MediaStreamDevices& devices); | 47 const content::MediaStreamDevices& devices); |
44 | 48 |
45 // Called on IO thread when MediaStream closes the opened devices. | 49 // Called on IO thread when MediaStream closes the opened devices. |
46 void CaptureDevicesClosed(int render_process_id, | 50 void CaptureDevicesClosed(int render_process_id, |
47 int render_view_id, | 51 int render_view_id, |
48 const content::MediaStreamDevices& devices); | 52 const content::MediaStreamDevices& devices); |
49 | 53 |
50 // Returns true if the render process is capturing media. | 54 // Returns true if the render view is capturing user media (e.g., webcam |
51 bool IsProcessCapturing(int render_process_id, int render_view_id) const; | 55 // or microphone input). |
56 bool IsCapturingUserMedia(int render_process_id, int render_view_id) const; | |
52 | 57 |
53 // Returns true if the render process is capturing tab media. | 58 // Returns true if the render view itself is being mirrored (i.e., a source of |
54 bool IsProcessCapturingTab(int render_process_id, int render_view_id) const; | 59 // media for remoting). |
justinlin
2013/01/28 22:31:21
s/remoting/remote broadcast ?
miu
2013/01/28 23:57:13
Done.
| |
60 bool IsBeingMirrored(int render_process_id, int render_view_id) const; | |
55 | 61 |
56 // ImageLoader callback. | 62 // ImageLoader callback. |
57 void OnImageLoaded(const string16& message, const gfx::Image& image); | 63 void OnImageLoaded(const string16& message, const gfx::Image& image); |
58 | 64 |
59 private: | 65 private: |
60 // Struct to store the usage information of the capture devices for each tab. | 66 class WebContentsDeviceUsage; |
61 // Note: It is not safe to dereference WebContents pointer. This is used to | |
62 // track the tab after navigations as render_view_id's can change. | |
63 // TODO(estade): this should be called CaptureDeviceContents; not all the | |
64 // render views it represents are tabs. | |
65 struct CaptureDeviceTab { | |
66 CaptureDeviceTab(content::WebContents* web_contents, | |
67 int render_process_id, | |
68 int render_view_id) | |
69 : web_contents(web_contents), | |
70 render_process_id(render_process_id), | |
71 render_view_id(render_view_id), | |
72 audio_ref_count(0), | |
73 video_ref_count(0), | |
74 tab_capture_ref_count(0) {} | |
75 | |
76 content::WebContents* web_contents; | |
77 int render_process_id; | |
78 int render_view_id; | |
79 int audio_ref_count; | |
80 int video_ref_count; | |
81 int tab_capture_ref_count; | |
82 }; | |
83 | |
84 // A private predicate used in std::find_if to find a |CaptureDeviceTab| | |
85 // which matches the information specified at construction. A tab will match | |
86 // if either the web_contents pointer is the same or the render_process_id and | |
87 // render_view_id's are the same. In the first case, a tab with a UI indicator | |
88 // may have changed page so the id's are different. In the second case, the | |
89 // web_contents may have already been destroyed before the indicator was | |
90 // hidden. | |
91 class TabEquals { | |
92 public: | |
93 TabEquals(content::WebContents* web_contents, | |
94 int render_process_id, | |
95 int render_view_id); | |
96 | |
97 bool operator() ( | |
98 const MediaStreamCaptureIndicator::CaptureDeviceTab& tab); | |
99 | |
100 private: | |
101 content::WebContents* web_contents_; | |
102 int render_process_id_; | |
103 int render_view_id_; | |
104 }; | |
105 | 67 |
106 friend class base::RefCountedThreadSafe<MediaStreamCaptureIndicator>; | 68 friend class base::RefCountedThreadSafe<MediaStreamCaptureIndicator>; |
107 virtual ~MediaStreamCaptureIndicator(); | 69 virtual ~MediaStreamCaptureIndicator(); |
108 | 70 |
109 // Called by the public functions, executed on UI thread. | 71 // Called by the public functions, executed on UI thread. |
110 void DoDevicesOpenedOnUIThread(int render_process_id, | 72 void DoDevicesOpenedOnUIThread(int render_process_id, |
111 int render_view_id, | 73 int render_view_id, |
112 const content::MediaStreamDevices& devices); | 74 const content::MediaStreamDevices& devices); |
113 void DoDevicesClosedOnUIThread(int render_process_id, | 75 void DoDevicesClosedOnUIThread(int render_process_id, |
114 int render_view_id, | 76 int render_view_id, |
115 const content::MediaStreamDevices& devices); | 77 const content::MediaStreamDevices& devices); |
116 | 78 |
117 // Following functions/variables are executed/accessed only on UI thread. | 79 // Following functions/variables are executed/accessed only on UI thread. |
118 // Creates the status tray if it has not been created. | 80 |
119 void CreateStatusTray(); | 81 // Creates and shows the status tray icon if it has not been created and is |
82 // supported on the current platform. | |
83 void MaybeCreateStatusTrayIcon(); | |
120 | 84 |
121 // Makes sure we have done one-time initialization of the images. | 85 // Makes sure we have done one-time initialization of the images. |
122 void EnsureStatusTrayIconResources(); | 86 void EnsureStatusTrayIconResources(); |
123 | 87 |
124 // Adds the new tab to the device usage list. | 88 // Finds a WebContents instance by its RenderView's current or |
125 void AddCaptureDeviceTab(int render_process_id, | 89 // previously-known IDs. This is necessary since clients that called |
126 int render_view_id, | 90 // CaptureDevicesOpened() may later call CaptureDevicesClosed() with stale |
127 const content::MediaStreamDevices& devices); | 91 // IDs. Do not attempt to dereference the pointer without first consulting |
92 // WebContentsDeviceUsage::IsWebContentsDestroyed(). | |
93 content::WebContents* LookUpByKnownAlias(int render_process_id, | |
justinlin
2013/01/28 22:31:21
I see why it's currently like this, but I wish the
miu
2013/01/28 23:57:13
I liked this idea, and tried working the code arou
| |
94 int render_view_id) const; | |
128 | 95 |
129 // Removes the tab from the device usage list. | 96 // Adds devices to usage map and triggers necessary UI updates. |
130 void RemoveCaptureDeviceTab(int render_process_id, | 97 void AddCaptureDevices(int render_process_id, |
131 int render_view_id, | 98 int render_view_id, |
132 const content::MediaStreamDevices& devices); | 99 const content::MediaStreamDevices& devices); |
100 | |
101 // Removes devices from the usage map and triggers necessary UI updates. | |
102 void RemoveCaptureDevices(int render_process_id, | |
103 int render_view_id, | |
104 const content::MediaStreamDevices& devices); | |
133 | 105 |
134 // Triggers a balloon in the corner telling capture devices are being used. | 106 // Triggers a balloon in the corner telling capture devices are being used. |
135 // This function is called by AddCaptureDeviceTab(). | 107 // This function is called by AddCaptureDevices(). |
136 void ShowBalloon(int render_process_id, int render_view_id, | 108 void ShowBalloon(content::WebContents* web_contents, |
137 bool audio, bool video); | 109 int balloon_body_message_id); |
138 | 110 |
139 // Hides the status tray from the desktop. This function is called by | 111 // Removes the status tray icon from the desktop. This function is called by |
140 // RemoveCaptureDeviceTab() when the device usage list becomes empty. | 112 // RemoveCaptureDevices() when the device usage map becomes empty. |
141 void Hide(); | 113 void MaybeDestroyStatusTrayIcon(); |
142 | 114 |
143 // Updates the status tray menu with the new device list. This call will be | 115 // Updates the status tray menu with the new device list. This call will be |
144 // triggered by both AddCaptureDeviceTab() and RemoveCaptureDeviceTab(). | 116 // triggered by both AddCaptureDevices() and RemoveCaptureDevices(). |
145 void UpdateStatusTrayIconContextMenu(); | 117 void UpdateStatusTrayIconContextMenu(); |
146 | 118 |
147 // Updates the status tray tooltip and image according to which kind of | 119 // Updates the status tray tooltip and image according to which kind of |
148 // devices are being used. This function is called by | 120 // devices are being used. This function is called by |
149 // UpdateStatusTrayIconContextMenu(). | 121 // UpdateStatusTrayIconContextMenu(). |
150 void UpdateStatusTrayIconDisplay(bool audio, bool video); | 122 void UpdateStatusTrayIconDisplay(bool audio, bool video); |
151 | 123 |
152 // Reference to our status icon - owned by the StatusTray. If null, | 124 // Reference to our status icon - owned by the StatusTray. If null, |
153 // the platform doesn't support status icons. | 125 // the platform doesn't support status icons. |
154 StatusIcon* status_icon_; | 126 StatusIcon* status_icon_; |
155 | 127 |
156 // These images are owned by ResourceBundle and need not be destroyed. | 128 // These images are owned by ResourceBundle and need not be destroyed. |
157 gfx::ImageSkia* mic_image_; | 129 gfx::ImageSkia* mic_image_; |
158 gfx::ImageSkia* camera_image_; | 130 gfx::ImageSkia* camera_image_; |
159 gfx::ImageSkia* balloon_image_; | 131 gfx::ImageSkia* balloon_image_; |
160 | 132 |
161 // A list that contains the usage information of the opened capture devices. | 133 // A map that contains the usage counts of the opened capture devices for each |
162 typedef std::vector<CaptureDeviceTab> CaptureDeviceTabs; | 134 // WebContents instance. |
163 CaptureDeviceTabs tabs_; | 135 typedef std::map<content::WebContents*, WebContentsDeviceUsage*> UsageMap; |
136 UsageMap usage_map_; | |
137 | |
138 // A map of known render_process_id/render_view_id pairs that have been | |
justinlin
2013/01/28 22:31:21
maybe "/original/ known [id-pairs]" to clarify tha
miu
2013/01/28 23:57:13
Done.
| |
139 // associated with a WebContents instance. | |
140 typedef std::pair<int, int> RenderViewIDs; | |
141 typedef std::map<RenderViewIDs, content::WebContents*> AliasMap; | |
142 AliasMap aliases_; | |
143 | |
144 // A map from command_ids to their associated WebContents instance. This is | |
justinlin
2013/01/28 22:31:21
s/map/vector indexed by command_id? And, maybe thi
miu
2013/01/28 23:57:13
Done.
| |
145 // rebuilt each time the status tray icon context menu is updated. | |
146 typedef std::vector<content::WebContents*> CommandMap; | |
147 CommandMap command_map_; | |
164 | 148 |
165 bool should_show_balloon_; | 149 bool should_show_balloon_; |
166 }; | 150 }; |
167 | 151 |
168 #endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ | 152 #endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_ |
OLD | NEW |