Commit 7b79a196 authored by peter's avatar peter Committed by Commit bot

Expose bits of the Web Notification database in the //content API.

This CL moves the PlatformNotificationContext and the
NotificationDatabaseData classes into the //content API. Serialization
behavior for NotificationDatabaseData will remain content-private.

The primary users of this will be (1) clearing browser data, (2) UA-
triggered notification closures and (3) forced push notifications.

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

BUG=447628

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

Cr-Commit-Position: refs/heads/master@{#320993}
parent cfe3dec1
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "content/browser/notifications/notification_database_data.h" #include "content/browser/notifications/notification_database_data_conversions.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "storage/common/database/database_identifier.h" #include "storage/common/database/database_identifier.h"
#include "third_party/leveldatabase/src/helpers/memenv/memenv.h" #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
...@@ -125,8 +125,10 @@ NotificationDatabase::Status NotificationDatabase::ReadNotificationData( ...@@ -125,8 +125,10 @@ NotificationDatabase::Status NotificationDatabase::ReadNotificationData(
if (status != STATUS_OK) if (status != STATUS_OK)
return status; return status;
if (notification_database_data->ParseFromString(serialized_data)) if (DeserializeNotificationDatabaseData(serialized_data,
notification_database_data)) {
return STATUS_OK; return STATUS_OK;
}
DLOG(ERROR) << "Unable to deserialize data for notification " DLOG(ERROR) << "Unable to deserialize data for notification "
<< notification_id << " belonging to " << origin << "."; << notification_id << " belonging to " << origin << ".";
...@@ -143,7 +145,8 @@ NotificationDatabase::Status NotificationDatabase::WriteNotificationData( ...@@ -143,7 +145,8 @@ NotificationDatabase::Status NotificationDatabase::WriteNotificationData(
DCHECK(origin.is_valid()); DCHECK(origin.is_valid());
std::string serialized_data; std::string serialized_data;
if (!notification_database_data.SerializeToString(&serialized_data)) { if (!SerializeNotificationDatabaseData(notification_database_data,
&serialized_data)) {
DLOG(ERROR) << "Unable to serialize data for a notification belonging " DLOG(ERROR) << "Unable to serialize data for a notification belonging "
<< "to: " << origin; << "to: " << origin;
return STATUS_ERROR_FAILED; return STATUS_ERROR_FAILED;
......
...@@ -2,53 +2,56 @@ ...@@ -2,53 +2,56 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/notifications/notification_database_data.h" #include "content/browser/notifications/notification_database_data_conversions.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/browser/notifications/notification_database_data.pb.h" #include "content/browser/notifications/notification_database_data.pb.h"
#include "content/public/browser/notification_database_data.h"
namespace content { namespace content {
NotificationDatabaseData::NotificationDatabaseData() bool DeserializeNotificationDatabaseData(const std::string& input,
: notification_id(0), NotificationDatabaseData* output) {
service_worker_registration_id(0) { DCHECK(output);
}
NotificationDatabaseData::~NotificationDatabaseData() {}
bool NotificationDatabaseData::ParseFromString(const std::string& input) {
NotificationDatabaseDataProto message; NotificationDatabaseDataProto message;
if (!message.ParseFromString(input)) if (!message.ParseFromString(input))
return false; return false;
notification_id = message.notification_id(); output->notification_id = message.notification_id();
origin = GURL(message.origin()); output->origin = GURL(message.origin());
service_worker_registration_id = message.service_worker_registration_id(); output->service_worker_registration_id =
message.service_worker_registration_id();
PlatformNotificationData* notification_data = &output->notification_data;
const NotificationDatabaseDataProto::NotificationData& payload = const NotificationDatabaseDataProto::NotificationData& payload =
message.notification_data(); message.notification_data();
notification_data.title = base::UTF8ToUTF16(payload.title()); notification_data->title = base::UTF8ToUTF16(payload.title());
notification_data.direction = notification_data->direction =
payload.direction() == payload.direction() ==
NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT ? NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT ?
PlatformNotificationData::NotificationDirectionRightToLeft : PlatformNotificationData::NotificationDirectionRightToLeft :
PlatformNotificationData::NotificationDirectionLeftToRight; PlatformNotificationData::NotificationDirectionLeftToRight;
notification_data.lang = payload.lang(); notification_data->lang = payload.lang();
notification_data.body = base::UTF8ToUTF16(payload.body()); notification_data->body = base::UTF8ToUTF16(payload.body());
notification_data.tag = payload.tag(); notification_data->tag = payload.tag();
notification_data.icon = GURL(payload.icon()); notification_data->icon = GURL(payload.icon());
notification_data.silent = payload.silent(); notification_data->silent = payload.silent();
return true; return true;
} }
bool NotificationDatabaseData::SerializeToString(std::string* output) const { bool SerializeNotificationDatabaseData(const NotificationDatabaseData& input,
std::string* output) {
DCHECK(output); DCHECK(output);
scoped_ptr<NotificationDatabaseDataProto::NotificationData> payload( scoped_ptr<NotificationDatabaseDataProto::NotificationData> payload(
new NotificationDatabaseDataProto::NotificationData()); new NotificationDatabaseDataProto::NotificationData());
const PlatformNotificationData& notification_data = input.notification_data;
payload->set_title(base::UTF16ToUTF8(notification_data.title)); payload->set_title(base::UTF16ToUTF8(notification_data.title));
payload->set_direction( payload->set_direction(
notification_data.direction == notification_data.direction ==
...@@ -62,10 +65,10 @@ bool NotificationDatabaseData::SerializeToString(std::string* output) const { ...@@ -62,10 +65,10 @@ bool NotificationDatabaseData::SerializeToString(std::string* output) const {
payload->set_silent(notification_data.silent); payload->set_silent(notification_data.silent);
NotificationDatabaseDataProto message; NotificationDatabaseDataProto message;
message.set_notification_id(notification_id); message.set_notification_id(input.notification_id);
message.set_origin(origin.spec()); message.set_origin(input.origin.spec());
message.set_service_worker_registration_id( message.set_service_worker_registration_id(
service_worker_registration_id); input.service_worker_registration_id);
message.set_allocated_notification_data(payload.release()); message.set_allocated_notification_data(payload.release());
return message.SerializeToString(output); return message.SerializeToString(output);
......
// 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.
#ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_CONVERSIONS_H_
#define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_CONVERSIONS_H_
#include <string>
#include "content/common/content_export.h"
namespace content {
struct NotificationDatabaseData;
// Parses the serialized notification data |input| into a new object, |output|.
// Returns whether the serialized |input| could be deserialized successfully.
CONTENT_EXPORT bool DeserializeNotificationDatabaseData(
const std::string& input,
NotificationDatabaseData* output);
// Serializes the contents of |input| into the string |output|. Returns whether
// the notification data could be serialized successfully.
CONTENT_EXPORT bool SerializeNotificationDatabaseData(
const NotificationDatabaseData& input,
std::string* output);
} // namespace content
#endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_CONVERSIONS_H_
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/notifications/notification_database_data.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/browser/notifications/notification_database_data.pb.h" #include "content/browser/notifications/notification_database_data.pb.h"
#include "content/browser/notifications/notification_database_data_conversions.h"
#include "content/public/browser/notification_database_data.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace content { namespace content {
...@@ -40,12 +40,14 @@ TEST(NotificationDatabaseDataTest, SerializeAndDeserializeData) { ...@@ -40,12 +40,14 @@ TEST(NotificationDatabaseDataTest, SerializeAndDeserializeData) {
std::string serialized_data; std::string serialized_data;
// Serialize the data in |notification_data| to the string |serialized_data|. // Serialize the data in |notification_data| to the string |serialized_data|.
ASSERT_TRUE(database_data.SerializeToString(&serialized_data)); ASSERT_TRUE(SerializeNotificationDatabaseData(database_data,
&serialized_data));
NotificationDatabaseData copied_data; NotificationDatabaseData copied_data;
// Deserialize the data in |serialized_data| to |copied_data|. // Deserialize the data in |serialized_data| to |copied_data|.
ASSERT_TRUE(copied_data.ParseFromString(serialized_data)); ASSERT_TRUE(DeserializeNotificationDatabaseData(serialized_data,
&copied_data));
EXPECT_EQ(database_data.notification_id, copied_data.notification_id); EXPECT_EQ(database_data.notification_id, copied_data.notification_id);
EXPECT_EQ(database_data.origin, copied_data.origin); EXPECT_EQ(database_data.origin, copied_data.origin);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/browser/notifications/notification_database_data.h" #include "content/public/browser/notification_database_data.h"
#include "content/public/common/platform_notification_data.h" #include "content/public/common/platform_notification_data.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h" #include "third_party/leveldatabase/src/include/leveldb/db.h"
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/notifications/platform_notification_context.h" #include "content/browser/notifications/platform_notification_context_impl.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "content/browser/notifications/notification_database.h" #include "content/browser/notifications/notification_database.h"
#include "content/browser/notifications/notification_database_data.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_database_data.h"
namespace content { namespace content {
...@@ -16,12 +16,12 @@ namespace content { ...@@ -16,12 +16,12 @@ namespace content {
const base::FilePath::CharType kPlatformNotificationsDirectory[] = const base::FilePath::CharType kPlatformNotificationsDirectory[] =
FILE_PATH_LITERAL("Platform Notifications"); FILE_PATH_LITERAL("Platform Notifications");
PlatformNotificationContext::PlatformNotificationContext( PlatformNotificationContextImpl::PlatformNotificationContextImpl(
const base::FilePath& path) const base::FilePath& path)
: path_(path) { : path_(path) {
} }
PlatformNotificationContext::~PlatformNotificationContext() { PlatformNotificationContextImpl::~PlatformNotificationContextImpl() {
// If the database has been initialized, it must be deleted on the task runner // If the database has been initialized, it must be deleted on the task runner
// thread as closing it may cause file I/O. // thread as closing it may cause file I/O.
if (database_) { if (database_) {
...@@ -30,18 +30,18 @@ PlatformNotificationContext::~PlatformNotificationContext() { ...@@ -30,18 +30,18 @@ PlatformNotificationContext::~PlatformNotificationContext() {
} }
} }
void PlatformNotificationContext::ReadNotificationData( void PlatformNotificationContextImpl::ReadNotificationData(
int64_t notification_id, int64_t notification_id,
const GURL& origin, const GURL& origin,
const ReadResultCallback& callback) { const ReadResultCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
LazyInitialize( LazyInitialize(
base::Bind(&PlatformNotificationContext::DoReadNotificationData, base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData,
this, notification_id, origin, callback), this, notification_id, origin, callback),
base::Bind(callback, false /* success */, NotificationDatabaseData())); base::Bind(callback, false /* success */, NotificationDatabaseData()));
} }
void PlatformNotificationContext::DoReadNotificationData( void PlatformNotificationContextImpl::DoReadNotificationData(
int64_t notification_id, int64_t notification_id,
const GURL& origin, const GURL& origin,
const ReadResultCallback& callback) { const ReadResultCallback& callback) {
...@@ -71,18 +71,18 @@ void PlatformNotificationContext::DoReadNotificationData( ...@@ -71,18 +71,18 @@ void PlatformNotificationContext::DoReadNotificationData(
base::Bind(callback, false /* success */, NotificationDatabaseData())); base::Bind(callback, false /* success */, NotificationDatabaseData()));
} }
void PlatformNotificationContext::WriteNotificationData( void PlatformNotificationContextImpl::WriteNotificationData(
const GURL& origin, const GURL& origin,
const NotificationDatabaseData& database_data, const NotificationDatabaseData& database_data,
const WriteResultCallback& callback) { const WriteResultCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
LazyInitialize( LazyInitialize(
base::Bind(&PlatformNotificationContext::DoWriteNotificationData, base::Bind(&PlatformNotificationContextImpl::DoWriteNotificationData,
this, origin, database_data, callback), this, origin, database_data, callback),
base::Bind(callback, false /* success */, 0 /* notification_id */)); base::Bind(callback, false /* success */, 0 /* notification_id */));
} }
void PlatformNotificationContext::DoWriteNotificationData( void PlatformNotificationContextImpl::DoWriteNotificationData(
const GURL& origin, const GURL& origin,
const NotificationDatabaseData& database_data, const NotificationDatabaseData& database_data,
const WriteResultCallback& callback) { const WriteResultCallback& callback) {
...@@ -114,18 +114,18 @@ void PlatformNotificationContext::DoWriteNotificationData( ...@@ -114,18 +114,18 @@ void PlatformNotificationContext::DoWriteNotificationData(
base::Bind(callback, false /* success */, 0 /* notification_id */)); base::Bind(callback, false /* success */, 0 /* notification_id */));
} }
void PlatformNotificationContext::DeleteNotificationData( void PlatformNotificationContextImpl::DeleteNotificationData(
int64_t notification_id, int64_t notification_id,
const GURL& origin, const GURL& origin,
const DeleteResultCallback& callback) { const DeleteResultCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
LazyInitialize( LazyInitialize(
base::Bind(&PlatformNotificationContext::DoDeleteNotificationData, base::Bind(&PlatformNotificationContextImpl::DoDeleteNotificationData,
this, notification_id, origin, callback), this, notification_id, origin, callback),
base::Bind(callback, false /* success */)); base::Bind(callback, false /* success */));
} }
void PlatformNotificationContext::DoDeleteNotificationData( void PlatformNotificationContextImpl::DoDeleteNotificationData(
int64_t notification_id, int64_t notification_id,
const GURL& origin, const GURL& origin,
const DeleteResultCallback& callback) { const DeleteResultCallback& callback) {
...@@ -144,7 +144,7 @@ void PlatformNotificationContext::DoDeleteNotificationData( ...@@ -144,7 +144,7 @@ void PlatformNotificationContext::DoDeleteNotificationData(
base::Bind(callback, success)); base::Bind(callback, success));
} }
void PlatformNotificationContext::LazyInitialize( void PlatformNotificationContextImpl::LazyInitialize(
const base::Closure& success_closure, const base::Closure& success_closure,
const base::Closure& failure_closure) { const base::Closure& failure_closure) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -158,11 +158,11 @@ void PlatformNotificationContext::LazyInitialize( ...@@ -158,11 +158,11 @@ void PlatformNotificationContext::LazyInitialize(
task_runner_->PostTask( task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&PlatformNotificationContext::OpenDatabase, base::Bind(&PlatformNotificationContextImpl::OpenDatabase,
this, success_closure, failure_closure)); this, success_closure, failure_closure));
} }
void PlatformNotificationContext::OpenDatabase( void PlatformNotificationContextImpl::OpenDatabase(
const base::Closure& success_closure, const base::Closure& success_closure,
const base::Closure& failure_closure) { const base::Closure& failure_closure) {
DCHECK(task_runner_->RunsTasksOnCurrentThread()); DCHECK(task_runner_->RunsTasksOnCurrentThread());
...@@ -191,14 +191,14 @@ void PlatformNotificationContext::OpenDatabase( ...@@ -191,14 +191,14 @@ void PlatformNotificationContext::OpenDatabase(
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure);
} }
base::FilePath PlatformNotificationContext::GetDatabasePath() const { base::FilePath PlatformNotificationContextImpl::GetDatabasePath() const {
if (path_.empty()) if (path_.empty())
return path_; return path_;
return path_.Append(kPlatformNotificationsDirectory); return path_.Append(kPlatformNotificationsDirectory);
} }
void PlatformNotificationContext::SetTaskRunnerForTesting( void PlatformNotificationContextImpl::SetTaskRunnerForTesting(
const scoped_refptr<base::SequencedTaskRunner>& task_runner) { const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
task_runner_ = task_runner; task_runner_ = task_runner;
} }
......
...@@ -2,15 +2,17 @@ ...@@ -2,15 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ #ifndef CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_
#define CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ #define CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_
#include <stdint.h> #include <stdint.h>
#include "base/callback.h" #include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/public/browser/platform_notification_context.h"
class GURL; class GURL;
...@@ -23,55 +25,31 @@ namespace content { ...@@ -23,55 +25,31 @@ namespace content {
class NotificationDatabase; class NotificationDatabase;
struct NotificationDatabaseData; struct NotificationDatabaseData;
// Implementation of the Web Notification storage context. // Implementation of the Web Notification storage context. The public methods
// // defined in this interface must only be called on the IO thread.
// Represents the storage context for persistent Web Notifications, specific to class CONTENT_EXPORT PlatformNotificationContextImpl
// the storage partition owning the instance. The public methods defined in this : public NON_EXPORTED_BASE(PlatformNotificationContext) {
// interface must only be called on the IO thread.
class CONTENT_EXPORT PlatformNotificationContext
: public base::RefCountedThreadSafe<PlatformNotificationContext> {
public: public:
// Constructs a new platform notification context. If |path| is non-empty, the // Constructs a new platform notification context. If |path| is non-empty, the
// database will be initialized in the "Platform Notifications" subdirectory // database will be initialized in the "Platform Notifications" subdirectory
// of |path|. Otherwise, the database will be initialized in memory. // of |path|. Otherwise, the database will be initialized in memory.
explicit PlatformNotificationContext(const base::FilePath& path); explicit PlatformNotificationContextImpl(const base::FilePath& path);
using ReadResultCallback = // PlatformNotificationContext implementation.
base::Callback<void(bool /* success */,
const NotificationDatabaseData&)>;
using WriteResultCallback =
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.
void ReadNotificationData(int64_t notification_id, void ReadNotificationData(int64_t notification_id,
const GURL& origin, const GURL& origin,
const ReadResultCallback& callback); const ReadResultCallback& callback) override;
// Writes the data associated with a notification to a database. When this
// action completed, |callback| will be invoked with the success status and
// the persistent notification id when written successfully.
void WriteNotificationData(const GURL& origin, void WriteNotificationData(const GURL& origin,
const NotificationDatabaseData& database_data, const NotificationDatabaseData& database_data,
const WriteResultCallback& callback); const WriteResultCallback& callback) override;
// 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, void DeleteNotificationData(int64_t notification_id,
const GURL& origin, const GURL& origin,
const DeleteResultCallback& callback); const DeleteResultCallback& callback) override;
private: private:
friend class base::RefCountedThreadSafe<PlatformNotificationContext>;
friend class PlatformNotificationContextTest; friend class PlatformNotificationContextTest;
virtual ~PlatformNotificationContext(); ~PlatformNotificationContextImpl() override;
// Initializes the database if neccesary. Must be called on the IO thread. // Initializes the database if neccesary. Must be called on the IO thread.
// |success_closure| will be invoked on a the |task_runner_| thread when // |success_closure| will be invoked on a the |task_runner_| thread when
...@@ -119,9 +97,9 @@ class CONTENT_EXPORT PlatformNotificationContext ...@@ -119,9 +97,9 @@ class CONTENT_EXPORT PlatformNotificationContext
scoped_refptr<base::SequencedTaskRunner> task_runner_; scoped_refptr<base::SequencedTaskRunner> task_runner_;
scoped_ptr<NotificationDatabase> database_; scoped_ptr<NotificationDatabase> database_;
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContext); DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContextImpl);
}; };
} // namespace content } // namespace content
#endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/notifications/platform_notification_context.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "content/browser/notifications/notification_database_data.h" #include "content/browser/notifications/platform_notification_context_impl.h"
#include "content/public/browser/notification_database_data.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -38,12 +37,12 @@ class PlatformNotificationContextTest : public ::testing::Test { ...@@ -38,12 +37,12 @@ class PlatformNotificationContextTest : public ::testing::Test {
} }
protected: protected:
// Creates a new PlatformNotificationContext instance. When using this method, // Creates a new PlatformNotificationContextImpl instance. When using this
// the underlying database will always be created in memory. The current // method, the underlying database will always be created in memory. The
// message loop proxy will be used as the task runner. // current message loop proxy will be used as the task runner.
PlatformNotificationContext* CreatePlatformNotificationContext() { PlatformNotificationContextImpl* CreatePlatformNotificationContext() {
PlatformNotificationContext* context = PlatformNotificationContextImpl* context =
new PlatformNotificationContext(base::FilePath()); new PlatformNotificationContextImpl(base::FilePath());
context->SetTaskRunnerForTesting(base::MessageLoopProxy::current()); context->SetTaskRunnerForTesting(base::MessageLoopProxy::current());
return context; return context;
...@@ -70,7 +69,7 @@ class PlatformNotificationContextTest : public ::testing::Test { ...@@ -70,7 +69,7 @@ class PlatformNotificationContextTest : public ::testing::Test {
}; };
TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) { TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) {
scoped_refptr<PlatformNotificationContext> context = scoped_refptr<PlatformNotificationContextImpl> context =
CreatePlatformNotificationContext(); CreatePlatformNotificationContext();
context->ReadNotificationData( context->ReadNotificationData(
...@@ -86,7 +85,7 @@ TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) { ...@@ -86,7 +85,7 @@ TEST_F(PlatformNotificationContextTest, ReadNonExistentNotification) {
} }
TEST_F(PlatformNotificationContextTest, WriteReadNotification) { TEST_F(PlatformNotificationContextTest, WriteReadNotification) {
scoped_refptr<PlatformNotificationContext> context = scoped_refptr<PlatformNotificationContextImpl> context =
CreatePlatformNotificationContext(); CreatePlatformNotificationContext();
GURL origin("https://example.com"); GURL origin("https://example.com");
...@@ -121,7 +120,7 @@ TEST_F(PlatformNotificationContextTest, WriteReadNotification) { ...@@ -121,7 +120,7 @@ TEST_F(PlatformNotificationContextTest, WriteReadNotification) {
} }
TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) { TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) {
scoped_refptr<PlatformNotificationContext> context = scoped_refptr<PlatformNotificationContextImpl> context =
CreatePlatformNotificationContext(); CreatePlatformNotificationContext();
context->DeleteNotificationData( context->DeleteNotificationData(
...@@ -139,7 +138,7 @@ TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) { ...@@ -139,7 +138,7 @@ TEST_F(PlatformNotificationContextTest, DeleteInvalidNotification) {
} }
TEST_F(PlatformNotificationContextTest, DeleteNotification) { TEST_F(PlatformNotificationContextTest, DeleteNotification) {
scoped_refptr<PlatformNotificationContext> context = scoped_refptr<PlatformNotificationContextImpl> context =
CreatePlatformNotificationContext(); CreatePlatformNotificationContext();
GURL origin("https://example.com"); GURL origin("https://example.com");
......
...@@ -166,6 +166,7 @@ ...@@ -166,6 +166,7 @@
'public/browser/navigation_type.h', 'public/browser/navigation_type.h',
'public/browser/navigator_connect_context.h', 'public/browser/navigator_connect_context.h',
'public/browser/navigator_connect_service_factory.h', 'public/browser/navigator_connect_service_factory.h',
'public/browser/notification_database_data.h',
'public/browser/notification_details.h', 'public/browser/notification_details.h',
'public/browser/notification_event_dispatcher.h', 'public/browser/notification_event_dispatcher.h',
'public/browser/notification_observer.h', 'public/browser/notification_observer.h',
...@@ -179,6 +180,7 @@ ...@@ -179,6 +180,7 @@
'public/browser/page_navigator.h', 'public/browser/page_navigator.h',
'public/browser/pepper_flash_settings_helper.h', 'public/browser/pepper_flash_settings_helper.h',
'public/browser/permission_type.h', 'public/browser/permission_type.h',
'public/browser/platform_notification_context.h',
'public/browser/platform_notification_service.h', 'public/browser/platform_notification_service.h',
'public/browser/plugin_data_remover.h', 'public/browser/plugin_data_remover.h',
'public/browser/plugin_service.h', 'public/browser/plugin_service.h',
...@@ -997,16 +999,16 @@ ...@@ -997,16 +999,16 @@
'browser/notification_service_impl.h', 'browser/notification_service_impl.h',
'browser/notifications/notification_database.cc', 'browser/notifications/notification_database.cc',
'browser/notifications/notification_database.h', 'browser/notifications/notification_database.h',
'browser/notifications/notification_database_data.cc', 'browser/notifications/notification_database_data_conversions.cc',
'browser/notifications/notification_database_data.h', 'browser/notifications/notification_database_data_conversions.h',
'browser/notifications/notification_event_dispatcher_impl.cc', 'browser/notifications/notification_event_dispatcher_impl.cc',
'browser/notifications/notification_event_dispatcher_impl.h', 'browser/notifications/notification_event_dispatcher_impl.h',
'browser/notifications/notification_message_filter.cc', 'browser/notifications/notification_message_filter.cc',
'browser/notifications/notification_message_filter.h', 'browser/notifications/notification_message_filter.h',
'browser/notifications/page_notification_delegate.cc', 'browser/notifications/page_notification_delegate.cc',
'browser/notifications/page_notification_delegate.h', 'browser/notifications/page_notification_delegate.h',
'browser/notifications/platform_notification_context.cc', 'browser/notifications/platform_notification_context_impl.cc',
'browser/notifications/platform_notification_context.h', 'browser/notifications/platform_notification_context_impl.h',
'browser/permissions/permission_service_context.cc', 'browser/permissions/permission_service_context.cc',
'browser/permissions/permission_service_context.h', 'browser/permissions/permission_service_context.h',
'browser/permissions/permission_service_impl.cc', 'browser/permissions/permission_service_impl.cc',
......
...@@ -2,12 +2,11 @@ ...@@ -2,12 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_H_ #ifndef CONTENT_PUBLIC_BROWSER_NOTIFICATION_DATABASE_DATA_H_
#define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_H_ #define CONTENT_PUBLIC_BROWSER_NOTIFICATION_DATABASE_DATA_H_
#include <stdint.h> #include <stdint.h>
#include "content/common/content_export.h"
#include "content/public/common/platform_notification_data.h" #include "content/public/common/platform_notification_data.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -16,25 +15,15 @@ namespace content { ...@@ -16,25 +15,15 @@ namespace content {
// Stores information about a Web Notification as available in the notification // Stores information about a Web Notification as available in the notification
// database. Beyond the notification's own data, its id and attribution need // database. Beyond the notification's own data, its id and attribution need
// to be available for users of the database as well. // to be available for users of the database as well.
struct CONTENT_EXPORT NotificationDatabaseData { struct NotificationDatabaseData {
NotificationDatabaseData();
~NotificationDatabaseData();
// Parses the serialized notification database data |input| into this object.
bool ParseFromString(const std::string& input);
// Serializes the contents of this object to |output|. Returns if the data
// could be serialized successfully.
bool SerializeToString(std::string* output) const;
// Id of the notification as allocated by the NotificationDatabase. // Id of the notification as allocated by the NotificationDatabase.
int64_t notification_id; int64_t notification_id = 0;
// Origin of the website this notification is associated with. // Origin of the website this notification is associated with.
GURL origin; GURL origin;
// Id of the Service Worker registration this notification is associated with. // Id of the Service Worker registration this notification is associated with.
int64_t service_worker_registration_id; int64_t service_worker_registration_id = 0;
// Platform data of the notification that's being stored. // Platform data of the notification that's being stored.
PlatformNotificationData notification_data; PlatformNotificationData notification_data;
...@@ -42,4 +31,4 @@ struct CONTENT_EXPORT NotificationDatabaseData { ...@@ -42,4 +31,4 @@ struct CONTENT_EXPORT NotificationDatabaseData {
} // namespace content } // namespace content
#endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_H_ #endif // CONTENT_PUBLIC_BROWSER_NOTIFICATION_DATABASE_DATA_H_
// 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.
#ifndef CONTENT_PUBLIC_BROWSER_PLATFORM_NOTIFICATION_CONTEXT_H_
#define CONTENT_PUBLIC_BROWSER_PLATFORM_NOTIFICATION_CONTEXT_H_
#include <stdint.h>
#include "base/callback.h"
#include "base/memory/ref_counted.h"
class GURL;
namespace content {
struct NotificationDatabaseData;
// Represents the storage context for persistent Web Notifications, specific to
// the storage partition owning the instance. All methods defined in this
// interface may only be used on the IO thread.
class PlatformNotificationContext
: public base::RefCountedThreadSafe<PlatformNotificationContext> {
public:
using ReadResultCallback =
base::Callback<void(bool /* success */,
const NotificationDatabaseData&)>;
using WriteResultCallback =
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.
virtual void ReadNotificationData(int64_t notification_id,
const GURL& origin,
const ReadResultCallback& callback) = 0;
// Writes the data associated with a notification to a database. When this
// action completed, |callback| will be invoked with the success status and
// the persistent notification id when written successfully.
virtual void WriteNotificationData(
const GURL& origin,
const NotificationDatabaseData& database_data,
const WriteResultCallback& callback) = 0;
// 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.
virtual void DeleteNotificationData(int64_t notification_id,
const GURL& origin,
const DeleteResultCallback& callback) = 0;
protected:
friend class base::RefCountedThreadSafe<PlatformNotificationContext>;
virtual ~PlatformNotificationContext() {}
};
} // namespace content
#endif // CONTENT_PUBLIC_BROWSER_PLATFORM_NOTIFICATION_CONTEXT_H_
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