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 "SecurityOriginData.h" |
31 | #include <wtf/URL.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | class ServiceWorkerRegistrationKey { |
36 | public: |
37 | ServiceWorkerRegistrationKey() = default; |
38 | WEBCORE_EXPORT ServiceWorkerRegistrationKey(SecurityOriginData&& topOrigin, URL&& scope); |
39 | |
40 | static ServiceWorkerRegistrationKey emptyKey(); |
41 | unsigned hash() const; |
42 | |
43 | bool operator==(const ServiceWorkerRegistrationKey&) const; |
44 | WEBCORE_EXPORT bool isMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const; |
45 | bool originIsMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const; |
46 | size_t scopeLength() const { return m_scope.string().length(); } |
47 | |
48 | const SecurityOriginData& topOrigin() const { return m_topOrigin; } |
49 | const URL& scope() const { return m_scope; } |
50 | void setScope(URL&& scope) { m_scope = WTFMove(scope); } |
51 | |
52 | bool relatesToOrigin(const SecurityOriginData&) const; |
53 | |
54 | ServiceWorkerRegistrationKey isolatedCopy() const; |
55 | |
56 | template<class Encoder> void encode(Encoder&) const; |
57 | template<class Decoder> static Optional<ServiceWorkerRegistrationKey> decode(Decoder&); |
58 | |
59 | String toDatabaseKey() const; |
60 | static Optional<ServiceWorkerRegistrationKey> fromDatabaseKey(const String&); |
61 | |
62 | #if !LOG_DISABLED |
63 | String loggingString() const; |
64 | #endif |
65 | |
66 | private: |
67 | SecurityOriginData m_topOrigin; |
68 | URL m_scope; |
69 | }; |
70 | |
71 | template<class Encoder> |
72 | void ServiceWorkerRegistrationKey::encode(Encoder& encoder) const |
73 | { |
74 | encoder << m_topOrigin << m_scope; |
75 | } |
76 | |
77 | template<class Decoder> |
78 | Optional<ServiceWorkerRegistrationKey> ServiceWorkerRegistrationKey::decode(Decoder& decoder) |
79 | { |
80 | Optional<SecurityOriginData> topOrigin; |
81 | decoder >> topOrigin; |
82 | if (!topOrigin) |
83 | return WTF::nullopt; |
84 | |
85 | URL scope; |
86 | if (!decoder.decode(scope)) |
87 | return WTF::nullopt; |
88 | |
89 | return ServiceWorkerRegistrationKey { WTFMove(*topOrigin), WTFMove(scope) }; |
90 | } |
91 | |
92 | } // namespace WebCore |
93 | |
94 | namespace WTF { |
95 | |
96 | struct ServiceWorkerRegistrationKeyHash { |
97 | static unsigned hash(const WebCore::ServiceWorkerRegistrationKey& key) { return key.hash(); } |
98 | static bool equal(const WebCore::ServiceWorkerRegistrationKey& a, const WebCore::ServiceWorkerRegistrationKey& b) { return a == b; } |
99 | static const bool safeToCompareToEmptyOrDeleted = false; |
100 | }; |
101 | |
102 | template<> struct HashTraits<WebCore::ServiceWorkerRegistrationKey> : GenericHashTraits<WebCore::ServiceWorkerRegistrationKey> { |
103 | static WebCore::ServiceWorkerRegistrationKey emptyValue() { return WebCore::ServiceWorkerRegistrationKey::emptyKey(); } |
104 | |
105 | static void constructDeletedValue(WebCore::ServiceWorkerRegistrationKey& slot) { slot.setScope(URL(HashTableDeletedValue)); } |
106 | static bool isDeletedValue(const WebCore::ServiceWorkerRegistrationKey& slot) { return slot.scope().isHashTableDeletedValue(); } |
107 | }; |
108 | |
109 | template<> struct DefaultHash<WebCore::ServiceWorkerRegistrationKey> { |
110 | typedef ServiceWorkerRegistrationKeyHash Hash; |
111 | }; |
112 | |
113 | } // namespace WTF |
114 | |
115 | #endif // ENABLE(SERVICE_WORKER) |
116 | |