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

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

Issue 1260793007: Generalize validation of developer input for Web Notifications (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "config.h" 5 #include "config.h"
6 #include "modules/notifications/ServiceWorkerRegistrationNotifications.h" 6 #include "modules/notifications/ServiceWorkerRegistrationNotifications.h"
7 7
8 #include "bindings/core/v8/CallbackPromiseAdapter.h" 8 #include "bindings/core/v8/CallbackPromiseAdapter.h"
9 #include "bindings/core/v8/ExceptionState.h" 9 #include "bindings/core/v8/ExceptionState.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h" 10 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "bindings/core/v8/SerializedScriptValue.h"
12 #include "bindings/core/v8/SerializedScriptValueFactory.h"
13 #include "bindings/core/v8/V8ThrowException.h"
14 #include "core/dom/DOMException.h"
15 #include "core/dom/ExceptionCode.h"
16 #include "core/dom/ExecutionContext.h" 11 #include "core/dom/ExecutionContext.h"
17 #include "modules/notifications/GetNotificationOptions.h" 12 #include "modules/notifications/GetNotificationOptions.h"
18 #include "modules/notifications/Notification.h" 13 #include "modules/notifications/Notification.h"
14 #include "modules/notifications/NotificationData.h"
19 #include "modules/notifications/NotificationOptions.h" 15 #include "modules/notifications/NotificationOptions.h"
20 #include "modules/vibration/NavigatorVibration.h"
21 #include "platform/weborigin/KURL.h"
22 #include "public/platform/Platform.h" 16 #include "public/platform/Platform.h"
23 #include "public/platform/WebSecurityOrigin.h" 17 #include "public/platform/WebSecurityOrigin.h"
24 #include "public/platform/modules/notifications/WebNotificationData.h" 18 #include "public/platform/modules/notifications/WebNotificationData.h"
25 #include "public/platform/modules/notifications/WebNotificationManager.h" 19 #include "public/platform/modules/notifications/WebNotificationManager.h"
26 #include "wtf/PassOwnPtr.h" 20 #include "wtf/PassOwnPtr.h"
27 21
28 namespace blink { 22 namespace blink {
29 namespace { 23 namespace {
30 24
31 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the 25 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the
(...skipping 21 matching lines...) Expand all
53 ExecutionContext* executionContext = scriptState->executionContext(); 47 ExecutionContext* executionContext = scriptState->executionContext();
54 48
55 // If context object's active worker is null, reject promise with a TypeErro r exception. 49 // If context object's active worker is null, reject promise with a TypeErro r exception.
56 if (!serviceWorkerRegistration.active()) 50 if (!serviceWorkerRegistration.active())
57 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration.")); 51 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration."));
58 52
59 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. 53 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps.
60 if (Notification::checkPermission(executionContext) != WebNotificationPermis sionAllowed) 54 if (Notification::checkPermission(executionContext) != WebNotificationPermis sionAllowed)
61 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No notification permission has been granted for thi s origin.")); 55 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No notification permission has been granted for thi s origin."));
62 56
63 if (options.hasVibrate() && options.silent()) 57 // Validate the developer-provided values to get a WebNotificationData objec t.
64 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Silent notifications must not specify vibration pat terns.")); 58 WebNotificationData data = createWebNotificationData(executionContext, title , options, exceptionState);
65 59 if (exceptionState.hadException())
66 // FIXME: Unify the code path here with the Notification.create() function. 60 return exceptionState.reject(scriptState);
67 Vector<char> dataAsWireBytes;
68 if (options.hasData()) {
69 RefPtr<SerializedScriptValue> data = SerializedScriptValueFactory::insta nce().create(options.data().isolate(), options.data(), nullptr, exceptionState);
70 if (exceptionState.hadException())
71 return exceptionState.reject(scriptState);
72
73 data->toWireBytes(dataAsWireBytes);
74 }
75
76 for (const NotificationAction& action : options.actions()) {
77 if (action.title().isEmpty())
78 return ScriptPromise::reject(scriptState, V8ThrowException::createTy peError(scriptState->isolate(), "Notification action titles must not be empty.") );
79 }
80 61
81 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 62 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
82 ScriptPromise promise = resolver->promise(); 63 ScriptPromise promise = resolver->promise();
83 64
84 // FIXME: Do the appropriate CORS checks on the icon URL.
85
86 KURL iconUrl;
87 if (options.hasIcon() && !options.icon().isEmpty()) {
88 iconUrl = executionContext->completeURL(options.icon());
89 if (!iconUrl.isValid())
90 iconUrl = KURL();
91 }
92
93 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight;
94 NavigatorVibration::VibrationPattern vibrate = NavigatorVibration::sanitizeV ibrationPattern(options.vibrate());
95 WebVector<WebNotificationAction> webActions;
96 Notification::actionsToWebActions(options.actions(), &webActions);
97 WebNotificationData notification(title, dir, options.lang(), options.body(), options.tag(), iconUrl, vibrate, options.silent(), dataAsWireBytes, webActions) ;
98 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v oid>(resolver); 65 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v oid>(resolver);
99 66
100 SecurityOrigin* origin = executionContext->securityOrigin(); 67 SecurityOrigin* origin = executionContext->securityOrigin();
101 ASSERT(origin);
102
103 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 68 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
104 ASSERT(notificationManager); 69 ASSERT(notificationManager);
105 70
106 notificationManager->showPersistent(WebSecurityOrigin(origin), notification, serviceWorkerRegistration.webRegistration(), callbacks); 71 notificationManager->showPersistent(WebSecurityOrigin(origin), data, service WorkerRegistration.webRegistration(), callbacks);
107 return promise; 72 return promise;
108 } 73 }
109 74
110 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get NotificationOptions& options) 75 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get NotificationOptions& options)
111 { 76 {
112 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 77 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
113 ScriptPromise promise = resolver->promise(); 78 ScriptPromise promise = resolver->promise();
114 79
115 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver); 80 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver);
116 81
117 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 82 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
118 ASSERT(notificationManager); 83 ASSERT(notificationManager);
119 84
120 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati on.webRegistration(), callbacks); 85 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati on.webRegistration(), callbacks);
121 return promise; 86 return promise;
122 } 87 }
123 88
124 } // namespace blink 89 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698