Chromium Code Reviews| Index: Source/modules/push_messaging/PushManager.cpp |
| diff --git a/Source/modules/push_messaging/PushManager.cpp b/Source/modules/push_messaging/PushManager.cpp |
| index d84587e8e98484cc502c3c242aafdf41957e7328..449d859de592e313442bc2ac75f422f4225c15cf 100644 |
| --- a/Source/modules/push_messaging/PushManager.cpp |
| +++ b/Source/modules/push_messaging/PushManager.cpp |
| @@ -13,70 +13,79 @@ |
| #include "core/dom/Document.h" |
| #include "core/dom/ExceptionCode.h" |
| #include "core/dom/ExecutionContext.h" |
| -#include "core/frame/LocalDOMWindow.h" |
| #include "modules/push_messaging/PushController.h" |
| #include "modules/push_messaging/PushError.h" |
| #include "modules/push_messaging/PushPermissionStatusCallback.h" |
| #include "modules/push_messaging/PushRegistration.h" |
| -#include "modules/serviceworkers/NavigatorServiceWorker.h" |
| -#include "modules/serviceworkers/ServiceWorkerContainer.h" |
| +#include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| +#include "public/platform/Platform.h" |
| #include "public/platform/WebPushClient.h" |
| +#include "public/platform/WebPushProvider.h" |
| #include "wtf/RefPtr.h" |
| namespace blink { |
| +namespace { |
| -PushManager::PushManager() |
| + WebPushProvider* pushProvider() |
|
Peter Beverloo
2014/12/10 00:18:18
nit: no indentation.
Michael van Ouwerkerk
2014/12/10 15:13:06
Done.
|
| + { |
| + WebPushProvider* webPushProvider = Platform::current()->pushProvider(); |
| + ASSERT(webPushProvider); |
| + return webPushProvider; |
| + } |
| + |
| +} // namespace |
| + |
| +PushManager::PushManager(ServiceWorkerRegistration* registration) |
| + : m_registration(registration) |
| { |
| + ASSERT(registration); |
| } |
| -// FIXME: This call should be available from workers which will not have a Document object available. |
| -// See crbug.com/389194 |
| ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) |
| { |
| - ASSERT(scriptState->executionContext()->isDocument()); |
| - |
| - Document* document = toDocument(scriptState->executionContext()); |
| - if (!document->domWindow() || !document->frame()) |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Document is detached from window.")); |
| - |
| - WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::serviceWorker(*document->domWindow()->navigator())->provider(); |
| - if (!serviceWorkerProvider) |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "No Service Worker installed for this document.")); |
| - |
| - WebPushClient* client = PushController::clientFrom(document->frame()); |
| - ASSERT(client); |
| + if (!m_registration->active()) |
| + return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Registration failed - no Service Worker")); |
|
Peter Beverloo
2014/12/10 00:18:18
Please include a better error message here. The de
Michael van Ouwerkerk
2014/12/10 15:13:06
Done.
|
| RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - client->registerPushMessaging(new CallbackPromiseAdapter<PushRegistration, PushError>(resolver), serviceWorkerProvider); |
| + // The document context is the only reasonable context from which to ask the user for permission |
| + // to use the Push API. The embedder should persist the permission so that later calls in |
| + // different contexts can succeed. |
| + if (scriptState->executionContext()->isDocument()) { |
| + Document* document = toDocument(scriptState->executionContext()); |
| + // FIXME: add test coverage for this condition - https://crbug.com/440431 |
| + if (!document->domWindow() || !document->frame()) |
| + return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "Document is detached from window.")); |
| + |
| + WebPushClient* client = PushController::clientFrom(document->frame()); |
|
Peter Beverloo
2014/12/10 00:18:18
If we're asserting on every use anyway, we may as
Michael van Ouwerkerk
2014/12/10 15:13:06
Done.
|
| + ASSERT(client); |
| + client->registerPushMessaging(m_registration->webRegistration(), new CallbackPromiseAdapter<PushRegistration, PushError>(resolver)); |
| + } else { |
| + pushProvider()->registerPushMessaging(m_registration->webRegistration(), new CallbackPromiseAdapter<PushRegistration, PushError>(resolver)); |
| + } |
| return promise; |
| } |
| -// FIXME: This call should be available from workers which will not have a Document object available. |
| -// See crbug.com/389194 |
| ScriptPromise PushManager::hasPermission(ScriptState* scriptState) |
| { |
| - ASSERT(scriptState->executionContext()->isDocument()); |
| - |
| - Document* document = toDocument(scriptState->executionContext()); |
| - if (!document->domWindow() || !document->frame()) |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "Document is detached from window.")); |
| - blink::WebPushClient* client = PushController::clientFrom(document->frame()); |
| - ASSERT(client); |
| - |
| - // The currently implemented specification does not require a Service Worker to be present for the |
| - // hasPermission() call to work, but it will become a requirement soon. |
| - WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::serviceWorker(*document->domWindow()->navigator())->provider(); |
| - if (!serviceWorkerProvider) |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "No Service Worker installed for this document.")); |
| + if (scriptState->executionContext()->isDocument()) { |
| + Document* document = toDocument(scriptState->executionContext()); |
| + // FIXME: add test coverage for this condition - https://crbug.com/440431 |
| + if (!document->domWindow() || !document->frame()) |
| + return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "Document is detached from window.")); |
| + } |
| RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| - |
| ScriptPromise promise = resolver->promise(); |
| - client->getPermissionStatus(new PushPermissionStatusCallback(resolver), serviceWorkerProvider); |
| + pushProvider()->getPermissionStatus(m_registration->webRegistration(), new PushPermissionStatusCallback(resolver)); |
| return promise; |
| } |
| +void PushManager::trace(Visitor* visitor) |
| +{ |
| + visitor->trace(m_registration); |
| +} |
| + |
| } // namespace blink |