| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/child/notifications/notification_data_conversions.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "third_party/WebKit/public/platform/URLConversion.h" | |
| 12 #include "third_party/WebKit/public/platform/WebString.h" | |
| 13 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 14 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 15 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onAction.h" | |
| 16 | |
| 17 using blink::WebNotificationData; | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 PlatformNotificationData ToPlatformNotificationData( | |
| 22 const WebNotificationData& web_data) { | |
| 23 PlatformNotificationData platform_data; | |
| 24 platform_data.title = web_data.title; | |
| 25 | |
| 26 switch (web_data.direction) { | |
| 27 case WebNotificationData::DirectionLeftToRight: | |
| 28 platform_data.direction = | |
| 29 PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT; | |
| 30 break; | |
| 31 case WebNotificationData::DirectionRightToLeft: | |
| 32 platform_data.direction = | |
| 33 PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT; | |
| 34 break; | |
| 35 case WebNotificationData::DirectionAuto: | |
| 36 platform_data.direction = PlatformNotificationData::DIRECTION_AUTO; | |
| 37 break; | |
| 38 } | |
| 39 | |
| 40 platform_data.lang = base::UTF16ToUTF8(base::StringPiece16(web_data.lang)); | |
| 41 platform_data.body = web_data.body; | |
| 42 platform_data.tag = base::UTF16ToUTF8(base::StringPiece16(web_data.tag)); | |
| 43 platform_data.icon = blink::WebStringToGURL(web_data.icon.string()); | |
| 44 platform_data.badge = blink::WebStringToGURL(web_data.badge.string()); | |
| 45 platform_data.vibration_pattern.assign(web_data.vibrate.begin(), | |
| 46 web_data.vibrate.end()); | |
| 47 platform_data.timestamp = base::Time::FromJsTime(web_data.timestamp); | |
| 48 platform_data.renotify = web_data.renotify; | |
| 49 platform_data.silent = web_data.silent; | |
| 50 platform_data.require_interaction = web_data.requireInteraction; | |
| 51 platform_data.data.assign(web_data.data.begin(), web_data.data.end()); | |
| 52 platform_data.actions.resize(web_data.actions.size()); | |
| 53 for (size_t i = 0; i < web_data.actions.size(); ++i) { | |
| 54 switch (web_data.actions[i].type) { | |
| 55 case blink::WebNotificationAction::Button: | |
| 56 platform_data.actions[i].type = | |
| 57 PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON; | |
| 58 break; | |
| 59 case blink::WebNotificationAction::Text: | |
| 60 platform_data.actions[i].type = PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT; | |
| 61 break; | |
| 62 default: | |
| 63 NOTREACHED() << "Unknown notification action type: " | |
| 64 << web_data.actions[i].type; | |
| 65 } | |
| 66 platform_data.actions[i].action = | |
| 67 base::UTF16ToUTF8(base::StringPiece16(web_data.actions[i].action)); | |
| 68 platform_data.actions[i].title = web_data.actions[i].title; | |
| 69 platform_data.actions[i].icon = | |
| 70 blink::WebStringToGURL(web_data.actions[i].icon.string()); | |
| 71 platform_data.actions[i].placeholder = web_data.actions[i].placeholder; | |
| 72 } | |
| 73 | |
| 74 return platform_data; | |
| 75 } | |
| 76 | |
| 77 WebNotificationData ToWebNotificationData( | |
| 78 const PlatformNotificationData& platform_data) { | |
| 79 WebNotificationData web_data; | |
| 80 web_data.title = platform_data.title; | |
| 81 | |
| 82 switch (platform_data.direction) { | |
| 83 case PlatformNotificationData::DIRECTION_LEFT_TO_RIGHT: | |
| 84 web_data.direction = WebNotificationData::DirectionLeftToRight; | |
| 85 break; | |
| 86 case PlatformNotificationData::DIRECTION_RIGHT_TO_LEFT: | |
| 87 web_data.direction = WebNotificationData::DirectionRightToLeft; | |
| 88 break; | |
| 89 case PlatformNotificationData::DIRECTION_AUTO: | |
| 90 web_data.direction = WebNotificationData::DirectionAuto; | |
| 91 break; | |
| 92 } | |
| 93 | |
| 94 web_data.lang = blink::WebString::fromUTF8(platform_data.lang); | |
| 95 web_data.body = platform_data.body; | |
| 96 web_data.tag = blink::WebString::fromUTF8(platform_data.tag); | |
| 97 web_data.icon = blink::WebURL(platform_data.icon); | |
| 98 web_data.badge = blink::WebURL(platform_data.badge); | |
| 99 web_data.vibrate = platform_data.vibration_pattern; | |
| 100 web_data.timestamp = platform_data.timestamp.ToJsTime(); | |
| 101 web_data.renotify = platform_data.renotify; | |
| 102 web_data.silent = platform_data.silent; | |
| 103 web_data.requireInteraction = platform_data.require_interaction; | |
| 104 web_data.data = platform_data.data; | |
| 105 blink::WebVector<blink::WebNotificationAction> resized( | |
| 106 platform_data.actions.size()); | |
| 107 web_data.actions.swap(resized); | |
| 108 for (size_t i = 0; i < platform_data.actions.size(); ++i) { | |
| 109 switch (platform_data.actions[i].type) { | |
| 110 case PLATFORM_NOTIFICATION_ACTION_TYPE_BUTTON: | |
| 111 web_data.actions[i].type = blink::WebNotificationAction::Button; | |
| 112 break; | |
| 113 case PLATFORM_NOTIFICATION_ACTION_TYPE_TEXT: | |
| 114 web_data.actions[i].type = blink::WebNotificationAction::Text; | |
| 115 break; | |
| 116 default: | |
| 117 NOTREACHED() << "Unknown platform data type: " | |
| 118 << platform_data.actions[i].type; | |
| 119 } | |
| 120 web_data.actions[i].action = | |
| 121 blink::WebString::fromUTF8(platform_data.actions[i].action); | |
| 122 web_data.actions[i].title = platform_data.actions[i].title; | |
| 123 web_data.actions[i].icon = blink::WebURL(platform_data.actions[i].icon); | |
| 124 web_data.actions[i].placeholder = platform_data.actions[i].placeholder; | |
| 125 } | |
| 126 | |
| 127 return web_data; | |
| 128 } | |
| 129 | |
| 130 } // namespace content | |
| OLD | NEW |