| 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 "content/browser/notifications/platform_notification_context_impl.h" | 5 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 BrowserThread::PostTask( | 179 BrowserThread::PostTask( |
| 180 BrowserThread::IO, FROM_HERE, | 180 BrowserThread::IO, FROM_HERE, |
| 181 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 181 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void PlatformNotificationContextImpl:: | 184 void PlatformNotificationContextImpl:: |
| 185 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( | 185 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( |
| 186 const GURL& origin, | 186 const GURL& origin, |
| 187 int64_t service_worker_registration_id, | 187 int64_t service_worker_registration_id, |
| 188 const ReadAllResultCallback& callback, | 188 const ReadAllResultCallback& callback, |
| 189 std::set<std::string>* notification_ids, | 189 std::unique_ptr<std::set<std::string>> notification_ids, |
| 190 bool sync_supported) { | 190 bool sync_supported) { |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 192 LazyInitialize( | 192 LazyInitialize( |
| 193 base::Bind(&PlatformNotificationContextImpl:: | 193 base::Bind(&PlatformNotificationContextImpl:: |
| 194 DoReadAllNotificationDataForServiceWorkerRegistration, | 194 DoReadAllNotificationDataForServiceWorkerRegistration, |
| 195 this, origin, service_worker_registration_id, callback, | 195 this, origin, service_worker_registration_id, callback, |
| 196 base::Unretained(notification_ids), sync_supported), | 196 base::Passed(¬ification_ids), sync_supported), |
| 197 base::Bind(callback, false /* success */, | 197 base::Bind(callback, false /* success */, |
| 198 std::vector<NotificationDatabaseData>())); | 198 std::vector<NotificationDatabaseData>())); |
| 199 } | 199 } |
| 200 | 200 |
| 201 void PlatformNotificationContextImpl:: | 201 void PlatformNotificationContextImpl:: |
| 202 ReadAllNotificationDataForServiceWorkerRegistration( | 202 ReadAllNotificationDataForServiceWorkerRegistration( |
| 203 const GURL& origin, | 203 const GURL& origin, |
| 204 int64_t service_worker_registration_id, | 204 int64_t service_worker_registration_id, |
| 205 const ReadAllResultCallback& callback) { | 205 const ReadAllResultCallback& callback) { |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 207 | 207 |
| 208 // notification_ids becomes owned by | 208 std::unique_ptr<std::set<std::string>> notification_ids = |
| 209 // DoReadAllNotificationDataForServiceWorkerRegistration | 209 base::MakeUnique<std::set<std::string>>(); |
| 210 std::set<std::string>* notification_ids = new std::set<std::string>(); | 210 |
| 211 PlatformNotificationService* service = | 211 PlatformNotificationService* service = |
| 212 GetContentClient()->browser()->GetPlatformNotificationService(); | 212 GetContentClient()->browser()->GetPlatformNotificationService(); |
| 213 | 213 |
| 214 if (!service) { | 214 if (!service) { |
| 215 // Rely on the database only | 215 // Rely on the database only |
| 216 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( | 216 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( |
| 217 origin, service_worker_registration_id, callback, notification_ids, | 217 origin, service_worker_registration_id, callback, |
| 218 false /* sync_supported */); | 218 std::move(notification_ids), false /* sync_supported */); |
| 219 return; | 219 return; |
| 220 } | 220 } |
| 221 | 221 |
| 222 std::set<std::string>* notification_ids_ptr = notification_ids.get(); |
| 223 |
| 222 BrowserThread::PostTaskAndReplyWithResult( | 224 BrowserThread::PostTaskAndReplyWithResult( |
| 223 BrowserThread::UI, FROM_HERE, | 225 BrowserThread::UI, FROM_HERE, |
| 224 base::Bind(&PlatformNotificationService::GetDisplayedNotifications, | 226 base::Bind(&PlatformNotificationService::GetDisplayedNotifications, |
| 225 base::Unretained(service), browser_context_, | 227 base::Unretained(service), browser_context_, |
| 226 base::Unretained(notification_ids)), | 228 notification_ids_ptr), |
| 227 base::Bind( | 229 base::Bind( |
| 228 &PlatformNotificationContextImpl:: | 230 &PlatformNotificationContextImpl:: |
| 229 SynchronizeDisplayedNotificationsForServiceWorkerRegistration, | 231 SynchronizeDisplayedNotificationsForServiceWorkerRegistration, |
| 230 this, origin, service_worker_registration_id, callback, | 232 this, origin, service_worker_registration_id, callback, |
| 231 base::Unretained(notification_ids))); | 233 base::Passed(¬ification_ids))); |
| 232 } | 234 } |
| 233 | 235 |
| 234 void PlatformNotificationContextImpl:: | 236 void PlatformNotificationContextImpl:: |
| 235 DoReadAllNotificationDataForServiceWorkerRegistration( | 237 DoReadAllNotificationDataForServiceWorkerRegistration( |
| 236 const GURL& origin, | 238 const GURL& origin, |
| 237 int64_t service_worker_registration_id, | 239 int64_t service_worker_registration_id, |
| 238 const ReadAllResultCallback& callback, | 240 const ReadAllResultCallback& callback, |
| 239 std::set<std::string>* displayed_notifications, | 241 std::unique_ptr<std::set<std::string>> displayed_notifications, |
| 240 bool synchronization_supported) { | 242 bool synchronization_supported) { |
| 241 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 243 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 242 std::unique_ptr<std::set<std::string>> displayed_ids = | 244 DCHECK(displayed_notifications); |
| 243 base::WrapUnique(displayed_notifications); | |
| 244 | 245 |
| 245 std::vector<NotificationDatabaseData> notification_datas; | 246 std::vector<NotificationDatabaseData> notification_datas; |
| 246 | 247 |
| 247 NotificationDatabase::Status status = | 248 NotificationDatabase::Status status = |
| 248 database_->ReadAllNotificationDataForServiceWorkerRegistration( | 249 database_->ReadAllNotificationDataForServiceWorkerRegistration( |
| 249 origin, service_worker_registration_id, ¬ification_datas); | 250 origin, service_worker_registration_id, ¬ification_datas); |
| 250 | 251 |
| 251 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.ReadForServiceWorkerResult", | 252 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.ReadForServiceWorkerResult", |
| 252 status, NotificationDatabase::STATUS_COUNT); | 253 status, NotificationDatabase::STATUS_COUNT); |
| 253 | 254 |
| 254 if (status == NotificationDatabase::STATUS_OK) { | 255 if (status == NotificationDatabase::STATUS_OK) { |
| 255 if (synchronization_supported) { | 256 if (synchronization_supported) { |
| 256 DCHECK(displayed_ids); | |
| 257 // Filter out notifications that are not actually on display anymore. | 257 // Filter out notifications that are not actually on display anymore. |
| 258 // TODO(miguelg) synchronize the database if there are inconsistencies. | 258 // TODO(miguelg) synchronize the database if there are inconsistencies. |
| 259 for (auto it = notification_datas.begin(); | 259 for (auto it = notification_datas.begin(); |
| 260 it != notification_datas.end();) { | 260 it != notification_datas.end();) { |
| 261 if (NotificationIdGenerator::IsPersistentNotification( | 261 if (NotificationIdGenerator::IsPersistentNotification( |
| 262 it->notification_id) && | 262 it->notification_id) && |
| 263 displayed_ids->count(it->notification_id)) { | 263 displayed_notifications->count(it->notification_id)) { |
| 264 ++it; | 264 ++it; |
| 265 } else { | 265 } else { |
| 266 it = notification_datas.erase(it); | 266 it = notification_datas.erase(it); |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 | 270 |
| 271 BrowserThread::PostTask( | 271 BrowserThread::PostTask( |
| 272 BrowserThread::IO, FROM_HERE, | 272 BrowserThread::IO, FROM_HERE, |
| 273 base::Bind(callback, true /* success */, notification_datas)); | 273 base::Bind(callback, true /* success */, notification_datas)); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 | 533 |
| 534 return path_.Append(kPlatformNotificationsDirectory); | 534 return path_.Append(kPlatformNotificationsDirectory); |
| 535 } | 535 } |
| 536 | 536 |
| 537 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 537 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 538 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 538 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 539 task_runner_ = task_runner; | 539 task_runner_ = task_runner; |
| 540 } | 540 } |
| 541 | 541 |
| 542 } // namespace content | 542 } // namespace content |
| OLD | NEW |