Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: third_party/WebKit/Source/modules/push_messaging/PushSubscriptionOptions.cpp

Issue 2133673002: Push API: Implement and ship PushSubscription.options (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add MODULES_EXPORT Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
20
21 String bufferSourceToString(const ArrayBufferOrArrayBufferView& applicationServe rKey, ExceptionState& exceptionState)
22 {
23 // Check the validity of the sender info. It must be a 65 byte unencrypted k ey,
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()->byteLeng th();
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 to
41 // the push service.
42 if (length <= kMaxApplicationServerKeyLength)
43 return WebString::fromLatin1(input, length);
44
45 exceptionState.throwDOMException(InvalidAccessError, "The provided applicati onServerKey is not valid.");
46 return String();
47 }
48
49 } // namespace
50
51 // static
52 WebPushSubscriptionOptions PushSubscriptionOptions::toWeb(const PushSubscription OptionsInit& options, ExceptionState& exceptionState)
53 {
54 WebPushSubscriptionOptions webOptions;
55 webOptions.userVisibleOnly = options.userVisibleOnly();
56 if (options.hasApplicationServerKey())
57 webOptions.applicationServerKey = bufferSourceToString(options.applicati onServerKey(), exceptionState);
58 return webOptions;
59 }
60
61 PushSubscriptionOptions::PushSubscriptionOptions(const WebPushSubscriptionOption s& options)
62 : m_userVisibleOnly(options.userVisibleOnly)
63 , m_applicationServerKey(DOMArrayBuffer::create(options.applicationServerKey .latin1().data(), options.applicationServerKey.length()))
64 {
65 }
66
67 DEFINE_TRACE(PushSubscriptionOptions)
68 {
69 visitor->trace(m_applicationServerKey);
70 }
71
72 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698