| 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 |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 14 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/push_messaging/background_budget_service.h" |
| 16 #include "chrome/browser/push_messaging/push_messaging_constants.h" | 17 #include "chrome/browser/push_messaging/push_messaging_constants.h" |
| 17 #include "chrome/common/features.h" | 18 #include "chrome/common/features.h" |
| 18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 20 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 21 #include "components/rappor/rappor_utils.h" | 22 #include "components/rappor/rappor_utils.h" |
| 22 #include "components/url_formatter/elide_url.h" | 23 #include "components/url_formatter/elide_url.h" |
| 23 #include "content/public/browser/browser_context.h" | 24 #include "content/public/browser/browser_context.h" |
| 24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/platform_notification_context.h" | 26 #include "content/public/browser/platform_notification_context.h" |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 profile_, notification_database_data.notification_id, | 186 profile_, notification_database_data.notification_id, |
| 186 notification_database_data.origin, false /* by_user */); | 187 notification_database_data.origin, false /* by_user */); |
| 187 | 188 |
| 188 break; | 189 break; |
| 189 } | 190 } |
| 190 } | 191 } |
| 191 | 192 |
| 192 // Don't track push messages that didn't show a notification but were exempt | 193 // Don't track push messages that didn't show a notification but were exempt |
| 193 // from needing to do so. | 194 // from needing to do so. |
| 194 if (notification_shown || notification_needed) { | 195 if (notification_shown || notification_needed) { |
| 195 ServiceWorkerContext* service_worker_context = | 196 chrome::BackgroundBudgetService::GetBudget( |
| 196 GetStoragePartition(profile_, origin)->GetServiceWorkerContext(); | 197 profile_, origin, |
| 197 | 198 base::Bind(&PushMessagingNotificationManager::DidGetBudget, |
| 198 PushMessagingService::GetNotificationsShownByLastFewPushes( | |
| 199 service_worker_context, service_worker_registration_id, | |
| 200 base::Bind(&PushMessagingNotificationManager:: | |
| 201 DidGetNotificationsShownAndNeeded, | |
| 202 weak_factory_.GetWeakPtr(), origin, | 199 weak_factory_.GetWeakPtr(), origin, |
| 203 service_worker_registration_id, notification_shown, | 200 service_worker_registration_id, notification_shown, |
| 204 notification_needed, message_handled_closure)); | 201 notification_needed, message_handled_closure)); |
| 202 |
| 205 } else { | 203 } else { |
| 206 RecordUserVisibleStatus( | 204 RecordUserVisibleStatus( |
| 207 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 205 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 208 message_handled_closure.Run(); | 206 message_handled_closure.Run(); |
| 209 } | 207 } |
| 210 } | 208 } |
| 211 | 209 |
| 212 bool PushMessagingNotificationManager::IsTabVisible( | 210 bool PushMessagingNotificationManager::IsTabVisible( |
| 213 Profile* profile, | 211 Profile* profile, |
| 214 WebContents* active_web_contents, | 212 WebContents* active_web_contents, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 235 | 233 |
| 236 // view-source: pages are considered to be controlled Service Worker clients | 234 // view-source: pages are considered to be controlled Service Worker clients |
| 237 // and thus should be considered when checking the visible URL. However, the | 235 // and thus should be considered when checking the visible URL. However, the |
| 238 // prefix has to be removed before the origins can be compared. | 236 // prefix has to be removed before the origins can be compared. |
| 239 if (visible_url.SchemeIs(content::kViewSourceScheme)) | 237 if (visible_url.SchemeIs(content::kViewSourceScheme)) |
| 240 visible_url = GURL(visible_url.GetContent()); | 238 visible_url = GURL(visible_url.GetContent()); |
| 241 | 239 |
| 242 return visible_url.GetOrigin() == origin; | 240 return visible_url.GetOrigin() == origin; |
| 243 } | 241 } |
| 244 | 242 |
| 245 void PushMessagingNotificationManager::DidGetNotificationsShownAndNeeded( | 243 void PushMessagingNotificationManager::DidGetBudget( |
| 246 const GURL& origin, | 244 const GURL& origin, |
| 247 int64_t service_worker_registration_id, | 245 int64_t service_worker_registration_id, |
| 248 bool notification_shown, | 246 bool notification_shown, |
| 249 bool notification_needed, | 247 bool notification_needed, |
| 250 const base::Closure& message_handled_closure, | 248 const base::Closure& message_handled_closure, |
| 251 const std::string& data, | 249 const std::string& data) { |
| 252 bool success, | |
| 253 bool not_found) { | |
| 254 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 255 ServiceWorkerContext* service_worker_context = | |
| 256 GetStoragePartition(profile_, origin)->GetServiceWorkerContext(); | |
| 257 | 251 |
| 258 // We remember whether the last (up to) 10 pushes showed notifications. | 252 // We remember whether the last (up to) 10 pushes showed notifications. |
| 259 const size_t MISSED_NOTIFICATIONS_LENGTH = 10; | 253 const size_t MISSED_NOTIFICATIONS_LENGTH = 10; |
| 260 // data is a string like "0001000", where '0' means shown, and '1' means | 254 // data is a string like "0001000", where '0' means shown, and '1' means |
| 261 // needed but not shown. We manipulate it in bitset form. | 255 // needed but not shown. We manipulate it in bitset form. |
| 262 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); | 256 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); |
| 263 | 257 |
| 264 DCHECK(notification_shown || notification_needed); // Caller must ensure this | 258 DCHECK(notification_shown || notification_needed); // Caller must ensure this |
| 265 bool needed_but_not_shown = notification_needed && !notification_shown; | 259 bool needed_but_not_shown = notification_needed && !notification_shown; |
| 266 | 260 |
| 267 // New entries go at the end, and old ones are shifted off the beginning once | 261 // New entries go at the end, and old ones are shifted off the beginning once |
| 268 // the history length is exceeded. | 262 // the history length is exceeded. |
| 269 missed_notifications <<= 1; | 263 missed_notifications <<= 1; |
| 270 missed_notifications[0] = needed_but_not_shown; | 264 missed_notifications[0] = needed_but_not_shown; |
| 271 std::string updated_data(missed_notifications. | 265 std::string updated_data(missed_notifications. |
| 272 to_string<char, std::string::traits_type, std::string::allocator_type>()); | 266 to_string<char, std::string::traits_type, std::string::allocator_type>()); |
| 273 PushMessagingService::SetNotificationsShownByLastFewPushes( | 267 chrome::BackgroundBudgetService::StoreBudget( |
| 274 service_worker_context, service_worker_registration_id, origin, | 268 profile_, origin, updated_data, |
| 275 updated_data, | |
| 276 base::Bind(&IgnoreResult)); // This is a heuristic; ignore failure. | 269 base::Bind(&IgnoreResult)); // This is a heuristic; ignore failure. |
| 277 | 270 |
| 278 if (notification_shown) { | 271 if (notification_shown) { |
| 279 RecordUserVisibleStatus( | 272 RecordUserVisibleStatus( |
| 280 notification_needed | 273 notification_needed |
| 281 ? content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN | 274 ? content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN |
| 282 : content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); | 275 : content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); |
| 283 message_handled_closure.Run(); | 276 message_handled_closure.Run(); |
| 284 return; | 277 return; |
| 285 } | 278 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 message_handled_closure.Run(); | 336 message_handled_closure.Run(); |
| 344 return; | 337 return; |
| 345 } | 338 } |
| 346 | 339 |
| 347 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 340 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 348 profile_, persistent_notification_id, origin, notification_data, | 341 profile_, persistent_notification_id, origin, notification_data, |
| 349 NotificationResources()); | 342 NotificationResources()); |
| 350 | 343 |
| 351 message_handled_closure.Run(); | 344 message_handled_closure.Run(); |
| 352 } | 345 } |
| OLD | NEW |