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/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_system.h" |
| 15 #include "chrome/browser/extensions/image_loader.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/status_icons/status_icon.h" | 17 #include "chrome/browser/status_icons/status_icon.h" |
16 #include "chrome/browser/status_icons/status_tray.h" | 18 #include "chrome/browser/status_icons/status_tray.h" |
17 #include "chrome/browser/tab_contents/tab_util.h" | 19 #include "chrome/browser/tab_contents/tab_util.h" |
18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/content_browser_client.h" | 21 #include "content/public/browser/content_browser_client.h" |
20 #include "content/public/browser/invalidate_type.h" | 22 #include "content/public/browser/invalidate_type.h" |
21 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
22 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
23 #include "content/public/browser/web_contents_delegate.h" | 25 #include "content/public/browser/web_contents_delegate.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 const MediaStreamCaptureIndicator::CaptureDeviceTab& tab) { | 120 const MediaStreamCaptureIndicator::CaptureDeviceTab& tab) { |
119 return (render_process_id_ == tab.render_process_id && | 121 return (render_process_id_ == tab.render_process_id && |
120 render_view_id_ == tab.render_view_id); | 122 render_view_id_ == tab.render_view_id); |
121 } | 123 } |
122 | 124 |
123 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() | 125 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() |
124 : status_icon_(NULL), | 126 : status_icon_(NULL), |
125 mic_image_(NULL), | 127 mic_image_(NULL), |
126 camera_image_(NULL), | 128 camera_image_(NULL), |
127 balloon_image_(NULL), | 129 balloon_image_(NULL), |
128 request_index_(0), | 130 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
129 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 131 icon_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
130 } | 132 } |
131 | 133 |
132 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { | 134 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { |
133 // The user is responsible for cleaning up by closing all the opened devices. | 135 // The user is responsible for cleaning up by closing all the opened devices. |
134 DCHECK(tabs_.empty()); | 136 DCHECK(tabs_.empty()); |
135 } | 137 } |
136 | 138 |
137 bool MediaStreamCaptureIndicator::IsCommandIdChecked( | 139 bool MediaStreamCaptureIndicator::IsCommandIdChecked( |
138 int command_id) const { | 140 int command_id) const { |
139 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; | 141 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 if (!g_browser_process) | 226 if (!g_browser_process) |
225 return; | 227 return; |
226 | 228 |
227 StatusTray* status_tray = g_browser_process->status_tray(); | 229 StatusTray* status_tray = g_browser_process->status_tray(); |
228 if (!status_tray) | 230 if (!status_tray) |
229 return; | 231 return; |
230 | 232 |
231 status_icon_ = status_tray->CreateStatusIcon(); | 233 status_icon_ = status_tray->CreateStatusIcon(); |
232 | 234 |
233 EnsureStatusTrayIconResources(); | 235 EnsureStatusTrayIconResources(); |
234 EnsureImageLoadingTracker(); | |
235 } | 236 } |
236 | 237 |
237 void MediaStreamCaptureIndicator::EnsureStatusTrayIconResources() { | 238 void MediaStreamCaptureIndicator::EnsureStatusTrayIconResources() { |
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 if (!mic_image_) { | 240 if (!mic_image_) { |
240 mic_image_ = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 241 mic_image_ = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
241 IDR_INFOBAR_MEDIA_STREAM_MIC); | 242 IDR_INFOBAR_MEDIA_STREAM_MIC); |
242 } | 243 } |
243 if (!camera_image_) { | 244 if (!camera_image_) { |
244 camera_image_ = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 245 camera_image_ = ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
(...skipping 18 matching lines...) Expand all Loading... |
263 | 264 |
264 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO; | 265 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO; |
265 if (audio && !video) | 266 if (audio && !video) |
266 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_ONLY; | 267 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_ONLY; |
267 else if (!audio && video) | 268 else if (!audio && video) |
268 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY; | 269 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY; |
269 | 270 |
270 const extensions::Extension* extension = | 271 const extensions::Extension* extension = |
271 GetExtension(render_process_id, render_view_id); | 272 GetExtension(render_process_id, render_view_id); |
272 if (extension) { | 273 if (extension) { |
273 pending_messages_[request_index_++] = | 274 string16 message = |
274 l10n_util::GetStringFUTF16(message_id, | 275 l10n_util::GetStringFUTF16(message_id, |
275 UTF8ToUTF16(extension->name())); | 276 UTF8ToUTF16(extension->name())); |
276 tracker_->LoadImage( | 277 |
| 278 WebContents* web_contents = tab_util::GetWebContentsByID( |
| 279 render_process_id, render_view_id); |
| 280 |
| 281 Profile* profile = |
| 282 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 283 |
| 284 extensions::ExtensionSystem::Get(profile)->image_loader()->LoadImageAsync( |
277 extension, | 285 extension, |
278 extension->GetIconResource(32, ExtensionIconSet::MATCH_BIGGER), | 286 extension->GetIconResource(32, ExtensionIconSet::MATCH_BIGGER), |
279 gfx::Size(32, 32), | 287 gfx::Size(32, 32), |
280 ImageLoadingTracker::CACHE); | 288 base::Bind(&MediaStreamCaptureIndicator::OnImageLoaded, |
| 289 icon_tracker_.GetWeakPtr(), message)); |
281 return; | 290 return; |
282 } | 291 } |
283 | 292 |
284 string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | 293 string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
285 string16 body = l10n_util::GetStringFUTF16(message_id, | 294 string16 body = l10n_util::GetStringFUTF16(message_id, |
286 GetSecurityOrigin(render_process_id, render_view_id)); | 295 GetSecurityOrigin(render_process_id, render_view_id)); |
287 status_icon_->DisplayBalloon(*balloon_image_, title, body); | 296 status_icon_->DisplayBalloon(*balloon_image_, title, body); |
288 } | 297 } |
289 | 298 |
290 void MediaStreamCaptureIndicator::OnImageLoaded( | 299 void MediaStreamCaptureIndicator::OnImageLoaded( |
291 const gfx::Image& image, | 300 const string16& message, |
292 const std::string& extension_id, | 301 const gfx::Image& image) { |
293 int index) { | |
294 string16 message; | |
295 message.swap(pending_messages_[index]); | |
296 pending_messages_.erase(index); | |
297 | |
298 const gfx::ImageSkia* image_skia = !image.IsEmpty() ? image.ToImageSkia() : | 302 const gfx::ImageSkia* image_skia = !image.IsEmpty() ? image.ToImageSkia() : |
299 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 303 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
300 IDR_APP_DEFAULT_ICON); | 304 IDR_APP_DEFAULT_ICON); |
301 status_icon_->DisplayBalloon(*image_skia, string16(), message); | 305 status_icon_->DisplayBalloon(*image_skia, string16(), message); |
302 } | 306 } |
303 | 307 |
304 void MediaStreamCaptureIndicator::Hide() { | 308 void MediaStreamCaptureIndicator::Hide() { |
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
306 DCHECK(tabs_.empty()); | 310 DCHECK(tabs_.empty()); |
307 | 311 |
308 // We have to destroy |tracker_| on the UI thread. | 312 // Make sure no more image loaded messages get delivered. |
309 tracker_.reset(); | 313 icon_tracker_.InvalidateWeakPtrs(); |
310 | 314 |
311 if (!status_icon_) | 315 if (!status_icon_) |
312 return; | 316 return; |
313 | 317 |
314 // If there is no browser process, we should not do anything. | 318 // If there is no browser process, we should not do anything. |
315 if (!g_browser_process) | 319 if (!g_browser_process) |
316 return; | 320 return; |
317 | 321 |
318 StatusTray* status_tray = g_browser_process->status_tray(); | 322 StatusTray* status_tray = g_browser_process->status_tray(); |
319 if (status_tray != NULL) { | 323 if (status_tray != NULL) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 | 464 |
461 bool MediaStreamCaptureIndicator::IsProcessCapturing(int render_process_id, | 465 bool MediaStreamCaptureIndicator::IsProcessCapturing(int render_process_id, |
462 int render_view_id) const { | 466 int render_view_id) const { |
463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
464 CaptureDeviceTabs::const_iterator iter = std::find_if( | 468 CaptureDeviceTabs::const_iterator iter = std::find_if( |
465 tabs_.begin(), tabs_.end(), TabEquals(render_process_id, render_view_id)); | 469 tabs_.begin(), tabs_.end(), TabEquals(render_process_id, render_view_id)); |
466 if (iter == tabs_.end()) | 470 if (iter == tabs_.end()) |
467 return false; | 471 return false; |
468 return (iter->audio_ref_count > 0 || iter->video_ref_count > 0); | 472 return (iter->audio_ref_count > 0 || iter->video_ref_count > 0); |
469 } | 473 } |
470 | |
471 void MediaStreamCaptureIndicator::EnsureImageLoadingTracker() { | |
472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
473 if (tracker_.get()) | |
474 return; | |
475 | |
476 tracker_.reset(new ImageLoadingTracker(this)); | |
477 pending_messages_.clear(); | |
478 request_index_ = 0; | |
479 } | |
OLD | NEW |