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