OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/push_messaging/PushSubscriptionOptions.h" | |
6 | |
7 #include "bindings/core/v8/ExceptionState.h" | |
8 #include "core/dom/DOMArrayBuffer.h" | |
9 #include "core/dom/ExceptionCode.h" | |
10 #include "modules/push_messaging/PushSubscriptionOptionsInit.h" | |
11 #include "public/platform/WebString.h" | |
12 #include "public/platform/modules/push_messaging/WebPushSubscriptionOptions.h" | |
13 #include "wtf/Assertions.h" | |
14 #include "wtf/text/WTFString.h" | |
15 | |
16 namespace blink { | |
17 namespace { | |
18 | |
19 const int kMaxApplicationServerKeyLength = 255; | |
Peter Beverloo
2016/07/08 18:30:12
We don't indent code in namespaces.
johnme
2016/07/13 17:51:57
We *do* indent code in nested namespaces: https://
Peter Beverloo
2016/07/14 14:40:01
That does not seem to apply to anonymous namespace
johnme
2016/07/14 17:01:22
Done.
| |
20 | |
21 String bufferSourceToString(const ArrayBufferOrArrayBufferView& applicationS erverKey, ExceptionState& exceptionState) | |
22 { | |
23 // Check the validity of the sender info. It must be a 65 byte unencrypt ed key, | |
24 // which has the byte 0x04 as the first byte as a marker. | |
25 unsigned char* input; | |
26 int length; | |
27 if (applicationServerKey.isArrayBuffer()) { | |
28 input = static_cast<unsigned char*>( | |
29 applicationServerKey.getAsArrayBuffer()->data()); | |
30 length = applicationServerKey.getAsArrayBuffer()->byteLength(); | |
31 } else if (applicationServerKey.isArrayBufferView()) { | |
32 input = static_cast<unsigned char*>( | |
33 applicationServerKey.getAsArrayBufferView()->buffer()->data()); | |
34 length = applicationServerKey.getAsArrayBufferView()->buffer()->byte Length(); | |
35 } else { | |
36 NOTREACHED(); | |
37 return String(); | |
38 } | |
39 | |
40 // If the key is valid, just treat it as a string of bytes and pass it t o | |
41 // the push service. | |
42 if (length <= kMaxApplicationServerKeyLength) | |
43 return WebString::fromLatin1(input, length); | |
44 | |
45 exceptionState.throwDOMException(InvalidAccessError, "The provided appli cationServerKey is not valid."); | |
46 return String(); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 WebPushSubscriptionOptions PushSubscriptionOptions::toWeb(const PushSubscription OptionsInit& options, ExceptionState& exceptionState) | |
52 { | |
53 WebPushSubscriptionOptions webOptions; | |
54 webOptions.userVisibleOnly = options.userVisibleOnly(); | |
55 if (options.hasApplicationServerKey()) | |
56 webOptions.applicationServerKey = bufferSourceToString(options.applicati onServerKey(), exceptionState); | |
57 return webOptions; | |
58 } | |
59 | |
60 PushSubscriptionOptions::PushSubscriptionOptions(const WebPushSubscriptionOption s& options) | |
61 : m_userVisibleOnly(options.userVisibleOnly) | |
62 , m_applicationServerKey(DOMArrayBuffer::create(options.applicationServerKey .latin1().data(), options.applicationServerKey.length())) | |
63 { | |
64 } | |
65 | |
66 DEFINE_TRACE(PushSubscriptionOptions) | |
67 { | |
68 visitor->trace(m_applicationServerKey); | |
69 } | |
70 | |
71 } // namespace blink | |
OLD | NEW |