| 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 "core/frame/LocalDOMWindow.h" | |
| 17 #include "modules/push_messaging/PushController.h" | 16 #include "modules/push_messaging/PushController.h" |
| 18 #include "modules/push_messaging/PushError.h" | 17 #include "modules/push_messaging/PushError.h" |
| 19 #include "modules/push_messaging/PushPermissionStatusCallback.h" | 18 #include "modules/push_messaging/PushPermissionStatusCallback.h" |
| 20 #include "modules/push_messaging/PushRegistration.h" | 19 #include "modules/push_messaging/PushRegistration.h" |
| 21 #include "modules/serviceworkers/NavigatorServiceWorker.h" | 20 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 22 #include "modules/serviceworkers/ServiceWorkerContainer.h" | 21 #include "public/platform/Platform.h" |
| 23 #include "public/platform/WebPushClient.h" | 22 #include "public/platform/WebPushClient.h" |
| 23 #include "public/platform/WebPushProvider.h" |
| 24 #include "wtf/RefPtr.h" | 24 #include "wtf/RefPtr.h" |
| 25 | 25 |
| 26 namespace blink { | 26 namespace blink { |
| 27 namespace { |
| 27 | 28 |
| 28 PushManager::PushManager() | 29 WebPushProvider* pushProvider() |
| 29 { | 30 { |
| 31 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); |
| 32 ASSERT(webPushProvider); |
| 33 return webPushProvider; |
| 30 } | 34 } |
| 31 | 35 |
| 32 // FIXME: This call should be available from workers which will not have a Docum
ent object available. | 36 } // namespace |
| 33 // See crbug.com/389194 | 37 |
| 38 PushManager::PushManager(ServiceWorkerRegistration* registration) |
| 39 : m_registration(registration) |
| 40 { |
| 41 ASSERT(registration); |
| 42 } |
| 43 |
| 34 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) | 44 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) |
| 35 { | 45 { |
| 36 ASSERT(scriptState->executionContext()->isDocument()); | 46 if (!m_registration->active()) |
| 37 | 47 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); |
| 38 Document* document = toDocument(scriptState->executionContext()); | |
| 39 if (!document->domWindow() || !document->frame()) | |
| 40 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Document is detached from window.")); | |
| 41 | |
| 42 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se
rviceWorker(*document->domWindow()->navigator())->provider(); | |
| 43 if (!serviceWorkerProvider) | |
| 44 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "No Service Worker installed for this document.")); | |
| 45 | |
| 46 WebPushClient* client = PushController::clientFrom(document->frame()); | |
| 47 ASSERT(client); | |
| 48 | 48 |
| 49 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); | 49 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 50 ScriptPromise promise = resolver->promise(); | 50 ScriptPromise promise = resolver->promise(); |
| 51 | 51 |
| 52 client->registerPushMessaging(new CallbackPromiseAdapter<PushRegistration, P
ushError>(resolver), serviceWorkerProvider); | 52 // The document context is the only reasonable context from which to ask the
user for permission |
| 53 // to use the Push API. The embedder should persist the permission so that l
ater calls in |
| 54 // different contexts can succeed. |
| 55 if (scriptState->executionContext()->isDocument()) { |
| 56 Document* document = toDocument(scriptState->executionContext()); |
| 57 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 |
| 58 if (!document->domWindow() || !document->frame()) |
| 59 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
| 60 PushController::clientFrom(document->frame()).registerPushMessaging(m_re
gistration->webRegistration(), new CallbackPromiseAdapter<PushRegistration, Push
Error>(resolver)); |
| 61 } else { |
| 62 pushProvider()->registerPushMessaging(m_registration->webRegistration(),
new CallbackPromiseAdapter<PushRegistration, PushError>(resolver)); |
| 63 } |
| 53 | 64 |
| 54 return promise; | 65 return promise; |
| 55 } | 66 } |
| 56 | 67 |
| 57 // FIXME: This call should be available from workers which will not have a Docum
ent object available. | |
| 58 // See crbug.com/389194 | |
| 59 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) | 68 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) |
| 60 { | 69 { |
| 61 ASSERT(scriptState->executionContext()->isDocument()); | 70 if (scriptState->executionContext()->isDocument()) { |
| 62 | 71 Document* document = toDocument(scriptState->executionContext()); |
| 63 Document* document = toDocument(scriptState->executionContext()); | 72 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 |
| 64 if (!document->domWindow() || !document->frame()) | 73 if (!document->domWindow() || !document->frame()) |
| 65 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Document is detached from window.")); | 74 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
| 66 blink::WebPushClient* client = PushController::clientFrom(document->frame())
; | 75 } |
| 67 ASSERT(client); | |
| 68 | |
| 69 // The currently implemented specification does not require a Service Worker
to be present for the | |
| 70 // hasPermission() call to work, but it will become a requirement soon. | |
| 71 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se
rviceWorker(*document->domWindow()->navigator())->provider(); | |
| 72 if (!serviceWorkerProvider) | |
| 73 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "No Service Worker installed for this document.")); | |
| 74 | 76 |
| 75 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); | 77 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 76 | |
| 77 ScriptPromise promise = resolver->promise(); | 78 ScriptPromise promise = resolver->promise(); |
| 78 client->getPermissionStatus(new PushPermissionStatusCallback(resolver), serv
iceWorkerProvider); | 79 pushProvider()->getPermissionStatus(m_registration->webRegistration(), new P
ushPermissionStatusCallback(resolver)); |
| 79 return promise; | 80 return promise; |
| 80 } | 81 } |
| 81 | 82 |
| 83 void PushManager::trace(Visitor* visitor) |
| 84 { |
| 85 visitor->trace(m_registration); |
| 86 } |
| 87 |
| 82 } // namespace blink | 88 } // namespace blink |
| OLD | NEW |