OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/media/media_stream_capture_indicator.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/app/chrome_command_ids.h" | |
9 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/browser_shutdown.h" | |
11 #include "chrome/browser/status_icons/status_icon.h" | |
12 #include "chrome/browser/status_icons/status_tray.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "grit/chromium_strings.h" | |
15 #include "grit/generated_resources.h" | |
16 #include "grit/theme_resources.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 #include "ui/base/resource/resource_bundle.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 MediaStreamCaptureIndicator::MediaStreamCaptureIndicator() | |
23 : status_icon_(NULL) { | |
24 // We should not start up if there is no browser process or if we are | |
25 // currently quitting. | |
26 if (!g_browser_process || browser_shutdown::IsTryingToQuit()) | |
27 return; | |
tommi (sloooow) - chröme
2012/04/24 11:36:18
uhm... this doesn't make any sense :)
no longer working on chromium
2012/04/25 13:52:51
Done.
| |
28 } | |
29 | |
30 MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() { | |
31 Hide(); | |
32 } | |
33 | |
34 bool MediaStreamCaptureIndicator::IsCommandIdChecked( | |
35 int command_id) const { | |
36 NOTIMPLEMENTED() << "There are no checked items in the MediaStream menu."; | |
37 return false; | |
38 } | |
39 | |
40 bool MediaStreamCaptureIndicator::IsCommandIdEnabled( | |
41 int command_id) const { | |
42 return command_id != IDC_MinimumLabelValue; | |
43 } | |
44 | |
45 bool MediaStreamCaptureIndicator::GetAcceleratorForCommandId( | |
46 int command_id, ui::Accelerator* accelerator) { | |
47 // No accelerators for status icon context menu. | |
48 return false; | |
49 } | |
50 | |
51 void MediaStreamCaptureIndicator::ExecuteCommand(int command_id) { | |
52 // TODO(xians) : Implement all the following execute command function. | |
53 switch (command_id) { | |
54 case IDC_MEDIA_STREAM_DEVICE_STATUS_TRAY: | |
55 break; | |
56 case IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_AUDIO: | |
57 break; | |
58 case IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_VIDEO: | |
59 break; | |
60 case IDC_EXIT: | |
61 Hide(); | |
62 break; | |
63 default: | |
64 break; | |
tommi (sloooow) - chröme
2012/04/24 11:36:18
NOTREACHED()? or at least DLOG(ERROR)?
no longer working on chromium
2012/04/25 13:52:51
Done.
| |
65 } | |
66 } | |
67 | |
68 void MediaStreamCaptureIndicator::OnCaptureDevicesOpened( | |
69 const std::string& url, | |
70 const content::MediaStreamDevices& devices) { | |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
72 DCHECK(!url.empty() && !devices.empty()); | |
tommi (sloooow) - chröme
2012/04/24 11:36:18
nit: separate DCHECKs so that it's clear which one
no longer working on chromium
2012/04/25 13:52:51
Done.
| |
73 if (!g_browser_process) | |
74 return; | |
75 | |
76 CreateStatusTray(); | |
77 | |
78 // If we don't have a status icon or one could not be created successfully, | |
79 // then no need to continue. | |
80 if (!status_icon_) | |
81 return; | |
82 | |
83 AddCaptureDeviceUser(url, devices); | |
84 | |
85 ShowBalloon(url, devices); | |
86 } | |
87 | |
88 void MediaStreamCaptureIndicator::OnCaptureDevicesClosed( | |
89 const std::string& url, | |
90 const content::MediaStreamDevices& devices) { | |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
92 DCHECK(!url.empty() && !devices.empty()); | |
tommi (sloooow) - chröme
2012/04/24 11:36:18
separate dchecks
no longer working on chromium
2012/04/25 13:52:51
Done.
| |
93 if (!g_browser_process) | |
94 return; | |
95 | |
96 if (!status_icon_) | |
97 return; | |
98 | |
99 DCHECK(!users_.empty()); | |
100 RemoveCaptureDeviceUser(url, devices); | |
101 | |
102 if (users_.empty()) | |
103 Hide(); | |
104 } | |
105 | |
106 void MediaStreamCaptureIndicator::CreateStatusTray() { | |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
108 if (status_icon_) | |
109 return; | |
110 | |
111 StatusTray* status_tray = g_browser_process->status_tray(); | |
112 if (!status_tray) { | |
113 LOG(WARNING) << "This platform doesn't support notification icons"; | |
114 return; | |
115 } | |
116 | |
117 status_icon_ = status_tray->CreateStatusIcon(); | |
118 DCHECK(status_icon_); | |
tommi (sloooow) - chröme
2012/04/24 11:36:18
this DCHECK isn't necessary since you deref the po
no longer working on chromium
2012/04/25 13:52:51
Done.
| |
119 | |
120 status_icon_->SetToolTip(l10n_util::GetStringUTF16( | |
121 IDS_MEDIA_STREAM_STATUS_TRAY_TOOLTIP)); | |
122 | |
123 EnsureStatusTrayIcon(); | |
124 DCHECK(!icon_image_.empty()); | |
125 | |
126 status_icon_->SetImage(icon_image_); | |
127 } | |
128 | |
129 void MediaStreamCaptureIndicator::EnsureStatusTrayIcon() { | |
130 if (icon_image_.empty()) { | |
131 icon_image_ = *ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
132 IDR_MEDIA_STREAM_CAPTURE_LED); | |
133 } | |
134 } | |
135 | |
136 void MediaStreamCaptureIndicator::ShowBalloon( | |
137 const std::string& url, | |
138 const content::MediaStreamDevices& devices) { | |
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
140 DCHECK(status_icon_); | |
141 DCHECK(!icon_image_.empty()); | |
142 | |
143 string16 title = l10n_util::GetStringUTF16( | |
144 IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_TITLE); | |
145 | |
146 string16 message= l10n_util::GetStringFUTF16( | |
147 IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY, ASCIIToUTF16(url)); | |
148 | |
149 status_icon_->DisplayBalloon(icon_image_, title, message); | |
150 } | |
151 | |
152 void MediaStreamCaptureIndicator::Hide() { | |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
154 if (!status_icon_) | |
155 return; | |
156 | |
157 // If there is no browser process, we should not do anything. | |
158 if (!g_browser_process) | |
159 return; | |
160 | |
161 StatusTray* status_tray = g_browser_process->status_tray(); | |
162 if (status_tray != NULL) { | |
163 status_tray->RemoveStatusIcon(status_icon_); | |
164 status_icon_ = NULL; | |
165 } | |
166 } | |
167 | |
168 void MediaStreamCaptureIndicator::UpdateStatusTrayIconContextMenu() { | |
169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
170 DCHECK(status_icon_); | |
171 DCHECK(!users_.empty()); | |
172 | |
173 ui::SimpleMenuModel* menu = new ui::SimpleMenuModel(this); | |
174 // TODO(xians): Do we need a About MediaStream item for the menu? | |
175 menu->AddItem(IDC_MEDIA_STREAM_DEVICE_STATUS_TRAY, | |
176 l10n_util::GetStringUTF16(IDS_MEDIA_STREAM_STATUS_TRAY_TITLE)); | |
177 menu->AddSeparator(); | |
178 | |
179 for (CaptureDeviceUserList::const_iterator iter = users_.begin(); | |
180 iter != users_.end(); ++iter) { | |
181 int command_id = IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_AUDIO; | |
182 int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_ITEM_AUDIO; | |
183 if (iter->type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) { | |
184 command_id = IDC_MEDIA_CONTEXT_MEDIA_STREAM_CAPTURE_VIDEO; | |
185 message_id = IDS_MEDIA_STREAM_STATUS_TRAY_ITEM_VIDEO; | |
186 } | |
187 string16 message = l10n_util::GetStringFUTF16(message_id, | |
188 ASCIIToUTF16(iter->url), | |
189 ASCIIToUTF16(iter->device)); | |
190 menu->AddItem(command_id, message); | |
191 } | |
192 | |
193 menu->AddSeparator(); | |
194 menu->AddItem(IDC_EXIT, l10n_util::GetStringUTF16(IDS_EXIT)); | |
195 | |
196 status_icon_->SetContextMenu(menu); | |
197 } | |
198 | |
199 void MediaStreamCaptureIndicator::AddCaptureDeviceUser( | |
200 const std::string& url, const content::MediaStreamDevices& devices) { | |
201 for (content::MediaStreamDevices::const_iterator dev = devices.begin(); | |
202 dev != devices.end(); ++dev) { | |
203 DCHECK(dev->type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE || | |
204 dev->type == content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE); | |
205 users_.push_back(CaptureDeviceUser(url, dev->name, dev->type)); | |
206 } | |
207 | |
208 UpdateStatusTrayIconContextMenu(); | |
209 } | |
210 | |
211 void MediaStreamCaptureIndicator::RemoveCaptureDeviceUser( | |
212 const std::string& url, const content::MediaStreamDevices& devices) { | |
213 for (content::MediaStreamDevices::const_iterator dev = devices.begin(); | |
214 dev != devices.end(); ++dev) { | |
215 CaptureDeviceUserList::iterator iter = users_.begin(); | |
216 while (iter != users_.end()) { | |
217 if (url == iter->url && | |
218 dev->name == iter->device && | |
219 dev->type == iter->type) { | |
220 users_.erase(iter); | |
221 break; | |
222 } | |
223 } | |
224 if (iter == users_.end()) { | |
225 DLOG(ERROR) << "Failed to find Media Stream user " << url | |
226 << " for device " << dev->name | |
227 << " with type " << dev->type; | |
228 } | |
229 } | |
230 | |
231 if (!users_.empty()) | |
232 UpdateStatusTrayIconContextMenu(); | |
233 } | |
OLD | NEW |