1 | /* |
2 | * Copyright (C) 2017 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 "DocumentIdentifier.h" |
31 | #include "ServiceWorkerTypes.h" |
32 | #include <wtf/text/StringConcatenateNumbers.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | struct ServiceWorkerClientIdentifier { |
37 | SWServerConnectionIdentifier serverConnectionIdentifier; |
38 | DocumentIdentifier contextIdentifier; |
39 | |
40 | unsigned hash() const; |
41 | |
42 | String toString() const { return makeString(serverConnectionIdentifier.toUInt64(), '-', contextIdentifier.toUInt64()); } |
43 | static Optional<ServiceWorkerClientIdentifier> fromString(StringView); |
44 | |
45 | template<class Encoder> void encode(Encoder&) const; |
46 | template<class Decoder> static Optional<ServiceWorkerClientIdentifier> decode(Decoder&); |
47 | }; |
48 | |
49 | inline bool operator==(const ServiceWorkerClientIdentifier& a, const ServiceWorkerClientIdentifier& b) |
50 | { |
51 | return a.serverConnectionIdentifier == b.serverConnectionIdentifier && a.contextIdentifier == b.contextIdentifier; |
52 | } |
53 | |
54 | inline Optional<ServiceWorkerClientIdentifier> ServiceWorkerClientIdentifier::fromString(StringView string) |
55 | { |
56 | ServiceWorkerClientIdentifier clientIdentifier; |
57 | |
58 | unsigned counter = 0; |
59 | for (auto item : string.split('-')) { |
60 | auto identifier = item.toUInt64Strict(); |
61 | if (!identifier || !*identifier) |
62 | return WTF::nullopt; |
63 | if (!counter++) |
64 | clientIdentifier.serverConnectionIdentifier = makeObjectIdentifier<SWServerConnectionIdentifierType>(identifier.value()); |
65 | else if (counter == 2) |
66 | clientIdentifier.contextIdentifier = makeObjectIdentifier<DocumentIdentifierType>(identifier.value()); |
67 | } |
68 | return (counter == 2) ? makeOptional(WTFMove(clientIdentifier)) : WTF::nullopt; |
69 | } |
70 | |
71 | template<class Encoder> |
72 | void ServiceWorkerClientIdentifier::encode(Encoder& encoder) const |
73 | { |
74 | encoder << serverConnectionIdentifier << contextIdentifier; |
75 | } |
76 | |
77 | template<class Decoder> |
78 | Optional<ServiceWorkerClientIdentifier> ServiceWorkerClientIdentifier::decode(Decoder& decoder) |
79 | { |
80 | Optional<SWServerConnectionIdentifier> serverConnectionIdentifier; |
81 | decoder >> serverConnectionIdentifier; |
82 | if (!serverConnectionIdentifier) |
83 | return WTF::nullopt; |
84 | |
85 | Optional<DocumentIdentifier> contextIdentifier; |
86 | decoder >> contextIdentifier; |
87 | if (!contextIdentifier) |
88 | return WTF::nullopt; |
89 | |
90 | return { { WTFMove(*serverConnectionIdentifier), WTFMove(*contextIdentifier) } }; |
91 | } |
92 | |
93 | inline unsigned ServiceWorkerClientIdentifier::hash() const |
94 | { |
95 | unsigned hashes[2]; |
96 | hashes[0] = WTF::intHash(serverConnectionIdentifier.toUInt64()); |
97 | hashes[1] = WTF::intHash(contextIdentifier.toUInt64()); |
98 | |
99 | return StringHasher::hashMemory(hashes, sizeof(hashes)); |
100 | } |
101 | |
102 | } // namespace WebCore |
103 | |
104 | namespace WTF { |
105 | |
106 | struct ServiceWorkerClientIdentifierHash { |
107 | static unsigned hash(const WebCore::ServiceWorkerClientIdentifier& key) { return key.hash(); } |
108 | static bool equal(const WebCore::ServiceWorkerClientIdentifier& a, const WebCore::ServiceWorkerClientIdentifier& b) { return a == b; } |
109 | static const bool safeToCompareToEmptyOrDeleted = true; |
110 | }; |
111 | |
112 | template<> struct HashTraits<WebCore::ServiceWorkerClientIdentifier> : GenericHashTraits<WebCore::ServiceWorkerClientIdentifier> { |
113 | static WebCore::ServiceWorkerClientIdentifier emptyValue() { return { }; } |
114 | |
115 | static void constructDeletedValue(WebCore::ServiceWorkerClientIdentifier& slot) { slot.serverConnectionIdentifier = makeObjectIdentifier<WebCore::SWServerConnectionIdentifierType>(std::numeric_limits<uint64_t>::max()); } |
116 | |
117 | static bool isDeletedValue(const WebCore::ServiceWorkerClientIdentifier& slot) { return slot.serverConnectionIdentifier.toUInt64() == std::numeric_limits<uint64_t>::max(); } |
118 | }; |
119 | |
120 | template<> struct DefaultHash<WebCore::ServiceWorkerClientIdentifier> { |
121 | typedef ServiceWorkerClientIdentifierHash Hash; |
122 | }; |
123 | |
124 | } |
125 | |
126 | #endif // ENABLE(SERVICE_WORKER) |
127 | |