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 "modules/notifications/ServiceWorkerRegistrationNotifications.h" | 5 #include "modules/notifications/ServiceWorkerRegistrationNotifications.h" |
6 | 6 |
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
(...skipping 14 matching lines...) Loading... |
25 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the | 25 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the |
26 // getNotifications() promise with a HeapVector owning Notifications. | 26 // getNotifications() promise with a HeapVector owning Notifications. |
27 class NotificationArray { | 27 class NotificationArray { |
28 public: | 28 public: |
29 using WebType = const WebVector<WebPersistentNotificationInfo>&; | 29 using WebType = const WebVector<WebPersistentNotificationInfo>&; |
30 | 30 |
31 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver
, const WebVector<WebPersistentNotificationInfo>& notificationInfos) | 31 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver
, const WebVector<WebPersistentNotificationInfo>& notificationInfos) |
32 { | 32 { |
33 HeapVector<Member<Notification>> notifications; | 33 HeapVector<Member<Notification>> notifications; |
34 for (const WebPersistentNotificationInfo& notificationInfo : notificatio
nInfos) | 34 for (const WebPersistentNotificationInfo& notificationInfo : notificatio
nInfos) |
35 notifications.append(Notification::create(resolver->executionContext
(), notificationInfo.persistentId, notificationInfo.data, true /* showing */)); | 35 notifications.append(Notification::create(resolver->getExecutionCont
ext(), notificationInfo.persistentId, notificationInfo.data, true /* showing */)
); |
36 | 36 |
37 return notifications; | 37 return notifications; |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 NotificationArray() = delete; | 41 NotificationArray() = delete; |
42 }; | 42 }; |
43 | 43 |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str
ing& title, const NotificationOptions& options, ExceptionState& exceptionState) | 46 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str
ing& title, const NotificationOptions& options, ExceptionState& exceptionState) |
47 { | 47 { |
48 ExecutionContext* executionContext = scriptState->executionContext(); | 48 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
49 | 49 |
50 // If context object's active worker is null, reject promise with a TypeErro
r exception. | 50 // If context object's active worker is null, reject promise with a TypeErro
r exception. |
51 if (!serviceWorkerRegistration.active()) | 51 if (!serviceWorkerRegistration.active()) |
52 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); | 52 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); |
53 | 53 |
54 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. | 54 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. |
55 if (Notification::checkPermission(executionContext) != WebNotificationPermis
sionAllowed) | 55 if (Notification::checkPermission(executionContext) != WebNotificationPermis
sionAllowed) |
56 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); | 56 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); |
57 | 57 |
58 // Validate the developer-provided values to get a WebNotificationData objec
t. | 58 // Validate the developer-provided values to get a WebNotificationData objec
t. |
59 WebNotificationData data = createWebNotificationData(executionContext, title
, options, exceptionState); | 59 WebNotificationData data = createWebNotificationData(executionContext, title
, options, exceptionState); |
60 if (exceptionState.hadException()) | 60 if (exceptionState.hadException()) |
61 return exceptionState.reject(scriptState); | 61 return exceptionState.reject(scriptState); |
62 | 62 |
63 // Log number of actions developer provided in linear histogram: 0 -> underf
low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. | 63 // Log number of actions developer provided in linear histogram: 0 -> underf
low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. |
64 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto
gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount"
, 17)); | 64 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto
gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount"
, 17)); |
65 notificationCountHistogram.count(options.actions().size()); | 65 notificationCountHistogram.count(options.actions().size()); |
66 | 66 |
67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
68 ScriptPromise promise = resolver->promise(); | 68 ScriptPromise promise = resolver->promise(); |
69 | 69 |
70 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); | 70 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v
oid>(resolver); |
71 | 71 |
72 SecurityOrigin* origin = executionContext->securityOrigin(); | 72 SecurityOrigin* origin = executionContext->getSecurityOrigin(); |
73 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 73 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
74 ASSERT(notificationManager); | 74 ASSERT(notificationManager); |
75 | 75 |
76 notificationManager->showPersistent(WebSecurityOrigin(origin), data, service
WorkerRegistration.webRegistration(), callbacks); | 76 notificationManager->showPersistent(WebSecurityOrigin(origin), data, service
WorkerRegistration.webRegistration(), callbacks); |
77 return promise; | 77 return promise; |
78 } | 78 } |
79 | 79 |
80 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get
NotificationOptions& options) | 80 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get
NotificationOptions& options) |
81 { | 81 { |
82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
83 ScriptPromise promise = resolver->promise(); | 83 ScriptPromise promise = resolver->promise(); |
84 | 84 |
85 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); | 85 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); |
86 | 86 |
87 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 87 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
88 ASSERT(notificationManager); | 88 ASSERT(notificationManager); |
89 | 89 |
90 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); | 90 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati
on.webRegistration(), callbacks); |
91 return promise; | 91 return promise; |
92 } | 92 } |
93 | 93 |
94 } // namespace blink | 94 } // namespace blink |
OLD | NEW |