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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 platform_notification_service->ClosePersistentNotification( | 179 platform_notification_service->ClosePersistentNotification( |
| 180 profile_, notification_database_data.notification_id); | 180 profile_, notification_database_data.notification_id); |
| 181 platform_notification_service->OnPersistentNotificationClose( | 181 platform_notification_service->OnPersistentNotificationClose( |
| 182 profile_, notification_database_data.notification_id, | 182 profile_, notification_database_data.notification_id, |
| 183 notification_database_data.origin, false /* by_user */); | 183 notification_database_data.origin, false /* by_user */); |
| 184 | 184 |
| 185 break; | 185 break; |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Don't track push messages that didn't show a notification but were exempt | 189 if (notification_needed && !notification_shown) { |
| 190 // from needing to do so. | 190 // Only track budget for messages that needed to show a notification but |
| 191 if (notification_shown || notification_needed) { | 191 // did not. |
| 192 BackgroundBudgetService* service = | 192 BackgroundBudgetService* service = |
| 193 BackgroundBudgetServiceFactory::GetForProfile(profile_); | 193 BackgroundBudgetServiceFactory::GetForProfile(profile_); |
| 194 std::string notification_history = service->GetBudget(origin); | 194 double budget = service->GetBudget(origin); |
| 195 DidGetBudget(origin, service_worker_registration_id, notification_shown, | 195 DidGetBudget(origin, service_worker_registration_id, |
| 196 notification_needed, message_handled_closure, | 196 message_handled_closure, budget); |
| 197 notification_history); | 197 return; |
| 198 } | |
| 199 | |
| 200 if (notification_needed && notification_shown) { | |
| 201 RecordUserVisibleStatus( | |
| 202 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN); | |
| 203 } else if (!notification_needed && !notification_shown) { | |
| 204 RecordUserVisibleStatus( | |
| 205 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | |
| 198 } else { | 206 } else { |
| 199 RecordUserVisibleStatus( | 207 RecordUserVisibleStatus( |
| 200 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 208 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); |
| 201 message_handled_closure.Run(); | |
| 202 } | 209 } |
| 210 | |
| 211 message_handled_closure.Run(); | |
| 203 } | 212 } |
| 204 | 213 |
| 205 bool PushMessagingNotificationManager::IsTabVisible( | 214 bool PushMessagingNotificationManager::IsTabVisible( |
| 206 Profile* profile, | 215 Profile* profile, |
| 207 WebContents* active_web_contents, | 216 WebContents* active_web_contents, |
| 208 const GURL& origin) { | 217 const GURL& origin) { |
| 209 if (!active_web_contents || !active_web_contents->GetMainFrame()) | 218 if (!active_web_contents || !active_web_contents->GetMainFrame()) |
| 210 return false; | 219 return false; |
| 211 | 220 |
| 212 // Don't leak information from other profiles. | 221 // 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. | 240 // prefix has to be removed before the origins can be compared. |
| 232 if (visible_url.SchemeIs(content::kViewSourceScheme)) | 241 if (visible_url.SchemeIs(content::kViewSourceScheme)) |
| 233 visible_url = GURL(visible_url.GetContent()); | 242 visible_url = GURL(visible_url.GetContent()); |
| 234 | 243 |
| 235 return visible_url.GetOrigin() == origin; | 244 return visible_url.GetOrigin() == origin; |
| 236 } | 245 } |
| 237 | 246 |
| 238 void PushMessagingNotificationManager::DidGetBudget( | 247 void PushMessagingNotificationManager::DidGetBudget( |
| 239 const GURL& origin, | 248 const GURL& origin, |
| 240 int64_t service_worker_registration_id, | 249 int64_t service_worker_registration_id, |
| 241 bool notification_shown, | |
| 242 bool notification_needed, | |
| 243 const base::Closure& message_handled_closure, | 250 const base::Closure& message_handled_closure, |
| 244 const std::string& data) { | 251 const double budget) { |
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 252 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 246 | 253 |
| 247 // We remember whether the last (up to) 10 pushes showed notifications. | 254 // If the service needed to show a notification but did not, update the |
| 248 const size_t MISSED_NOTIFICATIONS_LENGTH = 10; | 255 // budget. Currently this just subtracts 1.0, but we may expand the cost to |
| 249 // data is a string like "0001000", where '0' means shown, and '1' means | 256 // include things like whether wifi is available in the mobile case. |
| 250 // needed but not shown. We manipulate it in bitset form. | 257 double cost = 1.0; |
|
Peter Beverloo
2016/05/04 15:41:52
nit: this should be a constant at the top of this
harkness
2016/05/09 15:33:27
Done.
| |
| 251 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); | 258 if (budget >= cost) { |
| 259 // Update the stored budget. | |
| 260 BackgroundBudgetService* service = | |
| 261 BackgroundBudgetServiceFactory::GetForProfile(profile_); | |
| 262 service->StoreBudget(origin, budget - cost); | |
| 252 | 263 |
| 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( | 264 RecordUserVisibleStatus( |
| 277 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); | 265 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_USED_GRACE); |
|
Peter Beverloo
2016/05/04 15:41:52
I think we should introduce a new enum value for b
harkness
2016/05/09 15:33:27
Do you mind if I do that in my next CL, when I do
Peter Beverloo
2016/05/09 17:12:06
OK.
| |
| 278 message_handled_closure.Run(); | 266 message_handled_closure.Run(); |
| 279 return; | 267 return; |
| 280 } | 268 } |
| 269 | |
| 281 RecordUserVisibleStatus( | 270 RecordUserVisibleStatus( |
| 282 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); | 271 content::PUSH_USER_VISIBLE_STATUS_REQUIRED_BUT_NOT_SHOWN_GRACE_EXCEEDED); |
| 283 rappor::SampleDomainAndRegistryFromGURL( | 272 rappor::SampleDomainAndRegistryFromGURL( |
| 284 g_browser_process->rappor_service(), | 273 g_browser_process->rappor_service(), |
| 285 "PushMessaging.GenericNotificationShown.Origin", origin); | 274 "PushMessaging.GenericNotificationShown.Origin", origin); |
| 286 | 275 |
| 287 // The site failed to show a notification when one was needed, and they have | 276 // 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 | 277 // already failed once in the previous 10 push messages, so we will show a |
| 289 // generic notification. See https://crbug.com/437277. | 278 // generic notification. See https://crbug.com/437277. |
| 290 NotificationDatabaseData database_data = | 279 NotificationDatabaseData database_data = |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 message_handled_closure.Run(); | 319 message_handled_closure.Run(); |
| 331 return; | 320 return; |
| 332 } | 321 } |
| 333 | 322 |
| 334 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 323 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 335 profile_, persistent_notification_id, origin, notification_data, | 324 profile_, persistent_notification_id, origin, notification_data, |
| 336 NotificationResources()); | 325 NotificationResources()); |
| 337 | 326 |
| 338 message_handled_closure.Run(); | 327 message_handled_closure.Run(); |
| 339 } | 328 } |
| OLD | NEW |