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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 platform_notification_service->ClosePersistentNotification( | 180 platform_notification_service->ClosePersistentNotification( |
| 181 profile_, notification_database_data.notification_id); | 181 profile_, notification_database_data.notification_id); |
| 182 platform_notification_service->OnPersistentNotificationClose( | 182 platform_notification_service->OnPersistentNotificationClose( |
| 183 profile_, notification_database_data.notification_id, | 183 profile_, notification_database_data.notification_id, |
| 184 notification_database_data.origin, false /* by_user */); | 184 notification_database_data.origin, false /* by_user */); |
| 185 | 185 |
| 186 break; | 186 break; |
| 187 } | 187 } |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Get the budget for the origin. | |
| 191 BudgetManager* manager = BudgetManagerFactory::GetForProfile(profile_); | |
| 192 manager->GetBudget( | |
| 193 origin, | |
| 194 base::Bind(&PushMessagingNotificationManager::DidGetBudget, | |
| 195 weak_factory_.GetWeakPtr(), origin, | |
| 196 service_worker_registration_id, message_handled_closure, | |
| 197 notification_needed, notification_shown)); | |
| 198 } | |
| 199 | |
| 200 void PushMessagingNotificationManager::DidGetBudget( | |
| 201 const GURL& origin, | |
| 202 int64_t service_worker_registration_id, | |
| 203 const base::Closure& message_handled_closure, | |
| 204 bool notification_needed, | |
| 205 bool notification_shown, | |
| 206 const double budget) { | |
| 207 // Record the budget available any time the budget is queried. | |
| 208 UMA_HISTOGRAM_COUNTS_100("PushMessaging.BackgroundBudget", budget); | |
| 209 | |
| 210 // Get the site engagement score. Only used for UMA recording. | |
| 211 SiteEngagementService* ses_service = SiteEngagementService::Get(profile_); | |
| 212 double ses_score = ses_service->GetScore(origin); | |
| 213 | |
| 214 // Generate histograms for the GetBudget calls which would return "no budget" | |
| 215 // or "low budget" if an API was available to app developers. | |
| 216 double cost = | |
| 217 BudgetManager::GetCost(blink::mojom::BudgetOperationType::SILENT_PUSH); | |
| 218 if (budget < cost) | |
| 219 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", ses_score); | |
| 220 else if (budget < 2.0 * cost) | |
| 221 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForLowBudgetOrigin", ses_score); | |
| 222 | |
| 223 if (notification_needed && !notification_shown) { | 190 if (notification_needed && !notification_shown) { |
| 224 // If the worker needed to show a notification and didn't, check the budget | 191 // If the worker needed to show a notification and didn't, see if a silent |
| 225 // and take appropriate action. | 192 // push was allowed. |
| 226 CheckForMissedNotification(origin, service_worker_registration_id, | 193 BudgetManager* manager = BudgetManagerFactory::GetForProfile(profile_); |
| 227 message_handled_closure, budget); | 194 BudgetManager::ConsumeCallback consumeCallback = |
|
Peter Beverloo
2016/08/26 14:56:44
nit: I wouldn't use the local here. Up to you.
harkness
2016/08/31 13:15:35
Done.
| |
| 195 base::Bind(&PushMessagingNotificationManager::ProcessSilentPush, | |
| 196 weak_factory_.GetWeakPtr(), origin, | |
| 197 service_worker_registration_id, message_handled_closure); | |
| 198 manager->Consume(origin, blink::mojom::BudgetOperationType::SILENT_PUSH, | |
| 199 consumeCallback); | |
| 228 return; | 200 return; |
| 229 } | 201 } |
| 230 | 202 |
| 231 if (notification_needed && notification_shown) { | 203 if (notification_needed && notification_shown) { |
| 232 RecordUserVisibleStatus( | 204 RecordUserVisibleStatus( |
| 233 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); | 205 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); |
| 234 } else if (!notification_needed && !notification_shown) { | 206 } else if (!notification_needed && !notification_shown) { |
| 235 RecordUserVisibleStatus( | 207 RecordUserVisibleStatus( |
| 236 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 208 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 237 } else { | 209 } else { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 268 | 240 |
| 269 // view-source: pages are considered to be controlled Service Worker clients | 241 // view-source: pages are considered to be controlled Service Worker clients |
| 270 // and thus should be considered when checking the visible URL. However, the | 242 // and thus should be considered when checking the visible URL. However, the |
| 271 // prefix has to be removed before the origins can be compared. | 243 // prefix has to be removed before the origins can be compared. |
| 272 if (visible_url.SchemeIs(content::kViewSourceScheme)) | 244 if (visible_url.SchemeIs(content::kViewSourceScheme)) |
| 273 visible_url = GURL(visible_url.GetContent()); | 245 visible_url = GURL(visible_url.GetContent()); |
| 274 | 246 |
| 275 return visible_url.GetOrigin() == origin; | 247 return visible_url.GetOrigin() == origin; |
| 276 } | 248 } |
| 277 | 249 |
| 278 void PushMessagingNotificationManager::CheckForMissedNotification( | 250 void PushMessagingNotificationManager::ProcessSilentPush( |
| 279 const GURL& origin, | 251 const GURL& origin, |
| 280 int64_t service_worker_registration_id, | 252 int64_t service_worker_registration_id, |
| 281 const base::Closure& message_handled_closure, | 253 const base::Closure& message_handled_closure, |
| 282 const double budget) { | 254 bool silent_push_allowed) { |
| 283 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 284 | 256 |
| 285 // If the service needed to show a notification but did not, update the | 257 // If the service was allowed to issue a silent push, just return. |
| 286 // budget. | 258 if (silent_push_allowed) { |
| 287 double cost = | |
| 288 BudgetManager::GetCost(blink::mojom::BudgetOperationType::SILENT_PUSH); | |
| 289 if (budget >= cost) { | |
| 290 RecordUserVisibleStatus( | 259 RecordUserVisibleStatus( |
| 291 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); | 260 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); |
| 292 | |
| 293 BudgetManager* manager = BudgetManagerFactory::GetForProfile(profile_); | |
| 294 // Update the stored budget. | |
| 295 manager->StoreBudget(origin, budget - cost, message_handled_closure); | |
| 296 | |
| 297 return; | 261 return; |
| 298 } | 262 } |
| 299 | 263 |
| 300 RecordUserVisibleStatus( | 264 RecordUserVisibleStatus( |
| 301 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); | 265 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); |
| 302 rappor::SampleDomainAndRegistryFromGURL( | 266 rappor::SampleDomainAndRegistryFromGURL( |
| 303 g_browser_process->rappor_service(), | 267 g_browser_process->rappor_service(), |
| 304 "PushMessaging.GenericNotificationShown.Origin", origin); | 268 "PushMessaging.GenericNotificationShown.Origin", origin); |
| 305 | 269 |
| 306 // The site failed to show a notification when one was needed, and they don't | 270 // The site failed to show a notification when one was needed, and they don't |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 // Do not pass service worker scope. The origin will be used instead of the | 317 // Do not pass service worker scope. The origin will be used instead of the |
| 354 // service worker scope to determine whether a notification should be | 318 // service worker scope to determine whether a notification should be |
| 355 // attributed to a WebAPK on Android. This is OK because this code path is hit | 319 // attributed to a WebAPK on Android. This is OK because this code path is hit |
| 356 // rarely. | 320 // rarely. |
| 357 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 321 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 358 profile_, persistent_notification_id, GURL() /* service_worker_scope */, | 322 profile_, persistent_notification_id, GURL() /* service_worker_scope */, |
| 359 origin, notification_data, NotificationResources()); | 323 origin, notification_data, NotificationResources()); |
| 360 | 324 |
| 361 message_handled_closure.Run(); | 325 message_handled_closure.Run(); |
| 362 } | 326 } |
| OLD | NEW |