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) { |
Peter Beverloo
2017/03/15 18:07:51
Here's an idea: should we maybe just bail out if t
Miguel Garcia
2017/03/16 14:57:42
Done.
Miguel Garcia
2017/03/16 15:57:25
I had to revert this because several tests fail as
| |
61 std::set<std::string> displayed_notifications; | 61 std::unique_ptr<std::set<std::string>> displayed_notifications = |
62 base::MakeUnique<std::set<std::string>>(); | |
63 BrowserThread::PostTask( | |
64 BrowserThread::IO, FROM_HERE, | |
65 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this, | |
66 base::Passed(&displayed_notifications), false)); | |
67 return; | |
68 } | |
69 service->GetDisplayedNotifications( | |
70 browser_context_, | |
71 base::Bind(&PlatformNotificationContextImpl::InitializeOnUI, this)); | |
72 } | |
62 | 73 |
63 bool notification_synchronization_supported = | 74 void PlatformNotificationContextImpl::InitializeOnUI( |
64 service->GetDisplayedNotifications(browser_context_, | 75 std::unique_ptr<std::set<std::string>> displayed_notifications, |
65 &displayed_notifications); | 76 bool notification_synchronization_supported) { |
66 | 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
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 } | |
81 | |
82 BrowserThread::PostTask( | 78 BrowserThread::PostTask( |
83 BrowserThread::IO, FROM_HERE, | 79 BrowserThread::IO, FROM_HERE, |
84 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); | 80 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this, |
81 base::Passed(&displayed_notifications), | |
82 notification_synchronization_supported)); | |
85 } | 83 } |
86 | 84 |
87 void PlatformNotificationContextImpl::InitializeOnIO() { | 85 void PlatformNotificationContextImpl::InitializeOnIO( |
86 std::unique_ptr<std::set<std::string>> displayed_notifications, | |
87 bool notification_synchronization_supported) { | |
88 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
89 | 89 |
90 // Synchronize the notifications stored in the database with the set of | |
91 // displaying notifications in |displayed_notifications|. This is necessary | |
92 // because flakiness may cause a platform to inform Chrome of a notification | |
93 // that has since been closed, or because the platform does not support | |
94 // notifications that exceed the lifetime of the browser process. | |
95 | |
96 // TODO(peter): Synchronizing the actual notifications will be done when the | |
97 // persistent notification ids are stable. For M44 we need to support the | |
98 // case where there may be no notifications after a Chrome restart. | |
99 | |
100 if (notification_synchronization_supported && | |
101 displayed_notifications->empty()) { | |
102 prune_database_on_open_ = true; | |
103 } | |
104 | |
90 // |service_worker_context_| may be NULL in tests. | 105 // |service_worker_context_| may be NULL in tests. |
91 if (service_worker_context_) | 106 if (service_worker_context_) |
92 service_worker_context_->AddObserver(this); | 107 service_worker_context_->AddObserver(this); |
93 } | 108 } |
94 | 109 |
95 void PlatformNotificationContextImpl::Shutdown() { | 110 void PlatformNotificationContextImpl::Shutdown() { |
96 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 111 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
97 BrowserThread::PostTask( | 112 BrowserThread::PostTask( |
98 BrowserThread::IO, FROM_HERE, | 113 BrowserThread::IO, FROM_HERE, |
99 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); | 114 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. | 189 // Blow away the database if reading data failed due to corruption. |
175 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) | 190 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) |
176 DestroyDatabase(); | 191 DestroyDatabase(); |
177 | 192 |
178 BrowserThread::PostTask( | 193 BrowserThread::PostTask( |
179 BrowserThread::IO, FROM_HERE, | 194 BrowserThread::IO, FROM_HERE, |
180 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 195 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
181 } | 196 } |
182 | 197 |
183 void PlatformNotificationContextImpl:: | 198 void PlatformNotificationContextImpl:: |
184 SynchronizeDisplayedNotificationsForServiceWorkerRegistration( | 199 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnUI( |
185 const GURL& origin, | 200 const GURL& origin, |
186 int64_t service_worker_registration_id, | 201 int64_t service_worker_registration_id, |
187 const ReadAllResultCallback& callback, | 202 const ReadAllResultCallback& callback, |
203 std::unique_ptr<std::set<std::string>> notification_ids, | |
204 bool sync_supported) { | |
205 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
206 | |
207 BrowserThread::PostTask( | |
208 BrowserThread::IO, FROM_HERE, | |
209 base::Bind( | |
210 &PlatformNotificationContextImpl:: | |
211 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnIO, | |
212 this, origin, service_worker_registration_id, callback, | |
213 base::Passed(¬ification_ids), sync_supported)); | |
214 } | |
215 | |
216 void PlatformNotificationContextImpl:: | |
217 SynchronizeDisplayedNotificationsForServiceWorkerRegistrationOnIO( | |
218 const GURL& origin, | |
219 int64_t service_worker_registration_id, | |
220 const ReadAllResultCallback& callback, | |
188 std::unique_ptr<std::set<std::string>> notification_ids, | 221 std::unique_ptr<std::set<std::string>> notification_ids, |
189 bool sync_supported) { | 222 bool sync_supported) { |
190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
191 LazyInitialize( | 224 LazyInitialize( |
192 base::Bind(&PlatformNotificationContextImpl:: | 225 base::Bind(&PlatformNotificationContextImpl:: |
193 DoReadAllNotificationDataForServiceWorkerRegistration, | 226 DoReadAllNotificationDataForServiceWorkerRegistration, |
194 this, origin, service_worker_registration_id, callback, | 227 this, origin, service_worker_registration_id, callback, |
195 base::Passed(¬ification_ids), sync_supported), | 228 base::Passed(¬ification_ids), sync_supported), |
196 base::Bind(callback, false /* success */, | 229 base::Bind(callback, false /* success */, |
197 std::vector<NotificationDatabaseData>())); | 230 std::vector<NotificationDatabaseData>())); |
198 } | 231 } |
199 | 232 |
200 void PlatformNotificationContextImpl:: | 233 void PlatformNotificationContextImpl:: |
201 ReadAllNotificationDataForServiceWorkerRegistration( | 234 ReadAllNotificationDataForServiceWorkerRegistration( |
202 const GURL& origin, | 235 const GURL& origin, |
203 int64_t service_worker_registration_id, | 236 int64_t service_worker_registration_id, |
204 const ReadAllResultCallback& callback) { | 237 const ReadAllResultCallback& callback) { |
205 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 238 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
206 | 239 |
207 std::unique_ptr<std::set<std::string>> notification_ids = | 240 std::unique_ptr<std::set<std::string>> notification_ids = |
208 base::MakeUnique<std::set<std::string>>(); | 241 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 */); |
Peter Beverloo
2017/03/15 18:07:51
Here too, maybe we should just drop out?
call
Miguel Garcia
2017/03/16 14:57:42
I think we have some tests that rely on this right
| |
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 |