1 | /* |
2 | * Copyright (C) 2015 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 | #include "config.h" |
27 | #include "WebsiteDataRecord.h" |
28 | |
29 | #include <WebCore/LocalizedStrings.h> |
30 | #include <WebCore/PublicSuffix.h> |
31 | #include <WebCore/RegistrableDomain.h> |
32 | #include <WebCore/SecurityOrigin.h> |
33 | |
34 | #if PLATFORM(COCOA) |
35 | #import <pal/spi/cf/CFNetworkSPI.h> |
36 | #endif |
37 | |
38 | static String displayNameForLocalFiles() |
39 | { |
40 | return WEB_UI_STRING("Local documents on your computer" , "'Website' name displayed when local documents have stored local data" ); |
41 | } |
42 | |
43 | namespace WebKit { |
44 | |
45 | String WebsiteDataRecord::displayNameForCookieHostName(const String& hostName) |
46 | { |
47 | #if PLATFORM(COCOA) |
48 | if (hostName == String(kCFHTTPCookieLocalFileDomain)) |
49 | return displayNameForLocalFiles(); |
50 | #else |
51 | if (hostName == "localhost" ) |
52 | return hostName; |
53 | #endif |
54 | return displayNameForHostName(hostName); |
55 | } |
56 | |
57 | String WebsiteDataRecord::displayNameForHostName(const String& hostName) |
58 | { |
59 | #if ENABLE(PUBLIC_SUFFIX_LIST) |
60 | return WebCore::topPrivatelyControlledDomain(hostName); |
61 | #endif |
62 | |
63 | return String(); |
64 | } |
65 | |
66 | String WebsiteDataRecord::displayNameForOrigin(const WebCore::SecurityOriginData& securityOrigin) |
67 | { |
68 | const auto& protocol = securityOrigin.protocol; |
69 | |
70 | if (protocol == "file" ) |
71 | return displayNameForLocalFiles(); |
72 | |
73 | #if ENABLE(PUBLIC_SUFFIX_LIST) |
74 | if (protocol == "http" || protocol == "https" ) |
75 | return WebCore::topPrivatelyControlledDomain(securityOrigin.host); |
76 | #endif |
77 | |
78 | return String(); |
79 | } |
80 | |
81 | void WebsiteDataRecord::add(WebsiteDataType type, const WebCore::SecurityOriginData& origin) |
82 | { |
83 | types.add(type); |
84 | origins.add(origin); |
85 | } |
86 | |
87 | void WebsiteDataRecord::addCookieHostName(const String& hostName) |
88 | { |
89 | types.add(WebsiteDataType::Cookies); |
90 | cookieHostNames.add(hostName); |
91 | } |
92 | |
93 | #if ENABLE(NETSCAPE_PLUGIN_API) |
94 | void WebsiteDataRecord::addPluginDataHostName(const String& hostName) |
95 | { |
96 | types.add(WebsiteDataType::PlugInData); |
97 | pluginDataHostNames.add(hostName); |
98 | } |
99 | #endif |
100 | |
101 | void WebsiteDataRecord::addHSTSCacheHostname(const String& hostName) |
102 | { |
103 | types.add(WebsiteDataType::HSTSCache); |
104 | HSTSCacheHostNames.add(hostName); |
105 | } |
106 | |
107 | static inline bool hostIsInDomain(StringView host, StringView domain) |
108 | { |
109 | if (!host.endsWithIgnoringASCIICase(domain)) |
110 | return false; |
111 | |
112 | ASSERT(host.length() >= domain.length()); |
113 | unsigned suffixOffset = host.length() - domain.length(); |
114 | return !suffixOffset || host[suffixOffset - 1] == '.'; |
115 | } |
116 | |
117 | bool WebsiteDataRecord::matches(const WebCore::RegistrableDomain& domain) const |
118 | { |
119 | if (domain.isEmpty()) |
120 | return false; |
121 | |
122 | if (types.contains(WebsiteDataType::Cookies)) { |
123 | for (const auto& hostName : cookieHostNames) { |
124 | if (hostIsInDomain(hostName, domain.string())) |
125 | return true; |
126 | } |
127 | } |
128 | |
129 | for (const auto& dataRecordOriginData : origins) { |
130 | if (hostIsInDomain(dataRecordOriginData.host, domain.string())) |
131 | return true; |
132 | } |
133 | |
134 | return false; |
135 | } |
136 | |
137 | String WebsiteDataRecord::topPrivatelyControlledDomain() |
138 | { |
139 | #if ENABLE(PUBLIC_SUFFIX_LIST) |
140 | if (!cookieHostNames.isEmpty()) |
141 | return WebCore::topPrivatelyControlledDomain(cookieHostNames.takeAny()); |
142 | |
143 | if (!origins.isEmpty()) |
144 | return WebCore::topPrivatelyControlledDomain(origins.takeAny().securityOrigin().get().host()); |
145 | |
146 | #if ENABLE(NETSCAPE_PLUGIN_API) |
147 | if (!pluginDataHostNames.isEmpty()) |
148 | return WebCore::topPrivatelyControlledDomain(pluginDataHostNames.takeAny()); |
149 | #endif |
150 | |
151 | #endif // ENABLE(PUBLIC_SUFFIX_LIST) |
152 | |
153 | return emptyString(); |
154 | } |
155 | |
156 | } |
157 | |