| 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 <bitset> | 7 #include <bitset> |
| 8 | 8 |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 using content::BrowserThread; | 41 using content::BrowserThread; |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { | 45 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { |
| 46 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", | 46 UMA_HISTOGRAM_ENUMERATION("PushMessaging.UserVisibleStatus", |
| 47 status, | 47 status, |
| 48 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); | 48 content::PUSH_USER_VISIBLE_STATUS_LAST + 1); |
| 49 } | 49 } |
| 50 | 50 |
| 51 content::StoragePartition* GetStoragePartition(Profile* profile, |
| 52 const GURL& origin) { |
| 53 return content::BrowserContext::GetStoragePartitionForSite(profile, origin); |
| 54 } |
| 55 |
| 51 } // namespace | 56 } // namespace |
| 52 | 57 |
| 53 PushMessagingNotificationManager::PushMessagingNotificationManager( | 58 PushMessagingNotificationManager::PushMessagingNotificationManager( |
| 54 Profile* profile) | 59 Profile* profile) |
| 55 : profile_(profile), | 60 : profile_(profile), |
| 56 weak_factory_(this) {} | 61 weak_factory_(this) {} |
| 57 | 62 |
| 58 PushMessagingNotificationManager::~PushMessagingNotificationManager() {} | 63 PushMessagingNotificationManager::~PushMessagingNotificationManager() {} |
| 59 | 64 |
| 60 void PushMessagingNotificationManager::EnforceUserVisibleOnlyRequirements( | 65 void PushMessagingNotificationManager::EnforceUserVisibleOnlyRequirements( |
| 61 const GURL& requesting_origin, int64_t service_worker_registration_id, | 66 const GURL& requesting_origin, int64_t service_worker_registration_id, |
| 62 const base::Closure& message_handled_closure) { | 67 const base::Closure& message_handled_closure) { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 // TODO(johnme): Relax this heuristic slightly. | 69 // TODO(johnme): Relax this heuristic slightly. |
| 65 scoped_refptr<content::PlatformNotificationContext> notification_context = | 70 scoped_refptr<content::PlatformNotificationContext> notification_context = |
| 66 content::BrowserContext::GetStoragePartitionForSite( | 71 GetStoragePartition(profile_, requesting_origin) |
| 67 profile_, requesting_origin)->GetPlatformNotificationContext(); | 72 ->GetPlatformNotificationContext(); |
| 68 BrowserThread::PostTask( | 73 BrowserThread::PostTask( |
| 69 BrowserThread::IO, FROM_HERE, | 74 BrowserThread::IO, FROM_HERE, |
| 70 base::Bind( | 75 base::Bind( |
| 71 &content::PlatformNotificationContext | 76 &content::PlatformNotificationContext |
| 72 ::ReadAllNotificationDataForServiceWorkerRegistration, | 77 ::ReadAllNotificationDataForServiceWorkerRegistration, |
| 73 notification_context, | 78 notification_context, |
| 74 requesting_origin, service_worker_registration_id, | 79 requesting_origin, service_worker_registration_id, |
| 75 base::Bind( | 80 base::Bind( |
| 76 &PushMessagingNotificationManager | 81 &PushMessagingNotificationManager |
| 77 ::DidGetNotificationsFromDatabaseIOProxy, | 82 ::DidGetNotificationsFromDatabaseIOProxy, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 notification_database_data.origin, false /* by_user */); | 179 notification_database_data.origin, false /* by_user */); |
| 175 | 180 |
| 176 break; | 181 break; |
| 177 } | 182 } |
| 178 } | 183 } |
| 179 | 184 |
| 180 // Don't track push messages that didn't show a notification but were exempt | 185 // Don't track push messages that didn't show a notification but were exempt |
| 181 // from needing to do so. | 186 // from needing to do so. |
| 182 if (notification_shown || notification_needed) { | 187 if (notification_shown || notification_needed) { |
| 183 content::ServiceWorkerContext* service_worker_context = | 188 content::ServiceWorkerContext* service_worker_context = |
| 184 content::BrowserContext::GetStoragePartitionForSite( | 189 GetStoragePartition(profile_, requesting_origin) |
| 185 profile_, requesting_origin)->GetServiceWorkerContext(); | 190 ->GetServiceWorkerContext(); |
| 186 | 191 |
| 187 content::PushMessagingService::GetNotificationsShownByLastFewPushes( | 192 content::PushMessagingService::GetNotificationsShownByLastFewPushes( |
| 188 service_worker_context, service_worker_registration_id, | 193 service_worker_context, service_worker_registration_id, |
| 189 base::Bind(&PushMessagingNotificationManager | 194 base::Bind(&PushMessagingNotificationManager |
| 190 ::DidGetNotificationsShownAndNeeded, | 195 ::DidGetNotificationsShownAndNeeded, |
| 191 weak_factory_.GetWeakPtr(), | 196 weak_factory_.GetWeakPtr(), |
| 192 requesting_origin, service_worker_registration_id, | 197 requesting_origin, service_worker_registration_id, |
| 193 notification_shown, notification_needed, | 198 notification_shown, notification_needed, |
| 194 message_handled_closure)); | 199 message_handled_closure)); |
| 195 } else { | 200 } else { |
| 196 RecordUserVisibleStatus( | 201 RecordUserVisibleStatus( |
| 197 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 202 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 198 message_handled_closure.Run(); | 203 message_handled_closure.Run(); |
| 199 } | 204 } |
| 200 } | 205 } |
| 201 | 206 |
| 202 static void IgnoreResult(bool unused) { | 207 static void IgnoreResult(bool unused) { |
| 203 } | 208 } |
| 204 | 209 |
| 205 void PushMessagingNotificationManager::DidGetNotificationsShownAndNeeded( | 210 void PushMessagingNotificationManager::DidGetNotificationsShownAndNeeded( |
| 206 const GURL& requesting_origin, int64_t service_worker_registration_id, | 211 const GURL& requesting_origin, int64_t service_worker_registration_id, |
| 207 bool notification_shown, bool notification_needed, | 212 bool notification_shown, bool notification_needed, |
| 208 const base::Closure& message_handled_closure, | 213 const base::Closure& message_handled_closure, |
| 209 const std::string& data, bool success, bool not_found) { | 214 const std::string& data, bool success, bool not_found) { |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 215 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 211 content::ServiceWorkerContext* service_worker_context = | 216 content::ServiceWorkerContext* service_worker_context = |
| 212 content::BrowserContext::GetStoragePartitionForSite( | 217 GetStoragePartition(profile_, requesting_origin) |
| 213 profile_, requesting_origin)->GetServiceWorkerContext(); | 218 ->GetServiceWorkerContext(); |
| 214 | 219 |
| 215 // We remember whether the last (up to) 10 pushes showed notifications. | 220 // We remember whether the last (up to) 10 pushes showed notifications. |
| 216 const size_t MISSED_NOTIFICATIONS_LENGTH = 10; | 221 const size_t MISSED_NOTIFICATIONS_LENGTH = 10; |
| 217 // data is a string like "0001000", where '0' means shown, and '1' means | 222 // data is a string like "0001000", where '0' means shown, and '1' means |
| 218 // needed but not shown. We manipulate it in bitset form. | 223 // needed but not shown. We manipulate it in bitset form. |
| 219 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); | 224 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); |
| 220 | 225 |
| 221 DCHECK(notification_shown || notification_needed); // Caller must ensure this | 226 DCHECK(notification_shown || notification_needed); // Caller must ensure this |
| 222 bool needed_but_not_shown = notification_needed && !notification_shown; | 227 bool needed_but_not_shown = notification_needed && !notification_shown; |
| 223 | 228 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 notification_data.icon = GURL(); | 279 notification_data.icon = GURL(); |
| 275 notification_data.silent = true; | 280 notification_data.silent = true; |
| 276 | 281 |
| 277 content::NotificationDatabaseData database_data; | 282 content::NotificationDatabaseData database_data; |
| 278 database_data.origin = requesting_origin; | 283 database_data.origin = requesting_origin; |
| 279 database_data.service_worker_registration_id = | 284 database_data.service_worker_registration_id = |
| 280 service_worker_registration_id; | 285 service_worker_registration_id; |
| 281 database_data.notification_data = notification_data; | 286 database_data.notification_data = notification_data; |
| 282 | 287 |
| 283 scoped_refptr<content::PlatformNotificationContext> notification_context = | 288 scoped_refptr<content::PlatformNotificationContext> notification_context = |
| 284 content::BrowserContext::GetStoragePartitionForSite( | 289 GetStoragePartition(profile_, requesting_origin) |
| 285 profile_, requesting_origin)->GetPlatformNotificationContext(); | 290 ->GetPlatformNotificationContext(); |
| 286 BrowserThread::PostTask( | 291 BrowserThread::PostTask( |
| 287 BrowserThread::IO, FROM_HERE, | 292 BrowserThread::IO, FROM_HERE, |
| 288 base::Bind( | 293 base::Bind( |
| 289 &content::PlatformNotificationContext::WriteNotificationData, | 294 &content::PlatformNotificationContext::WriteNotificationData, |
| 290 notification_context, | 295 notification_context, |
| 291 requesting_origin, database_data, | 296 requesting_origin, database_data, |
| 292 base::Bind(&PushMessagingNotificationManager | 297 base::Bind(&PushMessagingNotificationManager |
| 293 ::DidWriteNotificationDataIOProxy, | 298 ::DidWriteNotificationDataIOProxy, |
| 294 weak_factory_.GetWeakPtr(), | 299 weak_factory_.GetWeakPtr(), |
| 295 requesting_origin, notification_data, | 300 requesting_origin, notification_data, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 326 return; | 331 return; |
| 327 } | 332 } |
| 328 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( | 333 PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| 329 profile_, | 334 profile_, |
| 330 persistent_notification_id, | 335 persistent_notification_id, |
| 331 requesting_origin, | 336 requesting_origin, |
| 332 SkBitmap() /* icon */, | 337 SkBitmap() /* icon */, |
| 333 notification_data); | 338 notification_data); |
| 334 message_handled_closure.Run(); | 339 message_handled_closure.Run(); |
| 335 } | 340 } |
| OLD | NEW |