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 "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 WebPushSubscriptionOptions toWebPushSubscriptionOptions(const PushSubscriptionOp tions& options) | |
| 38 { | |
| 39 WebPushSubscriptionOptions webOptions; | |
| 40 webOptions.userVisibleOnly = options.userVisibleOnly(); | |
| 41 return webOptions; | |
| 42 } | |
| 43 | |
| 44 } // namespace | 37 } // namespace |
| 45 | 38 |
| 46 PushManager::PushManager(ServiceWorkerRegistration* registration) | 39 PushManager::PushManager(ServiceWorkerRegistration* registration) |
| 47 : m_registration(registration) | 40 : m_registration(registration) |
| 48 { | 41 { |
| 49 ASSERT(registration); | 42 ASSERT(registration); |
| 50 } | 43 } |
| 51 | 44 |
| 45 String PushManager::toString(const ArrayBufferOrArrayBufferView& applicationServ erKey) | |
| 46 { | |
| 47 // Check the validity of the sender info. It should either be a numeric send er | |
| 48 // ID or a 65 byte unencrypted key. | |
|
Peter Beverloo
2016/02/23 14:28:29
Developers will have two ways in Chrome to provide
harkness
2016/02/26 11:44:26
Done.
| |
| 49 char* input; | |
| 50 int length; | |
| 51 if (applicationServerKey.isArrayBuffer()) { | |
| 52 input = static_cast<char*>( | |
| 53 applicationServerKey.getAsArrayBuffer()->data()); | |
| 54 length = applicationServerKey.getAsArrayBuffer()->byteLength(); | |
| 55 } else if (applicationServerKey.isArrayBufferView()) { | |
| 56 input = static_cast<char*>( | |
| 57 applicationServerKey.getAsArrayBufferView()->buffer()->data()); | |
| 58 length = applicationServerKey.getAsArrayBufferView()->buffer()->byteLeng th(); | |
| 59 } else { | |
| 60 return String(); | |
|
Peter Beverloo
2016/02/23 14:28:29
ASSERT_NOT_REACHED();
(The ArrayBufferOrArrayBuff
harkness
2016/02/26 11:44:26
Done.
| |
| 61 } | |
| 62 | |
| 63 if ((atoi(input) != 0) || (length == 65 && input[0] == 0x04)) | |
| 64 return WebString::fromUTF8(input, length); | |
| 65 return String(); | |
|
Peter Beverloo
2016/02/23 14:28:29
Per the specification:
harkness
2016/02/26 11:44:26
Done.
| |
| 66 } | |
| 67 | |
| 68 WebPushSubscriptionOptions PushManager::toWebPushSubscriptionOptions(const PushS ubscriptionOptions& options) | |
| 69 { | |
| 70 WebPushSubscriptionOptions webOptions; | |
| 71 webOptions.userVisibleOnly = options.userVisibleOnly(); | |
| 72 if (options.hasApplicationServerKey()) { | |
|
Peter Beverloo
2016/02/23 14:28:29
nit: no need for brackets on one-line statements.
harkness
2016/02/26 11:44:26
Done.
| |
| 73 webOptions.applicationServerKey = PushManager::toString(options.applicat ionServerKey()); | |
| 74 } | |
| 75 return webOptions; | |
| 76 } | |
| 77 | |
| 52 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri ptionOptions& options) | 78 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri ptionOptions& options) |
| 53 { | 79 { |
| 54 if (!m_registration->active()) | 80 if (!m_registration->active()) |
| 55 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker")); | 81 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker")); |
| 56 | 82 |
| 57 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 83 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 58 ScriptPromise promise = resolver->promise(); | 84 ScriptPromise promise = resolver->promise(); |
| 59 | 85 |
| 60 // The document context is the only reasonable context from which to ask the user for permission | 86 // The document context is the only reasonable context from which to ask the user for permission |
| 61 // to use the Push API. The embedder should persist the permission so that l ater calls in | 87 // to use the Push API. The embedder should persist the permission so that l ater calls in |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb PushSubscriptionOptions(options), new PushPermissionStatusCallbacks(resolver)); | 121 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb PushSubscriptionOptions(options), new PushPermissionStatusCallbacks(resolver)); |
| 96 return promise; | 122 return promise; |
| 97 } | 123 } |
| 98 | 124 |
| 99 DEFINE_TRACE(PushManager) | 125 DEFINE_TRACE(PushManager) |
| 100 { | 126 { |
| 101 visitor->trace(m_registration); | 127 visitor->trace(m_registration); |
| 102 } | 128 } |
| 103 | 129 |
| 104 } // namespace blink | 130 } // namespace blink |
| OLD | NEW |