| 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; |
| 55 | 58 |
| 56 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { | 59 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { |
| 57 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", status, | 60 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", status, |
| 58 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); | 61 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); |
| 59 } | 62 } |
| 60 | 63 |
| 61 content::StoragePartition* GetStoragePartition(Profile* profile, | 64 content::StoragePartition* GetStoragePartition(Profile* profile, |
| 62 const GURL& origin) { | 65 const GURL& origin) { |
| 63 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); | 66 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); |
| 64 } | 67 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 platform_notification_service->ClosePersistentNotification( | 182 platform_notification_service->ClosePersistentNotification( |
| 180 profile_, notification_database_data.notification_id); | 183 profile_, notification_database_data.notification_id); |
| 181 platform_notification_service->OnPersistentNotificationClose( | 184 platform_notification_service->OnPersistentNotificationClose( |
| 182 profile_, notification_database_data.notification_id, | 185 profile_, notification_database_data.notification_id, |
| 183 notification_database_data.origin, false /* by_user */); | 186 notification_database_data.origin, false /* by_user */); |
| 184 | 187 |
| 185 break; | 188 break; |
| 186 } | 189 } |
| 187 } | 190 } |
| 188 | 191 |
| 189 // Don't track push messages that didn't show a notification but were exempt | 192 if (notification_needed && !notification_shown) { |
| 190 // from needing to do so. | 193 // Only track budget for messages that needed to show a notification but |
| 191 if (notification_shown || notification_needed) { | 194 // did not. |
| 192 BackgroundBudgetService* service = | 195 BackgroundBudgetService* service = |
| 193 BackgroundBudgetServiceFactory::GetForProfile(profile_); | 196 BackgroundBudgetServiceFactory::GetForProfile(profile_); |
| 194 std::string notification_history = service->GetBudget(origin); | 197 double budget = service->GetBudget(origin); |
| 195 DidGetBudget(origin, service_worker_registration_id, notification_shown, | 198 DidGetBudget(origin, service_worker_registration_id, |
| 196 notification_needed, message_handled_closure, | 199 message_handled_closure, budget); |
| 197 notification_history); | 200 return; |
| 201 } |
| 202 |
| 203 if (notification_needed && notification_shown) { |
| 204 RecordUserVisibleStatus( |
| 205 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); |
| 206 } else if (!notification_needed && !notification_shown) { |
| 207 RecordUserVisibleStatus( |
| 208 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 198 } else { | 209 } else { |
| 199 RecordUserVisibleStatus( | 210 RecordUserVisibleStatus( |
| 200 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 211 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); |
| 201 message_handled_closure.Run(); | |
| 202 } | 212 } |
| 213 |
| 214 message_handled_closure.Run(); |
| 203 } | 215 } |
| 204 | 216 |
| 205 bool PushMessagingNotificationManager::IsTabVisible( | 217 bool PushMessagingNotificationManager::IsTabVisible( |
| 206 Profile* profile, | 218 Profile* profile, |
| 207 WebContents* active_web_contents, | 219 WebContents* active_web_contents, |
| 208 const GURL& origin) { | 220 const GURL& origin) { |
| 209 if (!active_web_contents || !active_web_contents->GetMainFrame()) | 221 if (!active_web_contents || !active_web_contents->GetMainFrame()) |
| 210 return false; | 222 return false; |
| 211 | 223 |
| 212 // Don't leak information from other profiles. | 224 // Don't leak information from other profiles. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 231 // prefix has to be removed before the origins can be compared. | 243 // prefix has to be removed before the origins can be compared. |
| 232 if (visible_url.SchemeIs(content::kViewSourceScheme)) | 244 if (visible_url.SchemeIs(content::kViewSourceScheme)) |
| 233 visible_url = GURL(visible_url.GetContent()); | 245 visible_url = GURL(visible_url.GetContent()); |
| 234 | 246 |
| 235 return visible_url.GetOrigin() == origin; | 247 return visible_url.GetOrigin() == origin; |
| 236 } | 248 } |
| 237 | 249 |
| 238 void PushMessagingNotificationManager::DidGetBudget( | 250 void PushMessagingNotificationManager::DidGetBudget( |
| 239 const GURL& origin, | 251 const GURL& origin, |
| 240 int64_t service_worker_registration_id, | 252 int64_t service_worker_registration_id, |
| 241 bool notification_shown, | |
| 242 bool notification_needed, | |
| 243 const base::Closure& message_handled_closure, | 253 const base::Closure& message_handled_closure, |
| 244 const std::string& data) { | 254 const double budget) { |
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 246 | 256 |
| 247 // We remember whether the last (up to) 10 pushes showed notifications. | 257 // If the service needed to show a notification but did not, update the |
| 248 const size_t MISSED_NOTIFICATIONS_LENGTH = 10; | 258 // budget. |
| 249 // data is a string like "0001000", where '0' means shown, and '1' means | 259 if (budget >= kBackgroundProcessingCost) { |
| 250 // needed but not shown. We manipulate it in bitset form. | 260 // Update the stored budget. |
| 251 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); | 261 BackgroundBudgetService* service = |
| 262 BackgroundBudgetServiceFactory::GetForProfile(profile_); |
| 263 service->StoreBudget(origin, budget - kBackgroundProcessingCost); |
| 252 | 264 |
| 253 DCHECK(notification_shown || notification_needed); // Caller must ensure this | |
| 254 bool needed_but_not_shown = notification_needed && !notification_shown; | |
| 255 | |
| 256 // New entries go at the end, and old ones are shifted off the beginning once | |
| 257 // the history length is exceeded. | |
| 258 missed_notifications <<= 1; | |
| 259 missed_notifications[0] = needed_but_not_shown; | |
| 260 std::string updated_data(missed_notifications. | |
| 261 to_string<char, std::string::traits_type, std::string::allocator_type>()); | |
| 262 BackgroundBudgetService* service = | |
| 263 BackgroundBudgetServiceFactory::GetForProfile(profile_); | |
| 264 service->StoreBudget(origin, updated_data); | |
| 265 | |
| 266 if (notification_shown) { | |
| 267 RecordUserVisibleStatus( | |
| 268 notification_needed | |
| 269 ? content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN | |
| 270 : content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); | |
| 271 message_handled_closure.Run(); | |
| 272 return; | |
| 273 } | |
| 274 DCHECK(needed_but_not_shown); | |
| 275 if (missed_notifications.count() <= 1) { // Apply grace. | |
| 276 RecordUserVisibleStatus( | 265 RecordUserVisibleStatus( |
| 277 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); | 266 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); |
| 278 message_handled_closure.Run(); | 267 message_handled_closure.Run(); |
| 279 return; | 268 return; |
| 280 } | 269 } |
| 270 |
| 281 RecordUserVisibleStatus( | 271 RecordUserVisibleStatus( |
| 282 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); | 272 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); |
| 283 rappor::SampleDomainAndRegistryFromGURL( | 273 rappor::SampleDomainAndRegistryFromGURL( |
| 284 g_browser_process->rappor_service(), | 274 g_browser_process->rappor_service(), |
| 285 "PushMessaging.GenericNotificationShown.Origin", origin); | 275 "PushMessaging.GenericNotificationShown.Origin", origin); |
| 286 | 276 |
| 287 // The site failed to show a notification when one was needed, and they have | 277 // The site failed to show a notification when one was needed, and they have |
| 288 // already failed once in the previous 10 push messages, so we will show a | 278 // already failed once in the previous 10 push messages, so we will show a |
| 289 // generic notification. See https://crbug.com/437277. | 279 // generic notification. See https://crbug.com/437277. |
| 290 NotificationDatabaseData database_data = | 280 NotificationDatabaseData database_data = |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 message_handled_closure.Run(); | 320 message_handled_closure.Run(); |
| 331 return; | 321 return; |
| 332 } | 322 } |
| 333 | 323 |
| 334 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 324 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 335 profile_, persistent_notification_id, origin, notification_data, | 325 profile_, persistent_notification_id, origin, notification_data, |
| 336 NotificationResources()); | 326 NotificationResources()); |
| 337 | 327 |
| 338 message_handled_closure.Run(); | 328 message_handled_closure.Run(); |
| 339 } | 329 } |
| OLD | NEW |