OLD | NEW |
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 Loading... |
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.action().isEmpty()) | |
78 return ScriptPromise::reject(scriptState, V8ThrowException::createTy
peError(scriptState->isolate(), "NotificationAction action must not be empty."))
; | |
79 if (action.title().isEmpty()) | |
80 return ScriptPromise::reject(scriptState, V8ThrowException::createTy
peError(scriptState->isolate(), "NotificationAction title must not be empty.")); | |
81 } | |
82 | 61 |
83 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 62 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
84 ScriptPromise promise = resolver->promise(); | 63 ScriptPromise promise = resolver->promise(); |
85 | 64 |
86 // FIXME: Do the appropriate CORS checks on the icon URL. | |
87 | |
88 KURL iconUrl; | |
89 if (options.hasIcon() && !options.icon().isEmpty()) { | |
90 iconUrl = executionContext->completeURL(options.icon()); | |
91 if (!iconUrl.isValid()) | |
92 iconUrl = KURL(); | |
93 } | |
94 | |
95 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; | |
96 NavigatorVibration::VibrationPattern vibrate = NavigatorVibration::sanitizeV
ibrationPattern(options.vibrate()); | |
97 WebVector<WebNotificationAction> webActions; | |
98 Notification::actionsToWebActions(options.actions(), &webActions); | |
99 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, vibrate, options.silent(), dataAsWireBytes, webActions)
; | |
100 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); | 65 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); |
101 | 66 |
102 SecurityOrigin* origin = executionContext->securityOrigin(); | 67 SecurityOrigin* origin = executionContext->securityOrigin(); |
103 ASSERT(origin); | |
104 | |
105 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 68 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
106 ASSERT(notificationManager); | 69 ASSERT(notificationManager); |
107 | 70 |
108 notificationManager->showPersistent(WebSecurityOrigin(origin), notification,
serviceWorkerRegistration.webRegistration(), callbacks); | 71 notificationManager->showPersistent(WebSecurityOrigin(origin), data, service
WorkerRegistration.webRegistration(), callbacks); |
109 return promise; | 72 return promise; |
110 } | 73 } |
111 | 74 |
112 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) |
113 { | 76 { |
114 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
115 ScriptPromise promise = resolver->promise(); | 78 ScriptPromise promise = resolver->promise(); |
116 | 79 |
117 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); | 80 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); |
118 | 81 |
119 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 82 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
120 ASSERT(notificationManager); | 83 ASSERT(notificationManager); |
121 | 84 |
122 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); | 85 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); |
123 return promise; | 86 return promise; |
124 } | 87 } |
125 | 88 |
126 } // namespace blink | 89 } // namespace blink |
OLD | NEW |