| 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" |
| 11 #include "modules/notifications/GetNotificationOptions.h" | 11 #include "modules/notifications/GetNotificationOptions.h" |
| 12 #include "modules/notifications/Notification.h" | 12 #include "modules/notifications/Notification.h" |
| 13 #include "modules/notifications/NotificationData.h" | 13 #include "modules/notifications/NotificationData.h" |
| 14 #include "modules/notifications/NotificationManager.h" |
| 14 #include "modules/notifications/NotificationOptions.h" | 15 #include "modules/notifications/NotificationOptions.h" |
| 15 #include "modules/notifications/NotificationResourcesLoader.h" | 16 #include "modules/notifications/NotificationResourcesLoader.h" |
| 16 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 17 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 17 #include "platform/Histogram.h" | 18 #include "platform/Histogram.h" |
| 18 #include "platform/heap/Handle.h" | 19 #include "platform/heap/Handle.h" |
| 20 #include "platform/mojo/MojoHelper.h" |
| 19 #include "public/platform/Platform.h" | 21 #include "public/platform/Platform.h" |
| 20 #include "public/platform/WebSecurityOrigin.h" | 22 #include "public/platform/WebSecurityOrigin.h" |
| 21 #include "public/platform/modules/notifications/WebNotificationData.h" | 23 #include "public/platform/modules/notifications/notification_resources.mojom-wtf
.h" |
| 24 #include "wtf/Functional.h" |
| 22 #include "wtf/RefPtr.h" | 25 #include "wtf/RefPtr.h" |
| 23 | 26 |
| 24 namespace blink { | 27 namespace blink { |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the | 30 // Object responsible for handling the result status of showing a persistent |
| 28 // getNotifications() promise with a HeapVector owning Notifications. | 31 // notification using the embedder. |
| 29 class NotificationArray { | 32 class ShowNotificationResponseHandler { |
| 33 WTF_MAKE_NONCOPYABLE(ShowNotificationResponseHandler); |
| 30 public: | 34 public: |
| 31 using WebType = const WebVector<WebPersistentNotificationInfo>&; | 35 explicit ShowNotificationResponseHandler(ScriptPromiseResolver* resolver) |
| 36 : m_resolver(resolver) {} |
| 32 | 37 |
| 33 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver
, const WebVector<WebPersistentNotificationInfo>& notificationInfos) | 38 void onResponse(mojom::wtf::NotificationDisplayResult result) |
| 34 { | 39 { |
| 35 HeapVector<Member<Notification>> notifications; | 40 m_resolver->resolve(); |
| 36 for (const WebPersistentNotificationInfo& notificationInfo : notificatio
nInfos) | |
| 37 notifications.append(Notification::create(resolver->getExecutionCont
ext(), notificationInfo.persistentId, notificationInfo.data, true /* showing */)
); | |
| 38 | |
| 39 return notifications; | |
| 40 } | 41 } |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 NotificationArray() = delete; | 44 Persistent<ScriptPromiseResolver> m_resolver; |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 } // namespace | 47 } // namespace |
| 47 | 48 |
| 48 ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(E
xecutionContext* executionContext, ServiceWorkerRegistration* registration) | 49 ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(E
xecutionContext* executionContext, ServiceWorkerRegistration* registration) |
| 49 : ContextLifecycleObserver(executionContext), m_registration(registration) | 50 : ContextLifecycleObserver(executionContext) |
| 51 , m_registration(registration) |
| 50 { | 52 { |
| 51 } | 53 } |
| 52 | 54 |
| 53 ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications(
) | 55 ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications(
) |
| 54 { | 56 { |
| 55 } | 57 } |
| 56 | 58 |
| 57 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& registration, const String& title, c
onst NotificationOptions& options, ExceptionState& exceptionState) | 59 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
te* scriptState, ServiceWorkerRegistration& registration, const String& title, c
onst NotificationOptions& options, ExceptionState& exceptionState) |
| 58 { | 60 { |
| 59 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 61 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 60 | 62 |
| 61 // If context object's active worker is null, reject promise with a TypeErro
r exception. | 63 // If context object's active worker is null, reject promise with a TypeErro
r exception. |
| 62 if (!registration.active()) | 64 if (!registration.active()) |
| 63 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); | 65 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No active registration available on the ServiceWork
erRegistration.")); |
| 64 | 66 |
| 67 NotificationManager* notificationManager = NotificationManager::from(executi
onContext); |
| 68 DCHECK(notificationManager); |
| 69 |
| 65 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. | 70 // If permission for notification's origin is not "granted", reject promise
with a TypeError exception, and terminate these substeps. |
| 66 if (Notification::checkPermission(executionContext) != mojom::PermissionStat
us::GRANTED) | 71 if (notificationManager->permissionStatus() != mojom::PermissionStatus::GRAN
TED) |
| 67 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); | 72 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "No notification permission has been granted for thi
s origin.")); |
| 68 | 73 |
| 69 // Validate the developer-provided values to get a WebNotificationData objec
t. | 74 // Validate the developer-provided values to get a Mojo Notification object. |
| 70 WebNotificationData data = createWebNotificationData(executionContext, title
, options, exceptionState); | 75 mojom::wtf::NotificationPtr data = createNotificationData(executionContext,
title, options, exceptionState); |
| 71 if (exceptionState.hadException()) | 76 if (exceptionState.hadException()) |
| 72 return exceptionState.reject(scriptState); | 77 return exceptionState.reject(scriptState); |
| 73 | 78 |
| 74 // Log number of actions developer provided in linear histogram: 0 -> underf
low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. | 79 // Log number of actions developer provided in linear histogram: 0 -> underf
low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. |
| 75 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto
gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount"
, 17)); | 80 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto
gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount"
, 17)); |
| 76 notificationCountHistogram.count(options.actions().size()); | 81 notificationCountHistogram.count(options.actions().size()); |
| 77 | 82 |
| 78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 83 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 79 ScriptPromise promise = resolver->promise(); | 84 ScriptPromise promise = resolver->promise(); |
| 80 | 85 |
| 81 OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromis
eAdapter<void, void>(resolver)); | 86 OwnPtr<ShowNotificationResponseHandler> responseHandler = adoptPtr(new ShowN
otificationResponseHandler(resolver)); |
| 82 ServiceWorkerRegistrationNotifications::from(executionContext, registration)
.prepareShow(data, callbacks.release()); | 87 mojom::wtf::NotificationService::ShowPersistentCallback callback = |
| 88 createBaseCallback(bind<mojom::wtf::NotificationDisplayResult>(&ShowNoti
ficationResponseHandler::onResponse, responseHandler.release())); |
| 89 |
| 90 // TODO(peter): Make the NotificationManager responsible for loading resourc
es. |
| 91 ServiceWorkerRegistrationNotifications::from(executionContext, registration)
.prepareShow(std::move(data), callback); |
| 83 | 92 |
| 84 return promise; | 93 return promise; |
| 85 } | 94 } |
| 86 | 95 |
| 87 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO
ptions& options) | 96 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO
ptions& options) |
| 88 { | 97 { |
| 89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 98 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 90 ScriptPromise promise = resolver->promise(); | 99 ScriptPromise promise = resolver->promise(); |
| 91 | 100 |
| 92 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica
tionArray, void>(resolver); | 101 //WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifi
cationArray, void>(resolver); |
| 93 | 102 |
| 94 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 103 //WebNotificationManager* notificationManager = Platform::current()->notific
ationManager(); |
| 95 ASSERT(notificationManager); | 104 //ASSERT(notificationManager); |
| 96 | 105 |
| 97 notificationManager->getNotifications(options.tag(), registration.webRegistr
ation(), callbacks); | 106 //notificationManager->getNotifications(options.tag(), registration.webRegis
tration(), callbacks); |
| 98 return promise; | 107 return promise; |
| 99 } | 108 } |
| 100 | 109 |
| 101 void ServiceWorkerRegistrationNotifications::contextDestroyed() | 110 void ServiceWorkerRegistrationNotifications::contextDestroyed() |
| 102 { | 111 { |
| 103 for (auto loader : m_loaders) | 112 for (auto loader : m_loaders) |
| 104 loader->stop(); | 113 loader->stop(); |
| 105 } | 114 } |
| 106 | 115 |
| 107 DEFINE_TRACE(ServiceWorkerRegistrationNotifications) | 116 DEFINE_TRACE(ServiceWorkerRegistrationNotifications) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 120 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications::
from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration
) | 129 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications::
from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration
) |
| 121 { | 130 { |
| 122 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork
erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist
ration, supplementName())); | 131 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork
erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist
ration, supplementName())); |
| 123 if (!supplement) { | 132 if (!supplement) { |
| 124 supplement = new ServiceWorkerRegistrationNotifications(executionContext
, ®istration); | 133 supplement = new ServiceWorkerRegistrationNotifications(executionContext
, ®istration); |
| 125 provideTo(registration, supplementName(), supplement); | 134 provideTo(registration, supplementName(), supplement); |
| 126 } | 135 } |
| 127 return *supplement; | 136 return *supplement; |
| 128 } | 137 } |
| 129 | 138 |
| 130 void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationDa
ta& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks) | 139 void ServiceWorkerRegistrationNotifications::prepareShow(mojom::wtf::Notificatio
nPtr notification, const mojom::wtf::NotificationService::ShowPersistentCallback
& callback) |
| 131 { | 140 { |
| 132 RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin(); | 141 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N
otificationResourcesLoader*, mojom::wtf::NotificationPtr>(&ServiceWorkerRegistra
tionNotifications::didLoadResources, WeakPersistentThisPointer<ServiceWorkerRegi
strationNotifications>(this), callback)); |
| 133 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N
otificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadRes
ources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this),
origin.release(), data, callbacks)); | |
| 134 m_loaders.add(loader); | 142 m_loaders.add(loader); |
| 135 loader->start(getExecutionContext(), data); | 143 |
| 144 loader->start(getExecutionContext(), std::move(notification)); |
| 136 } | 145 } |
| 137 | 146 |
| 138 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit
yOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShow
Callbacks> callbacks, NotificationResourcesLoader* loader) | 147 void ServiceWorkerRegistrationNotifications::didLoadResources(const mojom::wtf::
NotificationService::ShowPersistentCallback& callback, NotificationResourcesLoad
er* loader, mojom::wtf::NotificationPtr notification) |
| 139 { | 148 { |
| 140 DCHECK(m_loaders.contains(loader)); | 149 DCHECK(m_loaders.contains(loader)); |
| 141 | 150 |
| 142 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 151 NotificationManager::from(getExecutionContext())->showPersistent(std::move(n
otification), loader->getResources(), callback); |
| 143 DCHECK(notificationManager); | |
| 144 | 152 |
| 145 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l
oader->getResources(), m_registration->webRegistration(), callbacks.leakPtr()); | |
| 146 m_loaders.remove(loader); | 153 m_loaders.remove(loader); |
| 147 } | 154 } |
| 148 | 155 |
| 149 } // namespace blink | 156 } // namespace blink |
| OLD | NEW |