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 #include "chrome/browser/media/media_stream_capture_indicator.h" | 5 #include "chrome/browser/media/media_stream_capture_indicator.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | |
10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
11 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/extensions/image_loader.h" | 15 #include "chrome/browser/extensions/image_loader.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/status_icons/status_icon.h" | 17 #include "chrome/browser/status_icons/status_icon.h" |
17 #include "chrome/browser/status_icons/status_tray.h" | 18 #include "chrome/browser/status_icons/status_tray.h" |
18 #include "chrome/browser/tab_contents/tab_util.h" | 19 #include "chrome/browser/tab_contents/tab_util.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/content_browser_client.h" | 21 #include "content/public/browser/content_browser_client.h" |
21 #include "content/public/browser/invalidate_type.h" | 22 #include "content/public/browser/invalidate_type.h" |
22 #include "content/public/browser/render_view_host.h" | |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "content/public/browser/web_contents_delegate.h" | 24 #include "content/public/browser/web_contents_delegate.h" |
25 #include "content/public/common/media_stream_request.h" | |
26 #include "grit/chromium_strings.h" | 25 #include "grit/chromium_strings.h" |
27 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
28 #include "grit/theme_resources.h" | 27 #include "grit/theme_resources.h" |
29 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
30 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
31 #include "ui/gfx/image/image_skia.h" | |
32 | 32 |
33 using content::BrowserThread; | 33 using content::BrowserThread; |
34 using content::WebContents; | 34 using content::WebContents; |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 const extensions::Extension* GetExtension(int render_process_id, | 38 const extensions::Extension* GetExtension(WebContents* web_contents) { |
39 int render_view_id) { | |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
41 | 40 |
42 WebContents* web_contents = tab_util::GetWebContentsByID( | |
43 render_process_id, render_view_id); | |
44 if (!web_contents) | 41 if (!web_contents) |
45 return NULL; | 42 return NULL; |
46 | 43 |
47 Profile* profile = | 44 Profile* profile = |
48 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 45 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
49 if (!profile) | 46 if (!profile) |
50 return NULL; | 47 return NULL; |
51 | 48 |
52 ExtensionService* extension_service = profile->GetExtensionService(); | 49 ExtensionService* extension_service = profile->GetExtensionService(); |
53 if (!extension_service) | 50 if (!extension_service) |
54 return NULL; | 51 return NULL; |
55 | 52 |
56 return extension_service->extensions()->GetExtensionOrAppByURL( | 53 return extension_service->extensions()->GetExtensionOrAppByURL( |
57 ExtensionURLInfo(web_contents->GetURL())); | 54 ExtensionURLInfo(web_contents->GetURL())); |
58 } | 55 } |
59 | 56 |
60 // Gets the security originator of the tab. It returns a string with no '/' | 57 // Gets the security originator of the tab. It returns a string with no '/' |
61 // at the end to display in the UI. | 58 // at the end to display in the UI. |
62 string16 GetSecurityOrigin(int render_process_id, int render_view_id) { | 59 string16 GetSecurityOrigin(WebContents* web_contents) { |
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
64 WebContents* tab_content = tab_util::GetWebContentsByID( | 61 |
65 render_process_id, render_view_id); | 62 if (!web_contents) |
66 if (!tab_content) | |
67 return string16(); | 63 return string16(); |
68 | 64 |
69 std::string security_origin = tab_content->GetURL().GetOrigin().spec(); | 65 std::string security_origin = web_contents->GetURL().GetOrigin().spec(); |
70 | 66 |
71 // Remove the last character if it is a '/'. | 67 // Remove the last character if it is a '/'. |
72 if (!security_origin.empty()) { | 68 if (!security_origin.empty()) { |
73 std::string::iterator it = security_origin.end() - 1; | 69 std::string::iterator it = security_origin.end() - 1; |
74 if (*it == '/') | 70 if (*it == '/') |
75 security_origin.erase(it); | 71 security_origin.erase(it); |
76 } | 72 } |
77 | 73 |
78 return UTF8ToUTF16(security_origin); | 74 return UTF8ToUTF16(security_origin); |
79 } | 75 } |
80 | 76 |
81 string16 GetTitle(int render_process_id, int render_view_id) { | 77 string16 GetTitle(WebContents* web_contents) { |
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
83 | 79 |
84 const extensions::Extension* extension = | 80 if (!web_contents) |
85 GetExtension(render_process_id, render_view_id); | 81 return string16(); |
82 | |
83 const extensions::Extension* const extension = GetExtension(web_contents); | |
86 if (extension) | 84 if (extension) |
87 return UTF8ToUTF16(extension->name()); | 85 return UTF8ToUTF16(extension->name()); |
88 | 86 |
89 WebContents* tab_content = tab_util::GetWebContentsByID( | 87 string16 tab_title = web_contents->GetTitle(); |
90 render_process_id, render_view_id); | |
91 if (!tab_content) | |
92 return string16(); | |
93 | |
94 string16 tab_title = tab_content->GetTitle(); | |
95 | 88 |
96 if (tab_title.empty()) { | 89 if (tab_title.empty()) { |
97 // If the page's title is empty use its security originator. | 90 // If the page's title is empty use its security originator. |
98 tab_title = GetSecurityOrigin(render_process_id, render_view_id); | 91 tab_title = GetSecurityOrigin(web_contents); |
99 } else { | 92 } else { |
100 // If the page's title matches its URL, use its security originator. | 93 // If the page's title matches its URL, use its security originator. |
101 std::string languages = | 94 std::string languages = |
102 content::GetContentClient()->browser()->GetAcceptLangs( | 95 content::GetContentClient()->browser()->GetAcceptLangs( |
103 tab_content->GetBrowserContext()); | 96 web_contents->GetBrowserContext()); |
104 if (tab_title == net::FormatUrl(tab_content->GetURL(), languages)) | 97 if (tab_title == net::FormatUrl(web_contents->GetURL(), languages)) |
105 tab_title = GetSecurityOrigin(render_process_id, render_view_id); | 98 tab_title = GetSecurityOrigin(web_contents); |
106 } | 99 } |
107 | 100 |
108 return tab_title; | 101 return tab_title; |
109 } | 102 } |
110 | 103 |
104 void CountDevices(const content::MediaStreamDevices& devices, | |
105 int* num_audio, int* num_video, int* num_mirroring) { | |
106 for (content::MediaStreamDevices::const_iterator it = devices.begin(); | |
107 it != devices.end(); ++it) { | |
108 if (it->type == content::MEDIA_TAB_AUDIO_CAPTURE || | |
109 it->type == content::MEDIA_TAB_VIDEO_CAPTURE) { | |
110 ++(*num_mirroring); | |
111 } else if (content::IsAudioMediaType(it->type)) { | |
112 ++(*num_audio); | |
113 } else if (content::IsVideoMediaType(it->type)) { | |
114 ++(*num_video); | |
115 } else { | |
116 NOTIMPLEMENTED(); | |
117 } | |
118 } | |
119 } | |
120 | |
111 } // namespace | 121 } // namespace |
112 | 122 |
113 MediaStreamCaptureIndicator::TabEquals::TabEquals(WebContents* web_contents, | 123 // Stores usage counts for all the capture devices associated with a single |
114 int render_process_id, | 124 // WebContents instance. |
115 int render_view_id) | 125 class MediaStreamCaptureIndicator::CaptureDeviceUsage { |
116 : web_contents_(web_contents), | 126 public: |
117 render_process_id_(render_process_id), | 127 CaptureDeviceUsage(); |
118 render_view_id_(render_view_id) {} | |
119 | 128 |
120 bool MediaStreamCaptureIndicator::TabEquals::operator() ( | 129 bool IsCapturingAudio() const { return audio_ref_count_ > 0; } |
121 const MediaStreamCaptureIndicator::CaptureDeviceTab& tab) { | 130 bool IsCapturingVideo() const { return video_ref_count_ > 0; } |
122 return (web_contents_ == tab.web_contents || | 131 bool IsMirroring() const { return mirroring_ref_count_ > 0; } |
123 (render_process_id_ == tab.render_process_id && | 132 |
124 render_view_id_ == tab.render_view_id)); | 133 void TallyUsage(int num_audio, int num_video, int num_mirroring); |
134 | |
135 private: | |
136 int audio_ref_count_; | |
137 int video_ref_count_; | |
138 int mirroring_ref_count_; | |
139 }; | |
140 | |
141 MediaStreamCaptureIndicator::CaptureDeviceUsage::CaptureDeviceUsage() | |
142 : audio_ref_count_(0), video_ref_count_(0), mirroring_ref_count_(0) { | |
143 } | |
144 | |
145 void MediaStreamCaptureIndicator::CaptureDeviceUsage::TallyUsage( | |
146 int num_audio, int num_video, int num_mirroring) { | |
147 audio_ref_count_ += num_audio; | |
148 DCHECK_LE(0, audio_ref_count_); | |
149 video_ref_count_ += num_video; | |
150 DCHECK_LE(0, video_ref_count_); | |
151 mirroring_ref_count_ += num_mirroring; | |
152 DCHECK_LE(0, mirroring_ref_count_); | |
125 } | 153 } |
126 | 154 |
127 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() | 155 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() |
128 : status_icon_(NULL), | 156 : status_icon_(NULL), |
129 mic_image_(NULL), | 157 mic_image_(NULL), |
130 camera_image_(NULL), | 158 camera_image_(NULL), |
131 balloon_image_(NULL), | 159 balloon_image_(NULL), |
132 should_show_balloon_(false) { | 160 should_show_balloon_(false) { |
133 } | 161 } |
134 | 162 |
135 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { | 163 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { |
136 // The user is responsible for cleaning up by closing all the opened devices. | |
137 DCHECK(tabs_.empty()); | |
no longer working on chromium
2013/01/24 14:09:06
please add back DCHECKs for aliases_ and usage_map
miu
2013/01/28 19:36:52
Done. Originally, I thought this was an issue onl
| |
138 } | 164 } |
139 | 165 |
140 bool MediaStreamCaptureIndicator::IsCommandIdChecked( | 166 bool MediaStreamCaptureIndicator::IsCommandIdChecked( |
141 int command_id) const { | 167 int command_id) const { |
142 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; | 168 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; |
143 return false; | 169 return false; |
144 } | 170 } |
145 | 171 |
146 bool MediaStreamCaptureIndicator::IsCommandIdEnabled( | 172 bool MediaStreamCaptureIndicator::IsCommandIdEnabled( |
147 int command_id) const { | 173 int command_id) const { |
148 return command_id != IDC_MinimumLabelValue; | 174 return command_id != IDC_MinimumLabelValue; |
149 } | 175 } |
150 | 176 |
151 bool MediaStreamCaptureIndicator::GetAcceleratorForCommandId( | 177 bool MediaStreamCaptureIndicator::GetAcceleratorForCommandId( |
152 int command_id, ui::Accelerator* accelerator) { | 178 int command_id, ui::Accelerator* accelerator) { |
153 // No accelerators for status icon context menu. | 179 // No accelerators for status icon context menu. |
154 return false; | 180 return false; |
155 } | 181 } |
156 | 182 |
157 void MediaStreamCaptureIndicator::ExecuteCommand(int command_id) { | 183 void MediaStreamCaptureIndicator::ExecuteCommand(int command_id) { |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
159 DCHECK(command_id >= IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST && | 185 |
160 command_id <= IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_LAST); | 186 const int index = |
no longer working on chromium
2013/01/24 14:09:06
nit, you don't need const for int.
miu
2013/01/28 19:36:52
Ack. IMHO, it's a good habit to use const whereve
| |
161 int index = command_id - IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST; | 187 command_id - IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST; |
162 WebContents* web_content = tab_util::GetWebContentsByID( | 188 DCHECK_LE(0, index); |
163 tabs_[index].render_process_id, tabs_[index].render_view_id); | 189 DCHECK_GT(static_cast<int>(command_map_.size()), index); |
164 DCHECK(web_content); | 190 WebContents* const web_contents = command_map_[index]; |
165 if (!web_content) { | 191 if (!web_contents) { |
166 NOTREACHED(); | 192 NOTREACHED(); |
167 return; | 193 return; |
168 } | 194 } |
169 | 195 |
170 web_content->GetDelegate()->ActivateContents(web_content); | 196 web_contents->GetDelegate()->ActivateContents(web_contents); |
171 } | 197 } |
172 | 198 |
173 void MediaStreamCaptureIndicator::CaptureDevicesOpened( | 199 void MediaStreamCaptureIndicator::CaptureDevicesOpened( |
174 int render_process_id, | 200 int render_process_id, |
175 int render_view_id, | 201 int render_view_id, |
176 const content::MediaStreamDevices& devices) { | 202 const content::MediaStreamDevices& devices) { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
178 DCHECK(!devices.empty()); | 204 DCHECK(!devices.empty()); |
179 | 205 |
180 BrowserThread::PostTask( | 206 BrowserThread::PostTask( |
(...skipping 14 matching lines...) Expand all Loading... | |
195 base::Bind(&MediaStreamCaptureIndicator::DoDevicesClosedOnUIThread, | 221 base::Bind(&MediaStreamCaptureIndicator::DoDevicesClosedOnUIThread, |
196 this, render_process_id, render_view_id, devices)); | 222 this, render_process_id, render_view_id, devices)); |
197 } | 223 } |
198 | 224 |
199 void MediaStreamCaptureIndicator::DoDevicesOpenedOnUIThread( | 225 void MediaStreamCaptureIndicator::DoDevicesOpenedOnUIThread( |
200 int render_process_id, | 226 int render_process_id, |
201 int render_view_id, | 227 int render_view_id, |
202 const content::MediaStreamDevices& devices) { | 228 const content::MediaStreamDevices& devices) { |
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
204 | 230 |
205 CreateStatusTray(); | 231 AddCaptureDevices(render_process_id, render_view_id, devices); |
206 | |
207 AddCaptureDeviceTab(render_process_id, render_view_id, devices); | |
208 } | 232 } |
209 | 233 |
210 void MediaStreamCaptureIndicator::DoDevicesClosedOnUIThread( | 234 void MediaStreamCaptureIndicator::DoDevicesClosedOnUIThread( |
211 int render_process_id, | 235 int render_process_id, |
212 int render_view_id, | 236 int render_view_id, |
213 const content::MediaStreamDevices& devices) { | 237 const content::MediaStreamDevices& devices) { |
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
215 | 239 |
216 RemoveCaptureDeviceTab(render_process_id, render_view_id, devices); | 240 RemoveCaptureDevices(render_process_id, render_view_id, devices); |
217 } | 241 } |
218 | 242 |
219 void MediaStreamCaptureIndicator::CreateStatusTray() { | 243 void MediaStreamCaptureIndicator::MaybeCreateStatusTrayIcon() { |
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
221 if (status_icon_) | 245 if (status_icon_) |
222 return; | 246 return; |
223 | 247 |
224 // If there is no browser process, we should not create the status tray. | 248 // If there is no browser process, we should not create the status tray. |
225 if (!g_browser_process) | 249 if (!g_browser_process) |
226 return; | 250 return; |
227 | 251 |
228 StatusTray* status_tray = g_browser_process->status_tray(); | 252 StatusTray* status_tray = g_browser_process->status_tray(); |
229 if (!status_tray) | 253 if (!status_tray) |
(...skipping 17 matching lines...) Expand all Loading... | |
247 if (!balloon_image_) { | 271 if (!balloon_image_) { |
248 balloon_image_ = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 272 balloon_image_ = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
249 IDR_PRODUCT_LOGO_32); | 273 IDR_PRODUCT_LOGO_32); |
250 } | 274 } |
251 DCHECK(mic_image_); | 275 DCHECK(mic_image_); |
252 DCHECK(camera_image_); | 276 DCHECK(camera_image_); |
253 DCHECK(balloon_image_); | 277 DCHECK(balloon_image_); |
254 } | 278 } |
255 | 279 |
256 void MediaStreamCaptureIndicator::ShowBalloon( | 280 void MediaStreamCaptureIndicator::ShowBalloon( |
257 int render_process_id, | 281 WebContents* web_contents, bool audio, bool video) { |
258 int render_view_id, | |
259 bool audio, | |
260 bool video) { | |
261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
262 DCHECK(audio || video); | 283 DCHECK(audio || video); |
263 | 284 |
264 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO; | 285 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO; |
265 if (audio && !video) | 286 if (audio && !video) |
266 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_ONLY; | 287 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_ONLY; |
267 else if (!audio && video) | 288 else if (!audio && video) |
268 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY; | 289 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY; |
269 | 290 |
270 // Only show the balloon for extensions. | 291 // Only show the balloon for extensions. |
271 const extensions::Extension* extension = | 292 const extensions::Extension* const extension = GetExtension(web_contents); |
272 GetExtension(render_process_id, render_view_id); | |
273 if (!extension) { | 293 if (!extension) { |
274 DVLOG(1) << "Balloon is shown only for extensions"; | 294 DVLOG(1) << "Balloon is shown only for extensions"; |
275 return; | 295 return; |
276 } | 296 } |
277 | 297 |
278 string16 message = | 298 string16 message = |
279 l10n_util::GetStringFUTF16(message_id, | 299 l10n_util::GetStringFUTF16(message_id, |
280 UTF8ToUTF16(extension->name())); | 300 UTF8ToUTF16(extension->name())); |
281 | |
282 WebContents* web_contents = tab_util::GetWebContentsByID( | |
283 render_process_id, render_view_id); | |
284 | |
285 Profile* profile = | 301 Profile* profile = |
286 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 302 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
287 | 303 |
288 should_show_balloon_ = true; | 304 should_show_balloon_ = true; |
289 extensions::ImageLoader::Get(profile)->LoadImageAsync( | 305 extensions::ImageLoader::Get(profile)->LoadImageAsync( |
290 extension, | 306 extension, |
291 extension->GetIconResource(32, ExtensionIconSet::MATCH_BIGGER), | 307 extension->GetIconResource(32, ExtensionIconSet::MATCH_BIGGER), |
292 gfx::Size(32, 32), | 308 gfx::Size(32, 32), |
293 base::Bind(&MediaStreamCaptureIndicator::OnImageLoaded, | 309 base::Bind(&MediaStreamCaptureIndicator::OnImageLoaded, |
294 this, message)); | 310 this, message)); |
295 } | 311 } |
296 | 312 |
297 void MediaStreamCaptureIndicator::OnImageLoaded( | 313 void MediaStreamCaptureIndicator::OnImageLoaded( |
298 const string16& message, | 314 const string16& message, |
299 const gfx::Image& image) { | 315 const gfx::Image& image) { |
300 if (!should_show_balloon_) | 316 if (!should_show_balloon_ || !status_icon_) |
301 return; | 317 return; |
302 | 318 |
303 const gfx::ImageSkia* image_skia = !image.IsEmpty() ? image.ToImageSkia() : | 319 const gfx::ImageSkia* image_skia = !image.IsEmpty() ? image.ToImageSkia() : |
304 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 320 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
305 IDR_APP_DEFAULT_ICON); | 321 IDR_APP_DEFAULT_ICON); |
306 status_icon_->DisplayBalloon(*image_skia, string16(), message); | 322 status_icon_->DisplayBalloon(*image_skia, string16(), message); |
307 } | 323 } |
308 | 324 |
309 void MediaStreamCaptureIndicator::Hide() { | 325 void MediaStreamCaptureIndicator::MaybeDestroyStatusTrayIcon() { |
310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
311 | 327 |
312 // Make sure images that finish loading don't cause a balloon to be shown. | 328 // Make sure images that finish loading don't cause a balloon to be shown. |
313 should_show_balloon_ = false; | 329 should_show_balloon_ = false; |
314 | 330 |
315 if (!status_icon_) | 331 if (!status_icon_) |
316 return; | 332 return; |
317 | 333 |
318 // If there is no browser process, we should not do anything. | 334 // If there is no browser process, we should not do anything. |
319 if (!g_browser_process) | 335 if (!g_browser_process) |
320 return; | 336 return; |
321 | 337 |
322 StatusTray* status_tray = g_browser_process->status_tray(); | 338 StatusTray* status_tray = g_browser_process->status_tray(); |
323 if (status_tray != NULL) { | 339 if (status_tray != NULL) { |
324 status_tray->RemoveStatusIcon(status_icon_); | 340 status_tray->RemoveStatusIcon(status_icon_); |
325 status_icon_ = NULL; | 341 status_icon_ = NULL; |
326 } | 342 } |
327 } | 343 } |
328 | 344 |
329 void MediaStreamCaptureIndicator::UpdateStatusTrayIconContextMenu() { | 345 void MediaStreamCaptureIndicator::UpdateStatusTrayIconContextMenu() { |
330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
331 scoped_ptr<ui::SimpleMenuModel> menu(new ui::SimpleMenuModel(this)); | 347 scoped_ptr<ui::SimpleMenuModel> menu(new ui::SimpleMenuModel(this)); |
332 | 348 |
333 bool audio = false; | 349 bool audio = false; |
334 bool video = false; | 350 bool video = false; |
335 int command_id = IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST; | 351 int command_id = IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_FIRST; |
336 for (CaptureDeviceTabs::iterator iter = tabs_.begin(); | 352 command_map_.clear(); |
337 iter != tabs_.end();) { | 353 for (UsageMap::const_iterator iter = usage_map_.begin(); |
338 string16 tab_title = GetTitle(iter->render_process_id, | 354 iter != usage_map_.end(); ++iter) { |
339 iter->render_view_id); | 355 WebContents* const web_contents = iter->first; |
340 if (tab_title.empty()) { | 356 command_map_.push_back(web_contents); |
341 // Delete the entry since the tab has gone away. | 357 menu->AddItem(command_id, GetTitle(web_contents)); |
342 iter = tabs_.erase(iter); | |
343 continue; | |
344 } | |
345 | 358 |
346 // Check if any audio and video devices have been used. | 359 // Check if any audio and video devices have been used. |
347 audio = audio || iter->audio_ref_count > 0; | 360 const CaptureDeviceUsage& usage = *iter->second; |
348 video = video || iter->video_ref_count > 0; | 361 audio = audio || usage.IsCapturingAudio(); |
349 | 362 video = video || usage.IsCapturingVideo(); |
350 menu->AddItem(command_id, tab_title); | |
351 | 363 |
352 // If reaching the maximum number, no more item will be added to the menu. | 364 // If reaching the maximum number, no more item will be added to the menu. |
353 if (command_id == IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_LAST) | 365 if (command_id == IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_LIST_LAST) |
354 break; | 366 break; |
355 | |
356 ++command_id; | 367 ++command_id; |
357 ++iter; | |
358 } | 368 } |
359 | 369 |
360 if (!audio && !video) { | 370 if (!audio && !video) { |
361 Hide(); | 371 MaybeDestroyStatusTrayIcon(); |
362 return; | 372 return; |
363 } | 373 } |
364 | 374 |
365 // The icon will take the ownership of the passed context menu. | 375 // The icon will take the ownership of the passed context menu. |
366 status_icon_->SetContextMenu(menu.release()); | 376 MaybeCreateStatusTrayIcon(); |
367 UpdateStatusTrayIconDisplay(audio, video); | 377 if (status_icon_) { |
378 status_icon_->SetContextMenu(menu.release()); | |
379 UpdateStatusTrayIconDisplay(audio, video); | |
380 } | |
368 } | 381 } |
369 | 382 |
370 void MediaStreamCaptureIndicator::UpdateStatusTrayIconDisplay( | 383 void MediaStreamCaptureIndicator::UpdateStatusTrayIconDisplay( |
371 bool audio, bool video) { | 384 bool audio, bool video) { |
372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
373 DCHECK(audio || video); | 386 DCHECK(audio || video); |
387 DCHECK(status_icon_); | |
374 int message_id = 0; | 388 int message_id = 0; |
375 if (audio && video) { | 389 if (audio && video) { |
376 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_AUDIO_AND_VIDEO; | 390 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_AUDIO_AND_VIDEO; |
377 status_icon_->SetImage(*camera_image_); | 391 status_icon_->SetImage(*camera_image_); |
378 } else if (audio && !video) { | 392 } else if (audio && !video) { |
379 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_AUDIO_ONLY; | 393 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_AUDIO_ONLY; |
380 status_icon_->SetImage(*mic_image_); | 394 status_icon_->SetImage(*mic_image_); |
381 } else if (!audio && video) { | 395 } else if (!audio && video) { |
382 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_VIDEO_ONLY; | 396 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_TEXT_VIDEO_ONLY; |
383 status_icon_->SetImage(*camera_image_); | 397 status_icon_->SetImage(*camera_image_); |
384 } | 398 } |
385 | 399 |
386 status_icon_->SetToolTip(l10n_util::GetStringFUTF16( | 400 status_icon_->SetToolTip(l10n_util::GetStringFUTF16( |
387 message_id, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | 401 message_id, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); |
388 } | 402 } |
389 | 403 |
390 void MediaStreamCaptureIndicator::AddCaptureDeviceTab( | 404 content::WebContents* MediaStreamCaptureIndicator::LookUpByKnownAlias( |
405 int render_process_id, int render_view_id) const { | |
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
407 | |
408 WebContents* result = | |
409 tab_util::GetWebContentsByID(render_process_id, render_view_id); | |
410 if (!result) { | |
411 const RenderViewIDs key(render_process_id, render_view_id); | |
412 AliasMap::const_iterator it = aliases_.find(key); | |
413 if (it != aliases_.end()) | |
414 result = it->second; | |
415 } | |
416 return result; | |
417 } | |
418 | |
419 void MediaStreamCaptureIndicator::AddCaptureDevices( | |
391 int render_process_id, | 420 int render_process_id, |
392 int render_view_id, | 421 int render_view_id, |
393 const content::MediaStreamDevices& devices) { | 422 const content::MediaStreamDevices& devices) { |
394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
395 | 424 |
396 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, | 425 WebContents* const web_contents = |
397 render_view_id); | 426 LookUpByKnownAlias(render_process_id, render_view_id); |
398 if (!web_contents) | 427 if (!web_contents) |
399 return; | 428 return; |
400 | 429 |
401 CaptureDeviceTabs::iterator iter = std::find_if( | 430 // Increase the usage ref-counts. |
402 tabs_.begin(), tabs_.end(), TabEquals(web_contents, | 431 int num_audio = 0; |
403 render_process_id, | 432 int num_video = 0; |
404 render_view_id)); | 433 int num_mirroring = 0; |
405 if (iter == tabs_.end()) { | 434 CountDevices(devices, &num_audio, &num_video, &num_mirroring); |
justinlin
2013/01/24 07:04:44
Could the CountDevices function just be moved into
no longer working on chromium
2013/01/24 14:09:06
+1
miu
2013/01/28 19:36:52
Done. Good call! :)
BTW--It seemed cleanest to
| |
406 tabs_.push_back(CaptureDeviceTab(web_contents, | 435 DCHECK(num_audio > 0 || num_video > 0 || num_mirroring > 0); |
407 render_process_id, | 436 CaptureDeviceUsage*& usage = usage_map_[web_contents]; |
408 render_view_id)); | 437 if (!usage) |
409 iter = tabs_.end() - 1; | 438 usage = new CaptureDeviceUsage(); |
410 } | 439 usage->TallyUsage(num_audio, num_video, num_mirroring); |
411 | 440 |
412 bool audio = false; | 441 // Keep track of the IDs as a known alias to the WebContents instance. |
413 bool video = false; | 442 const AliasMap::iterator insert_it = aliases_.insert( |
414 bool tab_capture = false; | 443 make_pair(RenderViewIDs(render_process_id, render_view_id), |
415 content::MediaStreamDevices::const_iterator dev = devices.begin(); | 444 web_contents)).first; |
416 for (; dev != devices.end(); ++dev) { | 445 DCHECK_EQ(web_contents, insert_it->second) |
417 if (dev->type == content::MEDIA_TAB_AUDIO_CAPTURE || | 446 << "BUG: IDs refer to two different WebContents instances."; |
418 dev->type == content::MEDIA_TAB_VIDEO_CAPTURE) { | |
419 ++iter->tab_capture_ref_count; | |
420 tab_capture = true; | |
421 } else if (content::IsAudioMediaType(dev->type)) { | |
422 ++iter->audio_ref_count; | |
423 audio = true; | |
424 } else if (content::IsVideoMediaType(dev->type)) { | |
425 ++iter->video_ref_count; | |
426 video = true; | |
427 } else { | |
428 NOTIMPLEMENTED(); | |
429 } | |
430 } | |
431 | 447 |
432 DCHECK(web_contents); | |
433 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | 448 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
434 | 449 |
435 // Don't use desktop notifications for tab capture. We use a favicon | 450 // Don't use desktop notifications for tab capture. We use a favicon |
436 // glow notification instead. | 451 // glow notification instead. |
437 if (!status_icon_ || tab_capture) | 452 if (num_mirroring > 0 && num_audio == 0 && num_video == 0) |
438 return; | 453 return; |
439 | 454 |
440 UpdateStatusTrayIconContextMenu(); | 455 UpdateStatusTrayIconContextMenu(); |
441 ShowBalloon(render_process_id, render_view_id, audio, video); | 456 ShowBalloon(web_contents, num_audio > 0, num_video > 0); |
442 } | 457 } |
443 | 458 |
444 void MediaStreamCaptureIndicator::RemoveCaptureDeviceTab( | 459 void MediaStreamCaptureIndicator::RemoveCaptureDevices( |
445 int render_process_id, | 460 int render_process_id, |
446 int render_view_id, | 461 int render_view_id, |
447 const content::MediaStreamDevices& devices) { | 462 const content::MediaStreamDevices& devices) { |
448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
449 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, | |
450 render_view_id); | |
451 CaptureDeviceTabs::iterator iter = std::find_if( | |
452 tabs_.begin(), tabs_.end(), TabEquals(web_contents, | |
453 render_process_id, | |
454 render_view_id)); | |
455 | 464 |
456 if (iter != tabs_.end()) { | 465 WebContents* const web_contents = |
457 content::MediaStreamDevices::const_iterator dev = devices.begin(); | 466 LookUpByKnownAlias(render_process_id, render_view_id); |
458 for (; dev != devices.end(); ++dev) { | 467 if (!web_contents) |
459 if (dev->type == content::MEDIA_TAB_AUDIO_CAPTURE || | 468 return; |
460 dev->type == content::MEDIA_TAB_VIDEO_CAPTURE) { | 469 |
461 --iter->tab_capture_ref_count; | 470 // Decrease the usage ref-counts. |
462 } else if (content::IsAudioMediaType(dev->type)) { | 471 int num_audio = 0; |
463 --iter->audio_ref_count; | 472 int num_video = 0; |
464 } else if (content::IsVideoMediaType(dev->type)) { | 473 int num_mirroring = 0; |
465 --iter->video_ref_count; | 474 CountDevices(devices, &num_audio, &num_video, &num_mirroring); |
475 CaptureDeviceUsage* const usage = usage_map_[web_contents]; | |
476 DCHECK(usage); | |
477 usage->TallyUsage(-num_audio, -num_video, -num_mirroring); | |
478 | |
479 // Remove the usage and alias mappings if all the devices have been closed. | |
480 if (!usage->IsCapturingAudio() && !usage->IsCapturingVideo() && | |
481 !usage->IsMirroring()) { | |
482 for (AliasMap::iterator alias_it = aliases_.begin(); | |
483 alias_it != aliases_.end(); ) { | |
484 if (alias_it->second == web_contents) { | |
485 aliases_.erase(alias_it++); | |
466 } else { | 486 } else { |
467 NOTIMPLEMENTED(); | 487 ++alias_it; |
468 } | 488 } |
469 | |
470 DCHECK_GE(iter->audio_ref_count, 0); | |
471 DCHECK_GE(iter->video_ref_count, 0); | |
472 } | 489 } |
473 | 490 delete usage; |
474 // Remove the tab if all the devices have been closed. | 491 usage_map_.erase(web_contents); |
475 if (iter->audio_ref_count == 0 && iter->video_ref_count == 0 && | |
476 iter->tab_capture_ref_count == 0) | |
477 tabs_.erase(iter); | |
478 } | 492 } |
479 | 493 |
480 if (web_contents) | 494 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
no longer working on chromium
2013/01/24 14:09:06
what if the page has gone and the web_contents is
no longer working on chromium
2013/01/24 14:45:55
Note, you should do the test on Mac or windows 7,
miu
2013/01/28 19:36:52
With all my testing, I never saw this happen. How
miu
2013/01/28 19:36:52
Actually, gprecise has the system tray and it was
| |
481 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | |
482 | |
483 if (!status_icon_) | |
484 return; | |
485 | 495 |
486 UpdateStatusTrayIconContextMenu(); | 496 UpdateStatusTrayIconContextMenu(); |
487 } | 497 } |
488 | 498 |
489 bool MediaStreamCaptureIndicator::IsProcessCapturing(int render_process_id, | 499 bool MediaStreamCaptureIndicator::IsCapturingUserMedia( |
490 int render_view_id) const { | 500 int render_process_id, int render_view_id) const { |
491 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
492 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, | 502 |
493 render_view_id); | 503 WebContents* const web_contents = |
504 LookUpByKnownAlias(render_process_id, render_view_id); | |
494 if (!web_contents) | 505 if (!web_contents) |
495 return false; | 506 return false; |
496 | 507 |
497 CaptureDeviceTabs::const_iterator iter = std::find_if( | 508 UsageMap::const_iterator it = usage_map_.find(web_contents); |
498 tabs_.begin(), tabs_.end(), TabEquals(web_contents, | 509 return (it != usage_map_.end() && |
499 render_process_id, | 510 (it->second->IsCapturingAudio() || it->second->IsCapturingVideo())); |
500 render_view_id)); | |
501 if (iter == tabs_.end()) | |
502 return false; | |
503 return (iter->audio_ref_count > 0 || iter->video_ref_count > 0); | |
504 } | 511 } |
505 | 512 |
506 bool MediaStreamCaptureIndicator::IsProcessCapturingTab( | 513 bool MediaStreamCaptureIndicator::IsSupplyingMirroredMedia( |
507 int render_process_id, int render_view_id) const { | 514 int render_process_id, int render_view_id) const { |
508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
509 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, | 516 |
510 render_view_id); | 517 WebContents* const web_contents = |
518 LookUpByKnownAlias(render_process_id, render_view_id); | |
511 if (!web_contents) | 519 if (!web_contents) |
512 return false; | 520 return false; |
513 | 521 |
514 CaptureDeviceTabs::const_iterator iter = std::find_if( | 522 UsageMap::const_iterator it = usage_map_.find(web_contents); |
515 tabs_.begin(), tabs_.end(), TabEquals(web_contents, | 523 return it != usage_map_.end() && it->second->IsMirroring(); |
516 render_process_id, | |
517 render_view_id)); | |
518 if (iter == tabs_.end()) | |
519 return false; | |
520 return (iter->tab_capture_ref_count > 0); | |
521 } | 524 } |
OLD | NEW |