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" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/V8ThrowException.h" |
8 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
9 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
10 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "modules/notifications/Notification.h" |
11 #include "modules/notifications/NotificationOptions.h" | 15 #include "modules/notifications/NotificationOptions.h" |
12 #include "platform/weborigin/KURL.h" | 16 #include "platform/weborigin/KURL.h" |
| 17 #include "public/platform/Platform.h" |
13 #include "public/platform/WebNotificationData.h" | 18 #include "public/platform/WebNotificationData.h" |
| 19 #include "public/platform/WebNotificationManager.h" |
| 20 #include "public/platform/WebSerializedOrigin.h" |
14 | 21 |
15 namespace blink { | 22 namespace blink { |
16 | 23 |
17 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str
ing& title, const NotificationOptions& options) | 24 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str
ing& title, const NotificationOptions& options) |
18 { | 25 { |
| 26 ExecutionContext* executionContext = scriptState->executionContext(); |
| 27 |
| 28 // If context object's active worker is null, reject promise with a TypeErro
r exception. |
| 29 if (!serviceWorkerRegistration.active()) |
| 30 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); |
| 31 |
| 32 // 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) |
| 34 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); |
| 35 |
| 36 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 37 ScriptPromise promise = resolver->promise(); |
| 38 |
| 39 // FIXME: Do the appropriate CORS checks on the icon URL. |
| 40 // FIXME: Determine the text direction based on the options dictionary. |
| 41 |
19 KURL iconUrl; | 42 KURL iconUrl; |
20 if (options.hasIcon() && !options.icon().isEmpty()) { | 43 if (options.hasIcon() && !options.icon().isEmpty()) { |
21 iconUrl = scriptState->executionContext()->completeURL(options.icon()); | 44 iconUrl = executionContext->completeURL(options.icon()); |
22 if (!iconUrl.isValid()) | 45 if (!iconUrl.isValid()) |
23 iconUrl = KURL(); | 46 iconUrl = KURL(); |
24 } | 47 } |
25 | 48 |
26 WebNotificationData notification(title, WebNotificationData::DirectionLeftTo
Right, options.lang(), options.body(), options.tag(), iconUrl); | 49 WebNotificationData notification(title, WebNotificationData::DirectionLeftTo
Right, options.lang(), options.body(), options.tag(), iconUrl); |
| 50 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); |
27 | 51 |
28 // FIXME: Hook this up with the Blink API once it's been implemented. | 52 SecurityOrigin* origin = executionContext->securityOrigin(); |
29 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError, "showNotification is not implemented yet.")); | 53 ASSERT(origin); |
| 54 |
| 55 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
| 56 ASSERT(notificationManager); |
| 57 |
| 58 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); |
| 59 return promise; |
30 } | 60 } |
31 | 61 |
32 } // namespace blink | 62 } // namespace blink |
OLD | NEW |