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" | 11 #include "bindings/core/v8/SerializedScriptValue.h" |
12 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 12 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
13 #include "bindings/core/v8/V8ThrowException.h" | 13 #include "bindings/core/v8/V8ThrowException.h" |
14 #include "core/dom/DOMException.h" | 14 #include "core/dom/DOMException.h" |
15 #include "core/dom/ExceptionCode.h" | 15 #include "core/dom/ExceptionCode.h" |
16 #include "core/dom/ExecutionContext.h" | 16 #include "core/dom/ExecutionContext.h" |
17 #include "modules/notifications/GetNotificationOptions.h" | 17 #include "modules/notifications/GetNotificationOptions.h" |
18 #include "modules/notifications/Notification.h" | 18 #include "modules/notifications/Notification.h" |
19 #include "modules/notifications/NotificationOptions.h" | 19 #include "modules/notifications/NotificationOptions.h" |
| 20 #include "modules/vibration/NavigatorVibration.h" |
20 #include "platform/weborigin/KURL.h" | 21 #include "platform/weborigin/KURL.h" |
21 #include "public/platform/Platform.h" | 22 #include "public/platform/Platform.h" |
22 #include "public/platform/WebSerializedOrigin.h" | 23 #include "public/platform/WebSerializedOrigin.h" |
23 #include "public/platform/modules/notifications/WebNotificationData.h" | 24 #include "public/platform/modules/notifications/WebNotificationData.h" |
24 #include "public/platform/modules/notifications/WebNotificationManager.h" | 25 #include "public/platform/modules/notifications/WebNotificationManager.h" |
25 | 26 |
26 namespace blink { | 27 namespace blink { |
27 namespace { | 28 namespace { |
28 | 29 |
29 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the | 30 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the |
(...skipping 29 matching lines...) Expand all Loading... |
59 ExecutionContext* executionContext = scriptState->executionContext(); | 60 ExecutionContext* executionContext = scriptState->executionContext(); |
60 | 61 |
61 // If context object's active worker is null, reject promise with a TypeErro
r exception. | 62 // If context object's active worker is null, reject promise with a TypeErro
r exception. |
62 if (!serviceWorkerRegistration.active()) | 63 if (!serviceWorkerRegistration.active()) |
63 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); | 64 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); |
64 | 65 |
65 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. | 66 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. |
66 if (Notification::checkPermission(executionContext) != WebNotificationPermis
sionAllowed) | 67 if (Notification::checkPermission(executionContext) != WebNotificationPermis
sionAllowed) |
67 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); | 68 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); |
68 | 69 |
| 70 if (options.hasVibrate() && options.silent()) |
| 71 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Silent notifications must not specify vibration pat
terns.")); |
| 72 |
69 // FIXME: Unify the code path here with the Notification.create() function. | 73 // FIXME: Unify the code path here with the Notification.create() function. |
70 Vector<char> dataAsWireBytes; | 74 Vector<char> dataAsWireBytes; |
71 if (options.hasData()) { | 75 if (options.hasData()) { |
72 RefPtr<SerializedScriptValue> data = SerializedScriptValueFactory::insta
nce().create(options.data(), nullptr, exceptionState, options.data().isolate()); | 76 RefPtr<SerializedScriptValue> data = SerializedScriptValueFactory::insta
nce().create(options.data(), nullptr, exceptionState, options.data().isolate()); |
73 if (exceptionState.hadException()) | 77 if (exceptionState.hadException()) |
74 return exceptionState.reject(scriptState); | 78 return exceptionState.reject(scriptState); |
75 | 79 |
76 data->toWireBytes(dataAsWireBytes); | 80 data->toWireBytes(dataAsWireBytes); |
77 } | 81 } |
78 | 82 |
79 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 83 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
80 ScriptPromise promise = resolver->promise(); | 84 ScriptPromise promise = resolver->promise(); |
81 | 85 |
82 // FIXME: Do the appropriate CORS checks on the icon URL. | 86 // FIXME: Do the appropriate CORS checks on the icon URL. |
83 | 87 |
84 KURL iconUrl; | 88 KURL iconUrl; |
85 if (options.hasIcon() && !options.icon().isEmpty()) { | 89 if (options.hasIcon() && !options.icon().isEmpty()) { |
86 iconUrl = executionContext->completeURL(options.icon()); | 90 iconUrl = executionContext->completeURL(options.icon()); |
87 if (!iconUrl.isValid()) | 91 if (!iconUrl.isValid()) |
88 iconUrl = KURL(); | 92 iconUrl = KURL(); |
89 } | 93 } |
90 | 94 |
91 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; | 95 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; |
92 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, options.silent(), dataAsWireBytes); | 96 NavigatorVibration::VibrationPattern vibrate = NavigatorVibration::sanitizeV
ibrationPattern(options.vibrate()); |
| 97 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, vibrate, options.silent(), dataAsWireBytes); |
93 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); | 98 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); |
94 | 99 |
95 SecurityOrigin* origin = executionContext->securityOrigin(); | 100 SecurityOrigin* origin = executionContext->securityOrigin(); |
96 ASSERT(origin); | 101 ASSERT(origin); |
97 | 102 |
98 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 103 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
99 ASSERT(notificationManager); | 104 ASSERT(notificationManager); |
100 | 105 |
101 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); | 106 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); |
102 return promise; | 107 return promise; |
103 } | 108 } |
104 | 109 |
105 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get
NotificationOptions& options) | 110 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get
NotificationOptions& options) |
106 { | 111 { |
107 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 112 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
108 ScriptPromise promise = resolver->promise(); | 113 ScriptPromise promise = resolver->promise(); |
109 | 114 |
110 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); | 115 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); |
111 | 116 |
112 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 117 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
113 ASSERT(notificationManager); | 118 ASSERT(notificationManager); |
114 | 119 |
115 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); | 120 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); |
116 return promise; | 121 return promise; |
117 } | 122 } |
118 | 123 |
119 } // namespace blink | 124 } // namespace blink |
OLD | NEW |