1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/notifications/notification_database_data_conversions.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/browser/notifications/notification_database_data.pb.h"
10 #include "content/public/browser/notification_database_data.h"
14 bool DeserializeNotificationDatabaseData(const std::string
& input
,
15 NotificationDatabaseData
* output
) {
18 NotificationDatabaseDataProto message
;
19 if (!message
.ParseFromString(input
))
22 output
->notification_id
= message
.notification_id();
23 output
->origin
= GURL(message
.origin());
24 output
->service_worker_registration_id
=
25 message
.service_worker_registration_id();
27 PlatformNotificationData
* notification_data
= &output
->notification_data
;
28 const NotificationDatabaseDataProto::NotificationData
& payload
=
29 message
.notification_data();
31 notification_data
->title
= base::UTF8ToUTF16(payload
.title());
33 switch (payload
.direction()) {
34 case NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT
:
35 notification_data
->direction
=
36 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
;
38 case NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT
:
39 notification_data
->direction
=
40 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT
;
42 case NotificationDatabaseDataProto::NotificationData::AUTO
:
43 notification_data
->direction
= PlatformNotificationData::DIRECTION_AUTO
;
47 notification_data
->lang
= payload
.lang();
48 notification_data
->body
= base::UTF8ToUTF16(payload
.body());
49 notification_data
->tag
= payload
.tag();
50 notification_data
->icon
= GURL(payload
.icon());
52 if (payload
.vibration_pattern().size() > 0) {
53 notification_data
->vibration_pattern
.assign(
54 payload
.vibration_pattern().begin(),
55 payload
.vibration_pattern().end());
58 notification_data
->silent
= payload
.silent();
60 if (payload
.data().length()) {
61 notification_data
->data
.assign(payload
.data().begin(),
62 payload
.data().end());
65 for (const auto& payload_action
: payload
.actions()) {
66 PlatformNotificationAction action
;
67 action
.action
= payload_action
.action();
68 action
.title
= base::UTF8ToUTF16(payload_action
.title());
69 notification_data
->actions
.push_back(action
);
75 bool SerializeNotificationDatabaseData(const NotificationDatabaseData
& input
,
76 std::string
* output
) {
79 scoped_ptr
<NotificationDatabaseDataProto::NotificationData
> payload(
80 new NotificationDatabaseDataProto::NotificationData());
82 const PlatformNotificationData
& notification_data
= input
.notification_data
;
84 payload
->set_title(base::UTF16ToUTF8(notification_data
.title
));
86 switch (notification_data
.direction
) {
87 case PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT
:
88 payload
->set_direction(
89 NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT
);
91 case PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT
:
92 payload
->set_direction(
93 NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT
);
95 case PlatformNotificationData::DIRECTION_AUTO
:
96 payload
->set_direction(
97 NotificationDatabaseDataProto::NotificationData::AUTO
);
101 payload
->set_lang(notification_data
.lang
);
102 payload
->set_body(base::UTF16ToUTF8(notification_data
.body
));
103 payload
->set_tag(notification_data
.tag
);
104 payload
->set_icon(notification_data
.icon
.spec());
106 for (size_t i
= 0; i
< notification_data
.vibration_pattern
.size(); ++i
)
107 payload
->add_vibration_pattern(notification_data
.vibration_pattern
[i
]);
109 payload
->set_silent(notification_data
.silent
);
111 if (notification_data
.data
.size()) {
112 payload
->set_data(¬ification_data
.data
.front(),
113 notification_data
.data
.size());
116 for (const PlatformNotificationAction
& action
: notification_data
.actions
) {
117 NotificationDatabaseDataProto::NotificationAction
* payload_action
=
118 payload
->add_actions();
119 payload_action
->set_action(action
.action
);
120 payload_action
->set_title(base::UTF16ToUTF8(action
.title
));
123 NotificationDatabaseDataProto message
;
124 message
.set_notification_id(input
.notification_id
);
125 message
.set_origin(input
.origin
.spec());
126 message
.set_service_worker_registration_id(
127 input
.service_worker_registration_id
);
128 message
.set_allocated_notification_data(payload
.release());
130 return message
.SerializeToString(output
);
133 } // namespace content