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/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/SerializedScriptValue.h" |
| 12 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
10 #include "bindings/core/v8/V8ThrowException.h" | 13 #include "bindings/core/v8/V8ThrowException.h" |
11 #include "core/dom/DOMException.h" | 14 #include "core/dom/DOMException.h" |
12 #include "core/dom/ExceptionCode.h" | 15 #include "core/dom/ExceptionCode.h" |
13 #include "core/dom/ExecutionContext.h" | 16 #include "core/dom/ExecutionContext.h" |
14 #include "modules/notifications/Notification.h" | 17 #include "modules/notifications/Notification.h" |
15 #include "modules/notifications/NotificationOptions.h" | 18 #include "modules/notifications/NotificationOptions.h" |
16 #include "platform/weborigin/KURL.h" | 19 #include "platform/weborigin/KURL.h" |
17 #include "public/platform/Platform.h" | 20 #include "public/platform/Platform.h" |
18 #include "public/platform/WebSerializedOrigin.h" | 21 #include "public/platform/WebSerializedOrigin.h" |
19 #include "public/platform/modules/notifications/WebNotificationData.h" | 22 #include "public/platform/modules/notifications/WebNotificationData.h" |
20 #include "public/platform/modules/notifications/WebNotificationManager.h" | 23 #include "public/platform/modules/notifications/WebNotificationManager.h" |
21 | 24 |
22 namespace blink { | 25 namespace blink { |
23 | 26 |
24 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str
ing& title, const NotificationOptions& options) | 27 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str
ing& title, const NotificationOptions& options, ExceptionState& exceptionState) |
25 { | 28 { |
26 ExecutionContext* executionContext = scriptState->executionContext(); | 29 ExecutionContext* executionContext = scriptState->executionContext(); |
27 | 30 |
28 // If context object's active worker is null, reject promise with a TypeErro
r exception. | 31 // If context object's active worker is null, reject promise with a TypeErro
r exception. |
29 if (!serviceWorkerRegistration.active()) | 32 if (!serviceWorkerRegistration.active()) |
30 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); | 33 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); |
31 | 34 |
32 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. | 35 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. |
33 if (Notification::checkPermission(executionContext) != WebNotificationPermis
sionAllowed) | 36 if (Notification::checkPermission(executionContext) != WebNotificationPermis
sionAllowed) |
34 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); | 37 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); |
35 | 38 |
| 39 // FIXME: Unify the code path here with the Notification.create() function. |
| 40 String dataAsWireString; |
| 41 if (options.hasData()) { |
| 42 RefPtr<SerializedScriptValue> data = SerializedScriptValueFactory::insta
nce().create(options.data(), nullptr, exceptionState, options.data().isolate()); |
| 43 if (exceptionState.hadException()) |
| 44 return exceptionState.reject(scriptState); |
| 45 dataAsWireString = data->toWireString(); |
| 46 } |
| 47 |
36 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 48 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
37 ScriptPromise promise = resolver->promise(); | 49 ScriptPromise promise = resolver->promise(); |
38 | 50 |
39 // FIXME: Do the appropriate CORS checks on the icon URL. | 51 // FIXME: Do the appropriate CORS checks on the icon URL. |
40 | 52 |
41 KURL iconUrl; | 53 KURL iconUrl; |
42 if (options.hasIcon() && !options.icon().isEmpty()) { | 54 if (options.hasIcon() && !options.icon().isEmpty()) { |
43 iconUrl = executionContext->completeURL(options.icon()); | 55 iconUrl = executionContext->completeURL(options.icon()); |
44 if (!iconUrl.isValid()) | 56 if (!iconUrl.isValid()) |
45 iconUrl = KURL(); | 57 iconUrl = KURL(); |
46 } | 58 } |
47 | 59 |
48 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; | 60 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; |
49 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, options.silent()); | 61 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, options.silent(), dataAsWireString); |
50 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); | 62 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); |
51 | 63 |
52 SecurityOrigin* origin = executionContext->securityOrigin(); | 64 SecurityOrigin* origin = executionContext->securityOrigin(); |
53 ASSERT(origin); | 65 ASSERT(origin); |
54 | 66 |
55 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 67 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
56 ASSERT(notificationManager); | 68 ASSERT(notificationManager); |
57 | 69 |
58 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); | 70 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); |
59 return promise; | 71 return promise; |
60 } | 72 } |
61 | 73 |
62 } // namespace blink | 74 } // namespace blink |
OLD | NEW |