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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } | 53 } |
54 | 54 |
55 void PlatformNotificationContextImpl::Initialize() { | 55 void PlatformNotificationContextImpl::Initialize() { |
56 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 56 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
57 PlatformNotificationService* service = | 57 PlatformNotificationService* service = |
58 GetContentClient()->browser()->GetPlatformNotificationService(); | 58 GetContentClient()->browser()->GetPlatformNotificationService(); |
59 if (service) { | 59 if (service) { |
60 std::set<std::string> displayed_notifications; | 60 std::set<std::string> displayed_notifications; |
61 | 61 |
62 bool notification_synchronization_supported = | 62 bool notification_synchronization_supported = |
63 service->GetDisplayedPersistentNotifications(browser_context_, | 63 service->GetDisplayedNotifications(browser_context_, |
64 &displayed_notifications); | 64 &displayed_notifications); |
65 | 65 |
66 // Synchronize the notifications stored in the database with the set of | 66 // Synchronize the notifications stored in the database with the set of |
67 // displaying notifications in |displayed_notifications|. This is necessary | 67 // displaying notifications in |displayed_notifications|. This is necessary |
68 // because flakiness may cause a platform to inform Chrome of a notification | 68 // because flakiness may cause a platform to inform Chrome of a notification |
69 // that has since been closed, or because the platform does not support | 69 // that has since been closed, or because the platform does not support |
70 // notifications that exceed the lifetime of the browser process. | 70 // notifications that exceed the lifetime of the browser process. |
71 | 71 |
72 // TODO(peter): Synchronizing the actual notifications will be done when the | 72 // TODO(peter): Synchronizing the actual notifications will be done when the |
73 // persistent notification ids are stable. For M44 we need to support the | 73 // persistent notification ids are stable. For M44 we need to support the |
74 // case where there may be no notifications after a Chrome restart. | 74 // case where there may be no notifications after a Chrome restart. |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // Blow away the database if reading data failed due to corruption. | 175 // Blow away the database if reading data failed due to corruption. |
176 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) | 176 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) |
177 DestroyDatabase(); | 177 DestroyDatabase(); |
178 | 178 |
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( |
| 186 const GURL& origin, |
| 187 int64_t service_worker_registration_id, |
| 188 const ReadAllResultCallback& callback, |
| 189 std::unique_ptr<std::set<std::string>> notification_ids, |
| 190 bool sync_supported) { |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 192 LazyInitialize( |
| 193 base::Bind(&PlatformNotificationContextImpl:: |
| 194 DoReadAllNotificationDataForServiceWorkerRegistration, |
| 195 this, origin, service_worker_registration_id, callback, |
| 196 base::Passed(¬ification_ids), sync_supported), |
| 197 base::Bind(callback, false /* success */, |
| 198 std::vector<NotificationDatabaseData>())); |
| 199 } |
| 200 |
| 201 void PlatformNotificationContextImpl:: |
185 ReadAllNotificationDataForServiceWorkerRegistration( | 202 ReadAllNotificationDataForServiceWorkerRegistration( |
186 const GURL& origin, | 203 const GURL& origin, |
187 int64_t service_worker_registration_id, | 204 int64_t service_worker_registration_id, |
188 const ReadAllResultCallback& callback) { | 205 const ReadAllResultCallback& callback) { |
189 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
190 LazyInitialize( | 207 |
191 base::Bind(&PlatformNotificationContextImpl:: | 208 std::unique_ptr<std::set<std::string>> notification_ids = |
192 DoReadAllNotificationDataForServiceWorkerRegistration, | 209 base::MakeUnique<std::set<std::string>>(); |
193 this, origin, service_worker_registration_id, callback), | 210 |
194 base::Bind(callback, false /* success */, | 211 PlatformNotificationService* service = |
195 std::vector<NotificationDatabaseData>())); | 212 GetContentClient()->browser()->GetPlatformNotificationService(); |
| 213 |
| 214 if (!service) { |
| 215 // Rely on the database only |
| 216 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( |
| 217 origin, service_worker_registration_id, callback, |
| 218 std::move(notification_ids), false /* sync_supported */); |
| 219 return; |
| 220 } |
| 221 |
| 222 std::set<std::string>* notification_ids_ptr = notification_ids.get(); |
| 223 |
| 224 BrowserThread::PostTaskAndReplyWithResult( |
| 225 BrowserThread::UI, FROM_HERE, |
| 226 base::Bind(&PlatformNotificationService::GetDisplayedNotifications, |
| 227 base::Unretained(service), browser_context_, |
| 228 notification_ids_ptr), |
| 229 base::Bind( |
| 230 &PlatformNotificationContextImpl:: |
| 231 SynchronizeDisplayedNotificationsForServiceWorkerRegistration, |
| 232 this, origin, service_worker_registration_id, callback, |
| 233 base::Passed(¬ification_ids))); |
196 } | 234 } |
197 | 235 |
198 void PlatformNotificationContextImpl:: | 236 void PlatformNotificationContextImpl:: |
199 DoReadAllNotificationDataForServiceWorkerRegistration( | 237 DoReadAllNotificationDataForServiceWorkerRegistration( |
200 const GURL& origin, | 238 const GURL& origin, |
201 int64_t service_worker_registration_id, | 239 int64_t service_worker_registration_id, |
202 const ReadAllResultCallback& callback) { | 240 const ReadAllResultCallback& callback, |
| 241 std::unique_ptr<std::set<std::string>> displayed_notifications, |
| 242 bool synchronization_supported) { |
203 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 243 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 244 DCHECK(displayed_notifications); |
204 | 245 |
205 std::vector<NotificationDatabaseData> notification_datas; | 246 std::vector<NotificationDatabaseData> notification_datas; |
206 | 247 |
207 NotificationDatabase::Status status = | 248 NotificationDatabase::Status status = |
208 database_->ReadAllNotificationDataForServiceWorkerRegistration( | 249 database_->ReadAllNotificationDataForServiceWorkerRegistration( |
209 origin, service_worker_registration_id, ¬ification_datas); | 250 origin, service_worker_registration_id, ¬ification_datas); |
210 | 251 |
211 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.ReadForServiceWorkerResult", | 252 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.ReadForServiceWorkerResult", |
212 status, NotificationDatabase::STATUS_COUNT); | 253 status, NotificationDatabase::STATUS_COUNT); |
213 | 254 |
214 if (status == NotificationDatabase::STATUS_OK) { | 255 if (status == NotificationDatabase::STATUS_OK) { |
| 256 if (synchronization_supported) { |
| 257 // Filter out notifications that are not actually on display anymore. |
| 258 // TODO(miguelg) synchronize the database if there are inconsistencies. |
| 259 for (auto it = notification_datas.begin(); |
| 260 it != notification_datas.end();) { |
| 261 // The database is only used for persistent notifications. |
| 262 DCHECK(NotificationIdGenerator::IsPersistentNotification( |
| 263 it->notification_id)); |
| 264 if (displayed_notifications->count(it->notification_id)) { |
| 265 ++it; |
| 266 } else { |
| 267 it = notification_datas.erase(it); |
| 268 } |
| 269 } |
| 270 } |
| 271 |
215 BrowserThread::PostTask( | 272 BrowserThread::PostTask( |
216 BrowserThread::IO, FROM_HERE, | 273 BrowserThread::IO, FROM_HERE, |
217 base::Bind(callback, true /* success */, notification_datas)); | 274 base::Bind(callback, true /* success */, notification_datas)); |
218 return; | 275 return; |
219 } | 276 } |
220 | 277 |
221 // Blow away the database if reading data failed due to corruption. | 278 // Blow away the database if reading data failed due to corruption. |
222 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) | 279 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) |
223 DestroyDatabase(); | 280 DestroyDatabase(); |
224 | 281 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 | 534 |
478 return path_.Append(kPlatformNotificationsDirectory); | 535 return path_.Append(kPlatformNotificationsDirectory); |
479 } | 536 } |
480 | 537 |
481 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 538 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
482 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 539 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
483 task_runner_ = task_runner; | 540 task_runner_ = task_runner; |
484 } | 541 } |
485 | 542 |
486 } // namespace content | 543 } // namespace content |
OLD | NEW |