Chromium Code Reviews| 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() |
|
Peter Beverloo
2014/12/10 00:18:18
nit: no indentation.
Michael van Ouwerkerk
2014/12/10 15:13:06
Done.
| |
| 30 { | |
| 31 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); | |
| 32 ASSERT(webPushProvider); | |
| 33 return webPushProvider; | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 PushManager::PushManager(ServiceWorkerRegistration* registration) | |
| 39 : m_registration(registration) | |
| 29 { | 40 { |
| 41 ASSERT(registration); | |
| 30 } | 42 } |
| 31 | 43 |
| 32 // FIXME: This call should be available from workers which will not have a Docum ent object available. | |
| 33 // See crbug.com/389194 | |
| 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 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.
| |
| 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 | |
| 61 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.
| |
| 62 ASSERT(client); | |
| 63 client->registerPushMessaging(m_registration->webRegistration(), new Cal lbackPromiseAdapter<PushRegistration, PushError>(resolver)); | |
| 64 } else { | |
| 65 pushProvider()->registerPushMessaging(m_registration->webRegistration(), new CallbackPromiseAdapter<PushRegistration, PushError>(resolver)); | |
| 66 } | |
| 53 | 67 |
| 54 return promise; | 68 return promise; |
| 55 } | 69 } |
| 56 | 70 |
| 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) | 71 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) |
| 60 { | 72 { |
| 61 ASSERT(scriptState->executionContext()->isDocument()); | 73 if (scriptState->executionContext()->isDocument()) { |
| 62 | 74 Document* document = toDocument(scriptState->executionContext()); |
| 63 Document* document = toDocument(scriptState->executionContext()); | 75 // FIXME: add test coverage for this condition - https://crbug.com/44043 1 |
| 64 if (!document->domWindow() || !document->frame()) | 76 if (!document->domWindow() || !document->frame()) |
| 65 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(InvalidStateError, "Document is detached from window.")); | 77 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); |
| 66 blink::WebPushClient* client = PushController::clientFrom(document->frame()) ; | 78 } |
| 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 | 79 |
| 75 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | 80 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); |
| 76 | |
| 77 ScriptPromise promise = resolver->promise(); | 81 ScriptPromise promise = resolver->promise(); |
| 78 client->getPermissionStatus(new PushPermissionStatusCallback(resolver), serv iceWorkerProvider); | 82 pushProvider()->getPermissionStatus(m_registration->webRegistration(), new P ushPermissionStatusCallback(resolver)); |
| 79 return promise; | 83 return promise; |
| 80 } | 84 } |
| 81 | 85 |
| 86 void PushManager::trace(Visitor* visitor) | |
| 87 { | |
| 88 visitor->trace(m_registration); | |
| 89 } | |
| 90 | |
| 82 } // namespace blink | 91 } // namespace blink |
| OLD | NEW |