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