diff options
Diffstat (limited to 'content/browser/notifications')
6 files changed, 255 insertions, 0 deletions
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' ] + }, + ], +} |