| Index: third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp
|
| diff --git a/third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp b/third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp
|
| index 56eba48af0475fd81a3bde55ccb2148d3e794b24..5afd1efe23e5b9f36c082388a96b6ed458bc8920 100644
|
| --- a/third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp
|
| +++ b/third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp
|
| @@ -4,49 +4,68 @@
|
|
|
| #include "modules/notifications/ServiceWorkerRegistrationNotifications.h"
|
|
|
| -#include "bindings/core/v8/CallbackPromiseAdapter.h"
|
| #include "bindings/core/v8/ExceptionState.h"
|
| #include "bindings/core/v8/ScriptPromiseResolver.h"
|
| #include "core/dom/ExecutionContext.h"
|
| #include "modules/notifications/GetNotificationOptions.h"
|
| #include "modules/notifications/Notification.h"
|
| #include "modules/notifications/NotificationData.h"
|
| +#include "modules/notifications/NotificationManager.h"
|
| #include "modules/notifications/NotificationOptions.h"
|
| #include "modules/notifications/NotificationResourcesLoader.h"
|
| #include "modules/serviceworkers/ServiceWorkerRegistration.h"
|
| #include "platform/Histogram.h"
|
| #include "platform/heap/Handle.h"
|
| -#include "public/platform/Platform.h"
|
| -#include "public/platform/WebSecurityOrigin.h"
|
| -#include "public/platform/modules/notifications/WebNotificationData.h"
|
| -#include "wtf/RefPtr.h"
|
| +#include "platform/mojo/MojoHelper.h"
|
| +#include "public/platform/modules/notifications/notification_resources.mojom-blink.h"
|
| +#include "wtf/Functional.h"
|
|
|
| namespace blink {
|
| namespace {
|
|
|
| -// Allows using a CallbackPromiseAdapter with a WebVector to resolve the
|
| -// getNotifications() promise with a HeapVector owning Notifications.
|
| -class NotificationArray {
|
| +// Object responsible for handling the result status of showing a persistent
|
| +// notification using the embedder.
|
| +class ShowNotificationResponseHandler {
|
| + WTF_MAKE_NONCOPYABLE(ShowNotificationResponseHandler);
|
| public:
|
| - using WebType = const WebVector<WebPersistentNotificationInfo>&;
|
| + explicit ShowNotificationResponseHandler(ScriptPromiseResolver* resolver)
|
| + : m_resolver(resolver) {}
|
|
|
| - static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver, const WebVector<WebPersistentNotificationInfo>& notificationInfos)
|
| + void onResponse(mojom::blink::NotificationDisplayResult result)
|
| {
|
| - HeapVector<Member<Notification>> notifications;
|
| - for (const WebPersistentNotificationInfo& notificationInfo : notificationInfos)
|
| - notifications.append(Notification::create(resolver->getExecutionContext(), notificationInfo.persistentId, notificationInfo.data, true /* showing */));
|
| + m_resolver->resolve();
|
| + }
|
| +
|
| +private:
|
| + Persistent<ScriptPromiseResolver> m_resolver;
|
| +};
|
| +
|
| +// Object responsible for handling the result of getting the array of persistent
|
| +// notifications that are showing for an origin.
|
| +class GetNotificationsResponseHandler {
|
| + WTF_MAKE_NONCOPYABLE(GetNotificationsResponseHandler);
|
| +public:
|
| + explicit GetNotificationsResponseHandler(ScriptPromiseResolver* resolver)
|
| + : m_resolver(resolver) {}
|
| +
|
| + void onResponse(mojo::WTFArray<mojom::blink::NotificationPtr> notifications)
|
| + {
|
| + HeapVector<Member<Notification>> heapNotifications;
|
| + for (size_t i = 0; i < notifications.size(); ++i)
|
| + heapNotifications.append(Notification::create(m_resolver->getExecutionContext(), std::move(notifications[i]), true /* showing */));
|
|
|
| - return notifications;
|
| + m_resolver->resolve(heapNotifications);
|
| }
|
|
|
| private:
|
| - NotificationArray() = delete;
|
| + Persistent<ScriptPromiseResolver> m_resolver;
|
| };
|
|
|
| } // namespace
|
|
|
| ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(ExecutionContext* executionContext, ServiceWorkerRegistration* registration)
|
| - : ContextLifecycleObserver(executionContext), m_registration(registration)
|
| + : ContextLifecycleObserver(executionContext)
|
| + , m_registration(registration)
|
| {
|
| }
|
|
|
| @@ -62,12 +81,15 @@ ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
|
| if (!registration.active())
|
| return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No active registration available on the ServiceWorkerRegistration."));
|
|
|
| + NotificationManager* notificationManager = NotificationManager::from(executionContext);
|
| + DCHECK(notificationManager);
|
| +
|
| // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps.
|
| - if (Notification::checkPermission(executionContext) != mojom::PermissionStatus::GRANTED)
|
| + if (notificationManager->permissionStatus() != mojom::PermissionStatus::GRANTED)
|
| return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No notification permission has been granted for this origin."));
|
|
|
| - // Validate the developer-provided values to get a WebNotificationData object.
|
| - WebNotificationData data = createWebNotificationData(executionContext, title, options, exceptionState);
|
| + // Validate the developer-provided values to get a Mojo Notification object.
|
| + mojom::blink::NotificationPtr data = createNotificationData(executionContext, title, options, exceptionState);
|
| if (exceptionState.hadException())
|
| return exceptionState.reject(scriptState);
|
|
|
| @@ -78,9 +100,11 @@ ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
|
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
|
| ScriptPromise promise = resolver->promise();
|
|
|
| - OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromiseAdapter<void, void>(resolver));
|
| - ServiceWorkerRegistrationNotifications::from(executionContext, registration).prepareShow(data, callbacks.release());
|
| + OwnPtr<ShowNotificationResponseHandler> responseHandler = adoptPtr(new ShowNotificationResponseHandler(resolver));
|
| + mojom::blink::NotificationService::DisplayPersistentCallback callback =
|
| + createBaseCallback(bind<mojom::blink::NotificationDisplayResult>(&ShowNotificationResponseHandler::onResponse, responseHandler.release()));
|
|
|
| + ServiceWorkerRegistrationNotifications::from(executionContext, registration).prepareShow(std::move(data), callback);
|
| return promise;
|
| }
|
|
|
| @@ -89,12 +113,13 @@ ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
|
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
|
| ScriptPromise promise = resolver->promise();
|
|
|
| - WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<NotificationArray, void>(resolver);
|
| + OwnPtr<GetNotificationsResponseHandler> responseHandler = adoptPtr(new GetNotificationsResponseHandler(resolver));
|
| + mojom::blink::NotificationService::GetNotificationsCallback callback =
|
| + createBaseCallback(bind<mojo::WTFArray<mojom::blink::NotificationPtr>>(&GetNotificationsResponseHandler::onResponse, responseHandler.release()));
|
|
|
| - WebNotificationManager* notificationManager = Platform::current()->notificationManager();
|
| - ASSERT(notificationManager);
|
| + int64_t serviceWorkerRegistrationId = registration.webRegistration()->registrationId();
|
|
|
| - notificationManager->getNotifications(options.tag(), registration.webRegistration(), callbacks);
|
| + NotificationManager::from(scriptState->getExecutionContext())->getNotifications(serviceWorkerRegistrationId, options.tag(), callback);
|
| return promise;
|
| }
|
|
|
| @@ -127,22 +152,23 @@ ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications::
|
| return *supplement;
|
| }
|
|
|
| -void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationData& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks)
|
| +void ServiceWorkerRegistrationNotifications::prepareShow(mojom::blink::NotificationPtr notification, const mojom::blink::NotificationService::DisplayPersistentCallback& callback)
|
| {
|
| - RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin();
|
| - NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<NotificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadResources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this), origin.release(), data, passed(std::move(callbacks))));
|
| + NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<NotificationResourcesLoader*, mojom::blink::NotificationPtr>(&ServiceWorkerRegistrationNotifications::didLoadResources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this), callback));
|
| +
|
| m_loaders.add(loader);
|
| - loader->start(getExecutionContext(), data);
|
| +
|
| + loader->start(getExecutionContext(), std::move(notification));
|
| }
|
|
|
| -void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<SecurityOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks, NotificationResourcesLoader* loader)
|
| +void ServiceWorkerRegistrationNotifications::didLoadResources(const mojom::blink::NotificationService::DisplayPersistentCallback& callback, NotificationResourcesLoader* loader, mojom::blink::NotificationPtr notification)
|
| {
|
| DCHECK(m_loaders.contains(loader));
|
|
|
| - WebNotificationManager* notificationManager = Platform::current()->notificationManager();
|
| - DCHECK(notificationManager);
|
| + int64_t serviceWorkerRegistrationId = m_registration->webRegistration()->registrationId();
|
| +
|
| + NotificationManager::from(getExecutionContext())->displayPersistent(serviceWorkerRegistrationId, std::move(notification), loader->getResources(), callback);
|
|
|
| - notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, loader->getResources(), m_registration->webRegistration(), callbacks.leakPtr());
|
| m_loaders.remove(loader);
|
| }
|
|
|
|
|