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/notifications/desktop_notification_service.h" | 5 #include "chrome/browser/notifications/desktop_notification_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
25 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/desktop_notification_delegate.h" | 27 #include "content/public/browser/desktop_notification_delegate.h" |
28 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
29 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
30 #include "content/public/browser/render_process_host.h" | 30 #include "content/public/browser/render_process_host.h" |
31 #include "content/public/browser/render_view_host.h" | 31 #include "content/public/browser/render_view_host.h" |
32 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
33 #include "content/public/common/show_desktop_notification_params.h" | 33 #include "content/public/common/show_desktop_notification_params.h" |
34 #include "net/base/net_util.h" | |
34 #include "ui/base/webui/web_ui_util.h" | 35 #include "ui/base/webui/web_ui_util.h" |
35 #include "ui/message_center/notifier_settings.h" | 36 #include "ui/message_center/notifier_settings.h" |
36 | 37 |
37 #if defined(ENABLE_EXTENSIONS) | 38 #if defined(ENABLE_EXTENSIONS) |
38 #include "chrome/browser/extensions/api/notifications/notifications_api.h" | 39 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
39 #include "chrome/browser/extensions/extension_service.h" | 40 #include "chrome/browser/extensions/extension_service.h" |
40 #include "extensions/browser/event_router.h" | 41 #include "extensions/browser/event_router.h" |
41 #include "extensions/browser/extension_registry.h" | 42 #include "extensions/browser/extension_registry.h" |
42 #include "extensions/browser/extension_system.h" | 43 #include "extensions/browser/extension_system.h" |
43 #include "extensions/browser/extension_util.h" | 44 #include "extensions/browser/extension_util.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 void DesktopNotificationService::ShowDesktopNotification( | 156 void DesktopNotificationService::ShowDesktopNotification( |
156 const content::ShowDesktopNotificationHostMsgParams& params, | 157 const content::ShowDesktopNotificationHostMsgParams& params, |
157 content::RenderFrameHost* render_frame_host, | 158 content::RenderFrameHost* render_frame_host, |
158 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 159 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
159 base::Closure* cancel_callback) { | 160 base::Closure* cancel_callback) { |
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
161 const GURL& origin = params.origin; | 162 const GURL& origin = params.origin; |
162 NotificationObjectProxy* proxy = | 163 NotificationObjectProxy* proxy = |
163 new NotificationObjectProxy(render_frame_host, delegate.Pass()); | 164 new NotificationObjectProxy(render_frame_host, delegate.Pass()); |
164 | 165 |
165 base::string16 display_source = DisplayNameForOriginInProcessId( | 166 int process_id = render_frame_host->GetProcess()->GetID(); |
166 origin, render_frame_host->GetProcess()->GetID()); | 167 |
168 // TODO(dewittj): The origin of file:// URLs always appears to be the empty | |
169 // URL. We need a better way to display this than returning an empty string. | |
170 base::string16 display_source = | |
171 DisplayNameForOriginInProcessId(origin, process_id); | |
172 | |
167 Notification notification(origin, params.icon_url, params.title, | 173 Notification notification(origin, params.icon_url, params.title, |
168 params.body, params.direction, display_source, params.replace_id, | 174 params.body, params.direction, display_source, params.replace_id, |
169 proxy); | 175 proxy); |
170 | 176 |
177 // Web notifications should display their origin | |
178 notification.set_context_message(display_source); | |
179 | |
171 // The webkit notification doesn't timeout. | 180 // The webkit notification doesn't timeout. |
172 notification.set_never_timeout(true); | 181 notification.set_never_timeout(true); |
173 | 182 |
174 g_browser_process->notification_ui_manager()->Add(notification, profile_); | 183 g_browser_process->notification_ui_manager()->Add(notification, profile_); |
175 if (cancel_callback) | 184 if (cancel_callback) |
176 *cancel_callback = | 185 *cancel_callback = |
177 base::Bind(&CancelNotification, | 186 base::Bind(&CancelNotification, |
178 proxy->id(), | 187 proxy->id(), |
179 NotificationUIManager::GetProfileID(profile_)); | 188 NotificationUIManager::GetProfileID(profile_)); |
180 | 189 |
181 DesktopNotificationProfileUtil::UsePermission(profile_, origin); | 190 DesktopNotificationProfileUtil::UsePermission(profile_, origin); |
182 } | 191 } |
183 | 192 |
184 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( | 193 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( |
185 const GURL& origin, int process_id) { | 194 const GURL& origin, |
195 int process_id) const { | |
186 #if defined(ENABLE_EXTENSIONS) | 196 #if defined(ENABLE_EXTENSIONS) |
187 // If the source is an extension, lookup the display name. | 197 // If the source is an extension, lookup the display name. |
188 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 198 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
189 extensions::InfoMap* extension_info_map = | 199 base::string16 extension_display_name; |
190 extensions::ExtensionSystem::Get(profile_)->info_map(); | 200 if (ExtensionDisplayName(origin, process_id, &extension_display_name)) { |
191 if (extension_info_map) { | 201 return extension_display_name; |
192 extensions::ExtensionSet extensions; | |
193 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | |
194 origin, | |
195 process_id, | |
196 extensions::APIPermission::kNotifications, | |
197 &extensions); | |
198 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | |
199 iter != extensions.end(); ++iter) { | |
200 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id()); | |
201 if (IsNotifierEnabled(notifier_id)) | |
202 return base::UTF8ToUTF16((*iter)->name()); | |
203 } | |
204 } | 202 } |
205 } | 203 } |
206 #endif | 204 #endif |
207 | 205 |
208 return base::UTF8ToUTF16(origin.host()); | 206 std::string languages = |
207 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
208 | |
209 return OriginDisplayName(origin, languages); | |
210 } | |
211 | |
212 #if defined(ENABLE_EXTENSIONS) | |
213 bool DesktopNotificationService::ExtensionDisplayName( | |
214 const GURL& origin, | |
215 int process_id, | |
216 base::string16* out) const { | |
217 DCHECK(origin.SchemeIs(extensions::kExtensionScheme)); | |
218 DCHECK(out); | |
219 | |
220 extensions::InfoMap* extension_info_map = | |
221 extensions::ExtensionSystem::Get(profile_)->info_map(); | |
222 if (extension_info_map) { | |
223 extensions::ExtensionSet extensions; | |
224 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | |
225 origin, | |
226 process_id, | |
227 extensions::APIPermission::kNotifications, | |
228 &extensions); | |
229 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | |
Peter Beverloo
2014/10/18 09:31:02
nit: since you're moving this, might as well use C
dewittj
2014/10/29 16:21:25
Done.
| |
230 iter != extensions.end(); | |
231 ++iter) { | |
232 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id()); | |
233 if (IsNotifierEnabled(notifier_id)) { | |
234 *out = base::UTF8ToUTF16((*iter)->name()); | |
235 return true; | |
236 } | |
237 } | |
238 } | |
239 return false; | |
240 } | |
241 #endif // defined(ENABLE_EXTENSIONS) | |
242 | |
243 // static | |
244 base::string16 DesktopNotificationService::OriginDisplayName( | |
245 const GURL& origin, | |
246 std::string languages) { | |
247 if (origin.SchemeIsHTTPOrHTTPS()) { | |
248 base::string16 formatted_origin = | |
249 net::IDNToUnicode(origin.host(), languages); | |
250 if (origin.has_port()) { | |
251 formatted_origin.push_back(':'); | |
252 formatted_origin.append(base::UTF8ToUTF16(origin.port())); | |
253 } | |
254 return formatted_origin; | |
255 } | |
256 | |
257 // TODO(dewittj): Once file:// URLs are passed in to the origin | |
258 // GURL here, begin returning the path as the display name. | |
259 return net::FormatUrl(origin, languages); | |
209 } | 260 } |
210 | 261 |
211 bool DesktopNotificationService::IsNotifierEnabled( | 262 bool DesktopNotificationService::IsNotifierEnabled( |
212 const NotifierId& notifier_id) { | 263 const NotifierId& notifier_id) const { |
213 switch (notifier_id.type) { | 264 switch (notifier_id.type) { |
214 case NotifierId::APPLICATION: | 265 case NotifierId::APPLICATION: |
215 return disabled_extension_ids_.find(notifier_id.id) == | 266 return disabled_extension_ids_.find(notifier_id.id) == |
216 disabled_extension_ids_.end(); | 267 disabled_extension_ids_.end(); |
217 case NotifierId::WEB_PAGE: | 268 case NotifierId::WEB_PAGE: |
218 return DesktopNotificationProfileUtil::GetContentSetting( | 269 return DesktopNotificationProfileUtil::GetContentSetting( |
219 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; | 270 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; |
220 case NotifierId::SYSTEM_COMPONENT: | 271 case NotifierId::SYSTEM_COMPONENT: |
221 #if defined(OS_CHROMEOS) | 272 #if defined(OS_CHROMEOS) |
222 return disabled_system_component_ids_.find(notifier_id.id) == | 273 return disabled_system_component_ids_.find(notifier_id.id) == |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 // Tell the IO thread that this extension's permission for notifications | 400 // Tell the IO thread that this extension's permission for notifications |
350 // has changed. | 401 // has changed. |
351 extensions::InfoMap* extension_info_map = | 402 extensions::InfoMap* extension_info_map = |
352 extensions::ExtensionSystem::Get(profile_)->info_map(); | 403 extensions::ExtensionSystem::Get(profile_)->info_map(); |
353 BrowserThread::PostTask( | 404 BrowserThread::PostTask( |
354 BrowserThread::IO, FROM_HERE, | 405 BrowserThread::IO, FROM_HERE, |
355 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, | 406 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, |
356 extension_info_map, notifier_id.id, !enabled)); | 407 extension_info_map, notifier_id.id, !enabled)); |
357 #endif | 408 #endif |
358 } | 409 } |
OLD | NEW |