Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / notifications / notification_database_data_conversions.cc
blobc6095e4055a17381bcbbe015bc00b4312fc06da0
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"
12 namespace content {
14 bool DeserializeNotificationDatabaseData(const std::string& input,
15 NotificationDatabaseData* output) {
16 DCHECK(output);
18 NotificationDatabaseDataProto message;
19 if (!message.ParseFromString(input))
20 return false;
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;
37 break;
38 case NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT:
39 notification_data->direction =
40 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT;
41 break;
42 case NotificationDatabaseDataProto::NotificationData::AUTO:
43 notification_data->direction = PlatformNotificationData::DIRECTION_AUTO;
44 break;
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();
59 notification_data->require_interaction = payload.require_interaction();
61 if (payload.data().length()) {
62 notification_data->data.assign(payload.data().begin(),
63 payload.data().end());
66 for (const auto& payload_action : payload.actions()) {
67 PlatformNotificationAction action;
68 action.action = payload_action.action();
69 action.title = base::UTF8ToUTF16(payload_action.title());
70 notification_data->actions.push_back(action);
73 return true;
76 bool SerializeNotificationDatabaseData(const NotificationDatabaseData& input,
77 std::string* output) {
78 DCHECK(output);
80 scoped_ptr<NotificationDatabaseDataProto::NotificationData> payload(
81 new NotificationDatabaseDataProto::NotificationData());
83 const PlatformNotificationData& notification_data = input.notification_data;
85 payload->set_title(base::UTF16ToUTF8(notification_data.title));
87 switch (notification_data.direction) {
88 case PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT:
89 payload->set_direction(
90 NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT);
91 break;
92 case PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT:
93 payload->set_direction(
94 NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT);
95 break;
96 case PlatformNotificationData::DIRECTION_AUTO:
97 payload->set_direction(
98 NotificationDatabaseDataProto::NotificationData::AUTO);
99 break;
102 payload->set_lang(notification_data.lang);
103 payload->set_body(base::UTF16ToUTF8(notification_data.body));
104 payload->set_tag(notification_data.tag);
105 payload->set_icon(notification_data.icon.spec());
107 for (size_t i = 0; i < notification_data.vibration_pattern.size(); ++i)
108 payload->add_vibration_pattern(notification_data.vibration_pattern[i]);
110 payload->set_silent(notification_data.silent);
111 payload->set_require_interaction(notification_data.require_interaction);
113 if (notification_data.data.size()) {
114 payload->set_data(&notification_data.data.front(),
115 notification_data.data.size());
118 for (const PlatformNotificationAction& action : notification_data.actions) {
119 NotificationDatabaseDataProto::NotificationAction* payload_action =
120 payload->add_actions();
121 payload_action->set_action(action.action);
122 payload_action->set_title(base::UTF16ToUTF8(action.title));
125 NotificationDatabaseDataProto message;
126 message.set_notification_id(input.notification_id);
127 message.set_origin(input.origin.spec());
128 message.set_service_worker_registration_id(
129 input.service_worker_registration_id);
130 message.set_allocated_notification_data(payload.release());
132 return message.SerializeToString(output);
135 } // namespace content