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

Side by Side 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: Rebase. Fix ASAN. Cleanups. 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 unified diff | Download patch
OLDNEW
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/NotificationOptions.h" 14 #include "modules/notifications/NotificationOptions.h"
15 #include "modules/notifications/NotificationResourcesLoader.h"
15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" 16 #include "modules/serviceworkers/ServiceWorkerRegistration.h"
16 #include "platform/Histogram.h" 17 #include "platform/Histogram.h"
18 #include "platform/heap/Handle.h"
17 #include "public/platform/Platform.h" 19 #include "public/platform/Platform.h"
18 #include "public/platform/WebSecurityOrigin.h" 20 #include "public/platform/WebSecurityOrigin.h"
19 #include "public/platform/modules/notifications/WebNotificationData.h" 21 #include "public/platform/modules/notifications/WebNotificationData.h"
20 #include "public/platform/modules/notifications/WebNotificationManager.h" 22 #include "wtf/RefPtr.h"
21 23
22 namespace blink { 24 namespace blink {
23 namespace { 25 namespace {
24 26
25 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the 27 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the
26 // getNotifications() promise with a HeapVector owning Notifications. 28 // getNotifications() promise with a HeapVector owning Notifications.
27 class NotificationArray { 29 class NotificationArray {
28 public: 30 public:
29 using WebType = const WebVector<WebPersistentNotificationInfo>&; 31 using WebType = const WebVector<WebPersistentNotificationInfo>&;
30 32
31 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver , const WebVector<WebPersistentNotificationInfo>& notificationInfos) 33 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver , const WebVector<WebPersistentNotificationInfo>& notificationInfos)
32 { 34 {
33 HeapVector<Member<Notification>> notifications; 35 HeapVector<Member<Notification>> notifications;
34 for (const WebPersistentNotificationInfo& notificationInfo : notificatio nInfos) 36 for (const WebPersistentNotificationInfo& notificationInfo : notificatio nInfos)
35 notifications.append(Notification::create(resolver->getExecutionCont ext(), notificationInfo.persistentId, notificationInfo.data, true /* showing */) ); 37 notifications.append(Notification::create(resolver->getExecutionCont ext(), notificationInfo.persistentId, notificationInfo.data, true /* showing */) );
36 38
37 return notifications; 39 return notifications;
38 } 40 }
39 41
40 private: 42 private:
41 NotificationArray() = delete; 43 NotificationArray() = delete;
42 }; 44 };
43 45
44 } // namespace 46 } // namespace
45 47
46 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str ing& title, const NotificationOptions& options, ExceptionState& exceptionState) 48 ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(E xecutionContext* executionContext, ServiceWorkerRegistration* registration) : Co ntextLifecycleObserver(executionContext), m_registration(registration)
49 {
50 }
51
52 ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications( )
53 {
54 }
55
56 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const String& title, c onst NotificationOptions& options, ExceptionState& exceptionState)
47 { 57 {
48 ExecutionContext* executionContext = scriptState->getExecutionContext(); 58 ExecutionContext* executionContext = scriptState->getExecutionContext();
49 59
50 // If context object's active worker is null, reject promise with a TypeErro r exception. 60 // If context object's active worker is null, reject promise with a TypeErro r exception.
51 if (!serviceWorkerRegistration.active()) 61 if (!registration.active())
52 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration.")); 62 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration."));
53 63
54 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. 64 // 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) 65 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.")); 66 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No notification permission has been granted for thi s origin."));
57 67
58 // Validate the developer-provided values to get a WebNotificationData objec t. 68 // Validate the developer-provided values to get a WebNotificationData objec t.
59 WebNotificationData data = createWebNotificationData(executionContext, title , options, exceptionState); 69 WebNotificationData data = createWebNotificationData(executionContext, title , options, exceptionState);
60 if (exceptionState.hadException()) 70 if (exceptionState.hadException())
61 return exceptionState.reject(scriptState); 71 return exceptionState.reject(scriptState);
62 72
63 // Log number of actions developer provided in linear histogram: 0 -> underf low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. 73 // 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)); 74 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount" , 17));
65 notificationCountHistogram.count(options.actions().size()); 75 notificationCountHistogram.count(options.actions().size());
66 76
67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
68 ScriptPromise promise = resolver->promise(); 78 ScriptPromise promise = resolver->promise();
69 79
70 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v oid>(resolver); 80 OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromis eAdapter<void, void>(resolver));
81 ServiceWorkerRegistrationNotifications::from(executionContext, registration) .prepareShow(data, callbacks.release());
71 82
72 SecurityOrigin* origin = executionContext->getSecurityOrigin();
73 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
74 ASSERT(notificationManager);
75
76 notificationManager->showPersistent(WebSecurityOrigin(origin), data, service WorkerRegistration.webRegistration(), callbacks);
77 return promise; 83 return promise;
78 } 84 }
79 85
80 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get NotificationOptions& options) 86 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO ptions& options)
81 { 87 {
82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 88 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
83 ScriptPromise promise = resolver->promise(); 89 ScriptPromise promise = resolver->promise();
84 90
85 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver); 91 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver);
86 92
87 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 93 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
88 ASSERT(notificationManager); 94 ASSERT(notificationManager);
89 95
90 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati on.webRegistration(), callbacks); 96 notificationManager->getNotifications(options.tag(), registration.webRegistr ation(), callbacks);
91 return promise; 97 return promise;
92 } 98 }
93 99
100 void ServiceWorkerRegistrationNotifications::contextDestroyed()
101 {
102 for (auto loader : m_loaders)
103 loader->stop();
104 }
105
106 DEFINE_TRACE(ServiceWorkerRegistrationNotifications)
107 {
108 visitor->trace(m_registration);
109 visitor->trace(m_loaders);
110 Supplement<ServiceWorkerRegistration>::trace(visitor);
111 ContextLifecycleObserver::trace(visitor);
112 }
113
114 const char* ServiceWorkerRegistrationNotifications::supplementName()
115 {
116 return "ServiceWorkerRegistrationNotifications";
117 }
118
119 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications:: from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration )
120 {
121 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist ration, supplementName()));
122 if (!supplement) {
123 supplement = new ServiceWorkerRegistrationNotifications(executionContext , &registration);
124 provideTo(registration, supplementName(), supplement);
125 }
126 return *supplement;
127 }
128
129 void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationDa ta& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks)
130 {
131 RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin();
132 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N otificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadRes ources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this), origin.release(), data, callbacks));
133 m_loaders.add(loader);
134 loader->start(getExecutionContext(), data);
135 }
136
137 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit yOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShow Callbacks> callbacks, NotificationResourcesLoader* loader)
138 {
139 DCHECK(m_loaders.contains(loader));
140
141 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
142 DCHECK(notificationManager);
143
144 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l oader->getResources(), m_registration->webRegistration(), callbacks.leakPtr());
145 m_loaders.remove(loader);
146 }
147
94 } // namespace blink 148 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698