Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: content/browser/notifications/type_converters.cc

Issue 1898193003: Add type converters from Mojo notifications to the content types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@notification-permission-mojom
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "content/browser/notifications/type_converters.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/time/time.h"
9
10 using blink::mojom::NotificationDirection;
11 using content::PlatformNotificationData;
12
13 namespace mojo {
14
15 PlatformNotificationData
16 TypeConverter<PlatformNotificationData, blink::mojom::NotificationPtr>::Convert(
17 const blink::mojom::NotificationPtr& notification) {
18 PlatformNotificationData notification_data;
19
20 notification_data.title = base::UTF8ToUTF16(notification->title.get());
21
22 switch (notification->direction) {
23 case NotificationDirection::LEFT_TO_RIGHT:
24 notification_data.direction =
25 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT;
26 break;
27 case NotificationDirection::RIGHT_TO_LEFT:
28 notification_data.direction =
29 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT;
30 break;
31 case NotificationDirection::AUTO:
32 notification_data.direction = PlatformNotificationData::DIRECTION_AUTO;
33 break;
34 }
35
36 notification_data.lang = notification->lang;
37 notification_data.body = base::UTF8ToUTF16(notification->body.get());
38 notification_data.tag = notification->tag;
39 notification_data.icon = GURL(notification->icon.get());
40 notification_data.badge = GURL(notification->badge.get());
41
42 for (uint32_t vibration : notification->vibration_pattern)
43 notification_data.vibration_pattern.push_back(static_cast<int>(vibration));
44
45 notification_data.timestamp = base::Time::FromJsTime(notification->timestamp);
46 notification_data.renotify = notification->renotify;
47 notification_data.silent = notification->silent;
48 notification_data.require_interaction = notification->require_interaction;
49 notification_data.data.assign(notification->data.begin(),
50 notification->data.end());
51
52 for (const auto& action : notification->actions) {
53 content::PlatformNotificationAction data_action;
54
55 switch (action->type) {
56 case blink::mojom::NotificationActionType::BUTTON:
57 data_action.type = content::PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON;
58 break;
59 case blink::mojom::NotificationActionType::TEXT:
60 data_action.type = content::PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT;
61 break;
62 }
63
64 data_action.action = action->action;
65 data_action.title = base::UTF8ToUTF16(action->title.get());
66 data_action.icon = GURL(action->icon.get());
67 if (!action->placeholder.is_null()) {
68 data_action.placeholder = base::NullableString16(
69 base::UTF8ToUTF16(action->placeholder.get()), false /* is_null */);
70 }
71
72 notification_data.actions.push_back(data_action);
73 }
74
75 return notification_data;
76 }
77
78 blink::mojom::NotificationPtr
79 TypeConverter<blink::mojom::NotificationPtr, PlatformNotificationData>::Convert(
80 const PlatformNotificationData& notification_data) {
81 blink::mojom::NotificationPtr notification =
82 blink::mojom::Notification::New();
83
84 notification->title = base::UTF16ToUTF8(notification_data.title);
85
86 switch (notification_data.direction) {
87 case PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT:
88 notification->direction = NotificationDirection::LEFT_TO_RIGHT;
89 break;
90 case PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT:
91 notification->direction = NotificationDirection::RIGHT_TO_LEFT;
92 break;
93 case PlatformNotificationData::DIRECTION_AUTO:
94 notification->direction = NotificationDirection::AUTO;
95 break;
96 }
97
98 notification->lang = notification_data.lang;
99 notification->body = base::UTF16ToUTF8(notification_data.body);
100 notification->tag = notification_data.tag;
101 notification->icon = notification_data.icon.spec();
102 notification->badge = notification_data.badge.spec();
103
104 for (int vibration : notification_data.vibration_pattern)
105 notification->vibration_pattern.push_back(static_cast<uint32_t>(vibration));
106
107 notification->timestamp = notification_data.timestamp.ToJsTime();
108 notification->renotify = notification_data.renotify;
109 notification->silent = notification_data.silent;
110 notification->require_interaction = notification_data.require_interaction;
111 notification->data = std::vector<int8_t>(notification_data.data.begin(),
112 notification_data.data.end());
113
114 for (const auto& data_action : notification_data.actions) {
115 blink::mojom::NotificationActionPtr action =
116 blink::mojom::NotificationAction::New();
117
118 switch (data_action.type) {
119 case content::PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON:
120 action->type = blink::mojom::NotificationActionType::BUTTON;
121 break;
122 case content::PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT:
123 action->type = blink::mojom::NotificationActionType::TEXT;
124 break;
125 }
126
127 action->action = data_action.action;
128 action->title = base::UTF16ToUTF8(data_action.title);
129 action->icon = data_action.icon.spec();
130 if (!data_action.placeholder.is_null())
131 action->placeholder = base::UTF16ToUTF8(data_action.placeholder.string());
132
133 notification->actions.push_back(std::move(action));
134 }
135
136 return notification;
137 }
138
139 } // namespace mojo
OLDNEW
« no previous file with comments | « content/browser/notifications/type_converters.h ('k') | content/browser/notifications/type_converters_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698