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