| 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/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 11 #include "chrome/browser/notifications/notification_object_proxy.h" | 11 #include "chrome/browser/notifications/notification_object_proxy.h" |
| 12 #include "chrome/browser/notifications/notification_ui_manager.h" | 12 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 13 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 13 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_io_data.h" | 15 #include "chrome/browser/profiles/profile_io_data.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/desktop_notification_delegate.h" | 20 #include "content/public/browser/desktop_notification_delegate.h" |
| 21 #include "content/public/browser/notification_event_dispatcher.h" | 21 #include "content/public/browser/notification_event_dispatcher.h" |
| 22 #include "content/public/browser/platform_notification_context.h" |
| 23 #include "content/public/browser/storage_partition.h" |
| 22 #include "content/public/common/platform_notification_data.h" | 24 #include "content/public/common/platform_notification_data.h" |
| 23 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 24 #include "ui/message_center/notifier_settings.h" | 26 #include "ui/message_center/notifier_settings.h" |
| 25 #include "url/url_constants.h" | 27 #include "url/url_constants.h" |
| 26 | 28 |
| 27 #if defined(ENABLE_EXTENSIONS) | 29 #if defined(ENABLE_EXTENSIONS) |
| 28 #include "chrome/browser/notifications/desktop_notification_service.h" | 30 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 29 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 31 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 30 #include "extensions/browser/extension_registry.h" | 32 #include "extensions/browser/extension_registry.h" |
| 31 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
| 32 #include "extensions/browser/info_map.h" | 34 #include "extensions/browser/info_map.h" |
| 33 #include "extensions/common/constants.h" | 35 #include "extensions/common/constants.h" |
| 34 #include "extensions/common/extension_set.h" | 36 #include "extensions/common/extension_set.h" |
| 35 #include "extensions/common/permissions/api_permission.h" | 37 #include "extensions/common/permissions/api_permission.h" |
| 36 #include "extensions/common/permissions/permissions_data.h" | 38 #include "extensions/common/permissions/permissions_data.h" |
| 37 #endif | 39 #endif |
| 38 | 40 |
| 39 #if defined(OS_ANDROID) | 41 #if defined(OS_ANDROID) |
| 40 #include "base/strings/string_number_conversions.h" | 42 #include "base/strings/string_number_conversions.h" |
| 41 #endif | 43 #endif |
| 42 | 44 |
| 45 using content::BrowserContext; |
| 43 using content::BrowserThread; | 46 using content::BrowserThread; |
| 47 using content::PlatformNotificationContext; |
| 44 using message_center::NotifierId; | 48 using message_center::NotifierId; |
| 45 | 49 |
| 46 namespace { | 50 namespace { |
| 47 | 51 |
| 52 // Callback to provide when deleting the data associated with persistent Web |
| 53 // Notifications from the notification database. |
| 54 void OnPersistentNotificationDataDeleted(bool success) { |
| 55 // TODO(peter): Record UMA for notification deletion requests created by the |
| 56 // PlatformNotificationService. |
| 57 } |
| 58 |
| 48 // Persistent notifications fired through the delegate do not care about the | 59 // Persistent notifications fired through the delegate do not care about the |
| 49 // lifetime of the Service Worker responsible for executing the event. | 60 // lifetime of the Service Worker responsible for executing the event. |
| 50 void OnEventDispatchComplete(content::PersistentNotificationStatus status) { | 61 void OnEventDispatchComplete(content::PersistentNotificationStatus status) { |
| 51 // TODO(peter): Record UMA statistics about the result status of running | 62 // TODO(peter): Record UMA statistics about the result status of running |
| 52 // events for persistent Web Notifications. | 63 // events for persistent Web Notifications. |
| 53 } | 64 } |
| 54 | 65 |
| 55 void CancelNotification(const std::string& id, ProfileID profile_id) { | 66 void CancelNotification(const std::string& id, ProfileID profile_id) { |
| 56 PlatformNotificationServiceImpl::GetInstance() | 67 PlatformNotificationServiceImpl::GetInstance() |
| 57 ->GetNotificationUIManager()->CancelById(id, profile_id); | 68 ->GetNotificationUIManager()->CancelById(id, profile_id); |
| 58 } | 69 } |
| 59 | 70 |
| 60 } // namespace | 71 } // namespace |
| 61 | 72 |
| 62 // static | 73 // static |
| 63 PlatformNotificationServiceImpl* | 74 PlatformNotificationServiceImpl* |
| 64 PlatformNotificationServiceImpl::GetInstance() { | 75 PlatformNotificationServiceImpl::GetInstance() { |
| 65 return Singleton<PlatformNotificationServiceImpl>::get(); | 76 return Singleton<PlatformNotificationServiceImpl>::get(); |
| 66 } | 77 } |
| 67 | 78 |
| 68 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() | 79 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
| 69 : notification_ui_manager_for_tests_(nullptr) {} | 80 : notification_ui_manager_for_tests_(nullptr) {} |
| 70 | 81 |
| 71 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 82 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
| 72 | 83 |
| 73 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( | 84 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( |
| 74 content::BrowserContext* browser_context, | 85 BrowserContext* browser_context, |
| 75 int64_t persistent_notification_id, | 86 int64_t persistent_notification_id, |
| 76 const GURL& origin) const { | 87 const GURL& origin) const { |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 78 content::NotificationEventDispatcher::GetInstance() | 89 content::NotificationEventDispatcher::GetInstance() |
| 79 ->DispatchNotificationClickEvent( | 90 ->DispatchNotificationClickEvent( |
| 80 browser_context, | 91 browser_context, |
| 81 persistent_notification_id, | 92 persistent_notification_id, |
| 82 origin, | 93 origin, |
| 83 base::Bind(&OnEventDispatchComplete)); | 94 base::Bind(&OnEventDispatchComplete)); |
| 84 } | 95 } |
| 85 | 96 |
| 97 void PlatformNotificationServiceImpl::OnPersistentNotificationClose( |
| 98 BrowserContext* browser_context, |
| 99 int64_t persistent_notification_id, |
| 100 const GURL& origin) const { |
| 101 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 102 PlatformNotificationContext* context = |
| 103 BrowserContext::GetStoragePartitionForSite(browser_context, origin) |
| 104 ->GetPlatformNotificationContext(); |
| 105 |
| 106 BrowserThread::PostTask( |
| 107 BrowserThread::IO, |
| 108 FROM_HERE, |
| 109 base::Bind(&PlatformNotificationContext::DeleteNotificationData, |
| 110 context, |
| 111 persistent_notification_id, |
| 112 origin, |
| 113 base::Bind(&OnPersistentNotificationDataDeleted))); |
| 114 } |
| 115 |
| 86 blink::WebNotificationPermission | 116 blink::WebNotificationPermission |
| 87 PlatformNotificationServiceImpl::CheckPermissionOnUIThread( | 117 PlatformNotificationServiceImpl::CheckPermissionOnUIThread( |
| 88 content::BrowserContext* browser_context, | 118 BrowserContext* browser_context, |
| 89 const GURL& origin, | 119 const GURL& origin, |
| 90 int render_process_id) { | 120 int render_process_id) { |
| 91 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 92 | 122 |
| 93 Profile* profile = Profile::FromBrowserContext(browser_context); | 123 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 94 DCHECK(profile); | 124 DCHECK(profile); |
| 95 | 125 |
| 96 #if defined(ENABLE_EXTENSIONS) | 126 #if defined(ENABLE_EXTENSIONS) |
| 97 // Extensions support an API permission named "notification". This will grant | 127 // Extensions support an API permission named "notification". This will grant |
| 98 // not only grant permission for using the Chrome App extension API, but also | 128 // not only grant permission for using the Chrome App extension API, but also |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 218 |
| 189 if (setting == CONTENT_SETTING_ALLOW) | 219 if (setting == CONTENT_SETTING_ALLOW) |
| 190 return blink::WebNotificationPermissionAllowed; | 220 return blink::WebNotificationPermissionAllowed; |
| 191 if (setting == CONTENT_SETTING_BLOCK) | 221 if (setting == CONTENT_SETTING_BLOCK) |
| 192 return blink::WebNotificationPermissionDenied; | 222 return blink::WebNotificationPermissionDenied; |
| 193 | 223 |
| 194 return blink::WebNotificationPermissionDefault; | 224 return blink::WebNotificationPermissionDefault; |
| 195 } | 225 } |
| 196 | 226 |
| 197 void PlatformNotificationServiceImpl::DisplayNotification( | 227 void PlatformNotificationServiceImpl::DisplayNotification( |
| 198 content::BrowserContext* browser_context, | 228 BrowserContext* browser_context, |
| 199 const GURL& origin, | 229 const GURL& origin, |
| 200 const SkBitmap& icon, | 230 const SkBitmap& icon, |
| 201 const content::PlatformNotificationData& notification_data, | 231 const content::PlatformNotificationData& notification_data, |
| 202 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 232 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
| 203 base::Closure* cancel_callback) { | 233 base::Closure* cancel_callback) { |
| 204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 234 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 205 | 235 |
| 206 Profile* profile = Profile::FromBrowserContext(browser_context); | 236 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 207 DCHECK(profile); | 237 DCHECK(profile); |
| 208 | 238 |
| 209 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 239 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
| 210 Notification notification = CreateNotificationFromData( | 240 Notification notification = CreateNotificationFromData( |
| 211 profile, origin, icon, notification_data, proxy); | 241 profile, origin, icon, notification_data, proxy); |
| 212 | 242 |
| 213 GetNotificationUIManager()->Add(notification, profile); | 243 GetNotificationUIManager()->Add(notification, profile); |
| 214 if (cancel_callback) | 244 if (cancel_callback) |
| 215 *cancel_callback = | 245 *cancel_callback = |
| 216 base::Bind(&CancelNotification, | 246 base::Bind(&CancelNotification, |
| 217 notification.delegate_id(), | 247 notification.delegate_id(), |
| 218 NotificationUIManager::GetProfileID(profile)); | 248 NotificationUIManager::GetProfileID(profile)); |
| 219 | 249 |
| 220 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 250 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
| 221 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 251 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 222 } | 252 } |
| 223 | 253 |
| 224 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 254 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
| 225 content::BrowserContext* browser_context, | 255 BrowserContext* browser_context, |
| 226 int64_t persistent_notification_id, | 256 int64_t persistent_notification_id, |
| 227 const GURL& origin, | 257 const GURL& origin, |
| 228 const SkBitmap& icon, | 258 const SkBitmap& icon, |
| 229 const content::PlatformNotificationData& notification_data) { | 259 const content::PlatformNotificationData& notification_data) { |
| 230 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 260 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 231 | 261 |
| 232 Profile* profile = Profile::FromBrowserContext(browser_context); | 262 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 233 DCHECK(profile); | 263 DCHECK(profile); |
| 234 | 264 |
| 235 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( | 265 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
| 236 browser_context, persistent_notification_id, origin); | 266 browser_context, persistent_notification_id, origin); |
| 237 | 267 |
| 238 Notification notification = CreateNotificationFromData( | 268 Notification notification = CreateNotificationFromData( |
| 239 profile, origin, icon, notification_data, delegate); | 269 profile, origin, icon, notification_data, delegate); |
| 240 | 270 |
| 241 // TODO(peter): Remove this mapping when we have reliable id generation for | 271 // TODO(peter): Remove this mapping when we have reliable id generation for |
| 242 // the message_center::Notification objects. | 272 // the message_center::Notification objects. |
| 243 persistent_notifications_[persistent_notification_id] = notification.id(); | 273 persistent_notifications_[persistent_notification_id] = notification.id(); |
| 244 | 274 |
| 245 GetNotificationUIManager()->Add(notification, profile); | 275 GetNotificationUIManager()->Add(notification, profile); |
| 246 | 276 |
| 247 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 277 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
| 248 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 278 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 249 } | 279 } |
| 250 | 280 |
| 251 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 281 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
| 252 content::BrowserContext* browser_context, | 282 BrowserContext* browser_context, |
| 253 int64_t persistent_notification_id) { | 283 int64_t persistent_notification_id) { |
| 254 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 284 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 255 | 285 |
| 256 Profile* profile = Profile::FromBrowserContext(browser_context); | 286 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 257 DCHECK(profile); | 287 DCHECK(profile); |
| 258 | 288 |
| 259 #if defined(OS_ANDROID) | 289 #if defined(OS_ANDROID) |
| 260 // TODO(peter): Remove this conversion when the notification ids are being | 290 // TODO(peter): Remove this conversion when the notification ids are being |
| 261 // generated by the caller of this method. | 291 // generated by the caller of this method. |
| 262 std::string textual_persistent_notification_id = | 292 std::string textual_persistent_notification_id = |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 formatted_origin.push_back(':'); | 384 formatted_origin.push_back(':'); |
| 355 formatted_origin.append(base::UTF8ToUTF16(origin.port())); | 385 formatted_origin.append(base::UTF8ToUTF16(origin.port())); |
| 356 } | 386 } |
| 357 return formatted_origin; | 387 return formatted_origin; |
| 358 } | 388 } |
| 359 | 389 |
| 360 // TODO(dewittj): Once file:// URLs are passed in to the origin | 390 // TODO(dewittj): Once file:// URLs are passed in to the origin |
| 361 // GURL here, begin returning the path as the display name. | 391 // GURL here, begin returning the path as the display name. |
| 362 return net::FormatUrl(origin, languages); | 392 return net::FormatUrl(origin, languages); |
| 363 } | 393 } |
| OLD | NEW |