1 | /* |
2 | * Copyright (C) 2019 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 | #include "PublicSuffix.h" |
29 | #include "SecurityOriginData.h" |
30 | #include <wtf/HashTraits.h> |
31 | #include <wtf/URL.h> |
32 | #include <wtf/text/WTFString.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | class RegistrableDomain { |
37 | WTF_MAKE_FAST_ALLOCATED; |
38 | public: |
39 | RegistrableDomain() = default; |
40 | |
41 | explicit RegistrableDomain(const URL& url) |
42 | : RegistrableDomain(registrableDomainFromHost(url.host().toString())) |
43 | { |
44 | } |
45 | |
46 | explicit RegistrableDomain(const SecurityOriginData& origin) |
47 | : RegistrableDomain(registrableDomainFromHost(origin.host)) |
48 | { |
49 | } |
50 | |
51 | bool isEmpty() const { return m_registrableDomain.isEmpty() || m_registrableDomain == "nullOrigin"_s ; } |
52 | const String& string() const { return m_registrableDomain; } |
53 | |
54 | bool operator!=(const RegistrableDomain& other) const { return m_registrableDomain != other.m_registrableDomain; } |
55 | bool operator==(const RegistrableDomain& other) const { return m_registrableDomain == other.m_registrableDomain; } |
56 | |
57 | bool matches(const URL& url) const |
58 | { |
59 | return matches(url.host()); |
60 | } |
61 | |
62 | bool matches(const SecurityOriginData& origin) const |
63 | { |
64 | return matches(origin.host); |
65 | } |
66 | |
67 | RegistrableDomain isolatedCopy() const { return RegistrableDomain { m_registrableDomain.isolatedCopy() }; } |
68 | |
69 | RegistrableDomain(WTF::HashTableDeletedValueType) |
70 | : m_registrableDomain(WTF::HashTableDeletedValue) { } |
71 | bool isHashTableDeletedValue() const { return m_registrableDomain.isHashTableDeletedValue(); } |
72 | unsigned hash() const { return m_registrableDomain.hash(); } |
73 | |
74 | struct RegistrableDomainHash { |
75 | static unsigned hash(const RegistrableDomain& registrableDomain) { return registrableDomain.m_registrableDomain.hash(); } |
76 | static bool equal(const RegistrableDomain& a, const RegistrableDomain& b) { return a == b; } |
77 | static const bool safeToCompareToEmptyOrDeleted = false; |
78 | }; |
79 | |
80 | static RegistrableDomain uncheckedCreateFromRegistrableDomainString(const String& domain) |
81 | { |
82 | return RegistrableDomain { domain }; |
83 | } |
84 | |
85 | static RegistrableDomain uncheckedCreateFromHost(const String& host) |
86 | { |
87 | #if ENABLE(PUBLIC_SUFFIX_LIST) |
88 | auto registrableDomain = topPrivatelyControlledDomain(host); |
89 | if (registrableDomain.isEmpty()) |
90 | return uncheckedCreateFromRegistrableDomainString(host); |
91 | return RegistrableDomain { registrableDomain }; |
92 | #else |
93 | return uncheckedCreateFromRegistrableDomainString(host); |
94 | #endif |
95 | } |
96 | |
97 | template<class Encoder> void encode(Encoder&) const; |
98 | template<class Decoder> static Optional<RegistrableDomain> decode(Decoder&); |
99 | |
100 | protected: |
101 | |
102 | private: |
103 | explicit RegistrableDomain(const String& domain) |
104 | : m_registrableDomain { domain.isEmpty() ? "nullOrigin"_s : domain } |
105 | { |
106 | } |
107 | |
108 | bool matches(StringView host) const |
109 | { |
110 | if (host.isEmpty() && m_registrableDomain == "nullOrigin"_s ) |
111 | return true; |
112 | if (!host.endsWith(m_registrableDomain)) |
113 | return false; |
114 | if (host.length() == m_registrableDomain.length()) |
115 | return true; |
116 | return host[host.length() - m_registrableDomain.length() - 1] == '.'; |
117 | } |
118 | |
119 | static inline String registrableDomainFromHost(const String& host) |
120 | { |
121 | #if ENABLE(PUBLIC_SUFFIX_LIST) |
122 | auto domain = topPrivatelyControlledDomain(host); |
123 | #else |
124 | auto domain = host; |
125 | #endif |
126 | if (host.isEmpty()) |
127 | domain = "nullOrigin"_s ; |
128 | else if (domain.isEmpty()) |
129 | domain = host; |
130 | return domain; |
131 | } |
132 | |
133 | String m_registrableDomain; |
134 | }; |
135 | |
136 | template<class Encoder> |
137 | void RegistrableDomain::encode(Encoder& encoder) const |
138 | { |
139 | encoder << m_registrableDomain; |
140 | } |
141 | |
142 | template<class Decoder> |
143 | Optional<RegistrableDomain> RegistrableDomain::decode(Decoder& decoder) |
144 | { |
145 | Optional<String> domain; |
146 | decoder >> domain; |
147 | if (!domain) |
148 | return WTF::nullopt; |
149 | |
150 | RegistrableDomain registrableDomain; |
151 | registrableDomain.m_registrableDomain = WTFMove(*domain); |
152 | return registrableDomain; |
153 | } |
154 | |
155 | inline bool areRegistrableDomainsEqual(const URL& a, const URL& b) |
156 | { |
157 | return RegistrableDomain(a).matches(b); |
158 | } |
159 | |
160 | } // namespace WebCore |
161 | |
162 | namespace WTF { |
163 | template<> struct DefaultHash<WebCore::RegistrableDomain> { |
164 | using Hash = WebCore::RegistrableDomain::RegistrableDomainHash; |
165 | }; |
166 | template<> struct HashTraits<WebCore::RegistrableDomain> : SimpleClassHashTraits<WebCore::RegistrableDomain> { }; |
167 | } |
168 | |