Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / weborigin / 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 COMPUTER, 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
27 #include "config.h"
28 #include "platform/weborigin/SchemeRegistry.h"
29
30 #include "wtf/MainThread.h"
31
32 namespace WebCore {
33
34 static URLSchemesMap& localURLSchemes()
35 {
36     DEFINE_STATIC_LOCAL(URLSchemesMap, localSchemes, ());
37
38     if (localSchemes.isEmpty())
39         localSchemes.add("file");
40
41     return localSchemes;
42 }
43
44 static URLSchemesMap& displayIsolatedURLSchemes()
45 {
46     DEFINE_STATIC_LOCAL(URLSchemesMap, displayIsolatedSchemes, ());
47     return displayIsolatedSchemes;
48 }
49
50 static URLSchemesMap& secureSchemes()
51 {
52     DEFINE_STATIC_LOCAL(URLSchemesMap, secureSchemes, ());
53
54     if (secureSchemes.isEmpty()) {
55         secureSchemes.add("https");
56         secureSchemes.add("about");
57         secureSchemes.add("data");
58         secureSchemes.add("wss");
59     }
60
61     return secureSchemes;
62 }
63
64 static URLSchemesMap& schemesWithUniqueOrigins()
65 {
66     DEFINE_STATIC_LOCAL(URLSchemesMap, schemesWithUniqueOrigins, ());
67
68     if (schemesWithUniqueOrigins.isEmpty()) {
69         schemesWithUniqueOrigins.add("about");
70         schemesWithUniqueOrigins.add("javascript");
71         // This is a willful violation of HTML5.
72         // See https://bugs.webkit.org/show_bug.cgi?id=11885
73         schemesWithUniqueOrigins.add("data");
74     }
75
76     return schemesWithUniqueOrigins;
77 }
78
79 static URLSchemesMap& emptyDocumentSchemes()
80 {
81     DEFINE_STATIC_LOCAL(URLSchemesMap, emptyDocumentSchemes, ());
82
83     if (emptyDocumentSchemes.isEmpty())
84         emptyDocumentSchemes.add("about");
85
86     return emptyDocumentSchemes;
87 }
88
89 static HashSet<String>& schemesForbiddenFromDomainRelaxation()
90 {
91     DEFINE_STATIC_LOCAL(HashSet<String>, schemes, ());
92     return schemes;
93 }
94
95 static URLSchemesMap& canDisplayOnlyIfCanRequestSchemes()
96 {
97     DEFINE_STATIC_LOCAL(URLSchemesMap, canDisplayOnlyIfCanRequestSchemes, ());
98
99     if (canDisplayOnlyIfCanRequestSchemes.isEmpty()) {
100         canDisplayOnlyIfCanRequestSchemes.add("blob");
101         canDisplayOnlyIfCanRequestSchemes.add("filesystem");
102     }
103
104     return canDisplayOnlyIfCanRequestSchemes;
105 }
106
107 static URLSchemesMap& notAllowingJavascriptURLsSchemes()
108 {
109     DEFINE_STATIC_LOCAL(URLSchemesMap, notAllowingJavascriptURLsSchemes, ());
110     return notAllowingJavascriptURLsSchemes;
111 }
112
113 void SchemeRegistry::registerURLSchemeAsLocal(const String& scheme)
114 {
115     localURLSchemes().add(scheme);
116 }
117
118 void SchemeRegistry::removeURLSchemeRegisteredAsLocal(const String& scheme)
119 {
120     if (scheme == "file")
121         return;
122     localURLSchemes().remove(scheme);
123 }
124
125 const URLSchemesMap& SchemeRegistry::localSchemes()
126 {
127     return localURLSchemes();
128 }
129
130 static URLSchemesMap& CORSEnabledSchemes()
131 {
132     // FIXME: http://bugs.webkit.org/show_bug.cgi?id=77160
133     DEFINE_STATIC_LOCAL(URLSchemesMap, CORSEnabledSchemes, ());
134
135     if (CORSEnabledSchemes.isEmpty()) {
136         CORSEnabledSchemes.add("http");
137         CORSEnabledSchemes.add("https");
138     }
139
140     return CORSEnabledSchemes;
141 }
142
143 static URLSchemesMap& ContentSecurityPolicyBypassingSchemes()
144 {
145     DEFINE_STATIC_LOCAL(URLSchemesMap, schemes, ());
146     return schemes;
147 }
148
149 bool SchemeRegistry::shouldTreatURLSchemeAsLocal(const String& scheme)
150 {
151     if (scheme.isEmpty())
152         return false;
153     return localURLSchemes().contains(scheme);
154 }
155
156 void SchemeRegistry::registerURLSchemeAsNoAccess(const String& scheme)
157 {
158     schemesWithUniqueOrigins().add(scheme);
159 }
160
161 bool SchemeRegistry::shouldTreatURLSchemeAsNoAccess(const String& scheme)
162 {
163     if (scheme.isEmpty())
164         return false;
165     return schemesWithUniqueOrigins().contains(scheme);
166 }
167
168 void SchemeRegistry::registerURLSchemeAsDisplayIsolated(const String& scheme)
169 {
170     displayIsolatedURLSchemes().add(scheme);
171 }
172
173 bool SchemeRegistry::shouldTreatURLSchemeAsDisplayIsolated(const String& scheme)
174 {
175     if (scheme.isEmpty())
176         return false;
177     return displayIsolatedURLSchemes().contains(scheme);
178 }
179
180 void SchemeRegistry::registerURLSchemeAsSecure(const String& scheme)
181 {
182     secureSchemes().add(scheme);
183 }
184
185 bool SchemeRegistry::shouldTreatURLSchemeAsSecure(const String& scheme)
186 {
187     if (scheme.isEmpty())
188         return false;
189     return secureSchemes().contains(scheme);
190 }
191
192 void SchemeRegistry::registerURLSchemeAsEmptyDocument(const String& scheme)
193 {
194     emptyDocumentSchemes().add(scheme);
195 }
196
197 bool SchemeRegistry::shouldLoadURLSchemeAsEmptyDocument(const String& scheme)
198 {
199     if (scheme.isEmpty())
200         return false;
201     return emptyDocumentSchemes().contains(scheme);
202 }
203
204 void SchemeRegistry::setDomainRelaxationForbiddenForURLScheme(bool forbidden, const String& scheme)
205 {
206     if (scheme.isEmpty())
207         return;
208
209     if (forbidden)
210         schemesForbiddenFromDomainRelaxation().add(scheme);
211     else
212         schemesForbiddenFromDomainRelaxation().remove(scheme);
213 }
214
215 bool SchemeRegistry::isDomainRelaxationForbiddenForURLScheme(const String& scheme)
216 {
217     if (scheme.isEmpty())
218         return false;
219     return schemesForbiddenFromDomainRelaxation().contains(scheme);
220 }
221
222 bool SchemeRegistry::canDisplayOnlyIfCanRequest(const String& scheme)
223 {
224     if (scheme.isEmpty())
225         return false;
226     return canDisplayOnlyIfCanRequestSchemes().contains(scheme);
227 }
228
229 void SchemeRegistry::registerAsCanDisplayOnlyIfCanRequest(const String& scheme)
230 {
231     canDisplayOnlyIfCanRequestSchemes().add(scheme);
232 }
233
234 void SchemeRegistry::registerURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
235 {
236     notAllowingJavascriptURLsSchemes().add(scheme);
237 }
238
239 bool SchemeRegistry::shouldTreatURLSchemeAsNotAllowingJavascriptURLs(const String& scheme)
240 {
241     if (scheme.isEmpty())
242         return false;
243     return notAllowingJavascriptURLsSchemes().contains(scheme);
244 }
245
246 void SchemeRegistry::registerURLSchemeAsCORSEnabled(const String& scheme)
247 {
248     CORSEnabledSchemes().add(scheme);
249 }
250
251 bool SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(const String& scheme)
252 {
253     if (scheme.isEmpty())
254         return false;
255     return CORSEnabledSchemes().contains(scheme);
256 }
257
258 void SchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme)
259 {
260     ContentSecurityPolicyBypassingSchemes().add(scheme);
261 }
262
263 void SchemeRegistry::removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme)
264 {
265     ContentSecurityPolicyBypassingSchemes().remove(scheme);
266 }
267
268 bool SchemeRegistry::schemeShouldBypassContentSecurityPolicy(const String& scheme)
269 {
270     if (scheme.isEmpty())
271         return false;
272     return ContentSecurityPolicyBypassingSchemes().contains(scheme);
273 }
274
275 } // namespace WebCore