summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpeter <peter@chromium.org>2015-03-13 07:33:38 -0700
committerCommit bot <commit-bot@chromium.org>2015-03-13 14:34:26 +0000
commit05cb4db3e707e8858588fdedeab831d91c5c7f5c (patch)
tree223374f23cf245dbb45bfe60ad215f50ae78366b
parent9f5893dca3b430f4c3edb64ca9019d45bdc1ddc2 (diff)
downloadchromium_src-05cb4db3e707e8858588fdedeab831d91c5c7f5c.zip
chromium_src-05cb4db3e707e8858588fdedeab831d91c5c7f5c.tar.gz
chromium_src-05cb4db3e707e8858588fdedeab831d91c5c7f5c.tar.bz2
Add the NotificationDatabaseData structure and protobuf
This represents the payload of notifications to be stored in the notification database. Included are two conversion functions in order to make sure that we can isolate that operation in one, tested location. Design document: http://goo.gl/TciXVp BUG=447628 Review URL: https://codereview.chromium.org/1003843002 Cr-Commit-Position: refs/heads/master@{#320497}
-rw-r--r--content/browser/BUILD.gn1
-rw-r--r--content/browser/notifications/BUILD.gn11
-rw-r--r--content/browser/notifications/notification_database_data.cc74
-rw-r--r--content/browser/notifications/notification_database_data.h45
-rw-r--r--content/browser/notifications/notification_database_data.proto41
-rw-r--r--content/browser/notifications/notification_database_data_unittest.cc67
-rw-r--r--content/browser/notifications/notification_proto.gyp17
-rw-r--r--content/content_browser.gypi3
-rw-r--r--content/content_tests.gypi2
-rw-r--r--content/test/BUILD.gn1
10 files changed, 262 insertions, 0 deletions
diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
index 345d8db..294fe13 100644
--- a/content/browser/BUILD.gn
+++ b/content/browser/BUILD.gn
@@ -22,6 +22,7 @@ source_set("browser") {
"//base",
"//base:base_static",
"//content:resources",
+ "//content/browser/notifications:notification_proto",
"//content/browser/service_worker:service_worker_proto",
"//content/browser/speech/proto",
"//content/public/common:common_sources",
diff --git a/content/browser/notifications/BUILD.gn b/content/browser/notifications/BUILD.gn
new file mode 100644
index 0000000..0f6d545
--- /dev/null
+++ b/content/browser/notifications/BUILD.gn
@@ -0,0 +1,11 @@
+# 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.
+
+import("//third_party/protobuf/proto_library.gni")
+
+proto_library("notification_proto") {
+ sources = [
+ "notification_database_data.proto",
+ ]
+}
diff --git a/content/browser/notifications/notification_database_data.cc b/content/browser/notifications/notification_database_data.cc
new file mode 100644
index 0000000..bf4a541
--- /dev/null
+++ b/content/browser/notifications/notification_database_data.cc
@@ -0,0 +1,74 @@
+// 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/notification_database_data.h"
+
+#include "base/logging.h"
+#include "base/strings/utf_string_conversions.h"
+#include "content/browser/notifications/notification_database_data.pb.h"
+
+namespace content {
+
+NotificationDatabaseData::NotificationDatabaseData()
+ : notification_id(0),
+ service_worker_registration_id(0) {
+}
+
+NotificationDatabaseData::~NotificationDatabaseData() {}
+
+bool NotificationDatabaseData::ParseFromString(const std::string& input) {
+ NotificationDatabaseDataProto message;
+ if (!message.ParseFromString(input))
+ return false;
+
+ notification_id = message.notification_id();
+ origin = GURL(message.origin());
+ service_worker_registration_id = message.service_worker_registration_id();
+
+ const NotificationDatabaseDataProto::NotificationData& payload =
+ message.notification_data();
+
+ notification_data.title = base::UTF8ToUTF16(payload.title());
+ notification_data.direction =
+ payload.direction() ==
+ NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT ?
+ PlatformNotificationData::NotificationDirectionRightToLeft :
+ PlatformNotificationData::NotificationDirectionLeftToRight;
+ notification_data.lang = payload.lang();
+ notification_data.body = base::UTF8ToUTF16(payload.body());
+ notification_data.tag = payload.tag();
+ notification_data.icon = GURL(payload.icon());
+ notification_data.silent = payload.silent();
+
+ return true;
+}
+
+bool NotificationDatabaseData::SerializeToString(std::string* output) const {
+ DCHECK(output);
+
+ scoped_ptr<NotificationDatabaseDataProto::NotificationData> payload(
+ new NotificationDatabaseDataProto::NotificationData());
+ payload->set_title(base::UTF16ToUTF8(notification_data.title));
+ payload->set_direction(
+ notification_data.direction ==
+ PlatformNotificationData::NotificationDirectionRightToLeft ?
+ NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT :
+ NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT);
+ payload->set_lang(notification_data.lang);
+ payload->set_body(base::UTF16ToUTF8(notification_data.body));
+ payload->set_tag(notification_data.tag);
+ payload->set_icon(notification_data.icon.spec());
+ payload->set_silent(notification_data.silent);
+
+ NotificationDatabaseDataProto message;
+ message.set_notification_id(notification_id);
+ message.set_origin(origin.spec());
+ message.set_service_worker_registration_id(
+ service_worker_registration_id);
+ message.set_allocated_notification_data(payload.release());
+
+ return message.SerializeToString(output);
+}
+
+} // namespace content
diff --git a/content/browser/notifications/notification_database_data.h b/content/browser/notifications/notification_database_data.h
new file mode 100644
index 0000000..ea4bf38
--- /dev/null
+++ b/content/browser/notifications/notification_database_data.h
@@ -0,0 +1,45 @@
+// 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_H_
+#define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_H_
+
+#include <stdint.h>
+
+#include "content/common/content_export.h"
+#include "content/public/common/platform_notification_data.h"
+#include "url/gurl.h"
+
+namespace content {
+
+// Stores information about a Web Notification as available in the notification
+// database. Beyond the notification's own data, its id and attribution need
+// to be available for users of the database as well.
+struct CONTENT_EXPORT 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.
+ int64_t notification_id;
+
+ // Origin of the website this notification is associated with.
+ GURL origin;
+
+ // Id of the Service Worker registration this notification is associated with.
+ int64_t service_worker_registration_id;
+
+ // Platform data of the notification that's being stored.
+ PlatformNotificationData notification_data;
+};
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_DATA_H_
diff --git a/content/browser/notifications/notification_database_data.proto b/content/browser/notifications/notification_database_data.proto
new file mode 100644
index 0000000..8b02197
--- /dev/null
+++ b/content/browser/notifications/notification_database_data.proto
@@ -0,0 +1,41 @@
+// 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.
+
+syntax = "proto2";
+
+option optimize_for = LITE_RUNTIME;
+
+package content;
+
+// Stores information about a Web Notification. This message is the protocol
+// buffer meant to serialize the content::NotificationDatabaseData structure.
+//
+// Next tag: 5
+message NotificationDatabaseDataProto {
+ optional int64 notification_id = 1;
+
+ optional string origin = 2;
+ optional int64 service_worker_registration_id = 3;
+
+ // Actual data payload of the notification. This message is the protocol
+ // buffer meant to serialize the content::PlatformNotificationData structure.
+ //
+ // Next tag: 8
+ message NotificationData {
+ enum Direction {
+ LEFT_TO_RIGHT = 0;
+ RIGHT_TO_LEFT = 1;
+ }
+
+ optional string title = 1;
+ optional Direction direction = 2;
+ optional string lang = 3;
+ optional string body = 4;
+ optional string tag = 5;
+ optional string icon = 6;
+ optional bool silent = 7;
+ }
+
+ optional NotificationData notification_data = 4;
+}
diff --git a/content/browser/notifications/notification_database_data_unittest.cc b/content/browser/notifications/notification_database_data_unittest.cc
new file mode 100644
index 0000000..80acae1
--- /dev/null
+++ b/content/browser/notifications/notification_database_data_unittest.cc
@@ -0,0 +1,67 @@
+// 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/notification_database_data.h"
+
+#include "base/strings/utf_string_conversions.h"
+#include "content/browser/notifications/notification_database_data.pb.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+const int64_t kNotificationId = 42;
+const int64_t kServiceWorkerRegistrationId = 9001;
+
+const char kOrigin[] = "https://example.com/";
+const char kNotificationTitle[] = "My Notification";
+const char kNotificationLang[] = "nl";
+const char kNotificationBody[] = "Hello, world!";
+const char kNotificationTag[] = "my_tag";
+const char kNotificationIconUrl[] = "https://example.com/icon.png";
+
+TEST(NotificationDatabaseDataTest, SerializeAndDeserializeData) {
+ PlatformNotificationData notification_data;
+ notification_data.title = base::ASCIIToUTF16(kNotificationTitle);
+ notification_data.direction =
+ PlatformNotificationData::NotificationDirectionRightToLeft;
+ notification_data.lang = kNotificationLang;
+ notification_data.body = base::ASCIIToUTF16(kNotificationBody);
+ notification_data.tag = kNotificationTag;
+ notification_data.icon = GURL(kNotificationIconUrl);
+ notification_data.silent = true;
+
+ NotificationDatabaseData database_data;
+ database_data.notification_id = kNotificationId;
+ database_data.origin = GURL(kOrigin);
+ database_data.service_worker_registration_id = kServiceWorkerRegistrationId;
+ database_data.notification_data = notification_data;
+
+ std::string serialized_data;
+
+ // Serialize the data in |notification_data| to the string |serialized_data|.
+ ASSERT_TRUE(database_data.SerializeToString(&serialized_data));
+
+ NotificationDatabaseData copied_data;
+
+ // Deserialize the data in |serialized_data| to |copied_data|.
+ ASSERT_TRUE(copied_data.ParseFromString(serialized_data));
+
+ EXPECT_EQ(database_data.notification_id, copied_data.notification_id);
+ EXPECT_EQ(database_data.origin, copied_data.origin);
+ EXPECT_EQ(database_data.service_worker_registration_id,
+ copied_data.service_worker_registration_id);
+
+ const PlatformNotificationData& copied_notification_data =
+ copied_data.notification_data;
+
+ EXPECT_EQ(notification_data.title, copied_notification_data.title);
+ EXPECT_EQ(notification_data.direction, copied_notification_data.direction);
+ EXPECT_EQ(notification_data.lang, copied_notification_data.lang);
+ EXPECT_EQ(notification_data.body, copied_notification_data.body);
+ EXPECT_EQ(notification_data.tag, copied_notification_data.tag);
+ EXPECT_EQ(notification_data.icon, copied_notification_data.icon);
+ EXPECT_EQ(notification_data.silent, copied_notification_data.silent);
+}
+
+} // namespace content
diff --git a/content/browser/notifications/notification_proto.gyp b/content/browser/notifications/notification_proto.gyp
new file mode 100644
index 0000000..effe03e
--- /dev/null
+++ b/content/browser/notifications/notification_proto.gyp
@@ -0,0 +1,17 @@
+{
+ 'targets': [
+ {
+ # GN version: //content/browser/notifications:notification_proto
+ 'target_name': 'notification_proto',
+ 'type': 'static_library',
+ 'sources': [
+ 'notification_database_data.proto',
+ ],
+ 'variables': {
+ 'proto_in_dir': '.',
+ 'proto_out_dir': 'content/browser/notifications',
+ },
+ 'includes': [ '../../../build/protoc.gypi' ]
+ },
+ ],
+}
diff --git a/content/content_browser.gypi b/content/content_browser.gypi
index a2ff319..48543ee 100644
--- a/content/content_browser.gypi
+++ b/content/content_browser.gypi
@@ -29,6 +29,7 @@
'../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/resources/ui_resources.gyp:ui_resources',
'../ui/snapshot/snapshot.gyp:snapshot',
+ 'browser/notifications/notification_proto.gyp:notification_proto',
'browser/service_worker/service_worker_proto.gyp:service_worker_proto',
'browser/speech/proto/speech_proto.gyp:speech_proto',
'content_common_mojo_bindings.gyp:content_common_mojo_bindings',
@@ -994,6 +995,8 @@
'browser/notification_service_impl.h',
'browser/notifications/notification_database.cc',
'browser/notifications/notification_database.h',
+ 'browser/notifications/notification_database_data.cc',
+ 'browser/notifications/notification_database_data.h',
'browser/notifications/notification_event_dispatcher_impl.cc',
'browser/notifications/notification_event_dispatcher_impl.h',
'browser/notifications/notification_message_filter.cc',
diff --git a/content/content_tests.gypi b/content/content_tests.gypi
index 561543e..81228ee 100644
--- a/content/content_tests.gypi
+++ b/content/content_tests.gypi
@@ -472,6 +472,7 @@
'browser/media/midi_host_unittest.cc',
'browser/media/webrtc_identity_store_unittest.cc',
'browser/net/sqlite_persistent_cookie_store_unittest.cc',
+ 'browser/notifications/notification_database_data_unittest.cc',
'browser/notifications/notification_database_unittest.cc',
'browser/notification_service_impl_unittest.cc',
'browser/power_monitor_message_broadcaster_unittest.cc',
@@ -970,6 +971,7 @@
'target_name': 'content_unittests',
'type': '<(gtest_target_type)',
'dependencies': [
+ 'browser/notifications/notification_proto.gyp:notification_proto',
'browser/service_worker/service_worker_proto.gyp:service_worker_proto',
'browser/speech/proto/speech_proto.gyp:speech_proto',
'content.gyp:content_browser',
diff --git a/content/test/BUILD.gn b/content/test/BUILD.gn
index 8bc4c30..9dc01e9 100644
--- a/content/test/BUILD.gn
+++ b/content/test/BUILD.gn
@@ -448,6 +448,7 @@ if (!is_mac) { # TODO(GYP) enable on Mac once it links.
":test_support",
"//base/allocator",
"//base/test:test_support",
+ "//content/browser/notifications:notification_proto",
"//content/browser/service_worker:service_worker_proto",
"//content/browser/speech/proto",
"//content/public/browser",