| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/push_messaging/push_messaging_notification_manager.h" | 5 #include "chrome/browser/push_messaging/push_messaging_notification_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <bitset> | 9 #include <bitset> |
| 10 | 10 |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/engagement/site_engagement_service.h" |
| 14 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 15 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/push_messaging/background_budget_service.h" | 17 #include "chrome/browser/push_messaging/background_budget_service.h" |
| 17 #include "chrome/browser/push_messaging/background_budget_service_factory.h" | 18 #include "chrome/browser/push_messaging/background_budget_service_factory.h" |
| 18 #include "chrome/browser/push_messaging/push_messaging_constants.h" | 19 #include "chrome/browser/push_messaging/push_messaging_constants.h" |
| 19 #include "chrome/common/features.h" | 20 #include "chrome/common/features.h" |
| 20 #include "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 21 #include "components/rappor/rappor_utils.h" | 22 #include "components/rappor/rappor_utils.h" |
| 22 #include "components/url_formatter/elide_url.h" | 23 #include "components/url_formatter/elide_url.h" |
| 23 #include "content/public/browser/browser_context.h" | 24 #include "content/public/browser/browser_context.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 45 using content::BrowserThread; | 46 using content::BrowserThread; |
| 46 using content::NotificationDatabaseData; | 47 using content::NotificationDatabaseData; |
| 47 using content::NotificationResources; | 48 using content::NotificationResources; |
| 48 using content::PlatformNotificationContext; | 49 using content::PlatformNotificationContext; |
| 49 using content::PlatformNotificationData; | 50 using content::PlatformNotificationData; |
| 50 using content::PushMessagingService; | 51 using content::PushMessagingService; |
| 51 using content::ServiceWorkerContext; | 52 using content::ServiceWorkerContext; |
| 52 using content::WebContents; | 53 using content::WebContents; |
| 53 | 54 |
| 54 namespace { | 55 namespace { |
| 55 // Currently this just costs 1.0, but we may expand the cost to | |
| 56 // include things like whether wifi is available in the mobile case. | |
| 57 const double kBackgroundProcessingCost = 1.0; | |
| 58 | |
| 59 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { | 56 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { |
| 60 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", status, | 57 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", status, |
| 61 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); | 58 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); |
| 62 } | 59 } |
| 63 | 60 |
| 64 content::StoragePartition* GetStoragePartition(Profile* profile, | 61 content::StoragePartition* GetStoragePartition(Profile* profile, |
| 65 const GURL& origin) { | 62 const GURL& origin) { |
| 66 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); | 63 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); |
| 67 } | 64 } |
| 68 | 65 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 platform_notification_service->ClosePersistentNotification( | 179 platform_notification_service->ClosePersistentNotification( |
| 183 profile_, notification_database_data.notification_id); | 180 profile_, notification_database_data.notification_id); |
| 184 platform_notification_service->OnPersistentNotificationClose( | 181 platform_notification_service->OnPersistentNotificationClose( |
| 185 profile_, notification_database_data.notification_id, | 182 profile_, notification_database_data.notification_id, |
| 186 notification_database_data.origin, false /* by_user */); | 183 notification_database_data.origin, false /* by_user */); |
| 187 | 184 |
| 188 break; | 185 break; |
| 189 } | 186 } |
| 190 } | 187 } |
| 191 | 188 |
| 189 // Get the budget for the service worker. This will internally record UMA |
| 190 // for budget development work in the future. |
| 191 BackgroundBudgetService* service = |
| 192 BackgroundBudgetServiceFactory::GetForProfile(profile_); |
| 193 double budget = service->GetBudget(origin); |
| 194 |
| 195 // Record the budget available any time the budget is queried. |
| 196 UMA_HISTOGRAM_COUNTS_100("PushMessaging.BackgroundBudget", budget); |
| 197 |
| 198 // Get the site engagement score. Only used for UMA recording. |
| 199 SiteEngagementService* ses_service = SiteEngagementService::Get(profile_); |
| 200 double ses_score = ses_service->GetScore(origin); |
| 201 |
| 202 // Generate histograms for the GetBudget calls which would return "no budget" |
| 203 // or "low budget" if an API was available to app developers. |
| 204 double cost = BackgroundBudgetService::GetCost( |
| 205 BackgroundBudgetService::CostType::SILENT_PUSH); |
| 206 if (budget < cost) |
| 207 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", ses_score); |
| 208 else if (budget < 2.0 * cost) |
| 209 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForLowBudgetOrigin", ses_score); |
| 210 |
| 192 if (notification_needed && !notification_shown) { | 211 if (notification_needed && !notification_shown) { |
| 193 // Only track budget for messages that needed to show a notification but | 212 // If the worker needed to show a notification and didn't, check the budget |
| 194 // did not. | 213 // and take appropriate action. |
| 195 BackgroundBudgetService* service = | 214 CheckForMissedNotification(origin, service_worker_registration_id, |
| 196 BackgroundBudgetServiceFactory::GetForProfile(profile_); | 215 message_handled_closure, budget); |
| 197 double budget = service->GetBudget(origin); | |
| 198 DidGetBudget(origin, service_worker_registration_id, | |
| 199 message_handled_closure, budget); | |
| 200 return; | 216 return; |
| 201 } | 217 } |
| 202 | 218 |
| 203 if (notification_needed && notification_shown) { | 219 if (notification_needed && notification_shown) { |
| 204 RecordUserVisibleStatus( | 220 RecordUserVisibleStatus( |
| 205 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); | 221 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); |
| 206 } else if (!notification_needed && !notification_shown) { | 222 } else if (!notification_needed && !notification_shown) { |
| 207 RecordUserVisibleStatus( | 223 RecordUserVisibleStatus( |
| 208 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 224 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 209 } else { | 225 } else { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 240 | 256 |
| 241 // view-source: pages are considered to be controlled Service Worker clients | 257 // view-source: pages are considered to be controlled Service Worker clients |
| 242 // and thus should be considered when checking the visible URL. However, the | 258 // and thus should be considered when checking the visible URL. However, the |
| 243 // prefix has to be removed before the origins can be compared. | 259 // prefix has to be removed before the origins can be compared. |
| 244 if (visible_url.SchemeIs(content::kViewSourceScheme)) | 260 if (visible_url.SchemeIs(content::kViewSourceScheme)) |
| 245 visible_url = GURL(visible_url.GetContent()); | 261 visible_url = GURL(visible_url.GetContent()); |
| 246 | 262 |
| 247 return visible_url.GetOrigin() == origin; | 263 return visible_url.GetOrigin() == origin; |
| 248 } | 264 } |
| 249 | 265 |
| 250 void PushMessagingNotificationManager::DidGetBudget( | 266 void PushMessagingNotificationManager::CheckForMissedNotification( |
| 251 const GURL& origin, | 267 const GURL& origin, |
| 252 int64_t service_worker_registration_id, | 268 int64_t service_worker_registration_id, |
| 253 const base::Closure& message_handled_closure, | 269 const base::Closure& message_handled_closure, |
| 254 const double budget) { | 270 const double budget) { |
| 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 271 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 256 | 272 |
| 257 // If the service needed to show a notification but did not, update the | 273 // If the service needed to show a notification but did not, update the |
| 258 // budget. | 274 // budget. |
| 259 if (budget >= kBackgroundProcessingCost) { | 275 double cost = BackgroundBudgetService::GetCost( |
| 276 BackgroundBudgetService::CostType::SILENT_PUSH); |
| 277 if (budget >= cost) { |
| 260 // Update the stored budget. | 278 // Update the stored budget. |
| 261 BackgroundBudgetService* service = | 279 BackgroundBudgetService* service = |
| 262 BackgroundBudgetServiceFactory::GetForProfile(profile_); | 280 BackgroundBudgetServiceFactory::GetForProfile(profile_); |
| 263 service->StoreBudget(origin, budget - kBackgroundProcessingCost); | 281 service->StoreBudget(origin, budget - cost); |
| 264 | 282 |
| 265 RecordUserVisibleStatus( | 283 RecordUserVisibleStatus( |
| 266 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); | 284 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); |
| 267 message_handled_closure.Run(); | 285 message_handled_closure.Run(); |
| 268 return; | 286 return; |
| 269 } | 287 } |
| 270 | 288 |
| 271 RecordUserVisibleStatus( | 289 RecordUserVisibleStatus( |
| 272 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); | 290 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); |
| 273 rappor::SampleDomainAndRegistryFromGURL( | 291 rappor::SampleDomainAndRegistryFromGURL( |
| 274 g_browser_process->rappor_service(), | 292 g_browser_process->rappor_service(), |
| 275 "PushMessaging.GenericNotificationShown.Origin", origin); | 293 "PushMessaging.GenericNotificationShown.Origin", origin); |
| 276 | 294 |
| 277 // The site failed to show a notification when one was needed, and they have | 295 // The site failed to show a notification when one was needed, and they don't |
| 278 // already failed once in the previous 10 push messages, so we will show a | 296 // have enough budget to cover the cost of suppressing, so we will show a |
| 279 // generic notification. See https://crbug.com/437277. | 297 // generic notification. |
| 280 NotificationDatabaseData database_data = | 298 NotificationDatabaseData database_data = |
| 281 CreateDatabaseData(origin, service_worker_registration_id); | 299 CreateDatabaseData(origin, service_worker_registration_id); |
| 282 scoped_refptr<PlatformNotificationContext> notification_context = | 300 scoped_refptr<PlatformNotificationContext> notification_context = |
| 283 GetStoragePartition(profile_, origin)->GetPlatformNotificationContext(); | 301 GetStoragePartition(profile_, origin)->GetPlatformNotificationContext(); |
| 284 BrowserThread::PostTask( | 302 BrowserThread::PostTask( |
| 285 BrowserThread::IO, FROM_HERE, | 303 BrowserThread::IO, FROM_HERE, |
| 286 base::Bind(&PlatformNotificationContext::WriteNotificationData, | 304 base::Bind(&PlatformNotificationContext::WriteNotificationData, |
| 287 notification_context, origin, database_data, | 305 notification_context, origin, database_data, |
| 288 base::Bind(&PushMessagingNotificationManager:: | 306 base::Bind(&PushMessagingNotificationManager:: |
| 289 DidWriteNotificationDataIOProxy, | 307 DidWriteNotificationDataIOProxy, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 320 message_handled_closure.Run(); | 338 message_handled_closure.Run(); |
| 321 return; | 339 return; |
| 322 } | 340 } |
| 323 | 341 |
| 324 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 342 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 325 profile_, persistent_notification_id, origin, notification_data, | 343 profile_, persistent_notification_id, origin, notification_data, |
| 326 NotificationResources()); | 344 NotificationResources()); |
| 327 | 345 |
| 328 message_handled_closure.Run(); | 346 message_handled_closure.Run(); |
| 329 } | 347 } |
| OLD | NEW |