| 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 "modules/push_messaging/PushManager.h" | 5 #include "modules/push_messaging/PushManager.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/Document.h" | 11 #include "core/dom/Document.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "modules/push_messaging/PushController.h" | 14 #include "modules/push_messaging/PushController.h" |
| 15 #include "modules/push_messaging/PushError.h" | 15 #include "modules/push_messaging/PushError.h" |
| 16 #include "modules/push_messaging/PushPermissionStatusCallbacks.h" | 16 #include "modules/push_messaging/PushPermissionStatusCallbacks.h" |
| 17 #include "modules/push_messaging/PushSubscription.h" | 17 #include "modules/push_messaging/PushSubscription.h" |
| 18 #include "modules/push_messaging/PushSubscriptionCallbacks.h" | 18 #include "modules/push_messaging/PushSubscriptionCallbacks.h" |
| 19 #include "modules/push_messaging/PushSubscriptionOptions.h" | 19 #include "modules/push_messaging/PushSubscriptionOptions.h" |
| 20 #include "modules/push_messaging/PushSubscriptionOptionsInit.h" |
| 20 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 21 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 21 #include "public/platform/Platform.h" | 22 #include "public/platform/Platform.h" |
| 22 #include "public/platform/modules/push_messaging/WebPushClient.h" | 23 #include "public/platform/modules/push_messaging/WebPushClient.h" |
| 23 #include "public/platform/modules/push_messaging/WebPushProvider.h" | 24 #include "public/platform/modules/push_messaging/WebPushProvider.h" |
| 24 #include "public/platform/modules/push_messaging/WebPushSubscriptionOptions.h" | 25 #include "public/platform/modules/push_messaging/WebPushSubscriptionOptions.h" |
| 25 #include "wtf/Assertions.h" | 26 #include "wtf/Assertions.h" |
| 26 #include "wtf/RefPtr.h" | 27 #include "wtf/RefPtr.h" |
| 27 | 28 |
| 28 namespace blink { | 29 namespace blink { |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 const int kMaxApplicationServerKeyLength = 255; | |
| 32 | |
| 33 WebPushProvider* pushProvider() | 32 WebPushProvider* pushProvider() |
| 34 { | 33 { |
| 35 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); | 34 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); |
| 36 DCHECK(webPushProvider); | 35 DCHECK(webPushProvider); |
| 37 return webPushProvider; | 36 return webPushProvider; |
| 38 } | 37 } |
| 39 | 38 |
| 40 String bufferSourceToString(const ArrayBufferOrArrayBufferView& applicationServe
rKey, ExceptionState& exceptionState) | |
| 41 { | |
| 42 // Check the validity of the sender info. It must be a 65 byte unencrypted k
ey, | |
| 43 // which has the byte 0x04 as the first byte as a marker. | |
| 44 unsigned char* input; | |
| 45 int length; | |
| 46 if (applicationServerKey.isArrayBuffer()) { | |
| 47 input = static_cast<unsigned char*>( | |
| 48 applicationServerKey.getAsArrayBuffer()->data()); | |
| 49 length = applicationServerKey.getAsArrayBuffer()->byteLength(); | |
| 50 } else if (applicationServerKey.isArrayBufferView()) { | |
| 51 input = static_cast<unsigned char*>( | |
| 52 applicationServerKey.getAsArrayBufferView()->buffer()->data()); | |
| 53 length = applicationServerKey.getAsArrayBufferView()->buffer()->byteLeng
th(); | |
| 54 } else { | |
| 55 NOTREACHED(); | |
| 56 return String(); | |
| 57 } | |
| 58 | |
| 59 // If the key is valid, just treat it as a string of bytes and pass it to | |
| 60 // the push service. | |
| 61 if (length <= kMaxApplicationServerKeyLength) | |
| 62 return WebString::fromLatin1(input, length); | |
| 63 | |
| 64 exceptionState.throwDOMException(InvalidAccessError, "The provided applicati
onServerKey is not valid."); | |
| 65 return String(); | |
| 66 } | |
| 67 | |
| 68 } // namespace | 39 } // namespace |
| 69 | 40 |
| 70 PushManager::PushManager(ServiceWorkerRegistration* registration) | 41 PushManager::PushManager(ServiceWorkerRegistration* registration) |
| 71 : m_registration(registration) | 42 : m_registration(registration) |
| 72 { | 43 { |
| 73 DCHECK(registration); | 44 DCHECK(registration); |
| 74 } | 45 } |
| 75 | 46 |
| 76 WebPushSubscriptionOptions PushManager::toWebPushSubscriptionOptions(const PushS
ubscriptionOptions& options, ExceptionState& exceptionState) | 47 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri
ptionOptionsInit& options, ExceptionState& exceptionState) |
| 77 { | |
| 78 WebPushSubscriptionOptions webOptions; | |
| 79 webOptions.userVisibleOnly = options.userVisibleOnly(); | |
| 80 if (options.hasApplicationServerKey()) { | |
| 81 webOptions.applicationServerKey = bufferSourceToString(options.applicati
onServerKey(), | |
| 82 exceptionState); | |
| 83 } | |
| 84 return webOptions; | |
| 85 } | |
| 86 | |
| 87 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri
ptionOptions& options, ExceptionState& exceptionState) | |
| 88 { | 48 { |
| 89 if (!m_registration->active()) | 49 if (!m_registration->active()) |
| 90 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Subscription failed - no active Service Worker")); | 50 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Subscription failed - no active Service Worker")); |
| 91 | 51 |
| 92 const WebPushSubscriptionOptions& webOptions = toWebPushSubscriptionOptions(
options, exceptionState); | 52 const WebPushSubscriptionOptions& webOptions = PushSubscriptionOptions::toWe
b(options, exceptionState); |
| 93 if (exceptionState.hadException()) | 53 if (exceptionState.hadException()) |
| 94 return ScriptPromise(); | 54 return ScriptPromise(); |
| 95 | 55 |
| 96 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 56 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 97 ScriptPromise promise = resolver->promise(); | 57 ScriptPromise promise = resolver->promise(); |
| 98 | 58 |
| 99 // The document context is the only reasonable context from which to ask the
user for permission | 59 // The document context is the only reasonable context from which to ask the
user for permission |
| 100 // to use the Push API. The embedder should persist the permission so that l
ater calls in | 60 // to use the Push API. The embedder should persist the permission so that l
ater calls in |
| 101 // different contexts can succeed. | 61 // different contexts can succeed. |
| 102 if (scriptState->getExecutionContext()->isDocument()) { | 62 if (scriptState->getExecutionContext()->isDocument()) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 113 | 73 |
| 114 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) | 74 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) |
| 115 { | 75 { |
| 116 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 76 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 117 ScriptPromise promise = resolver->promise(); | 77 ScriptPromise promise = resolver->promise(); |
| 118 | 78 |
| 119 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS
ubscriptionCallbacks(resolver, m_registration)); | 79 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS
ubscriptionCallbacks(resolver, m_registration)); |
| 120 return promise; | 80 return promise; |
| 121 } | 81 } |
| 122 | 82 |
| 123 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS
ubscriptionOptions& options, ExceptionState& exceptionState) | 83 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS
ubscriptionOptionsInit& options, ExceptionState& exceptionState) |
| 124 { | 84 { |
| 125 if (scriptState->getExecutionContext()->isDocument()) { | 85 if (scriptState->getExecutionContext()->isDocument()) { |
| 126 Document* document = toDocument(scriptState->getExecutionContext()); | 86 Document* document = toDocument(scriptState->getExecutionContext()); |
| 127 if (!document->domWindow() || !document->frame()) | 87 if (!document->domWindow() || !document->frame()) |
| 128 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | 88 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
| 129 } | 89 } |
| 130 | 90 |
| 131 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 91 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 132 ScriptPromise promise = resolver->promise(); | 92 ScriptPromise promise = resolver->promise(); |
| 133 | 93 |
| 134 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb
PushSubscriptionOptions(options, exceptionState), new PushPermissionStatusCallba
cks(resolver)); | 94 pushProvider()->getPermissionStatus(m_registration->webRegistration(), PushS
ubscriptionOptions::toWeb(options, exceptionState), new PushPermissionStatusCall
backs(resolver)); |
| 135 return promise; | 95 return promise; |
| 136 } | 96 } |
| 137 | 97 |
| 138 DEFINE_TRACE(PushManager) | 98 DEFINE_TRACE(PushManager) |
| 139 { | 99 { |
| 140 visitor->trace(m_registration); | 100 visitor->trace(m_registration); |
| 141 } | 101 } |
| 142 | 102 |
| 143 } // namespace blink | 103 } // namespace blink |
| OLD | NEW |