Chromium Code Reviews| 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/threading/sequenced_worker_pool.h" | 7 #include "base/threading/sequenced_worker_pool.h" |
| 8 #include "content/browser/notifications/notification_database.h" | 8 #include "content/browser/notifications/notification_database.h" |
| 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/notification_database_data.h" | 11 #include "content/public/browser/notification_database_data.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 14 namespace { | |
| 15 | |
| 16 // Used as a failure callback there is no further action to be made. | |
| 17 void EmptyFailureCallback() {} | |
|
johnme
2015/03/20 14:56:35
Use DoNothing from base/bind_helpers.h instead.
Peter Beverloo
2015/03/20 18:39:06
Done.
| |
| 18 | |
| 19 } // namespace | |
| 13 | 20 |
| 14 // Name of the directory in the user's profile directory where the notification | 21 // Name of the directory in the user's profile directory where the notification |
| 15 // database files should be stored. | 22 // database files should be stored. |
| 16 const base::FilePath::CharType kPlatformNotificationsDirectory[] = | 23 const base::FilePath::CharType kPlatformNotificationsDirectory[] = |
| 17 FILE_PATH_LITERAL("Platform Notifications"); | 24 FILE_PATH_LITERAL("Platform Notifications"); |
| 18 | 25 |
| 19 PlatformNotificationContextImpl::PlatformNotificationContextImpl( | 26 PlatformNotificationContextImpl::PlatformNotificationContextImpl( |
| 20 const base::FilePath& path) | 27 const base::FilePath& path, |
| 21 : path_(path) { | 28 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
| 29 : path_(path), | |
| 30 service_worker_context_(service_worker_context) { | |
| 31 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 32 BrowserThread::PostTask( | |
| 33 BrowserThread::IO, | |
| 34 FROM_HERE, | |
| 35 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); | |
| 22 } | 36 } |
| 23 | 37 |
| 24 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { | 38 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { |
| 25 // If the database has been initialized, it must be deleted on the task runner | 39 // If the database has been initialized, it must be deleted on the task runner |
| 26 // thread as closing it may cause file I/O. | 40 // thread as closing it may cause file I/O. |
| 27 if (database_) { | 41 if (database_) { |
| 28 DCHECK(task_runner_); | 42 DCHECK(task_runner_); |
| 29 task_runner_->DeleteSoon(FROM_HERE, database_.release()); | 43 task_runner_->DeleteSoon(FROM_HERE, database_.release()); |
| 30 } | 44 } |
| 31 } | 45 } |
| 32 | 46 |
| 47 void PlatformNotificationContextImpl::InitializeOnIO() { | |
| 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 49 | |
| 50 // |service_worker_context_| may be NULL in tests. | |
| 51 if (service_worker_context_) | |
| 52 service_worker_context_->AddObserver(this); | |
| 53 } | |
| 54 | |
| 55 void PlatformNotificationContextImpl::Shutdown() { | |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 57 BrowserThread::PostTask( | |
|
johnme
2015/03/20 14:56:35
My understanding is that there's now a race condit
Peter Beverloo
2015/03/20 18:39:05
Good point.
I have moved the RefCountedThreadSafe
| |
| 58 BrowserThread::IO, | |
| 59 FROM_HERE, | |
| 60 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); | |
| 61 } | |
| 62 | |
| 63 void PlatformNotificationContextImpl::ShutdownOnIO() { | |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 65 | |
| 66 // |service_worker_context_| may be NULL in tests. | |
| 67 if (service_worker_context_) | |
| 68 service_worker_context_->RemoveObserver(this); | |
| 69 } | |
| 70 | |
| 33 void PlatformNotificationContextImpl::ReadNotificationData( | 71 void PlatformNotificationContextImpl::ReadNotificationData( |
| 34 int64_t notification_id, | 72 int64_t notification_id, |
| 35 const GURL& origin, | 73 const GURL& origin, |
| 36 const ReadResultCallback& callback) { | 74 const ReadResultCallback& callback) { |
| 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 38 LazyInitialize( | 76 LazyInitialize( |
| 39 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, | 77 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, |
| 40 this, notification_id, origin, callback), | 78 this, notification_id, origin, callback), |
| 41 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 79 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 42 } | 80 } |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 const bool success = status == NotificationDatabase::STATUS_OK; | 175 const bool success = status == NotificationDatabase::STATUS_OK; |
| 138 | 176 |
| 139 // TODO(peter): Record UMA on |status| for reading from the database. | 177 // TODO(peter): Record UMA on |status| for reading from the database. |
| 140 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | 178 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| 141 | 179 |
| 142 BrowserThread::PostTask(BrowserThread::IO, | 180 BrowserThread::PostTask(BrowserThread::IO, |
| 143 FROM_HERE, | 181 FROM_HERE, |
| 144 base::Bind(callback, success)); | 182 base::Bind(callback, success)); |
| 145 } | 183 } |
| 146 | 184 |
| 185 void PlatformNotificationContextImpl::OnRegistrationDeleted( | |
| 186 int64_t registration_id, | |
| 187 const GURL& pattern) { | |
| 188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 189 LazyInitialize( | |
| 190 base::Bind(&PlatformNotificationContextImpl:: | |
| 191 DoDeleteNotificationsForServiceWorkerRegistration, | |
| 192 this, pattern.GetOrigin(), registration_id), | |
| 193 base::Bind(&EmptyFailureCallback)); | |
| 194 } | |
| 195 | |
| 196 void PlatformNotificationContextImpl:: | |
| 197 DoDeleteNotificationsForServiceWorkerRegistration( | |
| 198 const GURL& origin, | |
| 199 int64_t service_worker_registration_id) { | |
| 200 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 201 | |
| 202 std::set<int64_t> deleted_notifications_set; | |
| 203 database_->DeleteAllNotificationDataForServiceWorkerRegistration( | |
| 204 origin, service_worker_registration_id, &deleted_notifications_set); | |
| 205 | |
| 206 // TODO(peter): Record UMA on status for deleting from the database. | |
|
johnme
2015/03/20 14:56:35
Nit: add a matching "Do the DeleteAndStartOver dan
Peter Beverloo
2015/03/20 18:39:05
Done.
| |
| 207 // TODO(peter): Close the notifications in |deleted_notifications_set|. | |
| 208 } | |
| 209 | |
| 147 void PlatformNotificationContextImpl::LazyInitialize( | 210 void PlatformNotificationContextImpl::LazyInitialize( |
| 148 const base::Closure& success_closure, | 211 const base::Closure& success_closure, |
| 149 const base::Closure& failure_closure) { | 212 const base::Closure& failure_closure) { |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 213 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 | 214 |
| 152 if (!task_runner_) { | 215 if (!task_runner_) { |
| 153 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 216 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 154 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | 217 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| 155 | 218 |
| 156 task_runner_ = pool->GetSequencedTaskRunner(token); | 219 task_runner_ = pool->GetSequencedTaskRunner(token); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 | 260 |
| 198 return path_.Append(kPlatformNotificationsDirectory); | 261 return path_.Append(kPlatformNotificationsDirectory); |
| 199 } | 262 } |
| 200 | 263 |
| 201 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 264 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 202 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 265 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 203 task_runner_ = task_runner; | 266 task_runner_ = task_runner; |
| 204 } | 267 } |
| 205 | 268 |
| 206 } // namespace content | 269 } // namespace content |
| OLD | NEW |