Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / notifications / notification_database_data_conversions.cc
blobd183dd414e2cf2a67b37c52a639a7f83ed2100c7
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());
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());
41 notification_data->silent = payload.silent();
43 return true;
46 bool SerializeNotificationDatabaseData(const NotificationDatabaseData& input,
47 std::string* output) {
48 DCHECK(output);
50 scoped_ptr<NotificationDatabaseDataProto::NotificationData> payload(
51 new NotificationDatabaseDataProto::NotificationData());
53 const PlatformNotificationData& notification_data = input.notification_data;
55 payload->set_title(base::UTF16ToUTF8(notification_data.title));
56 payload->set_direction(
57 notification_data.direction ==
58 PlatformNotificationData::NotificationDirectionRightToLeft ?
59 NotificationDatabaseDataProto::NotificationData::RIGHT_TO_LEFT :
60 NotificationDatabaseDataProto::NotificationData::LEFT_TO_RIGHT);
61 payload->set_lang(notification_data.lang);
62 payload->set_body(base::UTF16ToUTF8(notification_data.body));
63 payload->set_tag(notification_data.tag);
64 payload->set_icon(notification_data.icon.spec());
65 payload->set_silent(notification_data.silent);
67 NotificationDatabaseDataProto message;
68 message.set_notification_id(input.notification_id);
69 message.set_origin(input.origin.spec());
70 message.set_service_worker_registration_id(
71 input.service_worker_registration_id);
72 message.set_allocated_notification_data(payload.release());
74 return message.SerializeToString(output);
77 } // namespace content