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

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

Issue 1016843002: Implement the ServiceWorkerRegistration.getNotifications() API. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 9 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 | Annotate | Revision Log
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 "config.h" 5 #include "config.h"
6 #include "modules/notifications/ServiceWorkerRegistrationNotifications.h" 6 #include "modules/notifications/ServiceWorkerRegistrationNotifications.h"
7 7
8 #include "bindings/core/v8/CallbackPromiseAdapter.h" 8 #include "bindings/core/v8/CallbackPromiseAdapter.h"
9 #include "bindings/core/v8/ExceptionState.h" 9 #include "bindings/core/v8/ExceptionState.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h" 10 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "bindings/core/v8/SerializedScriptValue.h" 11 #include "bindings/core/v8/SerializedScriptValue.h"
12 #include "bindings/core/v8/SerializedScriptValueFactory.h" 12 #include "bindings/core/v8/SerializedScriptValueFactory.h"
13 #include "bindings/core/v8/V8ThrowException.h" 13 #include "bindings/core/v8/V8ThrowException.h"
14 #include "core/dom/DOMException.h" 14 #include "core/dom/DOMException.h"
15 #include "core/dom/ExceptionCode.h" 15 #include "core/dom/ExceptionCode.h"
16 #include "core/dom/ExecutionContext.h" 16 #include "core/dom/ExecutionContext.h"
17 #include "modules/notifications/GetNotificationOptions.h"
17 #include "modules/notifications/Notification.h" 18 #include "modules/notifications/Notification.h"
18 #include "modules/notifications/NotificationOptions.h" 19 #include "modules/notifications/NotificationOptions.h"
19 #include "platform/weborigin/KURL.h" 20 #include "platform/weborigin/KURL.h"
20 #include "public/platform/Platform.h" 21 #include "public/platform/Platform.h"
21 #include "public/platform/WebSerializedOrigin.h" 22 #include "public/platform/WebSerializedOrigin.h"
22 #include "public/platform/modules/notifications/WebNotificationData.h" 23 #include "public/platform/modules/notifications/WebNotificationData.h"
23 #include "public/platform/modules/notifications/WebNotificationManager.h" 24 #include "public/platform/modules/notifications/WebNotificationManager.h"
24 25
25 namespace blink { 26 namespace blink {
27 namespace {
28
29 // Allows using a CallbackPromiseAdapter with a WebVector to resolve the
30 // getNotifications() promise with a HeapVector owning Notifications.
31 class NotificationArray {
32 public:
33 using WebType = WebVector<WebPersistentNotificationInfo>;
34
35 static HeapVector<Member<Notification>> take(ScriptPromiseResolver* resolver , WebType* notificationInfosRaw)
36 {
37 OwnPtr<WebType> notificationInfos = adoptPtr(notificationInfosRaw);
38 HeapVector<Member<Notification>> notifications;
39
40 for (size_t i = 0; i < notificationInfos->size(); ++i) {
41 const WebPersistentNotificationInfo& notificationInfo = (*notificati onInfos)[i];
johnme 2015/03/17 19:39:35 Nit - how about: for (const WebPersistentNotifica
Peter Beverloo 2015/03/19 15:43:07 I waited and applied it :-). Thanks!
42 notifications.append(Notification::create(resolver->executionContext (), notificationInfo.persistentNotificationId, notificationInfo.data));
43 }
44
45 return notifications;
46 }
47
48 static void dispose(WebType* notificationInfosRaw)
49 {
50 delete notificationInfosRaw;
haraken 2015/03/18 01:32:54 I don't fully understand why we need to take a raw
Peter Beverloo 2015/03/19 15:43:07 In some cases the CallbackPromiseAdapter is used t
51 }
52
53 private:
54 NotificationArray() = delete;
55 };
56
57 } // namespace
26 58
27 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str ing& title, const NotificationOptions& options, ExceptionState& exceptionState) 59 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str ing& title, const NotificationOptions& options, ExceptionState& exceptionState)
28 { 60 {
29 ExecutionContext* executionContext = scriptState->executionContext(); 61 ExecutionContext* executionContext = scriptState->executionContext();
30 62
31 // If context object's active worker is null, reject promise with a TypeErro r exception. 63 // If context object's active worker is null, reject promise with a TypeErro r exception.
32 if (!serviceWorkerRegistration.active()) 64 if (!serviceWorkerRegistration.active())
33 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration.")); 65 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "No active registration available on the ServiceWork erRegistration."));
34 66
35 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. 67 // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps.
(...skipping 28 matching lines...) Expand all
64 SecurityOrigin* origin = executionContext->securityOrigin(); 96 SecurityOrigin* origin = executionContext->securityOrigin();
65 ASSERT(origin); 97 ASSERT(origin);
66 98
67 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 99 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
68 ASSERT(notificationManager); 100 ASSERT(notificationManager);
69 101
70 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati on, serviceWorkerRegistration.webRegistration(), callbacks); 102 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati on, serviceWorkerRegistration.webRegistration(), callbacks);
71 return promise; 103 return promise;
72 } 104 }
73 105
106 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get NotificationOptions& options)
107 {
108 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
109 ScriptPromise promise = resolver->promise();
110
111 SecurityOrigin* origin = scriptState->executionContext()->securityOrigin();
112 ASSERT(origin);
113
114 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver);
115
116 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
117 ASSERT(notificationManager);
118
119 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati on.webRegistration(), callbacks);
120 return promise;
121 }
122
74 } // namespace blink 123 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698