Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/PushSubscriptionOptions.h" | 5 #include "modules/push_messaging/PushSubscriptionOptions.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "core/dom/DOMArrayBuffer.h" | 8 #include "core/dom/DOMArrayBuffer.h" |
| 9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "modules/push_messaging/PushSubscriptionOptionsInit.h" | 10 #include "modules/push_messaging/PushSubscriptionOptionsInit.h" |
| 11 #include "public/platform/WebString.h" | 11 #include "public/platform/WebString.h" |
| 12 #include "public/platform/modules/push_messaging/WebPushSubscriptionOptions.h" | 12 #include "public/platform/modules/push_messaging/WebPushSubscriptionOptions.h" |
| 13 #include "third_party/WebKit/Source/wtf/ASCIICType.h" | |
| 13 #include "wtf/Assertions.h" | 14 #include "wtf/Assertions.h" |
| 14 #include "wtf/text/WTFString.h" | 15 #include "wtf/text/WTFString.h" |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const int kMaxApplicationServerKeyLength = 255; | 20 const int kMaxApplicationServerKeyLength = 255; |
| 20 | 21 |
| 21 String bufferSourceToString( | 22 String bufferSourceToString( |
| 22 const ArrayBufferOrArrayBufferView& applicationServerKey, | 23 const ArrayBufferOrArrayBufferView& applicationServerKey, |
| 23 ExceptionState& exceptionState) { | 24 ExceptionState& exceptionState) { |
| 24 // Check the validity of the sender info. It must be a 65-byte uncompressed | |
| 25 // key, which has the byte 0x04 as the first byte as a marker. | |
| 26 unsigned char* input; | 25 unsigned char* input; |
| 27 int length; | 26 int length; |
| 27 // Convert the input array into a string of bytes. | |
| 28 if (applicationServerKey.isArrayBuffer()) { | 28 if (applicationServerKey.isArrayBuffer()) { |
| 29 input = static_cast<unsigned char*>( | 29 input = static_cast<unsigned char*>( |
| 30 applicationServerKey.getAsArrayBuffer()->data()); | 30 applicationServerKey.getAsArrayBuffer()->data()); |
| 31 length = applicationServerKey.getAsArrayBuffer()->byteLength(); | 31 length = applicationServerKey.getAsArrayBuffer()->byteLength(); |
| 32 } else if (applicationServerKey.isArrayBufferView()) { | 32 } else if (applicationServerKey.isArrayBufferView()) { |
| 33 input = static_cast<unsigned char*>( | 33 input = static_cast<unsigned char*>( |
| 34 applicationServerKey.getAsArrayBufferView()->buffer()->data()); | 34 applicationServerKey.getAsArrayBufferView()->buffer()->data()); |
| 35 length = | 35 length = |
| 36 applicationServerKey.getAsArrayBufferView()->buffer()->byteLength(); | 36 applicationServerKey.getAsArrayBufferView()->buffer()->byteLength(); |
| 37 } else { | 37 } else { |
| 38 NOTREACHED(); | 38 NOTREACHED(); |
| 39 return String(); | 39 return String(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // If the key is valid, just treat it as a string of bytes and pass it to | 42 // Check the validity of the sender info. It must either be a 65-byte |
| 43 // the push service. | 43 // uncompressed VAPID key, which has the byte 0x04 as the first byte or a |
| 44 if (length <= kMaxApplicationServerKeyLength) | 44 // numeric sender ID. |
| 45 const bool isVapid = length == 65 && input && *input == 0x04; | |
|
Peter Beverloo
2016/10/14 14:36:30
Is checking for |input| required? When would lengt
harkness
2016/10/14 15:13:47
Done.
| |
| 46 const bool isSenderId = | |
| 47 (length > 0) && (length < kMaxApplicationServerKeyLength) && | |
|
Peter Beverloo
2016/10/14 14:36:30
No parenthesis around the two comparisons on this
harkness
2016/10/14 15:13:47
Done.
| |
| 48 (std::find_if_not(input, input + length, | |
| 49 &WTF::isASCIIDigit<unsigned char>) == input + length); | |
| 50 | |
| 51 if (isVapid || isSenderId) | |
| 45 return WebString::fromLatin1(input, length); | 52 return WebString::fromLatin1(input, length); |
| 46 | 53 |
| 47 exceptionState.throwDOMException( | 54 exceptionState.throwDOMException( |
| 48 InvalidAccessError, "The provided applicationServerKey is not valid."); | 55 InvalidAccessError, "The provided applicationServerKey is not valid."); |
| 49 return String(); | 56 return String(); |
| 50 } | 57 } |
| 51 | 58 |
| 52 } // namespace | 59 } // namespace |
| 53 | 60 |
| 54 // static | 61 // static |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 68 : m_userVisibleOnly(options.userVisibleOnly), | 75 : m_userVisibleOnly(options.userVisibleOnly), |
| 69 m_applicationServerKey( | 76 m_applicationServerKey( |
| 70 DOMArrayBuffer::create(options.applicationServerKey.latin1().data(), | 77 DOMArrayBuffer::create(options.applicationServerKey.latin1().data(), |
| 71 options.applicationServerKey.length())) {} | 78 options.applicationServerKey.length())) {} |
| 72 | 79 |
| 73 DEFINE_TRACE(PushSubscriptionOptions) { | 80 DEFINE_TRACE(PushSubscriptionOptions) { |
| 74 visitor->trace(m_applicationServerKey); | 81 visitor->trace(m_applicationServerKey); |
| 75 } | 82 } |
| 76 | 83 |
| 77 } // namespace blink | 84 } // namespace blink |
| OLD | NEW |