WebCore/platform/SchemeRegistry.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 #include "config.h"
       
    27 #include "SchemeRegistry.h"
       
    28 
       
    29 namespace WebCore {
       
    30 
       
    31 static URLSchemesMap& localURLSchemes()
       
    32 {
       
    33     DEFINE_STATIC_LOCAL(URLSchemesMap, localSchemes, ());
       
    34 
       
    35     if (localSchemes.isEmpty()) {
       
    36         localSchemes.add("file");
       
    37 #if PLATFORM(MAC)
       
    38         localSchemes.add("applewebdata");
       
    39 #endif
       
    40 #if PLATFORM(QT)
       
    41         localSchemes.add("qrc");
       
    42 #endif
       
    43     }
       
    44 
       
    45     return localSchemes;
       
    46 }
       
    47 
       
    48 static URLSchemesMap& secureSchemes()
       
    49 {
       
    50     DEFINE_STATIC_LOCAL(URLSchemesMap, secureSchemes, ());
       
    51 
       
    52     if (secureSchemes.isEmpty()) {
       
    53         secureSchemes.add("https");
       
    54         secureSchemes.add("about");
       
    55         secureSchemes.add("data");
       
    56     }
       
    57 
       
    58     return secureSchemes;
       
    59 }
       
    60 
       
    61 static URLSchemesMap& schemesWithUniqueOrigins()
       
    62 {
       
    63     DEFINE_STATIC_LOCAL(URLSchemesMap, schemesWithUniqueOrigins, ());
       
    64 
       
    65     // This is a willful violation of HTML5.
       
    66     // See https://bugs.webkit.org/show_bug.cgi?id=11885
       
    67     if (schemesWithUniqueOrigins.isEmpty())
       
    68         schemesWithUniqueOrigins.add("data");
       
    69 
       
    70     return schemesWithUniqueOrigins;
       
    71 }
       
    72 
       
    73 void SchemeRegistry::registerURLSchemeAsLocal(const String& scheme)
       
    74 {
       
    75     WebCore::localURLSchemes().add(scheme);
       
    76 }
       
    77 
       
    78 void SchemeRegistry::removeURLSchemeRegisteredAsLocal(const String& scheme)
       
    79 {
       
    80     if (scheme == "file")
       
    81         return;
       
    82 #if PLATFORM(MAC)
       
    83     if (scheme == "applewebdata")
       
    84         return;
       
    85 #endif
       
    86     WebCore::localURLSchemes().remove(scheme);
       
    87 }
       
    88 
       
    89 const URLSchemesMap& SchemeRegistry::localURLSchemes()
       
    90 {
       
    91     return WebCore::localURLSchemes();
       
    92 }
       
    93 
       
    94 bool SchemeRegistry::shouldTreatURLAsLocal(const String& url)
       
    95 {
       
    96     // This avoids an allocation of another String and the HashSet contains()
       
    97     // call for the file: and http: schemes.
       
    98     if (url.length() >= 5) {
       
    99         const UChar* s = url.characters();
       
   100         if (s[0] == 'h' && s[1] == 't' && s[2] == 't' && s[3] == 'p' && s[4] == ':')
       
   101             return false;
       
   102         if (s[0] == 'f' && s[1] == 'i' && s[2] == 'l' && s[3] == 'e' && s[4] == ':')
       
   103             return true;
       
   104     }
       
   105 
       
   106     int loc = url.find(':');
       
   107     if (loc == -1)
       
   108         return false;
       
   109 
       
   110     String scheme = url.left(loc);
       
   111     return WebCore::localURLSchemes().contains(scheme);
       
   112 }
       
   113 
       
   114 bool SchemeRegistry::shouldTreatURLSchemeAsLocal(const String& scheme)
       
   115 {
       
   116     // This avoids an allocation of another String and the HashSet contains()
       
   117     // call for the file: and http: schemes.
       
   118     if (scheme.length() == 4) {
       
   119         const UChar* s = scheme.characters();
       
   120         if (s[0] == 'h' && s[1] == 't' && s[2] == 't' && s[3] == 'p')
       
   121             return false;
       
   122         if (s[0] == 'f' && s[1] == 'i' && s[2] == 'l' && s[3] == 'e')
       
   123             return true;
       
   124     }
       
   125 
       
   126     if (scheme.isEmpty())
       
   127         return false;
       
   128 
       
   129     return WebCore::localURLSchemes().contains(scheme);
       
   130 }
       
   131 
       
   132 void SchemeRegistry::registerURLSchemeAsNoAccess(const String& scheme)
       
   133 {
       
   134     schemesWithUniqueOrigins().add(scheme);
       
   135 }
       
   136 
       
   137 bool SchemeRegistry::shouldTreatURLSchemeAsNoAccess(const String& scheme)
       
   138 {
       
   139     return schemesWithUniqueOrigins().contains(scheme);
       
   140 }
       
   141 
       
   142 void SchemeRegistry::registerURLSchemeAsSecure(const String& scheme)
       
   143 {
       
   144     secureSchemes().add(scheme);
       
   145 }
       
   146 
       
   147 bool SchemeRegistry::shouldTreatURLSchemeAsSecure(const String& scheme)
       
   148 {
       
   149     return secureSchemes().contains(scheme);
       
   150 }
       
   151 
       
   152 } // namespace WebCore