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