2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebPreferencesStore.h"
29 #include "FontSmoothingLevel.h"
30 #include "WebCoreArgumentCoders.h"
31 #include <WebCore/Settings.h>
32 #include <wtf/NeverDestroyed.h>
35 #import <WebKitSystemInterfaceIOS.h>
40 namespace WebPreferencesKey {
42 #define DEFINE_KEY_GETTERS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) \
43 const String& KeyLower##Key() \
45 static NeverDestroyed<String> key(ASCIILiteral(#KeyUpper)); \
49 FOR_EACH_WEBKIT_PREFERENCE(DEFINE_KEY_GETTERS)
51 #undef DEFINE_KEY_GETTERS
53 } // namespace WebPreferencesKey
55 typedef HashMap<String, bool> BoolOverridesMap;
57 static BoolOverridesMap& boolTestRunnerOverridesMap()
59 static NeverDestroyed<BoolOverridesMap> map;
63 WebPreferencesStore::WebPreferencesStore()
67 void WebPreferencesStore::encode(IPC::ArgumentEncoder& encoder) const
69 encoder << m_stringValues;
70 encoder << m_boolValues;
71 encoder << m_uint32Values;
72 encoder << m_doubleValues;
73 encoder << m_floatValues;
76 bool WebPreferencesStore::decode(IPC::ArgumentDecoder& decoder, WebPreferencesStore& result)
78 if (!decoder.decode(result.m_stringValues))
80 if (!decoder.decode(result.m_boolValues))
82 if (!decoder.decode(result.m_uint32Values))
84 if (!decoder.decode(result.m_doubleValues))
86 if (!decoder.decode(result.m_floatValues))
91 void WebPreferencesStore::overrideBoolValueForKey(const String& key, bool value)
93 boolTestRunnerOverridesMap().set(key, value);
96 void WebPreferencesStore::removeTestRunnerOverrides()
98 boolTestRunnerOverridesMap().clear();
102 template<typename MappedType>
103 MappedType defaultValueForKey(const String&);
106 String defaultValueForKey(const String& key)
108 static HashMap<String, String>& defaults = *new HashMap<String, String>;
109 if (defaults.isEmpty()) {
110 #define DEFINE_STRING_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
111 FOR_EACH_WEBKIT_STRING_PREFERENCE(DEFINE_STRING_DEFAULTS)
112 FOR_EACH_WEBKIT_STRING_PREFERENCE_NOT_IN_WEBCORE(DEFINE_STRING_DEFAULTS)
113 #undef DEFINE_STRING_DEFAULTS
116 return defaults.get(key);
120 bool defaultValueForKey(const String& key)
122 static HashMap<String, bool>& defaults = *new HashMap<String, bool>;
123 if (defaults.isEmpty()) {
124 #define DEFINE_BOOL_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
125 FOR_EACH_WEBKIT_BOOL_PREFERENCE(DEFINE_BOOL_DEFAULTS)
126 #undef DEFINE_BOOL_DEFAULTS
129 return defaults.get(key);
133 uint32_t defaultValueForKey(const String& key)
135 static HashMap<String, uint32_t>& defaults = *new HashMap<String, uint32_t>;
136 if (defaults.isEmpty()) {
137 #define DEFINE_UINT32_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
138 FOR_EACH_WEBKIT_UINT32_PREFERENCE(DEFINE_UINT32_DEFAULTS)
139 #undef DEFINE_UINT32_DEFAULTS
142 return defaults.get(key);
146 double defaultValueForKey(const String& key)
148 static HashMap<String, double>& defaults = *new HashMap<String, double>;
149 if (defaults.isEmpty()) {
150 #define DEFINE_DOUBLE_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
151 FOR_EACH_WEBKIT_DOUBLE_PREFERENCE(DEFINE_DOUBLE_DEFAULTS)
152 #undef DEFINE_DOUBLE_DEFAULTS
155 return defaults.get(key);
159 float defaultValueForKey(const String& key)
161 static HashMap<String, float>& defaults = *new HashMap<String, float>;
162 if (defaults.isEmpty()) {
163 #define DEFINE_FLOAT_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
164 FOR_EACH_WEBKIT_FLOAT_PREFERENCE(DEFINE_FLOAT_DEFAULTS)
165 #undef DEFINE_FLOAT_DEFAULTS
168 return defaults.get(key);
171 template<typename MapType>
172 static typename MapType::MappedType valueForKey(const MapType& map, const typename MapType::KeyType& key)
174 typename MapType::const_iterator it = map.find(key);
178 return defaultValueForKey<typename MapType::MappedType>(key);
181 template<typename MapType>
182 static bool setValueForKey(MapType& map, const typename MapType::KeyType& key, const typename MapType::MappedType& value)
184 typename MapType::MappedType existingValue = valueForKey(map, key);
185 if (existingValue == value)
192 bool WebPreferencesStore::setStringValueForKey(const String& key, const String& value)
194 return setValueForKey(m_stringValues, key, value);
197 String WebPreferencesStore::getStringValueForKey(const String& key) const
199 return valueForKey(m_stringValues, key);
202 bool WebPreferencesStore::setBoolValueForKey(const String& key, bool value)
204 return setValueForKey(m_boolValues, key, value);
207 bool WebPreferencesStore::getBoolValueForKey(const String& key) const
209 // FIXME: Extend overriding to other key types used from TestRunner.
210 BoolOverridesMap::const_iterator it = boolTestRunnerOverridesMap().find(key);
211 if (it != boolTestRunnerOverridesMap().end())
213 return valueForKey(m_boolValues, key);
216 bool WebPreferencesStore::setUInt32ValueForKey(const String& key, uint32_t value)
218 return setValueForKey(m_uint32Values, key, value);
221 uint32_t WebPreferencesStore::getUInt32ValueForKey(const String& key) const
223 return valueForKey(m_uint32Values, key);
226 bool WebPreferencesStore::setDoubleValueForKey(const String& key, double value)
228 return setValueForKey(m_doubleValues, key, value);
231 double WebPreferencesStore::getDoubleValueForKey(const String& key) const
233 return valueForKey(m_doubleValues, key);
236 bool WebPreferencesStore::setFloatValueForKey(const String& key, float value)
238 return setValueForKey(m_floatValues, key, value);
241 float WebPreferencesStore::getFloatValueForKey(const String& key) const
243 return valueForKey(m_floatValues, key);
246 } // namespace WebKit