Home | History | Annotate | Download | only in Shared
      1 /*
      2  * Copyright (C) 2010, 2011 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 "WebPreferencesStore.h"
     28 
     29 #include "FontSmoothingLevel.h"
     30 #include "WebCoreArgumentCoders.h"
     31 #include <WebCore/Settings.h>
     32 
     33 namespace WebKit {
     34 
     35 namespace WebPreferencesKey {
     36 
     37 #define DEFINE_KEY_GETTERS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) \
     38         const String& KeyLower##Key() \
     39         { \
     40             DEFINE_STATIC_LOCAL(String, key, (#KeyUpper)); \
     41             return key; \
     42         }
     43 
     44     FOR_EACH_WEBKIT_PREFERENCE(DEFINE_KEY_GETTERS)
     45 
     46 #undef DEFINE_KEY_GETTERS
     47 
     48 } // namespace WebPreferencesKey
     49 
     50 
     51 static bool hasXSSAuditorEnabledTestRunnerOverride;
     52 static bool xssAuditorEnabledTestRunnerOverride;
     53 static bool hasAllowUniversalAccessFromFileURLsTestRunnerOverride;
     54 static bool allowUniversalAccessFromFileURLsTestRunnerOverride;
     55 static bool hasAllowFileAccessFromFileURLsTestRunnerOverride;
     56 static bool allowFileAccessFromFileURLsTestRunnerOverride;
     57 
     58 WebPreferencesStore::WebPreferencesStore()
     59 {
     60 }
     61 
     62 void WebPreferencesStore::encode(CoreIPC::ArgumentEncoder* encoder) const
     63 {
     64     encoder->encode(CoreIPC::In(m_stringValues, m_boolValues, m_uint32Values, m_doubleValues));
     65 }
     66 
     67 bool WebPreferencesStore::decode(CoreIPC::ArgumentDecoder* decoder, WebPreferencesStore& s)
     68 {
     69     if (!decoder->decode(CoreIPC::Out(s.m_stringValues, s.m_boolValues, s.m_uint32Values, s.m_doubleValues)))
     70         return false;
     71 
     72     if (hasXSSAuditorEnabledTestRunnerOverride)
     73         s.m_boolValues.set(WebPreferencesKey::xssAuditorEnabledKey(), xssAuditorEnabledTestRunnerOverride);
     74 
     75     if (hasAllowUniversalAccessFromFileURLsTestRunnerOverride)
     76         s.m_boolValues.set(WebPreferencesKey::allowUniversalAccessFromFileURLsKey(), allowUniversalAccessFromFileURLsTestRunnerOverride);
     77 
     78     if (hasAllowFileAccessFromFileURLsTestRunnerOverride)
     79         s.m_boolValues.set(WebPreferencesKey::allowFileAccessFromFileURLsKey(), allowFileAccessFromFileURLsTestRunnerOverride);
     80 
     81     return true;
     82 }
     83 
     84 void WebPreferencesStore::overrideXSSAuditorEnabledForTestRunner(bool enabled)
     85 {
     86     hasXSSAuditorEnabledTestRunnerOverride = true;
     87     xssAuditorEnabledTestRunnerOverride = enabled;
     88 }
     89 
     90 void WebPreferencesStore::overrideAllowUniversalAccessFromFileURLsForTestRunner(bool enabled)
     91 {
     92     hasAllowUniversalAccessFromFileURLsTestRunnerOverride = true;
     93     allowUniversalAccessFromFileURLsTestRunnerOverride = enabled;
     94 }
     95 
     96 void WebPreferencesStore::overrideAllowFileAccessFromFileURLsForTestRunner(bool enabled)
     97 {
     98     hasAllowFileAccessFromFileURLsTestRunnerOverride = true;
     99     allowFileAccessFromFileURLsTestRunnerOverride = enabled;
    100 }
    101 
    102 void WebPreferencesStore::removeTestRunnerOverrides()
    103 {
    104     hasXSSAuditorEnabledTestRunnerOverride = false;
    105 }
    106 
    107 
    108 template<typename MappedType>
    109 MappedType defaultValueForKey(const String&);
    110 
    111 template<>
    112 String defaultValueForKey(const String& key)
    113 {
    114     static HashMap<String, String>& defaults = *new HashMap<String, String>;
    115     if (defaults.isEmpty()) {
    116 #define DEFINE_STRING_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
    117         FOR_EACH_WEBKIT_STRING_PREFERENCE(DEFINE_STRING_DEFAULTS)
    118 #undef DEFINE_STRING_DEFAULTS
    119     }
    120 
    121     return defaults.get(key);
    122 }
    123 
    124 template<>
    125 bool defaultValueForKey(const String& key)
    126 {
    127     static HashMap<String, bool>& defaults = *new HashMap<String, bool>;
    128     if (defaults.isEmpty()) {
    129 #define DEFINE_BOOL_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
    130         FOR_EACH_WEBKIT_BOOL_PREFERENCE(DEFINE_BOOL_DEFAULTS)
    131 #undef DEFINE_BOOL_DEFAULTS
    132     }
    133 
    134     return defaults.get(key);
    135 }
    136 
    137 template<>
    138 uint32_t defaultValueForKey(const String& key)
    139 {
    140     static HashMap<String, uint32_t>& defaults = *new HashMap<String, uint32_t>;
    141     if (defaults.isEmpty()) {
    142 #define DEFINE_UINT32_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
    143         FOR_EACH_WEBKIT_UINT32_PREFERENCE(DEFINE_UINT32_DEFAULTS)
    144 #undef DEFINE_UINT32_DEFAULTS
    145     }
    146 
    147     return defaults.get(key);
    148 }
    149 
    150 template<>
    151 double defaultValueForKey(const String& key)
    152 {
    153     static HashMap<String, double>& defaults = *new HashMap<String, double>;
    154     if (defaults.isEmpty()) {
    155 #define DEFINE_DOUBLE_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
    156         FOR_EACH_WEBKIT_DOUBLE_PREFERENCE(DEFINE_DOUBLE_DEFAULTS)
    157 #undef DEFINE_DOUBLE_DEFAULTS
    158     }
    159 
    160     return defaults.get(key);
    161 }
    162 
    163 template<typename MapType>
    164 static typename MapType::MappedType valueForKey(const MapType& map, const typename MapType::KeyType& key)
    165 {
    166     typename MapType::const_iterator it = map.find(key);
    167     if (it != map.end())
    168         return it->second;
    169 
    170     return defaultValueForKey<typename MapType::MappedType>(key);
    171 }
    172 
    173 template<typename MapType>
    174 static bool setValueForKey(MapType& map, const typename MapType::KeyType& key, const typename MapType::MappedType& value)
    175 {
    176     typename MapType::MappedType existingValue = valueForKey(map, key);
    177     if (existingValue == value)
    178         return false;
    179 
    180     map.set(key, value);
    181     return true;
    182 }
    183 
    184 bool WebPreferencesStore::setStringValueForKey(const String& key, const String& value)
    185 {
    186     return setValueForKey(m_stringValues, key, value);
    187 }
    188 
    189 String WebPreferencesStore::getStringValueForKey(const String& key) const
    190 {
    191     return valueForKey(m_stringValues, key);
    192 }
    193 
    194 bool WebPreferencesStore::setBoolValueForKey(const String& key, bool value)
    195 {
    196     return setValueForKey(m_boolValues, key, value);
    197 }
    198 
    199 bool WebPreferencesStore::getBoolValueForKey(const String& key) const
    200 {
    201     return valueForKey(m_boolValues, key);
    202 }
    203 
    204 bool WebPreferencesStore::setUInt32ValueForKey(const String& key, uint32_t value)
    205 {
    206     return setValueForKey(m_uint32Values, key, value);
    207 }
    208 
    209 uint32_t WebPreferencesStore::getUInt32ValueForKey(const String& key) const
    210 {
    211     return valueForKey(m_uint32Values, key);
    212 }
    213 
    214 bool WebPreferencesStore::setDoubleValueForKey(const String& key, double value)
    215 {
    216     return setValueForKey(m_doubleValues, key, value);
    217 }
    218 
    219 double WebPreferencesStore::getDoubleValueForKey(const String& key) const
    220 {
    221     return valueForKey(m_doubleValues, key);
    222 }
    223 
    224 } // namespace WebKit
    225