| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/notifications/NotificationData.h" | 5 #include "modules/notifications/NotificationData.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/SerializedScriptValue.h" | 8 #include "bindings/core/v8/SerializedScriptValue.h" |
| 9 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 9 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
| 10 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
| 11 #include "modules/notifications/Notification.h" | 11 #include "modules/notifications/Notification.h" |
| 12 #include "modules/notifications/NotificationOptions.h" | 12 #include "modules/notifications/NotificationOptions.h" |
| 13 #include "modules/vibration/NavigatorVibration.h" | 13 #include "modules/vibration/NavigatorVibration.h" |
| 14 #include "public/platform/WebURL.h" | 14 #include "public/platform/WebURL.h" |
| 15 #include "wtf/CurrentTime.h" | 15 #include "wtf/CurrentTime.h" |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 WebNotificationData::Direction toDirectionEnumValue(const String& direction) | 20 mojom::blink::NotificationDirection toDirectionEnumValue(const String& direction
) |
| 21 { | 21 { |
| 22 if (direction == "ltr") | 22 if (direction == "ltr") |
| 23 return WebNotificationData::DirectionLeftToRight; | 23 return mojom::blink::NotificationDirection::LEFT_TO_RIGHT; |
| 24 if (direction == "rtl") | 24 if (direction == "rtl") |
| 25 return WebNotificationData::DirectionRightToLeft; | 25 return mojom::blink::NotificationDirection::RIGHT_TO_LEFT; |
| 26 | 26 |
| 27 return WebNotificationData::DirectionAuto; | 27 return mojom::blink::NotificationDirection::AUTO; |
| 28 } | 28 } |
| 29 | 29 |
| 30 WebURL completeURL(ExecutionContext* executionContext, const String& stringUrl) | 30 String completeURL(ExecutionContext* executionContext, const String& stringUrl) |
| 31 { | 31 { |
| 32 WebURL url = executionContext->completeURL(stringUrl); | 32 KURL url = executionContext->completeURL(stringUrl); |
| 33 if (url.isValid()) | 33 if (url.isValid()) |
| 34 return url; | 34 return url.getString(); |
| 35 return WebURL(); | 35 |
| 36 return String(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 WebNotificationData createWebNotificationData(ExecutionContext* executionContext
, const String& title, const NotificationOptions& options, ExceptionState& excep
tionState) | 41 mojom::blink::NotificationPtr createNotificationData(ExecutionContext* execution
Context, const String& title, const NotificationOptions& options, ExceptionState
& exceptionState) |
| 41 { | 42 { |
| 42 // If silent is true, the notification must not have a vibration pattern. | 43 // If silent is true, the notification must not have a vibration pattern. |
| 43 if (options.hasVibrate() && options.silent()) { | 44 if (options.hasVibrate() && options.silent()) { |
| 44 exceptionState.throwTypeError("Silent notifications must not specify vib
ration patterns."); | 45 exceptionState.throwTypeError("Silent notifications must not specify vib
ration patterns."); |
| 45 return WebNotificationData(); | 46 return mojom::blink::NotificationPtr(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 // If renotify is true, the notification must have a tag. | 49 // If renotify is true, the notification must have a tag. |
| 49 if (options.renotify() && options.tag().isEmpty()) { | 50 if (options.renotify() && options.tag().isEmpty()) { |
| 50 exceptionState.throwTypeError("Notifications which set the renotify flag
must specify a non-empty tag."); | 51 exceptionState.throwTypeError("Notifications which set the renotify flag
must specify a non-empty tag."); |
| 51 return WebNotificationData(); | 52 return mojom::blink::NotificationPtr(); |
| 52 } | 53 } |
| 53 | 54 |
| 54 WebNotificationData webData; | 55 mojom::blink::NotificationPtr notification = mojom::blink::Notification::New
(); |
| 55 | 56 |
| 56 webData.title = title; | 57 notification->title = title; |
| 57 webData.direction = toDirectionEnumValue(options.dir()); | 58 notification->direction = toDirectionEnumValue(options.dir()); |
| 58 webData.lang = options.lang(); | 59 notification->lang = options.lang(); |
| 59 webData.body = options.body(); | 60 notification->body = options.body(); |
| 60 webData.tag = options.tag(); | 61 notification->tag = options.tag(); |
| 61 | 62 |
| 62 if (options.hasIcon() && !options.icon().isEmpty()) | 63 if (options.hasIcon() && !options.icon().isEmpty()) |
| 63 webData.icon = completeURL(executionContext, options.icon()); | 64 notification->icon = completeURL(executionContext, options.icon()); |
| 64 | 65 |
| 65 if (options.hasBadge() && !options.badge().isEmpty()) | 66 if (options.hasBadge() && !options.badge().isEmpty()) |
| 66 webData.badge = completeURL(executionContext, options.badge()); | 67 notification->badge = completeURL(executionContext, options.badge()); |
| 67 | 68 |
| 68 webData.vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibra
te()); | 69 notification->vibration_pattern = NavigatorVibration::sanitizeVibrationPatte
rn(options.vibrate()); |
| 69 webData.timestamp = options.hasTimestamp() ? static_cast<double>(options.tim
estamp()) : WTF::currentTimeMS(); | 70 notification->timestamp = options.hasTimestamp() ? static_cast<double>(optio
ns.timestamp()) : WTF::currentTimeMS(); |
| 70 webData.renotify = options.renotify(); | 71 notification->renotify = options.renotify(); |
| 71 webData.silent = options.silent(); | 72 notification->silent = options.silent(); |
| 72 webData.requireInteraction = options.requireInteraction(); | 73 notification->require_interaction = options.requireInteraction(); |
| 73 | 74 |
| 74 if (options.hasData()) { | 75 if (options.hasData()) { |
| 75 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptVa
lueFactory::instance().create(options.data().isolate(), options.data(), nullptr,
exceptionState); | 76 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptVa
lueFactory::instance().create(options.data().isolate(), options.data(), nullptr,
exceptionState); |
| 76 if (exceptionState.hadException()) | 77 if (exceptionState.hadException()) |
| 77 return WebNotificationData(); | 78 return mojom::blink::NotificationPtr(); |
| 78 | 79 |
| 79 Vector<char> serializedData; | 80 Vector<char> serializedData; |
| 80 serializedScriptValue->toWireBytes(serializedData); | 81 serializedScriptValue->toWireBytes(serializedData); |
| 81 | 82 |
| 82 webData.data = serializedData; | 83 // Bail out if the developer provides more data than we allow them to pr
ovide. |
| 84 if (serializedData.size() > mojom::blink::Notification::kMaximumDevelope
rDataBytes) { |
| 85 exceptionState.throwTypeError("Notifications only support up to 1MB
of developer-provided payload."); |
| 86 return mojom::blink::NotificationPtr(); |
| 87 } |
| 88 |
| 89 // Mojo defines int8 as 'signed char', where signedness of 'char' is usu
ally left up |
| 90 // to the implementation. The sizes are identical, so this cast is ugly
but safe. |
| 91 notification->data.Swap(reinterpret_cast<Vector<signed char>*>(&serializ
edData)); |
| 83 } | 92 } |
| 84 | 93 |
| 85 Vector<WebNotificationAction> actions; | 94 Vector<mojom::blink::NotificationActionPtr> actions; |
| 86 | 95 |
| 87 const size_t maxActions = Notification::maxActions(); | 96 const size_t maxActions = Notification::maxActions(); |
| 88 for (const NotificationAction& action : options.actions()) { | 97 for (const NotificationAction& action : options.actions()) { |
| 89 if (actions.size() >= maxActions) | 98 if (actions.size() >= maxActions) |
| 90 break; | 99 break; |
| 91 | 100 |
| 92 WebNotificationAction webAction; | 101 mojom::blink::NotificationActionPtr mojoAction = mojom::blink::Notificat
ionAction::New(); |
| 93 webAction.action = action.action(); | |
| 94 webAction.title = action.title(); | |
| 95 | 102 |
| 96 if (action.type() == "button") | 103 if (action.type() == "button") |
| 97 webAction.type = WebNotificationAction::Button; | 104 mojoAction->type = mojom::blink::NotificationActionType::BUTTON; |
| 98 else if (action.type() == "text") | 105 else if (action.type() == "text") |
| 99 webAction.type = WebNotificationAction::Text; | 106 mojoAction->type = mojom::blink::NotificationActionType::TEXT; |
| 100 else | 107 else |
| 101 NOTREACHED() << "Unknown action type: " << action.type(); | 108 NOTREACHED() << "Unknown action type: " << action.type(); |
| 102 | 109 |
| 103 if (action.hasPlaceholder() && webAction.type == WebNotificationAction::
Button) { | 110 if (action.hasPlaceholder() && mojoAction->type == mojom::blink::Notific
ationActionType::BUTTON) { |
| 104 exceptionState.throwTypeError("Notifications of type \"button\" cann
ot specify a placeholder."); | 111 exceptionState.throwTypeError("Notifications of type \"button\" cann
ot specify a placeholder."); |
| 105 return WebNotificationData(); | 112 return mojom::blink::NotificationPtr(); |
| 106 } | 113 } |
| 107 | 114 |
| 108 webAction.placeholder = action.placeholder(); | 115 mojoAction->action = action.action(); |
| 116 mojoAction->title = action.title(); |
| 117 mojoAction->placeholder = action.placeholder(); |
| 109 | 118 |
| 110 if (action.hasIcon() && !action.icon().isEmpty()) | 119 if (action.hasIcon() && !action.icon().isEmpty()) |
| 111 webAction.icon = completeURL(executionContext, action.icon()); | 120 mojoAction->icon = completeURL(executionContext, action.icon()); |
| 112 | 121 |
| 113 actions.append(webAction); | 122 actions.append(std::move(mojoAction)); |
| 114 } | 123 } |
| 115 | 124 |
| 116 webData.actions = actions; | 125 notification->actions = std::move(actions); |
| 117 | 126 |
| 118 return webData; | 127 return notification; |
| 119 } | 128 } |
| 120 | 129 |
| 121 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |