| 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 <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/user_metrics.h" | 11 #include "base/metrics/user_metrics.h" |
| 12 #include "base/metrics/user_metrics_action.h" | 12 #include "base/metrics/user_metrics_action.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 17 #include "chrome/browser/engagement/site_engagement_service.h" | 17 #include "chrome/browser/engagement/site_engagement_service.h" |
| 18 #include "chrome/browser/notifications/native_notification_delegate.h" | |
| 19 #include "chrome/browser/notifications/notification_common.h" | |
| 20 #include "chrome/browser/notifications/notification_display_service_factory.h" | 18 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 21 #include "chrome/browser/notifications/notification_object_proxy.h" | 19 #include "chrome/browser/notifications/notification_object_proxy.h" |
| 22 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 20 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 23 #include "chrome/browser/permissions/permission_manager.h" | 21 #include "chrome/browser/permissions/permission_manager.h" |
| 24 #include "chrome/browser/permissions/permission_result.h" | 22 #include "chrome/browser/permissions/permission_result.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/profiles/profile_attributes_storage.h" | 24 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 27 #include "chrome/browser/profiles/profile_io_data.h" | 25 #include "chrome/browser/profiles/profile_io_data.h" |
| 28 #include "chrome/browser/profiles/profile_manager.h" | 26 #include "chrome/browser/profiles/profile_manager.h" |
| 29 #include "chrome/browser/safe_browsing/ping_manager.h" | 27 #include "chrome/browser/safe_browsing/ping_manager.h" |
| 30 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 28 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 31 #include "chrome/browser/ui/browser.h" | 29 #include "chrome/browser/ui/browser.h" |
| 32 #include "chrome/common/chrome_features.h" | |
| 33 #include "chrome/common/features.h" | |
| 34 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
| 35 #include "chrome/grit/generated_resources.h" | 31 #include "chrome/grit/generated_resources.h" |
| 36 #include "components/content_settings/core/browser/host_content_settings_map.h" | 32 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 37 #include "components/content_settings/core/common/content_settings.h" | 33 #include "components/content_settings/core/common/content_settings.h" |
| 38 #include "components/content_settings/core/common/content_settings_types.h" | 34 #include "components/content_settings/core/common/content_settings_types.h" |
| 39 #include "components/prefs/pref_service.h" | 35 #include "components/prefs/pref_service.h" |
| 40 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
| 41 #include "content/public/browser/desktop_notification_delegate.h" | 37 #include "content/public/browser/desktop_notification_delegate.h" |
| 42 #include "content/public/browser/notification_event_dispatcher.h" | 38 #include "content/public/browser/notification_event_dispatcher.h" |
| 43 #include "content/public/common/notification_resources.h" | 39 #include "content/public/common/notification_resources.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 113 |
| 118 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() | 114 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
| 119 : test_display_service_(nullptr) { | 115 : test_display_service_(nullptr) { |
| 120 #if BUILDFLAG(ENABLE_BACKGROUND) | 116 #if BUILDFLAG(ENABLE_BACKGROUND) |
| 121 pending_click_dispatch_events_ = 0; | 117 pending_click_dispatch_events_ = 0; |
| 122 #endif | 118 #endif |
| 123 } | 119 } |
| 124 | 120 |
| 125 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 121 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
| 126 | 122 |
| 127 // TODO(miguelg): Move this to PersistentNotificationHandler | |
| 128 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( | 123 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( |
| 129 BrowserContext* browser_context, | 124 BrowserContext* browser_context, |
| 130 const std::string& notification_id, | 125 const std::string& notification_id, |
| 131 const GURL& origin, | 126 const GURL& origin, |
| 132 int action_index, | 127 int action_index, |
| 133 const base::NullableString16& reply) { | 128 const base::NullableString16& reply) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 135 blink::mojom::PermissionStatus permission_status = | 130 blink::mojom::PermissionStatus permission_status = |
| 136 CheckPermissionOnUIThread(browser_context, origin, | 131 CheckPermissionOnUIThread(browser_context, origin, |
| 137 kInvalidRenderProcessId); | 132 kInvalidRenderProcessId); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 163 | 158 |
| 164 RecordSiteEngagement(browser_context, origin); | 159 RecordSiteEngagement(browser_context, origin); |
| 165 content::NotificationEventDispatcher::GetInstance() | 160 content::NotificationEventDispatcher::GetInstance() |
| 166 ->DispatchNotificationClickEvent( | 161 ->DispatchNotificationClickEvent( |
| 167 browser_context, notification_id, origin, action_index, reply, | 162 browser_context, notification_id, origin, action_index, reply, |
| 168 base::Bind( | 163 base::Bind( |
| 169 &PlatformNotificationServiceImpl::OnClickEventDispatchComplete, | 164 &PlatformNotificationServiceImpl::OnClickEventDispatchComplete, |
| 170 base::Unretained(this))); | 165 base::Unretained(this))); |
| 171 } | 166 } |
| 172 | 167 |
| 173 // TODO(miguelg): Move this to PersistentNotificationHandler | |
| 174 void PlatformNotificationServiceImpl::OnPersistentNotificationClose( | 168 void PlatformNotificationServiceImpl::OnPersistentNotificationClose( |
| 175 BrowserContext* browser_context, | 169 BrowserContext* browser_context, |
| 176 const std::string& notification_id, | 170 const std::string& notification_id, |
| 177 const GURL& origin, | 171 const GURL& origin, |
| 178 bool by_user) { | 172 bool by_user) { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 173 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 180 | 174 |
| 181 // If we programatically closed this notification, don't dispatch any event. | 175 // If we programatically closed this notification, don't dispatch any event. |
| 182 if (closed_notifications_.erase(notification_id) != 0) | 176 if (closed_notifications_.erase(notification_id) != 0) |
| 183 return; | 177 return; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 // crash (see |ScopedKeepAlive|). We just do nothing here, the user would not | 304 // crash (see |ScopedKeepAlive|). We just do nothing here, the user would not |
| 311 // see the notification anyway, since we are shutting down. | 305 // see the notification anyway, since we are shutting down. |
| 312 if (g_browser_process->IsShuttingDown()) | 306 if (g_browser_process->IsShuttingDown()) |
| 313 return; | 307 return; |
| 314 | 308 |
| 315 Profile* profile = Profile::FromBrowserContext(browser_context); | 309 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 316 DCHECK(profile); | 310 DCHECK(profile); |
| 317 DCHECK_EQ(0u, notification_data.actions.size()); | 311 DCHECK_EQ(0u, notification_data.actions.size()); |
| 318 DCHECK_EQ(0u, notification_resources.action_icons.size()); | 312 DCHECK_EQ(0u, notification_resources.action_icons.size()); |
| 319 | 313 |
| 320 // Temporary change while the delegates are merged. | 314 NotificationObjectProxy* proxy = new NotificationObjectProxy( |
| 321 NotificationDelegate* notification_delegate; | |
| 322 #if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) | |
| 323 if (base::FeatureList::IsEnabled(features::kNativeNotifications) && | |
| 324 g_browser_process->notification_platform_bridge()) { | |
| 325 notification_delegate = new NativeNotificationDelegate(notification_id); | |
| 326 } else { | |
| 327 notification_delegate = new NotificationObjectProxy( | |
| 328 browser_context, notification_id, origin, std::move(delegate)); | |
| 329 } | |
| 330 #else | |
| 331 notification_delegate = new NotificationObjectProxy( | |
| 332 browser_context, notification_id, origin, std::move(delegate)); | 315 browser_context, notification_id, origin, std::move(delegate)); |
| 333 #endif // BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) | |
| 334 | |
| 335 Notification notification = CreateNotificationFromData( | 316 Notification notification = CreateNotificationFromData( |
| 336 profile, GURL() /* service_worker_scope */, origin, notification_data, | 317 profile, GURL() /* service_worker_scope */, origin, notification_data, |
| 337 notification_resources, notification_delegate); | 318 notification_resources, proxy); |
| 338 | 319 |
| 339 GetNotificationDisplayService(profile)->Display( | 320 GetNotificationDisplayService(profile)->Display( |
| 340 NotificationCommon::NON_PERSISTENT, notification_id, notification); | 321 NotificationCommon::NON_PERSISTENT, notification.delegate_id(), |
| 322 notification); |
| 341 if (cancel_callback) { | 323 if (cancel_callback) { |
| 342 #if defined(OS_WIN) | 324 #if defined(OS_WIN) |
| 343 std::string profile_id = | 325 std::string profile_id = |
| 344 base::WideToUTF8(profile->GetPath().BaseName().value()); | 326 base::WideToUTF8(profile->GetPath().BaseName().value()); |
| 345 #elif defined(OS_POSIX) | 327 #elif defined(OS_POSIX) |
| 346 std::string profile_id = profile->GetPath().BaseName().value(); | 328 std::string profile_id = profile->GetPath().BaseName().value(); |
| 347 #endif | 329 #endif |
| 348 *cancel_callback = base::Bind(&CancelNotification, notification_id, | 330 *cancel_callback = |
| 349 profile_id, profile->IsOffTheRecord()); | 331 base::Bind(&CancelNotification, notification.delegate_id(), profile_id, |
| 332 profile->IsOffTheRecord()); |
| 350 } | 333 } |
| 351 } | 334 } |
| 352 | 335 |
| 353 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 336 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
| 354 BrowserContext* browser_context, | 337 BrowserContext* browser_context, |
| 355 const std::string& notification_id, | 338 const std::string& notification_id, |
| 356 const GURL& service_worker_scope, | 339 const GURL& service_worker_scope, |
| 357 const GURL& origin, | 340 const GURL& origin, |
| 358 const content::PlatformNotificationData& notification_data, | 341 const content::PlatformNotificationData& notification_data, |
| 359 const content::NotificationResources& notification_resources) { | 342 const content::NotificationResources& notification_resources) { |
| 360 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 343 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 361 | 344 |
| 362 // Posted tasks can request notifications to be added, which would cause a | 345 // Posted tasks can request notifications to be added, which would cause a |
| 363 // crash (see |ScopedKeepAlive|). We just do nothing here, the user would not | 346 // crash (see |ScopedKeepAlive|). We just do nothing here, the user would not |
| 364 // see the notification anyway, since we are shutting down. | 347 // see the notification anyway, since we are shutting down. |
| 365 if (g_browser_process->IsShuttingDown()) | 348 if (g_browser_process->IsShuttingDown()) |
| 366 return; | 349 return; |
| 367 | 350 |
| 368 Profile* profile = Profile::FromBrowserContext(browser_context); | 351 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 369 DCHECK(profile); | 352 DCHECK(profile); |
| 370 | 353 |
| 371 // The notification settings button will be appended after the developer- | 354 // The notification settings button will be appended after the developer- |
| 372 // supplied buttons, available in |notification_data.actions|. | 355 // supplied buttons, available in |notification_data.actions|. |
| 373 int settings_button_index = notification_data.actions.size(); | 356 int settings_button_index = notification_data.actions.size(); |
| 374 | |
| 375 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( | 357 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
| 376 browser_context, notification_id, origin, settings_button_index); | 358 browser_context, notification_id, origin, settings_button_index); |
| 377 | 359 |
| 378 Notification notification = CreateNotificationFromData( | 360 Notification notification = CreateNotificationFromData( |
| 379 profile, service_worker_scope, origin, notification_data, | 361 profile, service_worker_scope, origin, notification_data, |
| 380 notification_resources, delegate); | 362 notification_resources, delegate); |
| 381 | 363 |
| 382 GetNotificationDisplayService(profile)->Display( | 364 GetNotificationDisplayService(profile)->Display( |
| 383 NotificationCommon::PERSISTENT, notification_id, notification); | 365 NotificationCommon::PERSISTENT, notification_id, notification); |
| 384 base::RecordAction(base::UserMetricsAction("Notifications.Persistent.Shown")); | 366 base::RecordAction(base::UserMetricsAction("Notifications.Persistent.Shown")); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 | 535 |
| 554 void PlatformNotificationServiceImpl::RecordSiteEngagement( | 536 void PlatformNotificationServiceImpl::RecordSiteEngagement( |
| 555 BrowserContext* browser_context, | 537 BrowserContext* browser_context, |
| 556 const GURL& origin) { | 538 const GURL& origin) { |
| 557 // TODO(dominickn, peter): This would be better if the site engagement service | 539 // TODO(dominickn, peter): This would be better if the site engagement service |
| 558 // could directly observe each notification. | 540 // could directly observe each notification. |
| 559 SiteEngagementService* engagement_service = | 541 SiteEngagementService* engagement_service = |
| 560 SiteEngagementService::Get(Profile::FromBrowserContext(browser_context)); | 542 SiteEngagementService::Get(Profile::FromBrowserContext(browser_context)); |
| 561 engagement_service->HandleNotificationInteraction(origin); | 543 engagement_service->HandleNotificationInteraction(origin); |
| 562 } | 544 } |
| OLD | NEW |