Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(829)

Unified Diff: content/browser/notifications/platform_notification_context.cc

Issue 1006493005: Introduce the PlatformNotificationContext class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@n-db-ReadWriteDelete
Patch Set: fix a typo in the test Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..620a72d16d68dbf7c6e7352b644b48a4ad9b445d
--- /dev/null
+++ b/content/browser/notifications/platform_notification_context.cc
@@ -0,0 +1,168 @@
+// 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;
+ 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()));
Bernhard Bauer 2015/03/13 17:24:01 Hm, I think you might be able to simplify both thi
Peter Beverloo 2015/03/13 20:43:30 Done.
+ 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.
+ // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED.
+
+ 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(
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
+ task_runner_ = task_runner;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698