Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: chrome/browser/notifications/platform_notification_service_impl.cc

Issue 2888303004: Minimize the delegate dependencies for non persistent notifications. (Closed)
Patch Set: review Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 // Temporary change while the delegates are merged.
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(
315 browser_context, notification_id, origin, std::move(delegate)); 332 browser_context, notification_id, origin, std::move(delegate));
333 #endif // BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS)
334
316 Notification notification = CreateNotificationFromData( 335 Notification notification = CreateNotificationFromData(
317 profile, GURL() /* service_worker_scope */, origin, notification_data, 336 profile, GURL() /* service_worker_scope */, origin, notification_data,
318 notification_resources, proxy); 337 notification_resources, notification_delegate);
319 338
320 GetNotificationDisplayService(profile)->Display( 339 GetNotificationDisplayService(profile)->Display(
321 NotificationCommon::NON_PERSISTENT, notification.delegate_id(), 340 NotificationCommon::NON_PERSISTENT, notification_id, notification);
322 notification);
323 if (cancel_callback) { 341 if (cancel_callback) {
324 #if defined(OS_WIN) 342 #if defined(OS_WIN)
325 std::string profile_id = 343 std::string profile_id =
326 base::WideToUTF8(profile->GetPath().BaseName().value()); 344 base::WideToUTF8(profile->GetPath().BaseName().value());
327 #elif defined(OS_POSIX) 345 #elif defined(OS_POSIX)
328 std::string profile_id = profile->GetPath().BaseName().value(); 346 std::string profile_id = profile->GetPath().BaseName().value();
329 #endif 347 #endif
330 *cancel_callback = 348 *cancel_callback = base::Bind(&CancelNotification, notification_id,
331 base::Bind(&CancelNotification, notification.delegate_id(), profile_id, 349 profile_id, profile->IsOffTheRecord());
332 profile->IsOffTheRecord());
333 } 350 }
334 } 351 }
335 352
336 void PlatformNotificationServiceImpl::DisplayPersistentNotification( 353 void PlatformNotificationServiceImpl::DisplayPersistentNotification(
337 BrowserContext* browser_context, 354 BrowserContext* browser_context,
338 const std::string& notification_id, 355 const std::string& notification_id,
339 const GURL& service_worker_scope, 356 const GURL& service_worker_scope,
340 const GURL& origin, 357 const GURL& origin,
341 const content::PlatformNotificationData& notification_data, 358 const content::PlatformNotificationData& notification_data,
342 const content::NotificationResources& notification_resources) { 359 const content::NotificationResources& notification_resources) {
343 DCHECK_CURRENTLY_ON(BrowserThread::UI); 360 DCHECK_CURRENTLY_ON(BrowserThread::UI);
344 361
345 // Posted tasks can request notifications to be added, which would cause a 362 // 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 363 // crash (see |ScopedKeepAlive|). We just do nothing here, the user would not
347 // see the notification anyway, since we are shutting down. 364 // see the notification anyway, since we are shutting down.
348 if (g_browser_process->IsShuttingDown()) 365 if (g_browser_process->IsShuttingDown())
349 return; 366 return;
350 367
351 Profile* profile = Profile::FromBrowserContext(browser_context); 368 Profile* profile = Profile::FromBrowserContext(browser_context);
352 DCHECK(profile); 369 DCHECK(profile);
353 370
354 // The notification settings button will be appended after the developer- 371 // The notification settings button will be appended after the developer-
355 // supplied buttons, available in |notification_data.actions|. 372 // supplied buttons, available in |notification_data.actions|.
356 int settings_button_index = notification_data.actions.size(); 373 int settings_button_index = notification_data.actions.size();
374
357 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( 375 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate(
358 browser_context, notification_id, origin, settings_button_index); 376 browser_context, notification_id, origin, settings_button_index);
359 377
360 Notification notification = CreateNotificationFromData( 378 Notification notification = CreateNotificationFromData(
361 profile, service_worker_scope, origin, notification_data, 379 profile, service_worker_scope, origin, notification_data,
362 notification_resources, delegate); 380 notification_resources, delegate);
363 381
364 GetNotificationDisplayService(profile)->Display( 382 GetNotificationDisplayService(profile)->Display(
365 NotificationCommon::PERSISTENT, notification_id, notification); 383 NotificationCommon::PERSISTENT, notification_id, notification);
366 base::RecordAction(base::UserMetricsAction("Notifications.Persistent.Shown")); 384 base::RecordAction(base::UserMetricsAction("Notifications.Persistent.Shown"));
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 553
536 void PlatformNotificationServiceImpl::RecordSiteEngagement( 554 void PlatformNotificationServiceImpl::RecordSiteEngagement(
537 BrowserContext* browser_context, 555 BrowserContext* browser_context,
538 const GURL& origin) { 556 const GURL& origin) {
539 // TODO(dominickn, peter): This would be better if the site engagement service 557 // TODO(dominickn, peter): This would be better if the site engagement service
540 // could directly observe each notification. 558 // could directly observe each notification.
541 SiteEngagementService* engagement_service = 559 SiteEngagementService* engagement_service =
542 SiteEngagementService::Get(Profile::FromBrowserContext(browser_context)); 560 SiteEngagementService::Get(Profile::FromBrowserContext(browser_context));
543 engagement_service->HandleNotificationInteraction(origin); 561 engagement_service->HandleNotificationInteraction(origin);
544 } 562 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698