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

Side by Side Diff: third_party/WebKit/Source/modules/notifications/ServiceWorkerRegistrationNotifications.cpp

Issue 1904163002: Move Web Notifications to use Mojo Base URL: https://chromium.googlesource.com/chromium/src.git@skbitmap-blink
Patch Set: it works \o/ Created 4 years, 7 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"
8 #include "bindings/core/v8/ExceptionState.h" 7 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
10 #include "core/dom/ExecutionContext.h" 9 #include "core/dom/ExecutionContext.h"
11 #include "modules/notifications/GetNotificationOptions.h" 10 #include "modules/notifications/GetNotificationOptions.h"
12 #include "modules/notifications/Notification.h" 11 #include "modules/notifications/Notification.h"
13 #include "modules/notifications/NotificationData.h" 12 #include "modules/notifications/NotificationData.h"
13 #include "modules/notifications/NotificationManager.h"
14 #include "modules/notifications/NotificationOptions.h" 14 #include "modules/notifications/NotificationOptions.h"
15 #include "modules/notifications/NotificationResourcesLoader.h" 15 #include "modules/notifications/NotificationResourcesLoader.h"
16 #include "modules/serviceworkers/ServiceWorkerRegistration.h" 16 #include "modules/serviceworkers/ServiceWorkerRegistration.h"
17 #include "platform/Histogram.h" 17 #include "platform/Histogram.h"
18 #include "platform/heap/Handle.h" 18 #include "platform/heap/Handle.h"
19 #include "public/platform/Platform.h" 19 #include "platform/mojo/MojoHelper.h"
20 #include "public/platform/WebSecurityOrigin.h" 20 #include "public/platform/modules/notifications/notification_resources.mojom-bli nk.h"
21 #include "public/platform/modules/notifications/WebNotificationData.h" 21 #include "wtf/Functional.h"
22 #include "wtf/RefPtr.h"
23 22
24 namespace blink { 23 namespace blink {
25 namespace { 24 namespace {
26 25
27 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the 26 // Object responsible for handling the result status of showing a persistent
28 // getNotifications() promise with a HeapVector owning Notifications. 27 // notification using the embedder.
29 class NotificationArray { 28 class ShowNotificationResponseHandler {
29 WTF_MAKE_NONCOPYABLE(ShowNotificationResponseHandler);
30 public: 30 public:
31 using WebType = const WebVector<WebPersistentNotificationInfo>&; 31 explicit ShowNotificationResponseHandler(ScriptPromiseResolver* resolver)
32 : m_resolver(resolver) {}
32 33
33 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver , const WebVector<WebPersistentNotificationInfo>& notificationInfos) 34 void onResponse(mojom::blink::NotificationDisplayResult result)
34 { 35 {
35 HeapVector<Member<Notification>> notifications; 36 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 } 37 }
41 38
42 private: 39 private:
43 NotificationArray() = delete; 40 Persistent<ScriptPromiseResolver> m_resolver;
41 };
42
43 // Object responsible for handling the result of getting the array of persistent
44 // notifications that are showing for an origin.
45 class GetNotificationsResponseHandler {
46 WTF_MAKE_NONCOPYABLE(GetNotificationsResponseHandler);
47 public:
48 explicit GetNotificationsResponseHandler(ScriptPromiseResolver* resolver)
49 : m_resolver(resolver) {}
50
51 void onResponse(mojo::WTFArray<mojom::blink::NotificationPtr> notifications)
52 {
53 HeapVector<Member<Notification>> heapNotifications;
54 for (size_t i = 0; i < notifications.size(); ++i)
55 heapNotifications.append(Notification::create(m_resolver->getExecuti onContext(), std::move(notifications[i]), true /* showing */));
56
57 m_resolver->resolve(heapNotifications);
58 }
59
60 private:
61 Persistent<ScriptPromiseResolver> m_resolver;
44 }; 62 };
45 63
46 } // namespace 64 } // namespace
47 65
48 ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(E xecutionContext* executionContext, ServiceWorkerRegistration* registration) 66 ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(E xecutionContext* executionContext, ServiceWorkerRegistration* registration)
49 : ContextLifecycleObserver(executionContext), m_registration(registration) 67 : ContextLifecycleObserver(executionContext)
68 , m_registration(registration)
50 { 69 {
51 } 70 }
52 71
53 ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications( ) 72 ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications( )
54 { 73 {
55 } 74 }
56 75
57 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const String& title, c onst NotificationOptions& options, ExceptionState& exceptionState) 76 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const String& title, c onst NotificationOptions& options, ExceptionState& exceptionState)
58 { 77 {
59 ExecutionContext* executionContext = scriptState->getExecutionContext(); 78 ExecutionContext* executionContext = scriptState->getExecutionContext();
60 79
61 // If context object's active worker is null, reject promise with a TypeErro r exception. 80 // If context object's active worker is null, reject promise with a TypeErro r exception.
62 if (!registration.active()) 81 if (!registration.active())
63 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration.")); 82 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration."));
64 83
84 NotificationManager* notificationManager = NotificationManager::from(executi onContext);
85 DCHECK(notificationManager);
86
65 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. 87 // 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) 88 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.")); 89 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No notification permission has been granted for thi s origin."));
68 90
69 // Validate the developer-provided values to get a WebNotificationData objec t. 91 // Validate the developer-provided values to get a Mojo Notification object.
70 WebNotificationData data = createWebNotificationData(executionContext, title , options, exceptionState); 92 mojom::blink::NotificationPtr data = createNotificationData(executionContext , title, options, exceptionState);
71 if (exceptionState.hadException()) 93 if (exceptionState.hadException())
72 return exceptionState.reject(scriptState); 94 return exceptionState.reject(scriptState);
73 95
74 // Log number of actions developer provided in linear histogram: 0 -> underf low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. 96 // 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)); 97 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount" , 17));
76 notificationCountHistogram.count(options.actions().size()); 98 notificationCountHistogram.count(options.actions().size());
77 99
78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 100 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
79 ScriptPromise promise = resolver->promise(); 101 ScriptPromise promise = resolver->promise();
80 102
81 OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromis eAdapter<void, void>(resolver)); 103 OwnPtr<ShowNotificationResponseHandler> responseHandler = adoptPtr(new ShowN otificationResponseHandler(resolver));
82 ServiceWorkerRegistrationNotifications::from(executionContext, registration) .prepareShow(data, callbacks.release()); 104 mojom::blink::NotificationService::DisplayPersistentCallback callback =
105 createBaseCallback(bind<mojom::blink::NotificationDisplayResult>(&ShowNo tificationResponseHandler::onResponse, responseHandler.release()));
83 106
107 ServiceWorkerRegistrationNotifications::from(executionContext, registration) .prepareShow(std::move(data), callback);
84 return promise; 108 return promise;
85 } 109 }
86 110
87 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO ptions& options) 111 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO ptions& options)
88 { 112 {
89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
90 ScriptPromise promise = resolver->promise(); 114 ScriptPromise promise = resolver->promise();
91 115
92 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver); 116 OwnPtr<GetNotificationsResponseHandler> responseHandler = adoptPtr(new GetNo tificationsResponseHandler(resolver));
117 mojom::blink::NotificationService::GetNotificationsCallback callback =
118 createBaseCallback(bind<mojo::WTFArray<mojom::blink::NotificationPtr>>(& GetNotificationsResponseHandler::onResponse, responseHandler.release()));
93 119
94 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 120 int64_t serviceWorkerRegistrationId = registration.webRegistration()->regist rationId();
95 ASSERT(notificationManager);
96 121
97 notificationManager->getNotifications(options.tag(), registration.webRegistr ation(), callbacks); 122 NotificationManager::from(scriptState->getExecutionContext())->getNotificati ons(serviceWorkerRegistrationId, options.tag(), callback);
98 return promise; 123 return promise;
99 } 124 }
100 125
101 void ServiceWorkerRegistrationNotifications::contextDestroyed() 126 void ServiceWorkerRegistrationNotifications::contextDestroyed()
102 { 127 {
103 for (auto loader : m_loaders) 128 for (auto loader : m_loaders)
104 loader->stop(); 129 loader->stop();
105 } 130 }
106 131
107 DEFINE_TRACE(ServiceWorkerRegistrationNotifications) 132 DEFINE_TRACE(ServiceWorkerRegistrationNotifications)
(...skipping 12 matching lines...) Expand all
120 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications:: from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration ) 145 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications:: from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration )
121 { 146 {
122 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist ration, supplementName())); 147 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist ration, supplementName()));
123 if (!supplement) { 148 if (!supplement) {
124 supplement = new ServiceWorkerRegistrationNotifications(executionContext , &registration); 149 supplement = new ServiceWorkerRegistrationNotifications(executionContext , &registration);
125 provideTo(registration, supplementName(), supplement); 150 provideTo(registration, supplementName(), supplement);
126 } 151 }
127 return *supplement; 152 return *supplement;
128 } 153 }
129 154
130 void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationDa ta& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks) 155 void ServiceWorkerRegistrationNotifications::prepareShow(mojom::blink::Notificat ionPtr notification, const mojom::blink::NotificationService::DisplayPersistentC allback& callback)
131 { 156 {
132 RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin(); 157 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N otificationResourcesLoader*, mojom::blink::NotificationPtr>(&ServiceWorkerRegist rationNotifications::didLoadResources, WeakPersistentThisPointer<ServiceWorkerRe gistrationNotifications>(this), callback));
133 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N otificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadRes ources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this), origin.release(), data, passed(std::move(callbacks)))); 158
134 m_loaders.add(loader); 159 m_loaders.add(loader);
135 loader->start(getExecutionContext(), data); 160
161 loader->start(getExecutionContext(), std::move(notification));
136 } 162 }
137 163
138 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit yOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShow Callbacks> callbacks, NotificationResourcesLoader* loader) 164 void ServiceWorkerRegistrationNotifications::didLoadResources(const mojom::blink ::NotificationService::DisplayPersistentCallback& callback, NotificationResource sLoader* loader, mojom::blink::NotificationPtr notification)
139 { 165 {
140 DCHECK(m_loaders.contains(loader)); 166 DCHECK(m_loaders.contains(loader));
141 167
142 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 168 int64_t serviceWorkerRegistrationId = m_registration->webRegistration()->reg istrationId();
143 DCHECK(notificationManager);
144 169
145 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l oader->getResources(), m_registration->webRegistration(), callbacks.leakPtr()); 170 NotificationManager::from(getExecutionContext())->displayPersistent(serviceW orkerRegistrationId, std::move(notification), loader->getResources(), callback);
171
146 m_loaders.remove(loader); 172 m_loaders.remove(loader);
147 } 173 }
148 174
149 } // namespace blink 175 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698