OLD | NEW |
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/NotificationManager.h" | 14 #include "modules/notifications/NotificationManager.h" |
15 #include "modules/notifications/NotificationOptions.h" | 15 #include "modules/notifications/NotificationOptions.h" |
16 #include "modules/notifications/NotificationResourcesLoader.h" | 16 #include "modules/notifications/NotificationResourcesLoader.h" |
17 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 17 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
18 #include "platform/Histogram.h" | 18 #include "platform/Histogram.h" |
19 #include "platform/heap/Handle.h" | 19 #include "platform/heap/Handle.h" |
20 #include "public/platform/Platform.h" | 20 #include "public/platform/Platform.h" |
21 #include "public/platform/WebSecurityOrigin.h" | 21 #include "public/platform/WebSecurityOrigin.h" |
22 #include "public/platform/modules/notifications/WebNotificationData.h" | 22 #include "public/platform/modules/notifications/WebNotificationData.h" |
23 #include "wtf/Assertions.h" | 23 #include "wtf/Assertions.h" |
24 #include "wtf/PtrUtil.h" | |
25 #include "wtf/RefPtr.h" | 24 #include "wtf/RefPtr.h" |
26 #include <memory> | |
27 | 25 |
28 namespace blink { | 26 namespace blink { |
29 namespace { | 27 namespace { |
30 | 28 |
31 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the | 29 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the |
32 // getNotifications() promise with a HeapVector owning Notifications. | 30 // getNotifications() promise with a HeapVector owning Notifications. |
33 class NotificationArray { | 31 class NotificationArray { |
34 public: | 32 public: |
35 using WebType = const WebVector<WebPersistentNotificationInfo>&; | 33 using WebType = const WebVector<WebPersistentNotificationInfo>&; |
36 | 34 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 if (exceptionState.hadException()) | 69 if (exceptionState.hadException()) |
72 return exceptionState.reject(scriptState); | 70 return exceptionState.reject(scriptState); |
73 | 71 |
74 // Log number of actions developer provided in linear histogram: 0 -> underf
low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. | 72 // 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)); | 73 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto
gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount"
, 17)); |
76 notificationCountHistogram.count(options.actions().size()); | 74 notificationCountHistogram.count(options.actions().size()); |
77 | 75 |
78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 76 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
79 ScriptPromise promise = resolver->promise(); | 77 ScriptPromise promise = resolver->promise(); |
80 | 78 |
81 std::unique_ptr<WebNotificationShowCallbacks> callbacks = wrapUnique(new Cal
lbackPromiseAdapter<void, void>(resolver)); | 79 OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromis
eAdapter<void, void>(resolver)); |
82 ServiceWorkerRegistrationNotifications::from(executionContext, registration)
.prepareShow(data, std::move(callbacks)); | 80 ServiceWorkerRegistrationNotifications::from(executionContext, registration)
.prepareShow(data, std::move(callbacks)); |
83 | 81 |
84 return promise; | 82 return promise; |
85 } | 83 } |
86 | 84 |
87 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO
ptions& options) | 85 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta
te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO
ptions& options) |
88 { | 86 { |
89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 87 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
90 ScriptPromise promise = resolver->promise(); | 88 ScriptPromise promise = resolver->promise(); |
91 | 89 |
(...skipping 28 matching lines...) Expand all Loading... |
120 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications::
from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration
) | 118 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications::
from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration
) |
121 { | 119 { |
122 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork
erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist
ration, supplementName())); | 120 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork
erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist
ration, supplementName())); |
123 if (!supplement) { | 121 if (!supplement) { |
124 supplement = new ServiceWorkerRegistrationNotifications(executionContext
, ®istration); | 122 supplement = new ServiceWorkerRegistrationNotifications(executionContext
, ®istration); |
125 provideTo(registration, supplementName(), supplement); | 123 provideTo(registration, supplementName(), supplement); |
126 } | 124 } |
127 return *supplement; | 125 return *supplement; |
128 } | 126 } |
129 | 127 |
130 void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationDa
ta& data, std::unique_ptr<WebNotificationShowCallbacks> callbacks) | 128 void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationDa
ta& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks) |
131 { | 129 { |
132 RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin(); | 130 RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin(); |
133 NotificationResourcesLoader* loader = new NotificationResourcesLoader(WTF::b
ind<NotificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLo
adResources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(t
his), origin.release(), data, passed(std::move(callbacks)))); | 131 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N
otificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadRes
ources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this),
origin.release(), data, passed(std::move(callbacks)))); |
134 m_loaders.add(loader); | 132 m_loaders.add(loader); |
135 loader->start(getExecutionContext(), data); | 133 loader->start(getExecutionContext(), data); |
136 } | 134 } |
137 | 135 |
138 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit
yOrigin> origin, const WebNotificationData& data, std::unique_ptr<WebNotificatio
nShowCallbacks> callbacks, NotificationResourcesLoader* loader) | 136 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit
yOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShow
Callbacks> callbacks, NotificationResourcesLoader* loader) |
139 { | 137 { |
140 DCHECK(m_loaders.contains(loader)); | 138 DCHECK(m_loaders.contains(loader)); |
141 | 139 |
142 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); | 140 WebNotificationManager* notificationManager = Platform::current()->notificat
ionManager(); |
143 DCHECK(notificationManager); | 141 DCHECK(notificationManager); |
144 | 142 |
145 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l
oader->getResources(), m_registration->webRegistration(), callbacks.release()); | 143 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l
oader->getResources(), m_registration->webRegistration(), callbacks.leakPtr()); |
146 m_loaders.remove(loader); | 144 m_loaders.remove(loader); |
147 } | 145 } |
148 | 146 |
149 } // namespace blink | 147 } // namespace blink |
OLD | NEW |