Migrate from ints to unsigneds when referring to indices into strings
[WebKit-https.git] / Source / WebCore / platform / SchemeRegistry.cpp
1 /*
2  * Copyright (C) 2010 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 #include "config.h"
27 #include "SchemeRegistry.h"
28 #include <wtf/MainThread.h>
29 #include <wtf/NeverDestroyed.h>
30
31 #if USE(QUICK_LOOK)
32 #include "QuickLook.h"
33 #endif
34
35 namespace WebCore {
36
37 static URLSchemesMap& localURLSchemes()
38 {
39     static NeverDestroyed<URLSchemesMap> localSchemes;
40
41     if (localSchemes.get().isEmpty()) {
42         localSchemes.get().add("file");
43 #if PLATFORM(COCOA)
44         localSchemes.get().add("applewebdata");
45 #endif
46     }
47
48     return localSchemes;
49 }
50
51 static URLSchemesMap& displayIsolatedURLSchemes()
52 {
53     static NeverDestroyed<URLSchemesMap> displayIsolatedSchemes;
54     return displayIsolatedSchemes;
55 }
56
57 static URLSchemesMap& secureSchemes()
58 {
59     static NeverDestroyed<URLSchemesMap> secureSchemes;
60
61     if (secureSchemes.get().isEmpty()) {
62         secureSchemes.get().add("https");
63         secureSchemes.get().add("about");
64         secureSchemes.get().add("data");
65         secureSchemes.get().add("wss");
66 #if USE(QUICK_LOOK)
67         secureSchemes.get().add(QLPreviewProtocol());
68 #endif
69 #if PLATFORM(GTK)
70         secureSchemes.get().add("resource");
71 #endif
72     }
73
74     return secureSchemes;
75 }
76
77 static URLSchemesMap& schemesWithUniqueOrigins()
78 {
79     static NeverDestroyed<URLSchemesMap> schemesWithUniqueOrigins;
80
81     if (schemesWithUniqueOrigins.get().isEmpty()) {
82         schemesWithUniqueOrigins.get().add("about");
83         schemesWithUniqueOrigins.get().add("javascript");
84         // This is a willful violation of HTML5.
85         // See https://bugs.webkit.org/show_bug.cgi?id=11885
86         schemesWithUniqueOrigins.get().add("data");
87     }
88
89     return schemesWithUniqueOrigins;
90 }
91
92 static URLSchemesMap& emptyDocumentSchemes()
93 {
94     static NeverDestroyed<URLSchemesMap> emptyDocumentSchemes;
95
96     if (emptyDocumentSchemes.get().isEmpty())
97         emptyDocumentSchemes.get().add("about");
98
99     return emptyDocumentSchemes;
100 }
101
102 static HashSet<String, ASCIICaseInsensitiveHash>& schemesForbiddenFromDomainRelaxation()
103 {
104     static NeverDestroyed<HashSet<String, ASCIICaseInsensitiveHash>> schemes;
105     return schemes;
106 }
107
108 static URLSchemesMap& canDisplayOnlyIfCanRequestSchemes()
109 {
110     static NeverDestroyed<URLSchemesMap> canDisplayOnlyIfCanRequestSchemes;
111
112     if (canDisplayOnlyIfCanRequestSchemes.get().isEmpty()) {
113         canDisplayOnlyIfCanRequestSchemes.get().add("blob");
114     }
115
116     return canDisplayOnlyIfCanRequestSchemes;
117 }
118
119 static URLSchemesMap& notAllowingJavascriptURLsSchemes()
120 {
121     static NeverDestroyed<URLSchemesMap> notAllowingJavascriptURLsSchemes;
122     return notAllowingJavascriptURLsSchemes;
123 }
124
125 void SchemeRegistry::registerURLSchemeAsLocal(const String& scheme)
126 {
127     localURLSchemes().add(scheme);
128 }
129
130 void SchemeRegistry::removeURLSchemeRegisteredAsLocal(const String& scheme)
131 {
132     if (equalLettersIgnoringASCIICase(scheme, "file"))
133         return;
134 #if PLATFORM(COCOA)
135     if (equalLettersIgnoringASCIICase(scheme, "applewebdata"))
136         return;
137 #endif
138     localURLSchemes().remove(scheme);
139 }
140
141 const URLSchemesMap& SchemeRegistry::localSchemes()
142 {
143     return localURLSchemes();
144 }
145
146 static URLSchemesMap& schemesAllowingLocalStorageAccessInPrivateBrowsing()
147 {
148     static NeverDestroyed<URLSchemesMap> schemesAllowingLocalStorageAccessInPrivateBrowsing;
149     return schemesAllowingLocalStorageAccessInPrivateBrowsing;
150 }
151
152 static URLSchemesMap& schemesAllowingDatabaseAccessInPrivateBrowsing()
153 {
154     static NeverDestroyed<URLSchemesMap> schemesAllowingDatabaseAccessInPrivateBrowsing;
155     return schemesAllowingDatabaseAccessInPrivateBrowsing;
156 }
157
158 static URLSchemesMap& CORSEnabledSchemes()
159 {
160     // FIXME: http://bugs.webkit.org/show_bug.cgi?id=77160
161     static NeverDestroyed<URLSchemesMap> CORSEnabledSchemes;
162
163     if (CORSEnabledSchemes.get().isEmpty()) {
164         CORSEnabledSchemes.get().add("http");
165         CORSEnabledSchemes.get().add("https");
166     }
167
168     return CORSEnabledSchemes;
169 }
170
171 static URLSchemesMap& ContentSecurityPolicyBypassingSchemes()
172 {
173     static NeverDestroyed<URLSchemesMap> schemes;
174     return schemes;
175 }
176
177 #if ENABLE(CACHE_PARTITIONING)
178 static URLSchemesMap& cachePartitioningSchemes()
179 {
180     static NeverDestroyed<URLSchemesMap> schemes;
181     return schemes;
182 }
183 #endif
184
185 static URLSchemesMap& alwaysRevalidatedSchemes()
186 {
187     static NeverDestroyed<URLSchemesMap> schemes;
188     return schemes;
189 }
190
191 bool SchemeRegistry::shouldTreatURLSchemeAsLocal(const String& scheme)
192 {
193     if (scheme.isEmpty())
194         return false;
195     return localURLSchemes().contains(scheme);
196 }
197
198 void SchemeRegistry::registerURLSchemeAsNoAccess(const String& scheme)
199 {
200     schemesWithUniqueOrigins().add(scheme);
201 }
202
203 bool SchemeRegistry::shouldTreatURLSchemeAsNoAccess(const String& scheme)
204 {
205     if (scheme.isEmpty())
206         return false;
207     return schemesWithUniqueOrigins().contains(scheme);
208 }
209
210 void SchemeRegistry::registerURLSchemeAsDisplayIsolated(const String& scheme)
211 {
212     displayIsolatedURLSchemes().add(scheme);
213 }
214
215 bool SchemeRegistry::shouldTreatURLSchemeAsDisplayIsolated(const String& scheme)
216 {
217     if (scheme.isEmpty())
218         return false;
219     return displayIsolatedURLSchemes().contains(scheme);
220 }
221
222 void SchemeRegistry::registerURLSchemeAsSecure(const String& scheme)
223 {
224     secureSchemes().add(scheme);
225 }
226
227 bool SchemeRegistry::shouldTreatURLSchemeAsSecure(const String& scheme)
228 {
229     if (scheme.isEmpty())
230         return false;
231     return secureSchemes().contains(scheme);
232 }
233
234 void SchemeRegistry::registerURLSchemeAsEmptyDocument(const String& scheme)
235 {
236     emptyDocumentSchemes().add(scheme);
237 }
238
239 bool SchemeRegistry::shouldLoadURLSchemeAsEmptyDocument(const String& scheme)
240 {
241     if (scheme.isEmpty())
242         return false;
243     return emptyDocumentSchemes().contains(scheme);
244 }
245
246 void SchemeRegistry::setDomainRelaxationForbiddenForURLScheme(bool forbidden, const String& scheme)
247 {
248     if (scheme.isEmpty())
249         return;
250
251     if (forbidden)
252         schemesForbiddenFromDomainRelaxation().add(scheme);
253     else
254         schemesForbiddenFromDomainRelaxation().remove(scheme);
255 }
256
257 bool SchemeRegistry::isDomainRelaxationForbiddenForURLScheme(const String& scheme)
258 {
259     if (scheme.isEmpty())
260         return false;
261     return schemesForbiddenFromDomainRelaxation().contains(scheme);
262 }
263
264 bool SchemeRegistry::canDisplayOnlyIfCanRequest(const String& scheme)
265 {
266     if (scheme.isEmpty())
267         return false;
268     return canDisplayOnlyIfCanRequestSchemes().contains(scheme);
269 }
270
271 void SchemeRegistry::registerAsCanDisplayOnlyIfCanRequest(const String& scheme)
272 {
273     canDisplayOnlyIfCanRequestSchemes().add(scheme);
274 }
275
276 void SchemeRegistry::registerURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
277 {
278     notAllowingJavascriptURLsSchemes().add(scheme);
279 }
280
281 bool SchemeRegistry::shouldTreatURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
282 {
283     if (scheme.isEmpty())
284         return false;
285     return notAllowingJavascriptURLsSchemes().contains(scheme);
286 }
287
288 void SchemeRegistry::registerURLSchemeAsAllowingLocalStorageAccessInPrivateBrowsing(const String& scheme)
289 {
290     schemesAllowingLocalStorageAccessInPrivateBrowsing().add(scheme);
291 }
292
293 bool SchemeRegistry::allowsLocalStorageAccessInPrivateBrowsing(const String& scheme)
294 {
295     if (scheme.isEmpty())
296         return false;
297     return schemesAllowingLocalStorageAccessInPrivateBrowsing().contains(scheme);
298 }
299
300 void SchemeRegistry::registerURLSchemeAsAllowingDatabaseAccessInPrivateBrowsing(const String& scheme)
301 {
302     schemesAllowingDatabaseAccessInPrivateBrowsing().add(scheme);
303 }
304
305 bool SchemeRegistry::allowsDatabaseAccessInPrivateBrowsing(const String& scheme)
306 {
307     if (scheme.isEmpty())
308         return false;
309     return schemesAllowingDatabaseAccessInPrivateBrowsing().contains(scheme);
310 }
311
312 void SchemeRegistry::registerURLSchemeAsCORSEnabled(const String& scheme)
313 {
314     CORSEnabledSchemes().add(scheme);
315 }
316
317 bool SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(const String& scheme)
318 {
319     if (scheme.isEmpty())
320         return false;
321     return CORSEnabledSchemes().contains(scheme);
322 }
323
324 void SchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme)
325 {
326     ContentSecurityPolicyBypassingSchemes().add(scheme);
327 }
328
329 void SchemeRegistry::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme)
330 {
331     ContentSecurityPolicyBypassingSchemes().remove(scheme);
332 }
333
334 bool SchemeRegistry::schemeShouldBypassContentSecurityPolicy(const String& scheme)
335 {
336     if (scheme.isEmpty())
337         return false;
338     return ContentSecurityPolicyBypassingSchemes().contains(scheme);
339 }
340
341 void SchemeRegistry::registerURLSchemeAsAlwaysRevalidated(const String& scheme)
342 {
343     alwaysRevalidatedSchemes().add(scheme);
344 }
345
346 bool SchemeRegistry::shouldAlwaysRevalidateURLScheme(const String& scheme)
347 {
348     if (scheme.isEmpty())
349         return false;
350     return alwaysRevalidatedSchemes().contains(scheme);
351 }
352
353 #if ENABLE(CACHE_PARTITIONING)
354 void SchemeRegistry::registerURLSchemeAsCachePartitioned(const String& scheme)
355 {
356     cachePartitioningSchemes().add(scheme);
357 }
358
359 bool SchemeRegistry::shouldPartitionCacheForURLScheme(const String& scheme)
360 {
361     if (scheme.isEmpty())
362         return false;
363     return cachePartitioningSchemes().contains(scheme);
364 }
365 #endif
366
367 } // namespace WebCore