blob: bc21587be19a04b73f42a844f593d8486b1c86a0 [file] [log] [blame]
nham@apple.com98bffa52021-10-20 05:16:30 +00001/*
2 * Copyright (C) 2021 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(SERVICE_WORKER)
29
30#include "EpochTimeStamp.h"
nham@apple.com5aaf6012022-01-23 05:20:57 +000031#include "PushSubscriptionIdentifier.h"
nham@apple.com98bffa52021-10-20 05:16:30 +000032
don.olmstead@sony.comae640002021-11-03 02:26:54 +000033#include <wtf/text/WTFString.h>
nham@apple.com98bffa52021-10-20 05:16:30 +000034
35namespace WebCore {
36
37struct PushSubscriptionData {
nham@apple.com5aaf6012022-01-23 05:20:57 +000038 PushSubscriptionIdentifier identifier;
nham@apple.com98bffa52021-10-20 05:16:30 +000039 String endpoint;
40 std::optional<WebCore::EpochTimeStamp> expirationTime;
41 Vector<uint8_t> serverVAPIDPublicKey;
42 Vector<uint8_t> clientECDHPublicKey;
43 Vector<uint8_t> sharedAuthenticationSecret;
44
cdumez@apple.comc13cffc2022-03-10 19:43:15 +000045 WEBCORE_EXPORT PushSubscriptionData isolatedCopy() const &;
46 WEBCORE_EXPORT PushSubscriptionData isolatedCopy() &&;
nham@apple.com98bffa52021-10-20 05:16:30 +000047
48 template<class Encoder> void encode(Encoder&) const;
49 template<class Decoder> static std::optional<PushSubscriptionData> decode(Decoder&);
50};
51
52template<class Encoder>
53void PushSubscriptionData::encode(Encoder& encoder) const
54{
nham@apple.com5aaf6012022-01-23 05:20:57 +000055 encoder << identifier;
nham@apple.com98bffa52021-10-20 05:16:30 +000056 encoder << endpoint;
57 encoder << expirationTime;
58 encoder << serverVAPIDPublicKey;
59 encoder << clientECDHPublicKey;
60 encoder << sharedAuthenticationSecret;
61}
62
63template<class Decoder>
64std::optional<PushSubscriptionData> PushSubscriptionData::decode(Decoder& decoder)
65{
nham@apple.com5aaf6012022-01-23 05:20:57 +000066 std::optional<PushSubscriptionIdentifier> identifier;
67 decoder >> identifier;
68 if (!identifier)
nham@apple.com98bffa52021-10-20 05:16:30 +000069 return std::nullopt;
nham@apple.com5aaf6012022-01-23 05:20:57 +000070
71 std::optional<String> endpoint;
72 decoder >> endpoint;
73 if (!endpoint)
nham@apple.com98bffa52021-10-20 05:16:30 +000074 return std::nullopt;
nham@apple.com5aaf6012022-01-23 05:20:57 +000075
76 std::optional<std::optional<WebCore::EpochTimeStamp>> expirationTime;
77 decoder >> expirationTime;
78 if (!expirationTime)
nham@apple.com98bffa52021-10-20 05:16:30 +000079 return std::nullopt;
nham@apple.com5aaf6012022-01-23 05:20:57 +000080
81 std::optional<Vector<uint8_t>> serverVAPIDPublicKey;
82 decoder >> serverVAPIDPublicKey;
83 if (!serverVAPIDPublicKey)
nham@apple.com98bffa52021-10-20 05:16:30 +000084 return std::nullopt;
nham@apple.com5aaf6012022-01-23 05:20:57 +000085
86 std::optional<Vector<uint8_t>> clientECDHPublicKey;
87 decoder >> clientECDHPublicKey;
88 if (!clientECDHPublicKey)
nham@apple.com98bffa52021-10-20 05:16:30 +000089 return std::nullopt;
nham@apple.com5aaf6012022-01-23 05:20:57 +000090
91 std::optional<Vector<uint8_t>> sharedAuthenticationSecret;
92 decoder >> sharedAuthenticationSecret;
93 if (!sharedAuthenticationSecret)
94 return std::nullopt;
95
96 return PushSubscriptionData { WTFMove(*identifier), WTFMove(*endpoint), WTFMove(*expirationTime), WTFMove(*serverVAPIDPublicKey), WTFMove(*clientECDHPublicKey), WTFMove(*sharedAuthenticationSecret) };
nham@apple.com98bffa52021-10-20 05:16:30 +000097}
98
99} // namespace WebCore
100
101#endif // ENABLE(SERVICE_WORKER)