Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "modules/notifications/NotificationData.h" | |
| 7 | |
| 8 #include "bindings/core/v8/ExceptionState.h" | |
| 9 #include "bindings/core/v8/SerializedScriptValue.h" | |
| 10 #include "bindings/core/v8/SerializedScriptValueFactory.h" | |
| 11 #include "core/dom/ExecutionContext.h" | |
| 12 #include "modules/notifications/Notification.h" | |
| 13 #include "modules/notifications/NotificationOptions.h" | |
| 14 #include "modules/vibration/NavigatorVibration.h" | |
| 15 #include "platform/weborigin/KURL.h" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 WebNotificationData createWebNotificationData(ExecutionContext* executionContext , const String& title, const NotificationOptions& options, ExceptionState& excep tionState) | |
| 20 { | |
| 21 // If silent is true, the notification must not have a vibration pattern. | |
| 22 if (options.hasVibrate() && options.silent()) { | |
| 23 exceptionState.throwTypeError("Silent notifications must not specify vib ration patterns."); | |
| 24 return WebNotificationData(); | |
| 25 } | |
| 26 | |
| 27 WebNotificationData webData; | |
| 28 | |
| 29 // TODO(peter): Require a title to be given for notifications? | |
| 30 webData.title = title; | |
| 31 | |
| 32 // TODO(peter): WebNotificationData should be able to reflect "auto" as well . | |
| 33 webData.direction = options.dir() == "rtl" ? WebNotificationData::DirectionR ightToLeft : WebNotificationData::DirectionLeftToRight; | |
| 34 | |
| 35 webData.lang = options.lang(); | |
| 36 webData.body = options.body(); | |
| 37 webData.tag = options.tag(); | |
| 38 | |
| 39 KURL iconUrl; | |
| 40 | |
| 41 // TODO(peter): Apply the appropriate CORS checks on the |iconUrl|. | |
| 42 if (options.hasIcon() && !options.icon().isEmpty()) { | |
| 43 iconUrl = executionContext->completeURL(options.icon()); | |
| 44 if (!iconUrl.isValid()) | |
| 45 iconUrl = KURL(); | |
| 46 } | |
| 47 | |
| 48 webData.icon = iconUrl; | |
| 49 webData.vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibra te()); | |
| 50 webData.silent = options.silent(); | |
| 51 | |
| 52 if (options.hasData()) { | |
| 53 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptVa lueFactory::instance().create(options.data().isolate(), options.data(), nullptr, exceptionState); | |
| 54 if (exceptionState.hadException()) | |
| 55 return WebNotificationData(); | |
| 56 | |
| 57 Vector<char> serializedData; | |
| 58 serializedScriptValue->toWireBytes(serializedData); | |
| 59 | |
| 60 webData.data = serializedData; | |
| 61 } | |
| 62 | |
| 63 Vector<WebNotificationAction> actions; | |
| 64 | |
| 65 const size_t maxActions = Notification::maxActions(); | |
|
johnme
2015/08/03 15:41:36
Nit: seems more legible if you don't inline this.
Peter Beverloo
2015/08/03 17:29:30
Acknowledged.
| |
| 66 for (const NotificationAction& action : options.actions()) { | |
| 67 if (action.title().isEmpty()) { | |
| 68 exceptionState.throwTypeError("Notification action titles must not b e empty."); | |
| 69 return WebNotificationData(); | |
| 70 } | |
| 71 | |
| 72 // TODO(johnme): Ensure that |action.action()| isn't empty. | |
|
johnme
2015/08/03 15:41:36
https://codereview.chromium.org/1263043003 has bee
Peter Beverloo
2015/08/03 17:29:30
Done.
| |
| 73 | |
| 74 if (actions.size() >= maxActions) | |
| 75 continue; | |
| 76 | |
| 77 WebNotificationAction webAction; | |
| 78 webAction.title = action.title(); | |
| 79 | |
| 80 actions.append(webAction); | |
| 81 } | |
| 82 | |
| 83 webData.actions = actions; | |
| 84 | |
| 85 return webData; | |
| 86 } | |
| 87 | |
| 88 } // namespace blink | |
| OLD | NEW |