| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 namespace blink { | 27 namespace blink { |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 WebPushProvider* pushProvider() | 30 WebPushProvider* pushProvider() |
| 31 { | 31 { |
| 32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); | 32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); |
| 33 ASSERT(webPushProvider); | 33 ASSERT(webPushProvider); |
| 34 return webPushProvider; | 34 return webPushProvider; |
| 35 } | 35 } |
| 36 | 36 |
| 37 String bufferSourceToString(const ArrayBufferOrArrayBufferView& applicationServe
rKey, ExceptionState& exceptionState) | 37 WebPushSubscriptionOptions toWebPushSubscriptionOptions(const PushSubscriptionOp
tions& options) |
| 38 { | 38 { |
| 39 // Check the validity of the sender info. It must be a 65 byte unencrypted k
ey, | 39 WebPushSubscriptionOptions webOptions; |
| 40 // which has the byte 0x04 as the first byte as a marker. | 40 webOptions.userVisibleOnly = options.userVisibleOnly(); |
| 41 char* input; | 41 return webOptions; |
| 42 int length; | |
| 43 if (applicationServerKey.isArrayBuffer()) { | |
| 44 input = static_cast<char*>( | |
| 45 applicationServerKey.getAsArrayBuffer()->data()); | |
| 46 length = applicationServerKey.getAsArrayBuffer()->byteLength(); | |
| 47 } else if (applicationServerKey.isArrayBufferView()) { | |
| 48 input = static_cast<char*>( | |
| 49 applicationServerKey.getAsArrayBufferView()->buffer()->data()); | |
| 50 length = applicationServerKey.getAsArrayBufferView()->buffer()->byteLeng
th(); | |
| 51 } else { | |
| 52 ASSERT_NOT_REACHED(); | |
| 53 return String(); | |
| 54 } | |
| 55 | |
| 56 if (length == 65 && input[0] == 0x04) | |
| 57 return WebString::fromUTF8(input, length); | |
| 58 exceptionState.throwDOMException(InvalidAccessError, "The provided applicati
onServerKey is not valid."); | |
| 59 return String(); | |
| 60 } | 42 } |
| 61 | 43 |
| 62 } // namespace | 44 } // namespace |
| 63 | 45 |
| 64 PushManager::PushManager(ServiceWorkerRegistration* registration) | 46 PushManager::PushManager(ServiceWorkerRegistration* registration) |
| 65 : m_registration(registration) | 47 : m_registration(registration) |
| 66 { | 48 { |
| 67 ASSERT(registration); | 49 ASSERT(registration); |
| 68 } | 50 } |
| 69 | 51 |
| 70 WebPushSubscriptionOptions PushManager::toWebPushSubscriptionOptions(const PushS
ubscriptionOptions& options, ExceptionState& exceptionState) | 52 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri
ptionOptions& options) |
| 71 { | |
| 72 WebPushSubscriptionOptions webOptions; | |
| 73 webOptions.userVisibleOnly = options.userVisibleOnly(); | |
| 74 if (options.hasApplicationServerKey()) { | |
| 75 webOptions.applicationServerKey = bufferSourceToString(options.applicati
onServerKey(), | |
| 76 exceptionState); | |
| 77 } | |
| 78 return webOptions; | |
| 79 } | |
| 80 | |
| 81 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri
ptionOptions& options, ExceptionState& exceptionState) | |
| 82 { | 53 { |
| 83 if (!m_registration->active()) | 54 if (!m_registration->active()) |
| 84 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Subscription failed - no active Service Worker")); | 55 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Subscription failed - no active Service Worker")); |
| 85 | 56 |
| 86 const WebPushSubscriptionOptions& webOptions = toWebPushSubscriptionOptions(
options, exceptionState); | |
| 87 if (exceptionState.hadException()) | |
| 88 return ScriptPromise(); | |
| 89 | |
| 90 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 57 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 91 ScriptPromise promise = resolver->promise(); | 58 ScriptPromise promise = resolver->promise(); |
| 92 | 59 |
| 93 // The document context is the only reasonable context from which to ask the
user for permission | 60 // The document context is the only reasonable context from which to ask the
user for permission |
| 94 // to use the Push API. The embedder should persist the permission so that l
ater calls in | 61 // to use the Push API. The embedder should persist the permission so that l
ater calls in |
| 95 // different contexts can succeed. | 62 // different contexts can succeed. |
| 96 if (scriptState->executionContext()->isDocument()) { | 63 if (scriptState->executionContext()->isDocument()) { |
| 97 Document* document = toDocument(scriptState->executionContext()); | 64 Document* document = toDocument(scriptState->executionContext()); |
| 98 if (!document->domWindow() || !document->frame()) | 65 if (!document->domWindow() || !document->frame()) |
| 99 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | 66 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
| 100 PushController::clientFrom(document->frame()).subscribe(m_registration->
webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registr
ation)); | 67 PushController::clientFrom(document->frame()).subscribe(m_registration->
webRegistration(), toWebPushSubscriptionOptions(options), new PushSubscriptionCa
llbacks(resolver, m_registration)); |
| 101 } else { | 68 } else { |
| 102 pushProvider()->subscribe(m_registration->webRegistration(), webOptions,
new PushSubscriptionCallbacks(resolver, m_registration)); | 69 pushProvider()->subscribe(m_registration->webRegistration(), toWebPushSu
bscriptionOptions(options), new PushSubscriptionCallbacks(resolver, m_registrati
on)); |
| 103 } | 70 } |
| 104 | 71 |
| 105 return promise; | 72 return promise; |
| 106 } | 73 } |
| 107 | 74 |
| 108 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) | 75 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) |
| 109 { | 76 { |
| 110 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 111 ScriptPromise promise = resolver->promise(); | 78 ScriptPromise promise = resolver->promise(); |
| 112 | 79 |
| 113 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS
ubscriptionCallbacks(resolver, m_registration)); | 80 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS
ubscriptionCallbacks(resolver, m_registration)); |
| 114 return promise; | 81 return promise; |
| 115 } | 82 } |
| 116 | 83 |
| 117 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS
ubscriptionOptions& options, ExceptionState& exceptionState) | 84 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS
ubscriptionOptions& options) |
| 118 { | 85 { |
| 119 if (scriptState->executionContext()->isDocument()) { | 86 if (scriptState->executionContext()->isDocument()) { |
| 120 Document* document = toDocument(scriptState->executionContext()); | 87 Document* document = toDocument(scriptState->executionContext()); |
| 121 if (!document->domWindow() || !document->frame()) | 88 if (!document->domWindow() || !document->frame()) |
| 122 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | 89 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); |
| 123 } | 90 } |
| 124 | 91 |
| 125 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 126 ScriptPromise promise = resolver->promise(); | 93 ScriptPromise promise = resolver->promise(); |
| 127 | 94 |
| 128 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb
PushSubscriptionOptions(options, exceptionState), new PushPermissionStatusCallba
cks(resolver)); | 95 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb
PushSubscriptionOptions(options), new PushPermissionStatusCallbacks(resolver)); |
| 129 return promise; | 96 return promise; |
| 130 } | 97 } |
| 131 | 98 |
| 132 DEFINE_TRACE(PushManager) | 99 DEFINE_TRACE(PushManager) |
| 133 { | 100 { |
| 134 visitor->trace(m_registration); | 101 visitor->trace(m_registration); |
| 135 } | 102 } |
| 136 | 103 |
| 137 } // namespace blink | 104 } // namespace blink |
| OLD | NEW |