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/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/status_icons/status_icon.h" | 14 #include "chrome/browser/status_icons/status_icon.h" |
13 #include "chrome/browser/status_icons/status_tray.h" | 15 #include "chrome/browser/status_icons/status_tray.h" |
14 #include "chrome/browser/tab_contents/tab_util.h" | 16 #include "chrome/browser/tab_contents/tab_util.h" |
15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
17 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
18 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
19 #include "content/public/browser/web_contents_delegate.h" | 21 #include "content/public/browser/web_contents_delegate.h" |
20 #include "grit/chromium_strings.h" | 22 #include "grit/chromium_strings.h" |
21 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
22 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
23 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
24 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
25 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
26 | 28 |
27 using content::BrowserThread; | 29 using content::BrowserThread; |
28 using content::WebContents; | 30 using content::WebContents; |
29 | 31 |
| 32 namespace { |
| 33 |
| 34 const extensions::Extension* GetExtension(int render_process_id, |
| 35 int render_view_id) { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 37 |
| 38 WebContents* web_contents = tab_util::GetWebContentsByID( |
| 39 render_process_id, render_view_id); |
| 40 if (!web_contents) |
| 41 return NULL; |
| 42 |
| 43 Profile* profile = |
| 44 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 45 if (!profile) |
| 46 return NULL; |
| 47 |
| 48 ExtensionService* extension_service = profile->GetExtensionService(); |
| 49 if (!extension_service) |
| 50 return NULL; |
| 51 |
| 52 return extension_service->extensions()->GetExtensionOrAppByURL( |
| 53 ExtensionURLInfo(web_contents->GetURL())); |
| 54 } |
| 55 |
| 56 // Gets the security originator of the tab. It returns a string with no '/' |
| 57 // at the end to display in the UI. |
| 58 string16 GetSecurityOrigin(int render_process_id, int render_view_id) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 WebContents* tab_content = tab_util::GetWebContentsByID( |
| 61 render_process_id, render_view_id); |
| 62 if (!tab_content) |
| 63 return string16(); |
| 64 |
| 65 std::string security_origin = tab_content->GetURL().GetOrigin().spec(); |
| 66 |
| 67 // Remove the last character if it is a '/'. |
| 68 if (!security_origin.empty()) { |
| 69 std::string::iterator it = security_origin.end() - 1; |
| 70 if (*it == '/') |
| 71 security_origin.erase(it); |
| 72 } |
| 73 |
| 74 return UTF8ToUTF16(security_origin); |
| 75 } |
| 76 |
| 77 string16 GetTitle(int render_process_id, int render_view_id) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 79 |
| 80 const extensions::Extension* extension = |
| 81 GetExtension(render_process_id, render_view_id); |
| 82 if (extension) |
| 83 return UTF8ToUTF16(extension->name()); |
| 84 |
| 85 WebContents* tab_content = tab_util::GetWebContentsByID( |
| 86 render_process_id, render_view_id); |
| 87 if (!tab_content) |
| 88 return string16(); |
| 89 |
| 90 string16 tab_title = tab_content->GetTitle(); |
| 91 |
| 92 if (tab_title.empty()) { |
| 93 // If the page's title is empty use its security originator. |
| 94 tab_title = GetSecurityOrigin(render_process_id, render_view_id); |
| 95 } else { |
| 96 // If the page's title matches its URL, use its security originator. |
| 97 std::string languages = |
| 98 content::GetContentClient()->browser()->GetAcceptLangs( |
| 99 tab_content->GetBrowserContext()); |
| 100 if (tab_title == net::FormatUrl(tab_content->GetURL(), languages)) |
| 101 tab_title = GetSecurityOrigin(render_process_id, render_view_id); |
| 102 } |
| 103 |
| 104 return tab_title; |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
30 MediaStreamCaptureIndicator::TabEquals::TabEquals(int render_process_id, | 109 MediaStreamCaptureIndicator::TabEquals::TabEquals(int render_process_id, |
31 int render_view_id) | 110 int render_view_id) |
32 : render_process_id_(render_process_id), | 111 : render_process_id_(render_process_id), |
33 render_view_id_(render_view_id) {} | 112 render_view_id_(render_view_id) {} |
34 | 113 |
35 bool MediaStreamCaptureIndicator::TabEquals::operator() ( | 114 bool MediaStreamCaptureIndicator::TabEquals::operator() ( |
36 const MediaStreamCaptureIndicator::CaptureDeviceTab& tab) { | 115 const MediaStreamCaptureIndicator::CaptureDeviceTab& tab) { |
37 return (render_process_id_ == tab.render_process_id && | 116 return (render_process_id_ == tab.render_process_id && |
38 render_view_id_ == tab.render_view_id); | 117 render_view_id_ == tab.render_view_id); |
39 } | 118 } |
40 | 119 |
41 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() | 120 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() |
42 : status_icon_(NULL), | 121 : status_icon_(NULL), |
43 mic_image_(NULL), | 122 mic_image_(NULL), |
44 camera_image_(NULL), | 123 camera_image_(NULL), |
45 balloon_image_(NULL) { | 124 balloon_image_(NULL), |
| 125 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
| 126 request_index_(0) { |
46 } | 127 } |
47 | 128 |
48 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { | 129 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { |
49 // The user is responsible for cleaning up by closing all the opened devices. | 130 // The user is responsible for cleaning up by closing all the opened devices. |
50 DCHECK(tabs_.empty()); | 131 DCHECK(tabs_.empty()); |
51 } | 132 } |
52 | 133 |
53 bool MediaStreamCaptureIndicator::IsCommandIdChecked( | 134 bool MediaStreamCaptureIndicator::IsCommandIdChecked( |
54 int command_id) const { | 135 int command_id) const { |
55 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; | 136 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 252 } |
172 DCHECK(mic_image_); | 253 DCHECK(mic_image_); |
173 DCHECK(camera_image_); | 254 DCHECK(camera_image_); |
174 DCHECK(balloon_image_); | 255 DCHECK(balloon_image_); |
175 } | 256 } |
176 | 257 |
177 void MediaStreamCaptureIndicator::ShowBalloon( | 258 void MediaStreamCaptureIndicator::ShowBalloon( |
178 int render_process_id, | 259 int render_process_id, |
179 int render_view_id, | 260 int render_view_id, |
180 bool audio, | 261 bool audio, |
181 bool video) const { | 262 bool video) { |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
183 DCHECK(audio || video); | 264 DCHECK(audio || video); |
184 string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | |
185 | 265 |
186 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO; | 266 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO; |
187 if (audio && !video) | 267 if (audio && !video) |
188 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_ONLY; | 268 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_ONLY; |
189 else if (!audio && video) | 269 else if (!audio && video) |
190 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY; | 270 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY; |
191 | 271 |
192 string16 body = l10n_util::GetStringFUTF16( | 272 const extensions::Extension* extension = |
193 message_id, GetSecurityOrigin(render_process_id, render_view_id)); | 273 GetExtension(render_process_id, render_view_id); |
| 274 if (extension) { |
| 275 pending_messages_[request_index_++] = |
| 276 l10n_util::GetStringFUTF16(message_id, |
| 277 UTF8ToUTF16(extension->name())); |
| 278 tracker_.LoadImage( |
| 279 extension, |
| 280 extension->GetIconResource(32, ExtensionIconSet::MATCH_BIGGER), |
| 281 gfx::Size(32, 32), |
| 282 ImageLoadingTracker::CACHE); |
| 283 return; |
| 284 } |
194 | 285 |
| 286 string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 287 string16 body = l10n_util::GetStringFUTF16(message_id, |
| 288 GetSecurityOrigin(render_process_id, render_view_id)); |
195 status_icon_->DisplayBalloon(*balloon_image_, title, body); | 289 status_icon_->DisplayBalloon(*balloon_image_, title, body); |
196 } | 290 } |
197 | 291 |
| 292 void MediaStreamCaptureIndicator::OnImageLoaded( |
| 293 const gfx::Image& image, |
| 294 const std::string& extension_id, |
| 295 int index) { |
| 296 string16 message; |
| 297 message.swap(pending_messages_[index]); |
| 298 pending_messages_.erase(index); |
| 299 |
| 300 status_icon_->DisplayBalloon( |
| 301 !image.IsEmpty() ? *image.ToImageSkia() : |
| 302 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 303 IDR_APP_DEFAULT_ICON), |
| 304 string16(), |
| 305 message); |
| 306 } |
| 307 |
198 void MediaStreamCaptureIndicator::Hide() { | 308 void MediaStreamCaptureIndicator::Hide() { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
200 if (!status_icon_) | 310 if (!status_icon_) |
201 return; | 311 return; |
202 | 312 |
203 // If there is no browser process, we should not do anything. | 313 // If there is no browser process, we should not do anything. |
204 if (!g_browser_process) | 314 if (!g_browser_process) |
205 return; | 315 return; |
206 | 316 |
207 StatusTray* status_tray = g_browser_process->status_tray(); | 317 StatusTray* status_tray = g_browser_process->status_tray(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // Remove the tab if all the devices have been closed. | 438 // Remove the tab if all the devices have been closed. |
329 if (iter->audio_ref_count == 0 && iter->video_ref_count == 0) | 439 if (iter->audio_ref_count == 0 && iter->video_ref_count == 0) |
330 tabs_.erase(iter); | 440 tabs_.erase(iter); |
331 | 441 |
332 if (tabs_.empty()) | 442 if (tabs_.empty()) |
333 Hide(); | 443 Hide(); |
334 else | 444 else |
335 UpdateStatusTrayIconContextMenu(); | 445 UpdateStatusTrayIconContextMenu(); |
336 } | 446 } |
337 | 447 |
338 string16 MediaStreamCaptureIndicator::GetTitle(int render_process_id, | |
339 int render_view_id) const { | |
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
341 WebContents* tab_content = tab_util::GetWebContentsByID( | |
342 render_process_id, render_view_id); | |
343 if (!tab_content) | |
344 return string16(); | |
345 | |
346 string16 tab_title = tab_content->GetTitle(); | |
347 | |
348 if (tab_title.empty()) { | |
349 // If the page's title is empty use its security originator. | |
350 tab_title = GetSecurityOrigin(render_process_id, render_view_id); | |
351 } else { | |
352 // If the page's title matches its URL, use its security originator. | |
353 std::string languages = | |
354 content::GetContentClient()->browser()->GetAcceptLangs( | |
355 tab_content->GetBrowserContext()); | |
356 if (tab_title == net::FormatUrl(tab_content->GetURL(), languages)) | |
357 tab_title = GetSecurityOrigin(render_process_id, render_view_id); | |
358 } | |
359 | |
360 return tab_title; | |
361 } | |
362 | |
363 string16 MediaStreamCaptureIndicator::GetSecurityOrigin( | |
364 int render_process_id, int render_view_id) const { | |
365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
366 WebContents* tab_content = tab_util::GetWebContentsByID( | |
367 render_process_id, render_view_id); | |
368 if (!tab_content) | |
369 return string16(); | |
370 | |
371 std::string security_origin = tab_content->GetURL().GetOrigin().spec(); | |
372 | |
373 // Remove the last character if it is a '/'. | |
374 if (!security_origin.empty()) { | |
375 std::string::iterator it = security_origin.end() - 1; | |
376 if (*it == '/') | |
377 security_origin.erase(it); | |
378 } | |
379 | |
380 return UTF8ToUTF16(security_origin); | |
381 } | |
OLD | NEW |