| 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.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/notifications/notification_database_data.h" | |
| 10 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/notification_database_data.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 // Name of the directory in the user's profile directory where the notification | 14 // Name of the directory in the user's profile directory where the notification |
| 15 // database files should be stored. | 15 // database files should be stored. |
| 16 const base::FilePath::CharType kPlatformNotificationsDirectory[] = | 16 const base::FilePath::CharType kPlatformNotificationsDirectory[] = |
| 17 FILE_PATH_LITERAL("Platform Notifications"); | 17 FILE_PATH_LITERAL("Platform Notifications"); |
| 18 | 18 |
| 19 PlatformNotificationContext::PlatformNotificationContext( | 19 PlatformNotificationContextImpl::PlatformNotificationContextImpl( |
| 20 const base::FilePath& path) | 20 const base::FilePath& path) |
| 21 : path_(path) { | 21 : path_(path) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 PlatformNotificationContext::~PlatformNotificationContext() { | 24 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { |
| 25 // If the database has been initialized, it must be deleted on the task runner | 25 // If the database has been initialized, it must be deleted on the task runner |
| 26 // thread as closing it may cause file I/O. | 26 // thread as closing it may cause file I/O. |
| 27 if (database_) { | 27 if (database_) { |
| 28 DCHECK(task_runner_); | 28 DCHECK(task_runner_); |
| 29 task_runner_->DeleteSoon(FROM_HERE, database_.release()); | 29 task_runner_->DeleteSoon(FROM_HERE, database_.release()); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 void PlatformNotificationContext::ReadNotificationData( | 33 void PlatformNotificationContextImpl::ReadNotificationData( |
| 34 int64_t notification_id, | 34 int64_t notification_id, |
| 35 const GURL& origin, | 35 const GURL& origin, |
| 36 const ReadResultCallback& callback) { | 36 const ReadResultCallback& callback) { |
| 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 38 LazyInitialize( | 38 LazyInitialize( |
| 39 base::Bind(&PlatformNotificationContext::DoReadNotificationData, | 39 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, |
| 40 this, notification_id, origin, callback), | 40 this, notification_id, origin, callback), |
| 41 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 41 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void PlatformNotificationContext::DoReadNotificationData( | 44 void PlatformNotificationContextImpl::DoReadNotificationData( |
| 45 int64_t notification_id, | 45 int64_t notification_id, |
| 46 const GURL& origin, | 46 const GURL& origin, |
| 47 const ReadResultCallback& callback) { | 47 const ReadResultCallback& callback) { |
| 48 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 48 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 49 | 49 |
| 50 NotificationDatabaseData database_data; | 50 NotificationDatabaseData database_data; |
| 51 NotificationDatabase::Status status = | 51 NotificationDatabase::Status status = |
| 52 database_->ReadNotificationData(notification_id, | 52 database_->ReadNotificationData(notification_id, |
| 53 origin, | 53 origin, |
| 54 &database_data); | 54 &database_data); |
| 55 | 55 |
| 56 if (status == NotificationDatabase::STATUS_OK) { | 56 if (status == NotificationDatabase::STATUS_OK) { |
| 57 BrowserThread::PostTask(BrowserThread::IO, | 57 BrowserThread::PostTask(BrowserThread::IO, |
| 58 FROM_HERE, | 58 FROM_HERE, |
| 59 base::Bind(callback, | 59 base::Bind(callback, |
| 60 true /* success */, | 60 true /* success */, |
| 61 database_data)); | 61 database_data)); |
| 62 return; | 62 return; |
| 63 } | 63 } |
| 64 | 64 |
| 65 // TODO(peter): Record UMA on |status| for reading from the database. | 65 // TODO(peter): Record UMA on |status| for reading from the database. |
| 66 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | 66 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| 67 | 67 |
| 68 BrowserThread::PostTask( | 68 BrowserThread::PostTask( |
| 69 BrowserThread::IO, | 69 BrowserThread::IO, |
| 70 FROM_HERE, | 70 FROM_HERE, |
| 71 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 71 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void PlatformNotificationContext::WriteNotificationData( | 74 void PlatformNotificationContextImpl::WriteNotificationData( |
| 75 const GURL& origin, | 75 const GURL& origin, |
| 76 const NotificationDatabaseData& database_data, | 76 const NotificationDatabaseData& database_data, |
| 77 const WriteResultCallback& callback) { | 77 const WriteResultCallback& callback) { |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 79 LazyInitialize( | 79 LazyInitialize( |
| 80 base::Bind(&PlatformNotificationContext::DoWriteNotificationData, | 80 base::Bind(&PlatformNotificationContextImpl::DoWriteNotificationData, |
| 81 this, origin, database_data, callback), | 81 this, origin, database_data, callback), |
| 82 base::Bind(callback, false /* success */, 0 /* notification_id */)); | 82 base::Bind(callback, false /* success */, 0 /* notification_id */)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void PlatformNotificationContext::DoWriteNotificationData( | 85 void PlatformNotificationContextImpl::DoWriteNotificationData( |
| 86 const GURL& origin, | 86 const GURL& origin, |
| 87 const NotificationDatabaseData& database_data, | 87 const NotificationDatabaseData& database_data, |
| 88 const WriteResultCallback& callback) { | 88 const WriteResultCallback& callback) { |
| 89 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 89 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 90 | 90 |
| 91 int64_t notification_id = 0; | 91 int64_t notification_id = 0; |
| 92 NotificationDatabase::Status status = | 92 NotificationDatabase::Status status = |
| 93 database_->WriteNotificationData(origin, | 93 database_->WriteNotificationData(origin, |
| 94 database_data, | 94 database_data, |
| 95 ¬ification_id); | 95 ¬ification_id); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 107 | 107 |
| 108 // TODO(peter): Record UMA on |status| for reading from the database. | 108 // TODO(peter): Record UMA on |status| for reading from the database. |
| 109 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | 109 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| 110 | 110 |
| 111 BrowserThread::PostTask( | 111 BrowserThread::PostTask( |
| 112 BrowserThread::IO, | 112 BrowserThread::IO, |
| 113 FROM_HERE, | 113 FROM_HERE, |
| 114 base::Bind(callback, false /* success */, 0 /* notification_id */)); | 114 base::Bind(callback, false /* success */, 0 /* notification_id */)); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void PlatformNotificationContext::DeleteNotificationData( | 117 void PlatformNotificationContextImpl::DeleteNotificationData( |
| 118 int64_t notification_id, | 118 int64_t notification_id, |
| 119 const GURL& origin, | 119 const GURL& origin, |
| 120 const DeleteResultCallback& callback) { | 120 const DeleteResultCallback& callback) { |
| 121 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 121 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 122 LazyInitialize( | 122 LazyInitialize( |
| 123 base::Bind(&PlatformNotificationContext::DoDeleteNotificationData, | 123 base::Bind(&PlatformNotificationContextImpl::DoDeleteNotificationData, |
| 124 this, notification_id, origin, callback), | 124 this, notification_id, origin, callback), |
| 125 base::Bind(callback, false /* success */)); | 125 base::Bind(callback, false /* success */)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void PlatformNotificationContext::DoDeleteNotificationData( | 128 void PlatformNotificationContextImpl::DoDeleteNotificationData( |
| 129 int64_t notification_id, | 129 int64_t notification_id, |
| 130 const GURL& origin, | 130 const GURL& origin, |
| 131 const DeleteResultCallback& callback) { | 131 const DeleteResultCallback& callback) { |
| 132 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 132 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 133 | 133 |
| 134 NotificationDatabase::Status status = | 134 NotificationDatabase::Status status = |
| 135 database_->DeleteNotificationData(notification_id, origin); | 135 database_->DeleteNotificationData(notification_id, origin); |
| 136 | 136 |
| 137 const bool success = status == NotificationDatabase::STATUS_OK; | 137 const bool success = status == NotificationDatabase::STATUS_OK; |
| 138 | 138 |
| 139 // TODO(peter): Record UMA on |status| for reading from the database. | 139 // TODO(peter): Record UMA on |status| for reading from the database. |
| 140 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | 140 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| 141 | 141 |
| 142 BrowserThread::PostTask(BrowserThread::IO, | 142 BrowserThread::PostTask(BrowserThread::IO, |
| 143 FROM_HERE, | 143 FROM_HERE, |
| 144 base::Bind(callback, success)); | 144 base::Bind(callback, success)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void PlatformNotificationContext::LazyInitialize( | 147 void PlatformNotificationContextImpl::LazyInitialize( |
| 148 const base::Closure& success_closure, | 148 const base::Closure& success_closure, |
| 149 const base::Closure& failure_closure) { | 149 const base::Closure& failure_closure) { |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 | 151 |
| 152 if (!task_runner_) { | 152 if (!task_runner_) { |
| 153 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 153 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 154 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | 154 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| 155 | 155 |
| 156 task_runner_ = pool->GetSequencedTaskRunner(token); | 156 task_runner_ = pool->GetSequencedTaskRunner(token); |
| 157 } | 157 } |
| 158 | 158 |
| 159 task_runner_->PostTask( | 159 task_runner_->PostTask( |
| 160 FROM_HERE, | 160 FROM_HERE, |
| 161 base::Bind(&PlatformNotificationContext::OpenDatabase, | 161 base::Bind(&PlatformNotificationContextImpl::OpenDatabase, |
| 162 this, success_closure, failure_closure)); | 162 this, success_closure, failure_closure)); |
| 163 } | 163 } |
| 164 | 164 |
| 165 void PlatformNotificationContext::OpenDatabase( | 165 void PlatformNotificationContextImpl::OpenDatabase( |
| 166 const base::Closure& success_closure, | 166 const base::Closure& success_closure, |
| 167 const base::Closure& failure_closure) { | 167 const base::Closure& failure_closure) { |
| 168 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 168 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 169 | 169 |
| 170 if (database_) { | 170 if (database_) { |
| 171 success_closure.Run(); | 171 success_closure.Run(); |
| 172 return; | 172 return; |
| 173 } | 173 } |
| 174 | 174 |
| 175 database_.reset(new NotificationDatabase(GetDatabasePath())); | 175 database_.reset(new NotificationDatabase(GetDatabasePath())); |
| 176 | 176 |
| 177 // TODO(peter): Record UMA on |status| for opening the database. | 177 // TODO(peter): Record UMA on |status| for opening the database. |
| 178 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | 178 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| 179 | 179 |
| 180 NotificationDatabase::Status status = | 180 NotificationDatabase::Status status = |
| 181 database_->Open(true /* create_if_missing */); | 181 database_->Open(true /* create_if_missing */); |
| 182 | 182 |
| 183 if (status == NotificationDatabase::STATUS_OK) { | 183 if (status == NotificationDatabase::STATUS_OK) { |
| 184 success_closure.Run(); | 184 success_closure.Run(); |
| 185 return; | 185 return; |
| 186 } | 186 } |
| 187 | 187 |
| 188 // TODO(peter): Properly handle failures when opening the database. | 188 // TODO(peter): Properly handle failures when opening the database. |
| 189 database_.reset(); | 189 database_.reset(); |
| 190 | 190 |
| 191 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); | 191 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); |
| 192 } | 192 } |
| 193 | 193 |
| 194 base::FilePath PlatformNotificationContext::GetDatabasePath() const { | 194 base::FilePath PlatformNotificationContextImpl::GetDatabasePath() const { |
| 195 if (path_.empty()) | 195 if (path_.empty()) |
| 196 return path_; | 196 return path_; |
| 197 | 197 |
| 198 return path_.Append(kPlatformNotificationsDirectory); | 198 return path_.Append(kPlatformNotificationsDirectory); |
| 199 } | 199 } |
| 200 | 200 |
| 201 void PlatformNotificationContext::SetTaskRunnerForTesting( | 201 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 202 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 202 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 203 task_runner_ = task_runner; | 203 task_runner_ = task_runner; |
| 204 } | 204 } |
| 205 | 205 |
| 206 } // namespace content | 206 } // namespace content |
| OLD | NEW |