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 "config.h" | 5 #include "config.h" |
6 #include "modules/push_messaging/PushManager.h" | 6 #include "modules/push_messaging/PushManager.h" |
7 | 7 |
8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
11 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
12 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
13 #include "core/dom/Document.h" | 13 #include "core/dom/Document.h" |
14 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
15 #include "core/dom/ExecutionContext.h" | 15 #include "core/dom/ExecutionContext.h" |
16 #include "modules/push_messaging/PushController.h" | 16 #include "modules/push_messaging/PushController.h" |
17 #include "modules/push_messaging/PushError.h" | 17 #include "modules/push_messaging/PushError.h" |
18 #include "modules/push_messaging/PushPermissionStatusCallbacks.h" | 18 #include "modules/push_messaging/PushPermissionStatusCallbacks.h" |
19 #include "modules/push_messaging/PushRegistration.h" | 19 #include "modules/push_messaging/PushSubscription.h" |
20 #include "modules/push_messaging/PushRegistrationCallbacks.h" | 20 #include "modules/push_messaging/PushSubscriptionCallbacks.h" |
21 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 21 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
22 #include "public/platform/Platform.h" | 22 #include "public/platform/Platform.h" |
23 #include "public/platform/WebPushClient.h" | 23 #include "public/platform/WebPushClient.h" |
24 #include "public/platform/WebPushProvider.h" | 24 #include "public/platform/WebPushProvider.h" |
25 #include "wtf/RefPtr.h" | 25 #include "wtf/RefPtr.h" |
26 | 26 |
27 namespace blink { | 27 namespace blink { |
28 namespace { | 28 namespace { |
29 | 29 |
30 WebPushProvider* pushProvider() | 30 WebPushProvider* pushProvider() |
31 { | 31 { |
32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); | 32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); |
33 ASSERT(webPushProvider); | 33 ASSERT(webPushProvider); |
34 return webPushProvider; | 34 return webPushProvider; |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 PushManager::PushManager(ServiceWorkerRegistration* registration) | 39 PushManager::PushManager(ServiceWorkerRegistration* registration) |
40 : m_registration(registration) | 40 : m_registration(registration) |
41 { | 41 { |
42 ASSERT(registration); | 42 ASSERT(registration); |
43 } | 43 } |
44 | 44 |
45 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) | 45 ScriptPromise PushManager::subscribe(ScriptState* scriptState) |
46 { | 46 { |
47 if (!m_registration->active()) | 47 if (!m_registration->active()) |
48 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); | 48 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Subscription failed - no active Service Worker")); |
49 | 49 |
50 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 50 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
51 ScriptPromise promise = resolver->promise(); | 51 ScriptPromise promise = resolver->promise(); |
52 | 52 |
53 // The document context is the only reasonable context from which to ask the
user for permission | 53 // The document context is the only reasonable context from which to ask the
user for permission |
54 // to use the Push API. The embedder should persist the permission so that l
ater calls in | 54 // to use the Push API. The embedder should persist the permission so that l
ater calls in |
55 // different contexts can succeed. | 55 // different contexts can succeed. |
56 if (scriptState->executionContext()->isDocument()) { | 56 if (scriptState->executionContext()->isDocument()) { |
57 Document* document = toDocument(scriptState->executionContext()); | 57 Document* document = toDocument(scriptState->executionContext()); |
58 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 | 58 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 |
59 if (!document->domWindow() || !document->frame()) | 59 if (!document->domWindow() || !document->frame()) |
60 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | 60 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
61 PushController::clientFrom(document->frame()).registerPushMessaging(m_re
gistration->webRegistration(), new PushRegistrationCallbacks(resolver, m_registr
ation)); | 61 PushController::clientFrom(document->frame()).registerPushMessaging(m_re
gistration->webRegistration(), new PushSubscriptionCallbacks(resolver, m_registr
ation)); |
62 } else { | 62 } else { |
63 pushProvider()->registerPushMessaging(m_registration->webRegistration(),
new PushRegistrationCallbacks(resolver, m_registration)); | 63 pushProvider()->registerPushMessaging(m_registration->webRegistration(),
new PushSubscriptionCallbacks(resolver, m_registration)); |
64 } | 64 } |
65 | 65 |
66 return promise; | 66 return promise; |
67 } | 67 } |
68 | 68 |
69 ScriptPromise PushManager::getRegistration(ScriptState* scriptState) | 69 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) |
70 { | 70 { |
71 if (!m_registration->active()) | 71 if (!m_registration->active()) |
72 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); | 72 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Could not get subscription - no active Service Worker")); |
73 | 73 |
74 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 74 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
75 ScriptPromise promise = resolver->promise(); | 75 ScriptPromise promise = resolver->promise(); |
76 | 76 |
77 pushProvider()->getRegistration(m_registration->webRegistration(), new PushR
egistrationCallbacks(resolver, m_registration)); | 77 pushProvider()->getRegistration(m_registration->webRegistration(), new PushS
ubscriptionCallbacks(resolver, m_registration)); |
78 return promise; | 78 return promise; |
79 } | 79 } |
80 | 80 |
81 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) | 81 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) |
82 { | 82 { |
83 if (scriptState->executionContext()->isDocument()) { | 83 if (scriptState->executionContext()->isDocument()) { |
84 Document* document = toDocument(scriptState->executionContext()); | 84 Document* document = toDocument(scriptState->executionContext()); |
85 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 | 85 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 |
86 if (!document->domWindow() || !document->frame()) | 86 if (!document->domWindow() || !document->frame()) |
87 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | 87 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
88 } | 88 } |
89 | 89 |
90 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 90 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
91 ScriptPromise promise = resolver->promise(); | 91 ScriptPromise promise = resolver->promise(); |
92 pushProvider()->getPermissionStatus(m_registration->webRegistration(), new P
ushPermissionStatusCallbacks(resolver)); | 92 pushProvider()->getPermissionStatus(m_registration->webRegistration(), new P
ushPermissionStatusCallbacks(resolver)); |
93 return promise; | 93 return promise; |
94 } | 94 } |
95 | 95 |
96 void PushManager::trace(Visitor* visitor) | 96 void PushManager::trace(Visitor* visitor) |
97 { | 97 { |
98 visitor->trace(m_registration); | 98 visitor->trace(m_registration); |
99 } | 99 } |
100 | 100 |
101 } // namespace blink | 101 } // namespace blink |
OLD | NEW |