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