Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: content/browser/notifications/platform_notification_context_impl.cc

Issue 2534443002: Use notification display service to collect persistent notifications. (Closed)
Patch Set: fix layout tests Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 std::vector<NotificationDatabaseData>())); 195 std::vector<NotificationDatabaseData>()));
196 } 196 }
197 197
198 void PlatformNotificationContextImpl:: 198 void PlatformNotificationContextImpl::
199 DoReadAllNotificationDataForServiceWorkerRegistration( 199 DoReadAllNotificationDataForServiceWorkerRegistration(
200 const GURL& origin, 200 const GURL& origin,
201 int64_t service_worker_registration_id, 201 int64_t service_worker_registration_id,
202 const ReadAllResultCallback& callback) { 202 const ReadAllResultCallback& callback) {
203 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 203 DCHECK(task_runner_->RunsTasksOnCurrentThread());
204 204
205 PlatformNotificationService* service =
206 GetContentClient()->browser()->GetPlatformNotificationService();
207
208 // Ask the underlying implementation what notifications are still being
209 // displayed.
210 std::set<std::string> displayed_notifications;
211 bool notification_synchronization_supported =
212 service &&
213 service->GetDisplayedNotifications(browser_context_,
214 &displayed_notifications);
Peter Beverloo 2016/12/06 13:04:02 We're not only calling GetDisplayedNotifications()
215
205 std::vector<NotificationDatabaseData> notification_datas; 216 std::vector<NotificationDatabaseData> notification_datas;
206 217
207 NotificationDatabase::Status status = 218 NotificationDatabase::Status status =
208 database_->ReadAllNotificationDataForServiceWorkerRegistration( 219 database_->ReadAllNotificationDataForServiceWorkerRegistration(
209 origin, service_worker_registration_id, &notification_datas); 220 origin, service_worker_registration_id, &notification_datas);
210 221
211 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.ReadForServiceWorkerResult", 222 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.ReadForServiceWorkerResult",
212 status, NotificationDatabase::STATUS_COUNT); 223 status, NotificationDatabase::STATUS_COUNT);
213 224
214 if (status == NotificationDatabase::STATUS_OK) { 225 if (status == NotificationDatabase::STATUS_OK) {
226 if (notification_synchronization_supported) {
227 // Filter out notifications that are not actually on display anymore.
228 // TODO(miguelg) synchronize the database if there are inconsistencies.
229 for (auto it = notification_datas.begin();
230 it != notification_datas.end();) {
231 if (NotificationIdGenerator::IsPersistentNotification(
232 it->notification_id) &&
233 displayed_notifications.count(it->notification_id)) {
234 ++it;
235 } else {
236 it = notification_datas.erase(it);
237 }
238 }
239 }
240
215 BrowserThread::PostTask( 241 BrowserThread::PostTask(
216 BrowserThread::IO, FROM_HERE, 242 BrowserThread::IO, FROM_HERE,
217 base::Bind(callback, true /* success */, notification_datas)); 243 base::Bind(callback, true /* success */, notification_datas));
218 return; 244 return;
219 } 245 }
220 246
221 // Blow away the database if reading data failed due to corruption. 247 // Blow away the database if reading data failed due to corruption.
222 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) 248 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED)
223 DestroyDatabase(); 249 DestroyDatabase();
224 250
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 503
478 return path_.Append(kPlatformNotificationsDirectory); 504 return path_.Append(kPlatformNotificationsDirectory);
479 } 505 }
480 506
481 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( 507 void PlatformNotificationContextImpl::SetTaskRunnerForTesting(
482 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { 508 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
483 task_runner_ = task_runner; 509 task_runner_ = task_runner;
484 } 510 }
485 511
486 } // namespace content 512 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698