Commit 34e1a2c5 authored by Anita Woodruff's avatar Anita Woodruff Committed by Commit Bot

[Notifications] Use ScriptPromiseResolver directly in Mojo codepath

- Previously when displaying and getting notifications the promise
resolver was always converted to callbacks, for the legacy IPC code
which needed to store these callbacks in a map.

- Mojo renders a callback map unnecessary, so this patch refactors
the code to avoid the conversion when the Mojo pathway is taken.

Bug: 796991
Change-Id: I216730c06fdb42a3c2c9fe21708252cb652e8b6b
Reviewed-on: https://chromium-review.googlesource.com/1011422
Commit-Queue: Anita Woodruff <awdf@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551094}
parent 75a7e0fb
......@@ -18,6 +18,8 @@
#include "third_party/blink/renderer/modules/notifications/notification.h"
#include "third_party/blink/renderer/modules/permissions/permission_utils.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/histogram.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
......@@ -130,7 +132,7 @@ void NotificationManager::DisplayPersistentNotification(
blink::WebServiceWorkerRegistration* service_worker_registration,
const blink::WebNotificationData& notification_data,
std::unique_ptr<blink::WebNotificationResources> notification_resources,
std::unique_ptr<blink::WebNotificationShowCallbacks> callbacks) {
ScriptPromiseResolver* resolver) {
DCHECK(notification_resources);
DCHECK_EQ(notification_data.actions.size(),
notification_resources->action_icons.size());
......@@ -144,19 +146,20 @@ void NotificationManager::DisplayPersistentNotification(
service_worker_registration->RegistrationId(), notification_data,
*notification_resources,
WTF::Bind(&NotificationManager::DidDisplayPersistentNotification,
WrapPersistent(this), std::move(callbacks)));
WrapPersistent(this), WrapPersistent(resolver)));
}
void NotificationManager::DidDisplayPersistentNotification(
std::unique_ptr<blink::WebNotificationShowCallbacks> callbacks,
ScriptPromiseResolver* resolver,
mojom::blink::PersistentNotificationError error) {
switch (error) {
case mojom::blink::PersistentNotificationError::NONE:
callbacks->OnSuccess();
resolver->Resolve();
return;
case mojom::blink::PersistentNotificationError::INTERNAL_ERROR:
case mojom::blink::PersistentNotificationError::PERMISSION_DENIED:
callbacks->OnError();
// TODO(https://crbug.com/832944): Throw a TypeError if permission denied.
resolver->Reject();
return;
}
NOTREACHED();
......@@ -165,30 +168,29 @@ void NotificationManager::DidDisplayPersistentNotification(
void NotificationManager::GetNotifications(
WebServiceWorkerRegistration* service_worker_registration,
const WebString& filter_tag,
std::unique_ptr<WebNotificationGetCallbacks> callbacks) {
ScriptPromiseResolver* resolver) {
GetNotificationService()->GetNotifications(
service_worker_registration->RegistrationId(), filter_tag,
WTF::Bind(&NotificationManager::DidGetNotifications, WrapPersistent(this),
std::move(callbacks)));
WrapPersistent(resolver)));
}
void NotificationManager::DidGetNotifications(
std::unique_ptr<WebNotificationGetCallbacks> callbacks,
ScriptPromiseResolver* resolver,
const Vector<String>& notification_ids,
const Vector<WebNotificationData>& notification_datas) {
DCHECK_EQ(notification_ids.size(), notification_datas.size());
WebVector<WebPersistentNotificationInfo> notifications(
notification_ids.size());
HeapVector<Member<Notification>> notifications;
notifications.ReserveInitialCapacity(notification_ids.size());
for (size_t i = 0; i < notification_ids.size(); ++i) {
WebPersistentNotificationInfo notification_info;
notification_info.notification_id = notification_ids[i];
notification_info.data = notification_datas[i];
notifications[i] = notification_info;
notifications.push_back(Notification::Create(
resolver->GetExecutionContext(), notification_ids[i],
notification_datas[i], true /* showing */));
}
callbacks->OnSuccess(notifications);
resolver->Resolve(notifications);
}
const mojom::blink::NotificationServicePtr&
......
......@@ -64,7 +64,7 @@ class NotificationManager final
blink::WebServiceWorkerRegistration* service_worker_registration,
const blink::WebNotificationData& notification_data,
std::unique_ptr<blink::WebNotificationResources> notification_resources,
std::unique_ptr<blink::WebNotificationShowCallbacks> callbacks);
ScriptPromiseResolver* resolver);
// Asynchronously gets the persistent notifications belonging to the Service
// Worker Registration. If |filter_tag| is not an empty string, only the
......@@ -73,7 +73,7 @@ class NotificationManager final
void GetNotifications(
WebServiceWorkerRegistration* service_worker_registration,
const WebString& filter_tag,
std::unique_ptr<WebNotificationGetCallbacks> callbacks);
ScriptPromiseResolver* resolver);
virtual void Trace(blink::Visitor* visitor);
......@@ -81,11 +81,11 @@ class NotificationManager final
explicit NotificationManager(ExecutionContext& context);
void DidDisplayPersistentNotification(
std::unique_ptr<blink::WebNotificationShowCallbacks> callbacks,
ScriptPromiseResolver* resolver,
mojom::blink::PersistentNotificationError error);
void DidGetNotifications(
std::unique_ptr<WebNotificationGetCallbacks> callbacks,
ScriptPromiseResolver* resolver,
const Vector<String>& notification_ids,
const Vector<WebNotificationData>& notification_datas);
......
......@@ -4,7 +4,6 @@
#include "third_party/blink/renderer/modules/notifications/service_worker_registration_notifications.h"
#include <memory>
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/public/platform/modules/notifications/web_notification_data.h"
......@@ -12,7 +11,6 @@
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h"
#include "third_party/blink/renderer/bindings/core/v8/exception_state.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/notifications/get_notification_options.h"
#include "third_party/blink/renderer/modules/notifications/notification.h"
......@@ -104,10 +102,8 @@ ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise promise = resolver->Promise();
std::unique_ptr<WebNotificationShowCallbacks> callbacks =
std::make_unique<CallbackPromiseAdapter<void, void>>(resolver);
ServiceWorkerRegistrationNotifications::From(execution_context, registration)
.PrepareShow(data, std::move(callbacks));
.PrepareShow(data, resolver);
return promise;
}
......@@ -119,16 +115,16 @@ ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise promise = resolver->Promise();
auto callbacks =
std::make_unique<CallbackPromiseAdapter<NotificationArray, void>>(
resolver);
if (RuntimeEnabledFeatures::NotificationsWithMojoEnabled()) {
ExecutionContext* execution_context = ExecutionContext::From(script_state);
NotificationManager::From(execution_context)
->GetNotifications(registration.WebRegistration(), options.tag(),
std::move(callbacks));
WrapPersistent(resolver));
} else {
auto callbacks =
std::make_unique<CallbackPromiseAdapter<NotificationArray, void>>(
resolver);
WebNotificationManager* notification_manager =
Platform::Current()->GetWebNotificationManager();
DCHECK(notification_manager);
......@@ -172,13 +168,13 @@ ServiceWorkerRegistrationNotifications::From(
void ServiceWorkerRegistrationNotifications::PrepareShow(
const WebNotificationData& data,
std::unique_ptr<WebNotificationShowCallbacks> callbacks) {
ScriptPromiseResolver* resolver) {
scoped_refptr<const SecurityOrigin> origin =
GetExecutionContext()->GetSecurityOrigin();
NotificationResourcesLoader* loader = new NotificationResourcesLoader(
WTF::Bind(&ServiceWorkerRegistrationNotifications::DidLoadResources,
WrapWeakPersistent(this), std::move(origin), data,
WTF::Passed(std::move(callbacks))));
WrapPersistent(resolver)));
loaders_.insert(loader);
loader->Start(GetExecutionContext(), data);
}
......@@ -186,7 +182,7 @@ void ServiceWorkerRegistrationNotifications::PrepareShow(
void ServiceWorkerRegistrationNotifications::DidLoadResources(
scoped_refptr<const SecurityOrigin> origin,
const WebNotificationData& data,
std::unique_ptr<WebNotificationShowCallbacks> callbacks,
ScriptPromiseResolver* resolver,
NotificationResourcesLoader* loader) {
DCHECK(loaders_.Contains(loader));
......@@ -194,12 +190,15 @@ void ServiceWorkerRegistrationNotifications::DidLoadResources(
NotificationManager::From(GetExecutionContext())
->DisplayPersistentNotification(registration_->WebRegistration(), data,
loader->GetResources(),
std::move(callbacks));
WrapPersistent(resolver));
} else {
WebNotificationManager* notification_manager =
Platform::Current()->GetWebNotificationManager();
DCHECK(notification_manager);
std::unique_ptr<WebNotificationShowCallbacks> callbacks =
std::make_unique<CallbackPromiseAdapter<void, void>>(resolver);
notification_manager->ShowPersistent(
WebSecurityOrigin(origin.get()), data, loader->GetResources(),
registration_->WebRegistration(), std::move(callbacks));
......
......@@ -24,6 +24,7 @@ class ExceptionState;
class GetNotificationOptions;
class NotificationOptions;
class NotificationResourcesLoader;
class ScriptPromiseResolver;
class ScriptState;
class SecurityOrigin;
class ServiceWorkerRegistration;
......@@ -62,10 +63,11 @@ class ServiceWorkerRegistrationNotifications final
ServiceWorkerRegistration& registration);
void PrepareShow(const WebNotificationData& data,
std::unique_ptr<WebNotificationShowCallbacks> callbacks);
ScriptPromiseResolver* resolver);
void DidLoadResources(scoped_refptr<const SecurityOrigin> origin,
const WebNotificationData& data,
std::unique_ptr<WebNotificationShowCallbacks> callbacks,
ScriptPromiseResolver* resolver,
NotificationResourcesLoader* loader);
Member<ServiceWorkerRegistration> registration_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment