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