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

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: 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)
49 : ContextLifecycleObserver(executionContext), 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(executionContext, registration) .prepareShow(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 void ServiceWorkerRegistrationNotifications::contextDestroyed()
102 {
103 for (auto loader : m_loaders)
104 loader->stop();
105 }
106
107 DEFINE_TRACE(ServiceWorkerRegistrationNotifications)
108 {
109 visitor->trace(m_registration);
110 visitor->trace(m_loaders);
111 Supplement<ServiceWorkerRegistration>::trace(visitor);
112 ContextLifecycleObserver::trace(visitor);
113 }
114
115 const char* ServiceWorkerRegistrationNotifications::supplementName()
116 {
117 return "ServiceWorkerRegistrationNotifications";
118 }
119
120 ServiceWorkerRegistrationNotifications& ServiceWorkerRegistrationNotifications:: from(ExecutionContext* executionContext, ServiceWorkerRegistration& registration )
121 {
122 ServiceWorkerRegistrationNotifications* supplement = static_cast<ServiceWork erRegistrationNotifications*>(Supplement<ServiceWorkerRegistration>::from(regist ration, supplementName()));
123 if (!supplement) {
124 supplement = new ServiceWorkerRegistrationNotifications(executionContext , &registration);
125 provideTo(registration, supplementName(), supplement);
126 }
127 return *supplement;
128 }
129
130 void ServiceWorkerRegistrationNotifications::prepareShow(const WebNotificationDa ta& data, PassOwnPtr<WebNotificationShowCallbacks> callbacks)
131 {
132 RefPtr<SecurityOrigin> origin = getExecutionContext()->getSecurityOrigin();
133 NotificationResourcesLoader* loader = new NotificationResourcesLoader(bind<N otificationResourcesLoader*>(&ServiceWorkerRegistrationNotifications::didLoadRes ources, WeakPersistentThisPointer<ServiceWorkerRegistrationNotifications>(this), origin.release(), data, callbacks));
134 m_loaders.add(loader);
135 loader->start(getExecutionContext(), data);
136 }
137
138 void ServiceWorkerRegistrationNotifications::didLoadResources(PassRefPtr<Securit yOrigin> origin, const WebNotificationData& data, PassOwnPtr<WebNotificationShow Callbacks> callbacks, NotificationResourcesLoader* loader)
139 {
140 DCHECK(m_loaders.contains(loader));
141
142 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
143 DCHECK(notificationManager);
144
145 notificationManager->showPersistent(WebSecurityOrigin(origin.get()), data, l oader->getResources(), m_registration->webRegistration(), callbacks.leakPtr());
146 m_loaders.remove(loader);
147 }
148
94 } // namespace blink 149 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698