Commit 18fd3fcf authored by peter's avatar peter Committed by Commit bot

Teach the PlatformNotificationContext how to delete notifications.

This follows the very same pattern as the read and write methods, but does
so for deleting notifications instead. Note that the notification database
still isn't hooked up to production code, but is covered by a broad
series of unit tests.

Design document:
  http://goo.gl/TciXVp

BUG=447628

Review URL: https://codereview.chromium.org/1008003003

Cr-Commit-Position: refs/heads/master@{#320914}
parent 3b959f7a
......@@ -114,6 +114,36 @@ void PlatformNotificationContext::DoWriteNotificationData(
base::Bind(callback, false /* success */, 0 /* notification_id */));
}
void PlatformNotificationContext::DeleteNotificationData(
int64_t notification_id,
const GURL& origin,
const DeleteResultCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
LazyInitialize(
base::Bind(&PlatformNotificationContext::DoDeleteNotificationData,
this, notification_id, origin, callback),
base::Bind(callback, false /* success */));
}
void PlatformNotificationContext::DoDeleteNotificationData(
int64_t notification_id,
const GURL& origin,
const DeleteResultCallback& callback) {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
NotificationDatabase::Status status =
database_->DeleteNotificationData(notification_id, origin);
const bool success = status == NotificationDatabase::STATUS_OK;
// 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, success));
}
void PlatformNotificationContext::LazyInitialize(
const base::Closure& success_closure,
const base::Closure& failure_closure) {
......
......@@ -44,6 +44,8 @@ class CONTENT_EXPORT PlatformNotificationContext
base::Callback<void(bool /* success */,
int64_t /* notification_id */)>;
using DeleteResultCallback = base::Callback<void(bool /* success */)>;
// Reads the data associated with |notification_id| belonging to |origin|
// from the database. |callback| will be invoked with the success status
// and a reference to the notification database data when completed.
......@@ -58,6 +60,13 @@ class CONTENT_EXPORT PlatformNotificationContext
const NotificationDatabaseData& database_data,
const WriteResultCallback& callback);
// Deletes all data associated with |notification_id| belonging to |origin|
// from the database. |callback| will be invoked with the success status
// when the operation has completed.
void DeleteNotificationData(int64_t notification_id,
const GURL& origin,
const DeleteResultCallback& callback);
private:
friend class base::RefCountedThreadSafe<PlatformNotificationContext>;
friend class PlatformNotificationContextTest;
......@@ -78,19 +87,26 @@ class CONTENT_EXPORT PlatformNotificationContext
const base::Closure& failure_closure);
// Actually reads the notification data from the database. Must only be
// called on the |task_runner_| thread. Will post a task to |callback| on
// the IO thread when the operation has completed.
// called on the |task_runner_| thread. |callback| will be invoked on the
// IO thread when the operation has completed.
void DoReadNotificationData(int64_t notification_id,
const GURL& origin,
const ReadResultCallback& callback);
// Actually writes the notification database to the database. Must only be
// called on the |task_runner_| thread. Will post a task to |callback| on
// the IO thread when the operation has completed.
// called on the |task_runner_| thread. |callback| will be invoked on the
// IO thread when the operation has completed.
void DoWriteNotificationData(const GURL& origin,
const NotificationDatabaseData& database_data,
const WriteResultCallback& callback);
// Actually deletes the notification information from the database. Must only
// be called on the |task_runner_| thread. |callback| will be invoked on the
// IO thread when the operation has completed.
void DoDeleteNotificationData(int64_t notification_id,
const GURL& origin,
const DeleteResultCallback& callback);
// Returns the path in which the database should be initialized. May be empty.
base::FilePath GetDatabasePath() const;
......
......@@ -32,6 +32,11 @@ class PlatformNotificationContextTest : public ::testing::Test {
notification_id_ = notification_id;
}
// Callback to provide when deleting notification data from the database.
void DidDeleteNotificationData(bool success) {
success_ = success;
}
protected:
// Creates a new PlatformNotificationContext instance. When using this method,
// the underlying database will always be created in memory. The current
......@@ -115,4 +120,65 @@ TEST_F(PlatformNotificationContextTest, WriteReadNotification) {
EXPECT_EQ(notification_database_data.origin, read_database_data.origin);
}
TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) {
scoped_refptr<PlatformNotificationContext> context =
CreatePlatformNotificationContext();
context->DeleteNotificationData(
42 /* notification_id */,
GURL("https://example.com"),
base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
// The notification may not have existed, but since the goal of deleting data
// is to make sure that it's gone, the goal has been satisfied. As such,
// deleting a non-existent notification is considered to be a success.
EXPECT_TRUE(success());
}
TEST_F(PlatformNotificationContextTest, DeleteNotification) {
scoped_refptr<PlatformNotificationContext> context =
CreatePlatformNotificationContext();
GURL origin("https://example.com");
NotificationDatabaseData notification_database_data;
context->WriteNotificationData(
origin,
notification_database_data,
base::Bind(&PlatformNotificationContextTest::DidWriteNotificationData,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
// The write operation should have succeeded with a notification id.
ASSERT_TRUE(success());
EXPECT_GT(notification_id(), 0);
context->DeleteNotificationData(
notification_id(),
origin,
base::Bind(&PlatformNotificationContextTest::DidDeleteNotificationData,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
// The notification existed, so it should have been removed successfully.
ASSERT_TRUE(success());
context->ReadNotificationData(
notification_id(),
origin,
base::Bind(&PlatformNotificationContextTest::DidReadNotificationData,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
// The notification was removed, so we shouldn't be able to read it from
// the database anymore.
EXPECT_FALSE(success());
}
} // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment