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 "platform/weborigin/KURL.h" | 20 #include "platform/weborigin/KURL.h" |
21 #include "public/platform/Platform.h" | 21 #include "public/platform/Platform.h" |
22 #include "public/platform/WebSerializedOrigin.h" | 22 #include "public/platform/WebSerializedOrigin.h" |
23 #include "public/platform/modules/notifications/WebNotificationData.h" | 23 #include "public/platform/modules/notifications/WebNotificationData.h" |
24 #include "public/platform/modules/notifications/WebNotificationManager.h" | 24 #include "public/platform/modules/notifications/WebNotificationManager.h" |
| 25 #include "public/platform/modules/notifications/WebNotificationVibratePattern.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 |
30 // getNotifications() promise with a HeapVector owning Notifications. | 31 // getNotifications() promise with a HeapVector owning Notifications. |
31 class NotificationArray { | 32 class NotificationArray { |
32 public: | 33 public: |
33 using WebType = WebVector<WebPersistentNotificationInfo>; | 34 using WebType = WebVector<WebPersistentNotificationInfo>; |
34 | 35 |
(...skipping 24 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(), "If options's silent is true, options's vibrate shou
ld not be presented.")); |
| 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 String dataAsWireString; | 74 String dataAsWireString; |
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 dataAsWireString = data->toWireString(); | 79 dataAsWireString = data->toWireString(); |
76 } | 80 } |
77 | 81 |
78 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 82 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
79 ScriptPromise promise = resolver->promise(); | 83 ScriptPromise promise = resolver->promise(); |
80 | 84 |
81 // FIXME: Do the appropriate CORS checks on the icon URL. | 85 // FIXME: Do the appropriate CORS checks on the icon URL. |
82 | 86 |
83 KURL iconUrl; | 87 KURL iconUrl; |
84 if (options.hasIcon() && !options.icon().isEmpty()) { | 88 if (options.hasIcon() && !options.icon().isEmpty()) { |
85 iconUrl = executionContext->completeURL(options.icon()); | 89 iconUrl = executionContext->completeURL(options.icon()); |
86 if (!iconUrl.isValid()) | 90 if (!iconUrl.isValid()) |
87 iconUrl = KURL(); | 91 iconUrl = KURL(); |
88 } | 92 } |
89 | 93 |
90 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; | 94 WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificatio
nData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; |
91 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, options.silent(), dataAsWireString); | 95 WebNotificationData notification(title, dir, options.lang(), options.body(),
options.tag(), iconUrl, Notification::convertFromVibrationData(options.vibrate(
)), options.silent(), dataAsWireString); |
92 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); | 96 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); |
93 | 97 |
94 SecurityOrigin* origin = executionContext->securityOrigin(); | 98 SecurityOrigin* origin = executionContext->securityOrigin(); |
95 ASSERT(origin); | 99 ASSERT(origin); |
96 | 100 |
97 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 101 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
98 ASSERT(notificationManager); | 102 ASSERT(notificationManager); |
99 | 103 |
100 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); | 104 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati
on, serviceWorkerRegistration.webRegistration(), callbacks); |
101 return promise; | 105 return promise; |
102 } | 106 } |
103 | 107 |
104 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get
NotificationOptions& options) | 108 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get
NotificationOptions& options) |
105 { | 109 { |
106 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 110 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
107 ScriptPromise promise = resolver->promise(); | 111 ScriptPromise promise = resolver->promise(); |
108 | 112 |
109 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); | 113 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); |
110 | 114 |
111 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 115 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
112 ASSERT(notificationManager); | 116 ASSERT(notificationManager); |
113 | 117 |
114 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); | 118 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); |
115 return promise; | 119 return promise; |
116 } | 120 } |
117 | 121 |
118 } // namespace blink | 122 } // namespace blink |
OLD | NEW |