Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(818)

Unified Diff: third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp

Issue 1847863002: Move notification resource loading from content/child to blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address peter's comments. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 db1353dc0f7341ab1a032ace6e0e2a38d6faefe2..5a322e7157ad088e1a7c0151b8669102a658d07d 100644
--- a/third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp
+++ b/third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp
@@ -12,12 +12,14 @@
#include "modules/notifications/Notification.h"
#include "modules/notifications/NotificationData.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 "public/platform/modules/notifications/WebNotificationManager.h"
+#include "wtf/RefPtr.h"
namespace blink {
namespace {
@@ -43,12 +45,21 @@ private:
} // namespace
-ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptState* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const String& title, const NotificationOptions& options, ExceptionState& exceptionState)
+ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(ExecutionContext* executionContext, ServiceWorkerRegistration* registration)
+ : ContextLifecycleObserver(executionContext), m_registration(registration)
+{
+}
+
+ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications()
+{
+}
+
+ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptState* scriptState, ServiceWorkerRegistration& registration, const String& title, const NotificationOptions& options, ExceptionState& exceptionState)
{
ExecutionContext* executionContext = scriptState->getExecutionContext();
// If context object's active worker is null, reject promise with a TypeError exception.
- if (!serviceWorkerRegistration.active())
+ if (!registration.active())
return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No active registration available on the ServiceWorkerRegistration."));
// If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps.
@@ -67,17 +78,13 @@ ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
- WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, void>(resolver);
-
- SecurityOrigin* origin = executionContext->getSecurityOrigin();
- WebNotificationManager* notificationManager = Platform::current()->notificationManager();
- ASSERT(notificationManager);
+ OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromiseAdapter<void, void>(resolver));
+ ServiceWorkerRegistrationNotifications::from(executionContext, registration).prepareShow(data, callbacks.release());
- notificationManager->showPersistent(WebSecurityOrigin(origin), data, serviceWorkerRegistration.webRegistration(), callbacks);
return promise;
}
-ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptState* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const GetNotificationOptions& options)
+ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptState* scriptState, ServiceWorkerRegistration& registration, const GetNotificationOptions& options)
{
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
@@ -87,8 +94,56 @@ ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
WebNotificationManager* notificationManager = Platform::current()->notificationManager();
ASSERT(notificationManager);
- notificationManager->getNotifications(options.tag(), serviceWorkerRegistration.webRegistration(), callbacks);
+ notificationManager->getNotifications(options.tag(), registration.webRegistration(), callbacks);
return promise;
}
+void ServiceWorkerRegistrationNotifications::contextDestroyed()
+{
+ for (auto loader : m_loaders)
+ loader->stop();
+}
+
+DEFINE_TRACE(ServiceWorkerRegistrationNotifications)
+{
+ visitor->trace(m_registration);
+ visitor->trace(m_loaders);
+ Supplement<ServiceWorkerRegistration>::trace(visitor);
+ ContextLifecycleObserver::trace(visitor);
+}
+
+const char* ServiceWorkerRegistrationNotifications::supplementName()
+{
+ return "ServiceWorkerRegistrationNotifications";
+}
+
+ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications::from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration)
+{
+ ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWorkerRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(registration, supplementName()));
+ if (!supplement) {
+ supplement = new ServiceWorkerRegistrationNotifications(executionContext, &registration);
+ provideTo(registration, supplementName(), supplement);
+ }
+ return *supplement;
+}
+
+void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationData& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks)
+{
+ RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin();
+ NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<NotificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadResources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this), origin.release(), data, callbacks));
+ m_loaders.add(loader);
+ loader->start(getExecutionContext(), data);
+}
+
+void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<SecurityOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks, NotificationResourcesLoader* loader)
+{
+ DCHECK(m_loaders.contains(loader));
+
+ WebNotificationManager* notificationManager = Platform::current()->notificationManager();
+ DCHECK(notificationManager);
+
+ notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, loader->getResources(), m_registration->webRegistration(), callbacks.leakPtr());
+ m_loaders.remove(loader);
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698