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

Side by Side Diff: Source/modules/notifications/NotificationData.cpp

Issue 1260793007: Generalize validation of developer input for Web Notifications (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix actions Created 5 years, 4 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 | Annotate | Revision Log
OLDNEW
(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 namespace {
19
20 WebNotificationData::Direction toDirectionEnumValue(const String& direction)
21 {
22 if (direction == "ltr")
23 return WebNotificationData::DirectionLeftToRight;
24 if (direction == "rtl")
25 return WebNotificationData::DirectionRightToLeft;
26
27 return WebNotificationData::DirectionAuto;
28 }
29
30 } // namespace
31
32 WebNotificationData createWebNotificationData(ExecutionContext* executionContext , const String& title, const NotificationOptions& options, ExceptionState& excep tionState)
33 {
34 // If silent is true, the notification must not have a vibration pattern.
35 if (options.hasVibrate() && options.silent()) {
36 exceptionState.throwTypeError("Silent notifications must not specify vib ration patterns.");
37 return WebNotificationData();
38 }
39
40 WebNotificationData webData;
41
42 webData.title = title;
43 webData.direction = toDirectionEnumValue(options.dir());
44 webData.lang = options.lang();
45 webData.body = options.body();
46 webData.tag = options.tag();
47
48 KURL iconUrl;
49
50 // TODO(peter): Apply the appropriate CORS checks on the |iconUrl|.
51 if (options.hasIcon() && !options.icon().isEmpty()) {
52 iconUrl = executionContext->completeURL(options.icon());
53 if (!iconUrl.isValid())
54 iconUrl = KURL();
55 }
56
57 webData.icon = iconUrl;
58 webData.vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibra te());
59 webData.silent = options.silent();
60
61 if (options.hasData()) {
62 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptVa lueFactory::instance().create(options.data().isolate(), options.data(), nullptr, exceptionState);
63 if (exceptionState.hadException())
64 return WebNotificationData();
65
66 Vector<char> serializedData;
67 serializedScriptValue->toWireBytes(serializedData);
68
69 webData.data = serializedData;
70 }
71
72 Vector<WebNotificationAction> actions;
73
74 const size_t maxActions = Notification::maxActions();
75 for (const NotificationAction& action : options.actions()) {
76 if (action.action().isEmpty()) {
77 exceptionState.throwTypeError("NotificationAction `action` must not be empty.");
78 return WebNotificationData();
79 }
80
81 if (action.title().isEmpty()) {
82 exceptionState.throwTypeError("NotificationAction `title` must not b e empty.");
83 return WebNotificationData();
84 }
85
86 if (actions.size() >= maxActions)
87 continue;
88
89 WebNotificationAction webAction;
90 webAction.action = action.action();
91 webAction.title = action.title();
92
93 actions.append(webAction);
94 }
95
96 webData.actions = actions;
97
98 return webData;
99 }
100
101 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/notifications/NotificationData.h ('k') | Source/modules/notifications/NotificationDataTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698