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

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: update the webexposed test 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 (const WebPersistentNotificationInfo& notificationInfo : *notificati onInfos)
41 notifications.append(Notification::create(resolver->executionContext (), notificationInfo.persistentNotificationId, notificationInfo.data));
42
43 return notifications;
44 }
45
46 static void dispose(WebType* notificationInfosRaw)
47 {
48 delete notificationInfosRaw;
49 }
50
51 private:
52 NotificationArray() = delete;
53 };
54
55 } // namespace
26 56
27 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str ing& title, const NotificationOptions& options, ExceptionState& exceptionState) 57 ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Str ing& title, const NotificationOptions& options, ExceptionState& exceptionState)
28 { 58 {
29 ExecutionContext* executionContext = scriptState->executionContext(); 59 ExecutionContext* executionContext = scriptState->executionContext();
30 60
31 // 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.
32 if (!serviceWorkerRegistration.active()) 62 if (!serviceWorkerRegistration.active())
33 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."));
34 64
35 // 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.
(...skipping 28 matching lines...) Expand all
64 SecurityOrigin* origin = executionContext->securityOrigin(); 94 SecurityOrigin* origin = executionContext->securityOrigin();
65 ASSERT(origin); 95 ASSERT(origin);
66 96
67 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager(); 97 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
68 ASSERT(notificationManager); 98 ASSERT(notificationManager);
69 99
70 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati on, serviceWorkerRegistration.webRegistration(), callbacks); 100 notificationManager->showPersistent(WebSerializedOrigin(*origin), notificati on, serviceWorkerRegistration.webRegistration(), callbacks);
71 return promise; 101 return promise;
72 } 102 }
73 103
104 ScriptPromise ServiceWorkerRegistrationNotifications::getNotifications(ScriptSta te* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const Get NotificationOptions& options)
105 {
106 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
107 ScriptPromise promise = resolver->promise();
108
109 WebNotificationGetCallbacks* callbacks = new CallbackPromiseAdapter<Notifica tionArray, void>(resolver);
110
111 WebNotificationManager* notificationManager = Platform::current()->notificat ionManager();
112 ASSERT(notificationManager);
113
114 notificationManager->getNotifications(options.tag(), serviceWorkerRegistrati on.webRegistration(), callbacks);
115 return promise;
116 }
117
74 } // namespace blink 118 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698