| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 if (database_) { | 50 if (database_) { |
| 51 DCHECK(task_runner_); | 51 DCHECK(task_runner_); |
| 52 task_runner_->DeleteSoon(FROM_HERE, database_.release()); | 52 task_runner_->DeleteSoon(FROM_HERE, database_.release()); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 | 55 |
| 56 void PlatformNotificationContextImpl::Initialize() { | 56 void PlatformNotificationContextImpl::Initialize() { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 58 PlatformNotificationService* service = | 58 PlatformNotificationService* service = |
| 59 GetContentClient()->browser()->GetPlatformNotificationService(); | 59 GetContentClient()->browser()->GetPlatformNotificationService(); |
| 60 if (service) { | 60 if (!service) { |
| 61 std::set<std::string> displayed_notifications; | 61 auto displayed_notifications = base::MakeUnique<std::set<std::string>>(); |
| 62 | 62 BrowserThread::PostTask( |
| 63 bool notification_synchronization_supported = | 63 BrowserThread::IO, FROM_HERE, |
| 64 service->GetDisplayedNotifications(browser_context_, | 64 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this, |
| 65 &displayed_notifications); | 65 base::Passed(&displayed_notifications), false)); |
| 66 | 66 return; |
| 67 // Synchronize the notifications stored in the database with the set of | |
| 68 // displaying notifications in |displayed_notifications|. This is necessary | |
| 69 // because flakiness may cause a platform to inform Chrome of a notification | |
| 70 // that has since been closed, or because the platform does not support | |
| 71 // notifications that exceed the lifetime of the browser process. | |
| 72 | |
| 73 // TODO(peter): Synchronizing the actual notifications will be done when the | |
| 74 // persistent notification ids are stable. For M44 we need to support the | |
| 75 // case where there may be no notifications after a Chrome restart. | |
| 76 if (notification_synchronization_supported && | |
| 77 displayed_notifications.empty()) { | |
| 78 prune_database_on_open_ = true; | |
| 79 } | |
| 80 } | 67 } |
| 81 | 68 |
| 69 service->GetDisplayedNotifications( |
| 70 browser_context_, |
| 71 base::Bind(&PlatformNotificationContextImpl::DidGetNotificationsOnUI, |
| 72 this)); |
| 73 } |
| 74 |
| 75 void PlatformNotificationContextImpl::DidGetNotificationsOnUI( |
| 76 std::unique_ptr<std::set<std::string>> displayed_notifications, |
| 77 bool notification_synchronization_supported) { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 82 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
| 83 BrowserThread::IO, FROM_HERE, | 80 BrowserThread::IO, FROM_HERE, |
| 84 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); | 81 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this, |
| 82 base::Passed(&displayed_notifications), |
| 83 notification_synchronization_supported)); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void PlatformNotificationContextImpl::InitializeOnIO() { | 86 void PlatformNotificationContextImpl::InitializeOnIO( |
| 87 std::unique_ptr<std::set<std::string>> displayed_notifications, |
| 88 bool notification_synchronization_supported) { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 89 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 89 | 90 |
| 91 // Synchronize the notifications stored in the database with the set of |
| 92 // displaying notifications in |displayed_notifications|. This is necessary |
| 93 // because flakiness may cause a platform to inform Chrome of a notification |
| 94 // that has since been closed, or because the platform does not support |
| 95 // notifications that exceed the lifetime of the browser process. |
| 96 |
| 97 // TODO(peter): Synchronizing the actual notifications will be done when the |
| 98 // persistent notification ids are stable. For M44 we need to support the |
| 99 // case where there may be no notifications after a Chrome restart. |
| 100 |
| 101 if (notification_synchronization_supported && |
| 102 displayed_notifications->empty()) { |
| 103 prune_database_on_open_ = true; |
| 104 } |
| 105 |
| 90 // |service_worker_context_| may be NULL in tests. | 106 // |service_worker_context_| may be NULL in tests. |
| 91 if (service_worker_context_) | 107 if (service_worker_context_) |
| 92 service_worker_context_->AddObserver(this); | 108 service_worker_context_->AddObserver(this); |
| 93 } | 109 } |
| 94 | 110 |
| 95 void PlatformNotificationContextImpl::Shutdown() { | 111 void PlatformNotificationContextImpl::Shutdown() { |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 97 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
| 98 BrowserThread::IO, FROM_HERE, | 114 BrowserThread::IO, FROM_HERE, |
| 99 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); | 115 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 // Blow away the database if reading data failed due to corruption. | 190 // Blow away the database if reading data failed due to corruption. |
| 175 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) | 191 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) |
| 176 DestroyDatabase(); | 192 DestroyDatabase(); |
| 177 | 193 |
| 178 BrowserThread::PostTask( | 194 BrowserThread::PostTask( |
| 179 BrowserThread::IO, FROM_HERE, | 195 BrowserThread::IO, FROM_HERE, |
| 180 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 196 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 181 } | 197 } |
| 182 | 198 |
| 183 void PlatformNotificationContextImpl:: | 199 void PlatformNotificationContextImpl:: |
| 184 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( | 200 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnUI( |
| 185 const GURL& origin, | 201 const GURL& origin, |
| 186 int64_t service_worker_registration_id, | 202 int64_t service_worker_registration_id, |
| 187 const ReadAllResultCallback& callback, | 203 const ReadAllResultCallback& callback, |
| 204 std::unique_ptr<std::set<std::string>> notification_ids, |
| 205 bool sync_supported) { |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 207 |
| 208 BrowserThread::PostTask( |
| 209 BrowserThread::IO, FROM_HERE, |
| 210 base::Bind( |
| 211 &PlatformNotificationContextImpl:: |
| 212 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnIO, |
| 213 this, origin, service_worker_registration_id, callback, |
| 214 base::Passed(¬ification_ids), sync_supported)); |
| 215 } |
| 216 |
| 217 void PlatformNotificationContextImpl:: |
| 218 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnIO( |
| 219 const GURL& origin, |
| 220 int64_t service_worker_registration_id, |
| 221 const ReadAllResultCallback& callback, |
| 188 std::unique_ptr<std::set<std::string>> notification_ids, | 222 std::unique_ptr<std::set<std::string>> notification_ids, |
| 189 bool sync_supported) { | 223 bool sync_supported) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 LazyInitialize( | 225 LazyInitialize( |
| 192 base::Bind(&PlatformNotificationContextImpl:: | 226 base::Bind(&PlatformNotificationContextImpl:: |
| 193 DoReadAllNotificationDataForServiceWorkerRegistration, | 227 DoReadAllNotificationDataForServiceWorkerRegistration, |
| 194 this, origin, service_worker_registration_id, callback, | 228 this, origin, service_worker_registration_id, callback, |
| 195 base::Passed(¬ification_ids), sync_supported), | 229 base::Passed(¬ification_ids), sync_supported), |
| 196 base::Bind(callback, false /* success */, | 230 base::Bind(callback, false /* success */, |
| 197 std::vector<NotificationDatabaseData>())); | 231 std::vector<NotificationDatabaseData>())); |
| 198 } | 232 } |
| 199 | 233 |
| 200 void PlatformNotificationContextImpl:: | 234 void PlatformNotificationContextImpl:: |
| 201 ReadAllNotificationDataForServiceWorkerRegistration( | 235 ReadAllNotificationDataForServiceWorkerRegistration( |
| 202 const GURL& origin, | 236 const GURL& origin, |
| 203 int64_t service_worker_registration_id, | 237 int64_t service_worker_registration_id, |
| 204 const ReadAllResultCallback& callback) { | 238 const ReadAllResultCallback& callback) { |
| 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 239 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 206 | 240 |
| 207 std::unique_ptr<std::set<std::string>> notification_ids = | 241 auto notification_ids = base::MakeUnique<std::set<std::string>>(); |
| 208 base::MakeUnique<std::set<std::string>>(); | |
| 209 | 242 |
| 210 PlatformNotificationService* service = | 243 PlatformNotificationService* service = |
| 211 GetContentClient()->browser()->GetPlatformNotificationService(); | 244 GetContentClient()->browser()->GetPlatformNotificationService(); |
| 212 | 245 |
| 213 if (!service) { | 246 if (!service) { |
| 214 // Rely on the database only | 247 // Rely on the database only |
| 215 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( | 248 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnIO( |
| 216 origin, service_worker_registration_id, callback, | 249 origin, service_worker_registration_id, callback, |
| 217 std::move(notification_ids), false /* sync_supported */); | 250 std::move(notification_ids), false /* sync_supported */); |
| 218 return; | 251 return; |
| 219 } | 252 } |
| 220 | 253 |
| 221 std::set<std::string>* notification_ids_ptr = notification_ids.get(); | 254 BrowserThread::PostTask( |
| 222 | |
| 223 BrowserThread::PostTaskAndReplyWithResult( | |
| 224 BrowserThread::UI, FROM_HERE, | 255 BrowserThread::UI, FROM_HERE, |
| 225 base::Bind(&PlatformNotificationService::GetDisplayedNotifications, | |
| 226 base::Unretained(service), browser_context_, | |
| 227 notification_ids_ptr), | |
| 228 base::Bind( | 256 base::Bind( |
| 229 &PlatformNotificationContextImpl:: | 257 &PlatformNotificationService::GetDisplayedNotifications, |
| 230 SynchronizeDisplayedNotificationsForServiceWorkerRegistration, | 258 base::Unretained(service), browser_context_, |
| 231 this, origin, service_worker_registration_id, callback, | 259 base::Bind( |
| 232 base::Passed(¬ification_ids))); | 260 &PlatformNotificationContextImpl:: |
| 261 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationO
nUI, |
| 262 this, origin, service_worker_registration_id, callback))); |
| 233 } | 263 } |
| 234 | 264 |
| 235 void PlatformNotificationContextImpl:: | 265 void PlatformNotificationContextImpl:: |
| 236 DoReadAllNotificationDataForServiceWorkerRegistration( | 266 DoReadAllNotificationDataForServiceWorkerRegistration( |
| 237 const GURL& origin, | 267 const GURL& origin, |
| 238 int64_t service_worker_registration_id, | 268 int64_t service_worker_registration_id, |
| 239 const ReadAllResultCallback& callback, | 269 const ReadAllResultCallback& callback, |
| 240 std::unique_ptr<std::set<std::string>> displayed_notifications, | 270 std::unique_ptr<std::set<std::string>> displayed_notifications, |
| 241 bool synchronization_supported) { | 271 bool synchronization_supported) { |
| 242 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 272 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 | 563 |
| 534 return path_.Append(kPlatformNotificationsDirectory); | 564 return path_.Append(kPlatformNotificationsDirectory); |
| 535 } | 565 } |
| 536 | 566 |
| 537 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 567 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 538 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 568 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 539 task_runner_ = task_runner; | 569 task_runner_ = task_runner; |
| 540 } | 570 } |
| 541 | 571 |
| 542 } // namespace content | 572 } // namespace content |
| OLD | NEW |