Chromium Code Reviews| Index: content/browser/notifications/platform_notification_context.cc |
| diff --git a/content/browser/notifications/platform_notification_context.cc b/content/browser/notifications/platform_notification_context.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c2e928d370edce15a7fe3ee5a8ba6930f802428 |
| --- /dev/null |
| +++ b/content/browser/notifications/platform_notification_context.cc |
| @@ -0,0 +1,167 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/notifications/platform_notification_context.h" |
| + |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "content/browser/notifications/notification_database.h" |
| +#include "content/browser/notifications/notification_database_data.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace content { |
| + |
| +// Name of the directory in the user's profile directory where the notification |
| +// database files should be stored. |
| +const base::FilePath::CharType kPlatformNotificationsDirectory[] = |
| + FILE_PATH_LITERAL("Platform Notifications"); |
| + |
| +PlatformNotificationContext::PlatformNotificationContext( |
| + const base::FilePath& path) |
| + : path_(path), |
| + next_notification_id_(0) { |
| +} |
| + |
| +PlatformNotificationContext::~PlatformNotificationContext() { |
| + // If the database has been initialized, it must be deleted on the task runner |
| + // thread as closing it may cause file I/O. |
| + if (database_) { |
| + DCHECK(task_runner_); |
| + task_runner_->DeleteSoon(FROM_HERE, database_.release()); |
| + } |
| +} |
| + |
| +void PlatformNotificationContext::ReadNotificationData( |
| + int64_t notification_id, |
| + const GURL& origin, |
| + const ReadResultCallback& callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + LazyInitialize( |
| + base::Bind(&PlatformNotificationContext::DoReadNotificationData, |
| + this, notification_id, origin, callback), |
| + base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| +} |
| + |
| +void PlatformNotificationContext::DoReadNotificationData( |
| + int64_t notification_id, |
| + const GURL& origin, |
| + const ReadResultCallback& callback) { |
| + NotificationDatabaseData database_data; |
|
Bernhard Bauer
2015/03/13 16:51:18
DCHECK you're on the right thread? (Also below)
Peter Beverloo
2015/03/13 16:57:07
All the public methods in database_ have the appro
Bernhard Bauer
2015/03/13 17:24:01
Those do, but there are also methods like LazyInit
Peter Beverloo
2015/03/13 20:43:30
Done.
|
| + NotificationDatabase::Status status = |
| + database_->ReadNotificationData(notification_id, |
| + origin, |
| + &database_data); |
| + |
| + if (status == NotificationDatabase::STATUS_OK) { |
| + BrowserThread::PostTask(BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, |
| + true /* success */, |
| + database_data)); |
| + return; |
| + } |
| + |
| + // TODO(peter): Record UMA on |status| for reading from the database. |
| + // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| +} |
| + |
| +void PlatformNotificationContext::WriteNotificationData( |
| + const GURL& origin, |
| + const NotificationDatabaseData& database_data, |
| + const WriteResultCallback& callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + LazyInitialize( |
| + base::Bind(&PlatformNotificationContext::DoWriteNotificationData, |
| + this, origin, database_data, callback), |
| + base::Bind(callback, false /* success */, 0 /* notification_id */)); |
| +} |
| + |
| +void PlatformNotificationContext::DoWriteNotificationData( |
| + const GURL& origin, |
| + const NotificationDatabaseData& database_data, |
| + const WriteResultCallback& callback) { |
| + int64_t notification_id = next_notification_id_++; |
| + NotificationDatabase::Status status = |
| + database_->WriteNotificationData(notification_id, |
| + origin, |
| + database_data); |
| + |
| + if (status == NotificationDatabase::STATUS_OK) { |
| + BrowserThread::PostTask(BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, |
| + true /* success */, |
| + notification_id)); |
| + return; |
| + } |
| + |
| + // TODO(peter): Record UMA on |status| for reading from the database. |
| + // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, false /* success */, 0 /* notification_id */)); |
| +} |
| + |
| +void PlatformNotificationContext::LazyInitialize( |
| + const base::Closure& success_closure, |
| + const base::Closure& failure_closure) { |
| + if (!task_runner_) { |
| + base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| + base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); |
| + |
| + task_runner_ = pool->GetSequencedTaskRunner(token); |
| + } |
| + |
| + if (!database_) { |
| + database_.reset(new NotificationDatabase(GetDatabasePath())); |
| + |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PlatformNotificationContext::OpenDatabase, |
| + this, success_closure, failure_closure)); |
| + return; |
| + } |
| + |
| + task_runner_->PostTask(FROM_HERE, success_closure); |
| +} |
| + |
| +void PlatformNotificationContext::OpenDatabase( |
| + const base::Closure& success_closure, |
| + const base::Closure& failure_closure) { |
| + DCHECK(database_); |
| + |
| + // TODO(peter): Record UMA on |status| for opening the database. |
| + NotificationDatabase::Status status = |
| + database_->Open(true /* create_if_missing */); |
| + |
| + if (status == NotificationDatabase::STATUS_OK) { |
| + status = database_->GetNextNotificationId(&next_notification_id_); |
| + if (status == NotificationDatabase::STATUS_OK) { |
| + success_closure.Run(); |
| + return; |
| + } |
| + } |
| + |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); |
| +} |
| + |
| +base::FilePath PlatformNotificationContext::GetDatabasePath() const { |
| + if (path_.empty()) |
| + return path_; |
| + |
| + return path_.Append(kPlatformNotificationsDirectory); |
| +} |
| + |
| +void PlatformNotificationContext::SetTaskRunnerForTesting( |
| + scoped_refptr<base::SequencedTaskRunner> task_runner) { |
| + task_runner_ = task_runner; |
| +} |
| + |
| +} // namespace content |