Chromium Code Reviews| 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 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 using content::BrowserThread; | 45 using content::BrowserThread; |
| 46 using content::NotificationDatabaseData; | 46 using content::NotificationDatabaseData; |
| 47 using content::NotificationResources; | 47 using content::NotificationResources; |
| 48 using content::PlatformNotificationContext; | 48 using content::PlatformNotificationContext; |
| 49 using content::PlatformNotificationData; | 49 using content::PlatformNotificationData; |
| 50 using content::PushMessagingService; | 50 using content::PushMessagingService; |
| 51 using content::ServiceWorkerContext; | 51 using content::ServiceWorkerContext; |
| 52 using content::WebContents; | 52 using content::WebContents; |
| 53 | 53 |
| 54 namespace { | 54 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) { | 55 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { |
| 60 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", status, | 56 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", status, |
| 61 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); | 57 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); |
| 62 } | 58 } |
| 63 | 59 |
| 60 void RecordBackgroundBudget(double budget) { | |
|
Peter Beverloo
2016/05/17 11:04:34
dito re: inline
harkness
2016/05/17 13:08:16
Done.
| |
| 61 UMA_HISTOGRAM_COUNTS("PushMessaging.BackgroundBudget", budget); | |
| 62 } | |
| 63 | |
| 64 content::StoragePartition* GetStoragePartition(Profile* profile, | 64 content::StoragePartition* GetStoragePartition(Profile* profile, |
| 65 const GURL& origin) { | 65 const GURL& origin) { |
| 66 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); | 66 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); |
| 67 } | 67 } |
| 68 | 68 |
| 69 NotificationDatabaseData CreateDatabaseData( | 69 NotificationDatabaseData CreateDatabaseData( |
| 70 const GURL& origin, | 70 const GURL& origin, |
| 71 int64_t service_worker_registration_id) { | 71 int64_t service_worker_registration_id) { |
| 72 PlatformNotificationData notification_data; | 72 PlatformNotificationData notification_data; |
| 73 notification_data.title = url_formatter::FormatUrlForSecurityDisplay( | 73 notification_data.title = url_formatter::FormatUrlForSecurityDisplay( |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 platform_notification_service->ClosePersistentNotification( | 182 platform_notification_service->ClosePersistentNotification( |
| 183 profile_, notification_database_data.notification_id); | 183 profile_, notification_database_data.notification_id); |
| 184 platform_notification_service->OnPersistentNotificationClose( | 184 platform_notification_service->OnPersistentNotificationClose( |
| 185 profile_, notification_database_data.notification_id, | 185 profile_, notification_database_data.notification_id, |
| 186 notification_database_data.origin, false /* by_user */); | 186 notification_database_data.origin, false /* by_user */); |
| 187 | 187 |
| 188 break; | 188 break; |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 | 191 |
| 192 // Get the budget for the service worker. This will internally record UMA | |
| 193 // for budget development work in the future. | |
| 194 BackgroundBudgetService* service = | |
| 195 BackgroundBudgetServiceFactory::GetForProfile(profile_); | |
| 196 double budget = service->GetBudget(origin); | |
| 197 | |
| 198 // Record the budget available any time the budget is queried. | |
| 199 RecordBackgroundBudget(budget); | |
| 200 | |
| 192 if (notification_needed && !notification_shown) { | 201 if (notification_needed && !notification_shown) { |
| 193 // Only track budget for messages that needed to show a notification but | 202 // If the worker needed to show a notification and didn't, check the budget |
| 194 // did not. | 203 // and take appropriate action. |
| 195 BackgroundBudgetService* service = | 204 CheckForMissedNotification(origin, service_worker_registration_id, |
| 196 BackgroundBudgetServiceFactory::GetForProfile(profile_); | 205 message_handled_closure, budget); |
| 197 double budget = service->GetBudget(origin); | |
| 198 DidGetBudget(origin, service_worker_registration_id, | |
| 199 message_handled_closure, budget); | |
| 200 return; | 206 return; |
| 201 } | 207 } |
| 202 | 208 |
| 203 if (notification_needed && notification_shown) { | 209 if (notification_needed && notification_shown) { |
| 204 RecordUserVisibleStatus( | 210 RecordUserVisibleStatus( |
| 205 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); | 211 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); |
| 206 } else if (!notification_needed && !notification_shown) { | 212 } else if (!notification_needed && !notification_shown) { |
| 207 RecordUserVisibleStatus( | 213 RecordUserVisibleStatus( |
| 208 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 214 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 209 } else { | 215 } else { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 240 | 246 |
| 241 // view-source: pages are considered to be controlled Service Worker clients | 247 // view-source: pages are considered to be controlled Service Worker clients |
| 242 // and thus should be considered when checking the visible URL. However, the | 248 // and thus should be considered when checking the visible URL. However, the |
| 243 // prefix has to be removed before the origins can be compared. | 249 // prefix has to be removed before the origins can be compared. |
| 244 if (visible_url.SchemeIs(content::kViewSourceScheme)) | 250 if (visible_url.SchemeIs(content::kViewSourceScheme)) |
| 245 visible_url = GURL(visible_url.GetContent()); | 251 visible_url = GURL(visible_url.GetContent()); |
| 246 | 252 |
| 247 return visible_url.GetOrigin() == origin; | 253 return visible_url.GetOrigin() == origin; |
| 248 } | 254 } |
| 249 | 255 |
| 250 void PushMessagingNotificationManager::DidGetBudget( | 256 void PushMessagingNotificationManager::CheckForMissedNotification( |
| 251 const GURL& origin, | 257 const GURL& origin, |
| 252 int64_t service_worker_registration_id, | 258 int64_t service_worker_registration_id, |
| 253 const base::Closure& message_handled_closure, | 259 const base::Closure& message_handled_closure, |
| 254 const double budget) { | 260 const double budget) { |
| 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 261 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 256 | 262 |
| 257 // If the service needed to show a notification but did not, update the | 263 // If the service needed to show a notification but did not, update the |
| 258 // budget. | 264 // budget. |
| 259 if (budget >= kBackgroundProcessingCost) { | 265 double backgroundProcessingCost = |
|
Peter Beverloo
2016/05/17 11:04:34
nit: background_processing_cost
harkness
2016/05/17 13:08:16
Done.
| |
| 266 BackgroundBudgetService::GetBackgroundProcessingCost(); | |
| 267 if (budget >= backgroundProcessingCost) { | |
| 260 // Update the stored budget. | 268 // Update the stored budget. |
| 261 BackgroundBudgetService* service = | 269 BackgroundBudgetService* service = |
| 262 BackgroundBudgetServiceFactory::GetForProfile(profile_); | 270 BackgroundBudgetServiceFactory::GetForProfile(profile_); |
| 263 service->StoreBudget(origin, budget - kBackgroundProcessingCost); | 271 service->StoreBudget(origin, budget - backgroundProcessingCost); |
| 264 | 272 |
| 265 RecordUserVisibleStatus( | 273 RecordUserVisibleStatus( |
| 266 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); | 274 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); |
| 267 message_handled_closure.Run(); | 275 message_handled_closure.Run(); |
| 268 return; | 276 return; |
| 269 } | 277 } |
| 270 | 278 |
| 271 RecordUserVisibleStatus( | 279 RecordUserVisibleStatus( |
| 272 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); | 280 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); |
| 273 rappor::SampleDomainAndRegistryFromGURL( | 281 rappor::SampleDomainAndRegistryFromGURL( |
| 274 g_browser_process->rappor_service(), | 282 g_browser_process->rappor_service(), |
| 275 "PushMessaging.GenericNotificationShown.Origin", origin); | 283 "PushMessaging.GenericNotificationShown.Origin", origin); |
| 276 | 284 |
| 277 // The site failed to show a notification when one was needed, and they have | 285 // 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 | 286 // have enough budget to cover the cost of suppressing, so we will show a |
| 279 // generic notification. See https://crbug.com/437277. | 287 // generic notification. |
| 280 NotificationDatabaseData database_data = | 288 NotificationDatabaseData database_data = |
| 281 CreateDatabaseData(origin, service_worker_registration_id); | 289 CreateDatabaseData(origin, service_worker_registration_id); |
| 282 scoped_refptr<PlatformNotificationContext> notification_context = | 290 scoped_refptr<PlatformNotificationContext> notification_context = |
| 283 GetStoragePartition(profile_, origin)->GetPlatformNotificationContext(); | 291 GetStoragePartition(profile_, origin)->GetPlatformNotificationContext(); |
| 284 BrowserThread::PostTask( | 292 BrowserThread::PostTask( |
| 285 BrowserThread::IO, FROM_HERE, | 293 BrowserThread::IO, FROM_HERE, |
| 286 base::Bind(&PlatformNotificationContext::WriteNotificationData, | 294 base::Bind(&PlatformNotificationContext::WriteNotificationData, |
| 287 notification_context, origin, database_data, | 295 notification_context, origin, database_data, |
| 288 base::Bind(&PushMessagingNotificationManager:: | 296 base::Bind(&PushMessagingNotificationManager:: |
| 289 DidWriteNotificationDataIOProxy, | 297 DidWriteNotificationDataIOProxy, |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 320 message_handled_closure.Run(); | 328 message_handled_closure.Run(); |
| 321 return; | 329 return; |
| 322 } | 330 } |
| 323 | 331 |
| 324 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 332 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 325 profile_, persistent_notification_id, origin, notification_data, | 333 profile_, persistent_notification_id, origin, notification_data, |
| 326 NotificationResources()); | 334 NotificationResources()); |
| 327 | 335 |
| 328 message_handled_closure.Run(); | 336 message_handled_closure.Run(); |
| 329 } | 337 } |
| OLD | NEW |