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());
32 notification_data
->direction
=
33 payload
.direction() ==
34 NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT
?
35 PlatformNotificationData::NotificationDirectionRightToLeft
:
36 PlatformNotificationData::NotificationDirectionLeftToRight
;
37 notification_data
->lang
= payload
.lang();
38 notification_data
->body
= base::UTF8ToUTF16(payload
.body());
39 notification_data
->tag
= payload
.tag();
40 notification_data
->icon
= GURL(payload
.icon());
42 if (payload
.vibration_pattern().size() > 0) {
43 notification_data
->vibration_pattern
.assign(
44 payload
.vibration_pattern().begin(),
45 payload
.vibration_pattern().end());
48 notification_data
->silent
= payload
.silent();
50 if (payload
.data().length()) {
51 notification_data
->data
.assign(payload
.data().begin(),
52 payload
.data().end());
58 bool SerializeNotificationDatabaseData(const NotificationDatabaseData
& input
,
59 std::string
* output
) {
62 scoped_ptr
<NotificationDatabaseDataProto::NotificationData
> payload(
63 new NotificationDatabaseDataProto::NotificationData());
65 const PlatformNotificationData
& notification_data
= input
.notification_data
;
67 payload
->set_title(base::UTF16ToUTF8(notification_data
.title
));
68 payload
->set_direction(
69 notification_data
.direction
==
70 PlatformNotificationData::NotificationDirectionRightToLeft
?
71 NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT
:
72 NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT
);
73 payload
->set_lang(notification_data
.lang
);
74 payload
->set_body(base::UTF16ToUTF8(notification_data
.body
));
75 payload
->set_tag(notification_data
.tag
);
76 payload
->set_icon(notification_data
.icon
.spec());
78 for (size_t i
= 0; i
< notification_data
.vibration_pattern
.size(); ++i
)
79 payload
->add_vibration_pattern(notification_data
.vibration_pattern
[i
]);
81 payload
->set_silent(notification_data
.silent
);
83 if (notification_data
.data
.size()) {
84 payload
->set_data(¬ification_data
.data
.front(),
85 notification_data
.data
.size());
88 NotificationDatabaseDataProto message
;
89 message
.set_notification_id(input
.notification_id
);
90 message
.set_origin(input
.origin
.spec());
91 message
.set_service_worker_registration_id(
92 input
.service_worker_registration_id
);
93 message
.set_allocated_notification_data(payload
.release());
95 return message
.SerializeToString(output
);
98 } // namespace content