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/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(S erviceWorkerRegistration* registration) |
49 : m_registration(registration) | |
50 { | |
51 } | |
52 | |
53 ServiceWorkerRegistrationNotifications::~ServiceWorkerRegistrationNotifications( ) | |
54 { | |
55 } | |
56 | |
57 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const String& title, c onst NotificationOptions& options, ExceptionState& exceptionState) | |
47 { | 58 { |
48 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 59 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
49 | 60 |
50 // If context object's active worker is null, reject promise with a TypeErro r exception. | 61 // If context object's active worker is null, reject promise with a TypeErro r exception. |
51 if (!serviceWorkerRegistration.active()) | 62 if (!registration.active()) |
52 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration.")); | 63 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration.")); |
53 | 64 |
54 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. | 65 // 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) | 66 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.")); | 67 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No notification permission has been granted for thi s origin.")); |
57 | 68 |
58 // Validate the developer-provided values to get a WebNotificationData objec t. | 69 // Validate the developer-provided values to get a WebNotificationData objec t. |
59 WebNotificationData data = createWebNotificationData(executionContext, title , options, exceptionState); | 70 WebNotificationData data = createWebNotificationData(executionContext, title , options, exceptionState); |
60 if (exceptionState.hadException()) | 71 if (exceptionState.hadException()) |
61 return exceptionState.reject(scriptState); | 72 return exceptionState.reject(scriptState); |
62 | 73 |
63 // Log number of actions developer provided in linear histogram: 0 -> underf low bucket, 1-16 -> distinct buckets, 17+ -> overflow bucket. | 74 // 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)); | 75 DEFINE_THREAD_SAFE_STATIC_LOCAL(EnumerationHistogram, notificationCountHisto gram, new EnumerationHistogram("Notifications.PersistentNotificationActionCount" , 17)); |
65 notificationCountHistogram.count(options.actions().size()); | 76 notificationCountHistogram.count(options.actions().size()); |
66 | 77 |
67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
68 ScriptPromise promise = resolver->promise(); | 79 ScriptPromise promise = resolver->promise(); |
69 | 80 |
70 WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, v oid>(resolver); | 81 OwnPtr<WebNotificationShowCallbacks> callbacks = adoptPtr(new CallbackPromis eAdapter<void, void>(resolver)); |
82 ServiceWorkerRegistrationNotifications::from(registration).prepareShow(execu tionContext, data, callbacks.release()); | |
71 | 83 |
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; | 84 return promise; |
78 } | 85 } |
79 | 86 |
80 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get NotificationOptions& options) | 87 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& registration, const GetNotificationO ptions& options) |
81 { | 88 { |
82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 89 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
83 ScriptPromise promise = resolver->promise(); | 90 ScriptPromise promise = resolver->promise(); |
84 | 91 |
85 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver); | 92 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver); |
86 | 93 |
87 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); | 94 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); |
88 ASSERT(notificationManager); | 95 ASSERT(notificationManager); |
89 | 96 |
90 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati on.webRegistration(), callbacks); | 97 notificationManager->getNotifications(options.tag(), registration.webRegistr ation(), callbacks); |
91 return promise; | 98 return promise; |
92 } | 99 } |
93 | 100 |
101 DEFINE_TRACE(ServiceWorkerRegistrationNotifications) | |
102 { | |
103 visitor->trace(m_registration); | |
104 visitor->trace(m_loaders); | |
105 Supplement<ServiceWorkerRegistration>::trace(visitor); | |
106 } | |
107 | |
108 const char* ServiceWorkerRegistrationNotifications::supplementName() | |
109 { | |
110 return "ServiceWorkerRegistrationNotifications"; | |
111 } | |
112 | |
113 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications:: from(ServiceWorkerRegistration& registration) | |
114 { | |
115 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist ration, supplementName())); | |
116 if (!supplement) { | |
117 supplement = new ServiceWorkerRegistrationNotifications(®istration); | |
118 provideTo(registration, supplementName(), supplement); | |
119 } | |
120 return *supplement; | |
121 } | |
122 | |
123 void ServiceWorkerRegistrationNotifications::prepareShow(ExecutionContext* execu tionContext, const WebNotificationData& data, PassOwnPtr<WebNotificationShowCall backs> callbacks) | |
124 { | |
125 RefPtr<SecurityOrigin> origin = executionContext->getSecurityOrigin(); | |
126 NotificationResourcesLoader* loader = new NotificationResourcesLoader( | |
127 executionContext, | |
128 bind<NotificationResourcesLoader*>( | |
129 &ServiceWorkerRegistrationNotifications::didLoadResources, | |
130 WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(th is), | |
haraken
2016/04/14 02:05:52
+hiroshige
Michael van Ouwerkerk
2016/04/14 13:42:12
So I think now that this should be ok. The problem
hiroshige
2016/04/14 14:49:22
FYI for haraken: WeakPersistentThisPointer<> is ju
| |
131 origin.release(), data, callbacks)); | |
132 m_loaders.add(loader); | |
133 loader->start(data); | |
134 } | |
135 | |
136 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit yOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShow Callbacks> callbacks, NotificationResourcesLoader* loader) | |
137 { | |
138 DCHECK(m_loaders.contains(loader)); | |
139 | |
140 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); | |
141 ASSERT(notificationManager); | |
Peter Beverloo
2016/04/13 18:32:28
ASSERT or DCHECK?
Michael van Ouwerkerk
2016/04/14 13:42:12
DCHECK then. I think the codebase-wide refactor is
| |
142 | |
143 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l oader->getResources(), m_registration->webRegistration(), callbacks.leakPtr()); | |
144 m_loaders.remove(loader); | |
145 } | |
146 | |
94 } // namespace blink | 147 } // namespace blink |
OLD | NEW |