| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/platform_notification_service_impl.h" | 5 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 10 #include "chrome/browser/notifications/notification_object_proxy.h" | 11 #include "chrome/browser/notifications/notification_object_proxy.h" |
| 11 #include "chrome/browser/notifications/notification_ui_manager.h" | 12 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 12 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 13 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_io_data.h" | 15 #include "chrome/browser/profiles/profile_io_data.h" |
| 16 #include "chrome/common/pref_names.h" |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/desktop_notification_delegate.h" | 20 #include "content/public/browser/desktop_notification_delegate.h" |
| 19 #include "content/public/browser/notification_event_dispatcher.h" | 21 #include "content/public/browser/notification_event_dispatcher.h" |
| 20 #include "content/public/common/platform_notification_data.h" | 22 #include "content/public/common/platform_notification_data.h" |
| 23 #include "net/base/net_util.h" |
| 21 #include "ui/message_center/notifier_settings.h" | 24 #include "ui/message_center/notifier_settings.h" |
| 25 #include "url/url_constants.h" |
| 22 | 26 |
| 23 #if defined(ENABLE_EXTENSIONS) | 27 #if defined(ENABLE_EXTENSIONS) |
| 24 #include "chrome/browser/notifications/desktop_notification_service.h" | 28 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 25 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 29 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 26 #include "extensions/browser/extension_registry.h" | 30 #include "extensions/browser/extension_registry.h" |
| 27 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
| 28 #include "extensions/browser/info_map.h" | 32 #include "extensions/browser/info_map.h" |
| 29 #include "extensions/common/constants.h" | 33 #include "extensions/common/constants.h" |
| 30 #include "extensions/common/extension_set.h" | 34 #include "extensions/common/extension_set.h" |
| 31 #include "extensions/common/permissions/api_permission.h" | 35 #include "extensions/common/permissions/api_permission.h" |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 188 |
| 185 return blink::WebNotificationPermissionDefault; | 189 return blink::WebNotificationPermissionDefault; |
| 186 } | 190 } |
| 187 | 191 |
| 188 void PlatformNotificationServiceImpl::DisplayNotification( | 192 void PlatformNotificationServiceImpl::DisplayNotification( |
| 189 content::BrowserContext* browser_context, | 193 content::BrowserContext* browser_context, |
| 190 const GURL& origin, | 194 const GURL& origin, |
| 191 const SkBitmap& icon, | 195 const SkBitmap& icon, |
| 192 const content::PlatformNotificationData& notification_data, | 196 const content::PlatformNotificationData& notification_data, |
| 193 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 197 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
| 194 int render_process_id, | |
| 195 base::Closure* cancel_callback) { | 198 base::Closure* cancel_callback) { |
| 196 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 199 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 197 | 200 |
| 198 Profile* profile = Profile::FromBrowserContext(browser_context); | 201 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 199 DCHECK(profile); | 202 DCHECK(profile); |
| 200 | 203 |
| 201 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 204 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
| 202 Notification notification = CreateNotificationFromData( | 205 Notification notification = CreateNotificationFromData( |
| 203 profile, origin, icon, notification_data, proxy, render_process_id); | 206 profile, origin, icon, notification_data, proxy); |
| 204 | 207 |
| 205 GetNotificationUIManager()->Add(notification, profile); | 208 GetNotificationUIManager()->Add(notification, profile); |
| 206 if (cancel_callback) | 209 if (cancel_callback) |
| 207 *cancel_callback = | 210 *cancel_callback = |
| 208 base::Bind(&CancelNotification, | 211 base::Bind(&CancelNotification, |
| 209 notification.delegate_id(), | 212 notification.delegate_id(), |
| 210 NotificationUIManager::GetProfileID(profile)); | 213 NotificationUIManager::GetProfileID(profile)); |
| 211 | 214 |
| 212 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 215 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
| 213 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 216 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 214 } | 217 } |
| 215 | 218 |
| 216 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 219 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
| 217 content::BrowserContext* browser_context, | 220 content::BrowserContext* browser_context, |
| 218 int64 service_worker_registration_id, | 221 int64 service_worker_registration_id, |
| 219 const GURL& origin, | 222 const GURL& origin, |
| 220 const SkBitmap& icon, | 223 const SkBitmap& icon, |
| 221 const content::PlatformNotificationData& notification_data, | 224 const content::PlatformNotificationData& notification_data) { |
| 222 int render_process_id) { | |
| 223 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 224 | 226 |
| 225 Profile* profile = Profile::FromBrowserContext(browser_context); | 227 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 226 DCHECK(profile); | 228 DCHECK(profile); |
| 227 | 229 |
| 228 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( | 230 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
| 229 browser_context, | 231 browser_context, |
| 230 service_worker_registration_id, | 232 service_worker_registration_id, |
| 231 origin, | 233 origin, |
| 232 notification_data); | 234 notification_data); |
| 233 | 235 |
| 234 Notification notification = CreateNotificationFromData( | 236 Notification notification = CreateNotificationFromData( |
| 235 profile, origin, icon, notification_data, delegate, render_process_id); | 237 profile, origin, icon, notification_data, delegate); |
| 236 | 238 |
| 237 GetNotificationUIManager()->Add(notification, profile); | 239 GetNotificationUIManager()->Add(notification, profile); |
| 238 | 240 |
| 239 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 241 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
| 240 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 242 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 241 } | 243 } |
| 242 | 244 |
| 243 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 245 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
| 244 content::BrowserContext* browser_context, | 246 content::BrowserContext* browser_context, |
| 245 const std::string& persistent_notification_id) { | 247 const std::string& persistent_notification_id) { |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 248 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 247 | 249 |
| 248 Profile* profile = Profile::FromBrowserContext(browser_context); | 250 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 249 DCHECK(profile); | 251 DCHECK(profile); |
| 250 | 252 |
| 251 GetNotificationUIManager()->CancelById( | 253 GetNotificationUIManager()->CancelById( |
| 252 persistent_notification_id, NotificationUIManager::GetProfileID(profile)); | 254 persistent_notification_id, NotificationUIManager::GetProfileID(profile)); |
| 253 } | 255 } |
| 254 | 256 |
| 255 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( | 257 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( |
| 256 Profile* profile, | 258 Profile* profile, |
| 257 const GURL& origin, | 259 const GURL& origin, |
| 258 const SkBitmap& icon, | 260 const SkBitmap& icon, |
| 259 const content::PlatformNotificationData& notification_data, | 261 const content::PlatformNotificationData& notification_data, |
| 260 NotificationDelegate* delegate, | 262 NotificationDelegate* delegate) const { |
| 261 int render_process_id) const { | 263 base::string16 display_source = DisplayNameForOrigin(profile, origin); |
| 262 base::string16 display_source = DisplayNameForOriginInProcessId( | |
| 263 profile, origin, render_process_id); | |
| 264 | 264 |
| 265 // TODO(peter): Icons for Web Notifications are currently always requested for | 265 // TODO(peter): Icons for Web Notifications are currently always requested for |
| 266 // 1x scale, whereas the displays on which they can be displayed can have a | 266 // 1x scale, whereas the displays on which they can be displayed can have a |
| 267 // different pixel density. Be smarter about this when the API gets updated | 267 // different pixel density. Be smarter about this when the API gets updated |
| 268 // with a way for developers to specify images of different resolutions. | 268 // with a way for developers to specify images of different resolutions. |
| 269 Notification notification(origin, notification_data.title, | 269 Notification notification(origin, notification_data.title, |
| 270 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), | 270 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), |
| 271 display_source, notification_data.tag, delegate); | 271 display_source, notification_data.tag, delegate); |
| 272 | 272 |
| 273 notification.set_context_message(display_source); |
| 274 |
| 273 // Web Notifications do not timeout. | 275 // Web Notifications do not timeout. |
| 274 notification.set_never_timeout(true); | 276 notification.set_never_timeout(true); |
| 275 | 277 |
| 276 return notification; | 278 return notification; |
| 277 } | 279 } |
| 278 | 280 |
| 279 NotificationUIManager* | 281 NotificationUIManager* |
| 280 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 282 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
| 281 if (notification_ui_manager_for_tests_) | 283 if (notification_ui_manager_for_tests_) |
| 282 return notification_ui_manager_for_tests_; | 284 return notification_ui_manager_for_tests_; |
| 283 | 285 |
| 284 return g_browser_process->notification_ui_manager(); | 286 return g_browser_process->notification_ui_manager(); |
| 285 } | 287 } |
| 286 | 288 |
| 287 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 289 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
| 288 NotificationUIManager* manager) { | 290 NotificationUIManager* manager) { |
| 289 notification_ui_manager_for_tests_ = manager; | 291 notification_ui_manager_for_tests_ = manager; |
| 290 } | 292 } |
| 291 | 293 |
| 292 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( | 294 base::string16 PlatformNotificationServiceImpl::DisplayNameForOrigin( |
| 293 Profile* profile, const GURL& origin, int process_id) const { | 295 Profile* profile, |
| 296 const GURL& origin) const { |
| 294 #if defined(ENABLE_EXTENSIONS) | 297 #if defined(ENABLE_EXTENSIONS) |
| 295 // If the source is an extension, lookup the display name. | 298 // If the source is an extension, lookup the display name. |
| 296 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 299 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
| 297 extensions::InfoMap* extension_info_map = | 300 const extensions::Extension* extension = |
| 298 extensions::ExtensionSystem::Get(profile)->info_map(); | 301 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( |
| 299 if (extension_info_map) { | 302 origin.host(), extensions::ExtensionRegistry::EVERYTHING); |
| 300 extensions::ExtensionSet extensions; | 303 DCHECK(extension); |
| 301 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | |
| 302 origin, | |
| 303 process_id, | |
| 304 extensions::APIPermission::kNotifications, | |
| 305 &extensions); | |
| 306 DesktopNotificationService* desktop_notification_service = | |
| 307 DesktopNotificationServiceFactory::GetForProfile(profile); | |
| 308 DCHECK(desktop_notification_service); | |
| 309 | 304 |
| 310 for (const auto& extension : extensions) { | 305 return base::UTF8ToUTF16(extension->name()); |
| 311 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | |
| 312 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) | |
| 313 return base::UTF8ToUTF16(extension->name()); | |
| 314 } | |
| 315 } | |
| 316 } | 306 } |
| 317 #endif | 307 #endif |
| 318 | 308 |
| 319 return base::UTF8ToUTF16(origin.host()); | 309 std::string languages = |
| 310 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 311 |
| 312 return WebOriginDisplayName(origin, languages); |
| 320 } | 313 } |
| 314 |
| 315 // static |
| 316 base::string16 PlatformNotificationServiceImpl::WebOriginDisplayName( |
| 317 const GURL& origin, |
| 318 const std::string& languages) { |
| 319 if (origin.SchemeIsHTTPOrHTTPS()) { |
| 320 base::string16 formatted_origin; |
| 321 if (origin.SchemeIs(url::kHttpScheme)) { |
| 322 const url::Parsed& parsed = origin.parsed_for_possibly_invalid_spec(); |
| 323 const std::string& spec = origin.possibly_invalid_spec(); |
| 324 formatted_origin.append( |
| 325 spec.begin(), |
| 326 spec.begin() + |
| 327 parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); |
| 328 } |
| 329 formatted_origin.append(net::IDNToUnicode(origin.host(), languages)); |
| 330 if (origin.has_port()) { |
| 331 formatted_origin.push_back(':'); |
| 332 formatted_origin.append(base::UTF8ToUTF16(origin.port())); |
| 333 } |
| 334 return formatted_origin; |
| 335 } |
| 336 |
| 337 // TODO(dewittj): Once file:// URLs are passed in to the origin |
| 338 // GURL here, begin returning the path as the display name. |
| 339 return net::FormatUrl(origin, languages); |
| 340 } |
| OLD | NEW |