| 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" | 16 #include "core/frame/LocalDOMWindow.h" |
| 17 #include "modules/push_messaging/PushController.h" | 17 #include "modules/push_messaging/PushController.h" |
| 18 #include "modules/push_messaging/PushError.h" | 18 #include "modules/push_messaging/PushError.h" |
| 19 #include "modules/push_messaging/PushPermissionClient.h" | |
| 20 #include "modules/push_messaging/PushPermissionRequestCallback.h" | |
| 21 #include "modules/push_messaging/PushPermissionStatusCallback.h" | 19 #include "modules/push_messaging/PushPermissionStatusCallback.h" |
| 22 #include "modules/push_messaging/PushRegistration.h" | 20 #include "modules/push_messaging/PushRegistration.h" |
| 23 #include "modules/serviceworkers/NavigatorServiceWorker.h" | 21 #include "modules/serviceworkers/NavigatorServiceWorker.h" |
| 24 #include "modules/serviceworkers/ServiceWorkerContainer.h" | 22 #include "modules/serviceworkers/ServiceWorkerContainer.h" |
| 25 #include "public/platform/WebPushClient.h" | 23 #include "public/platform/WebPushClient.h" |
| 26 #include "wtf/RefPtr.h" | 24 #include "wtf/RefPtr.h" |
| 27 | 25 |
| 28 namespace blink { | 26 namespace blink { |
| 29 | 27 |
| 30 PushManager::PushManager() | 28 PushManager::PushManager() |
| 31 { | 29 { |
| 32 } | 30 } |
| 33 | 31 |
| 34 // FIXME: This call should be available from workers which will not have a Docum
ent object available. | 32 // FIXME: This call should be available from workers which will not have a Docum
ent object available. |
| 35 // See crbug.com/389194 | 33 // See crbug.com/389194 |
| 36 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) | 34 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) |
| 37 { | 35 { |
| 38 ASSERT(scriptState->executionContext()->isDocument()); | 36 ASSERT(scriptState->executionContext()->isDocument()); |
| 39 | 37 |
| 40 Document* document = toDocument(scriptState->executionContext()); | 38 Document* document = toDocument(scriptState->executionContext()); |
| 41 if (!document->domWindow() || !document->frame()) | 39 if (!document->domWindow() || !document->frame()) |
| 42 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Document is detached from window.")); | 40 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Document is detached from window.")); |
| 43 | 41 |
| 44 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se
rviceWorker(*document->domWindow()->navigator())->provider(); | 42 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se
rviceWorker(*document->domWindow()->navigator())->provider(); |
| 45 if (!serviceWorkerProvider) | 43 if (!serviceWorkerProvider) |
| 46 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "No Service Worker installed for this document.")); | 44 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "No Service Worker installed for this document.")); |
| 47 | 45 |
| 48 // FIXME: Once everything except permission request goes through platform, | |
| 49 // delete WebPushClient and usage such as this one. | |
| 50 // See crbug.com/389194 | |
| 51 WebPushClient* client = PushController::clientFrom(document->frame()); | 46 WebPushClient* client = PushController::clientFrom(document->frame()); |
| 52 ASSERT(client); | 47 ASSERT(client); |
| 53 | 48 |
| 54 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); | 49 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 55 ScriptPromise promise = resolver->promise(); | 50 ScriptPromise promise = resolver->promise(); |
| 56 | 51 |
| 57 PushPermissionClient* permissionClient = PushPermissionClient::from(scriptSt
ate->executionContext()); | 52 client->registerPushMessaging(new CallbackPromiseAdapter<PushRegistration, P
ushError>(resolver), serviceWorkerProvider); |
| 58 if (permissionClient) | |
| 59 permissionClient->requestPermission(scriptState->executionContext(), new
PushPermissionRequestCallback(this, client, resolver, serviceWorkerProvider)); | |
| 60 else | |
| 61 doRegister(client, resolver, serviceWorkerProvider); | |
| 62 | 53 |
| 63 return promise; | 54 return promise; |
| 64 } | 55 } |
| 65 | 56 |
| 66 // FIXME: This call should be available from workers which will not have a Docum
ent object available. | 57 // FIXME: This call should be available from workers which will not have a Docum
ent object available. |
| 67 // See crbug.com/389194 | 58 // See crbug.com/389194 |
| 68 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) | 59 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) |
| 69 { | 60 { |
| 70 ASSERT(scriptState->executionContext()->isDocument()); | 61 ASSERT(scriptState->executionContext()->isDocument()); |
| 71 | 62 |
| 72 Document* document = toDocument(scriptState->executionContext()); | 63 Document* document = toDocument(scriptState->executionContext()); |
| 73 if (!document->domWindow() || !document->frame()) | 64 if (!document->domWindow() || !document->frame()) |
| 74 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Document is detached from window.")); | 65 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Document is detached from window.")); |
| 75 blink::WebPushClient* client = PushController::clientFrom(document->frame())
; | 66 blink::WebPushClient* client = PushController::clientFrom(document->frame())
; |
| 76 ASSERT(client); | 67 ASSERT(client); |
| 77 | 68 |
| 78 // The currently implemented specification does not require a Service Worker
to be present for the | 69 // The currently implemented specification does not require a Service Worker
to be present for the |
| 79 // hasPermission() call to work, but it will become a requirement soon. | 70 // hasPermission() call to work, but it will become a requirement soon. |
| 80 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se
rviceWorker(*document->domWindow()->navigator())->provider(); | 71 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se
rviceWorker(*document->domWindow()->navigator())->provider(); |
| 81 if (!serviceWorkerProvider) | 72 if (!serviceWorkerProvider) |
| 82 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "No Service Worker installed for this document.")); | 73 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "No Service Worker installed for this document.")); |
| 83 | 74 |
| 84 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); | 75 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 85 | 76 |
| 86 ScriptPromise promise = resolver->promise(); | 77 ScriptPromise promise = resolver->promise(); |
| 87 client->getPermissionStatus(new PushPermissionStatusCallback(resolver), serv
iceWorkerProvider); | 78 client->getPermissionStatus(new PushPermissionStatusCallback(resolver), serv
iceWorkerProvider); |
| 88 return promise; | 79 return promise; |
| 89 } | 80 } |
| 90 | 81 |
| 91 void PushManager::doRegister(WebPushClient* client, PassRefPtr<ScriptPromiseReso
lver> resolver, WebServiceWorkerProvider* serviceWorkerProvider) | |
| 92 { | |
| 93 client->registerPushMessaging(new CallbackPromiseAdapter<PushRegistration, P
ushError>(resolver), serviceWorkerProvider); | |
| 94 } | |
| 95 | |
| 96 } // namespace blink | 82 } // namespace blink |
| OLD | NEW |