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>
35 namespace WebPreferencesKey {
37 #define DEFINE_KEY_GETTERS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) \
38 const String& KeyLower##Key() \
40 DEFINE_STATIC_LOCAL(String, key, (#KeyUpper)); \
44 FOR_EACH_WEBKIT_PREFERENCE(DEFINE_KEY_GETTERS)
46 #undef DEFINE_KEY_GETTERS
48 } // namespace WebPreferencesKey
50 typedef HashMap<String, bool> BoolOverridesMap;
52 static BoolOverridesMap& boolTestRunnerOverridesMap()
54 DEFINE_STATIC_LOCAL(BoolOverridesMap, map, ());
58 WebPreferencesStore::WebPreferencesStore()
62 void WebPreferencesStore::encode(CoreIPC::ArgumentEncoder* encoder) const
64 encoder->encode(m_stringValues);
65 encoder->encode(m_boolValues);
66 encoder->encode(m_uint32Values);
67 encoder->encode(m_doubleValues);
70 bool WebPreferencesStore::decode(CoreIPC::ArgumentDecoder* decoder, WebPreferencesStore& result)
72 if (!decoder->decode(result.m_stringValues))
74 if (!decoder->decode(result.m_boolValues))
76 if (!decoder->decode(result.m_uint32Values))
78 if (!decoder->decode(result.m_doubleValues))
83 void WebPreferencesStore::overrideBoolValueForKey(const String& key, bool value)
85 boolTestRunnerOverridesMap().set(key, value);
88 void WebPreferencesStore::removeTestRunnerOverrides()
90 boolTestRunnerOverridesMap().clear();
94 template<typename MappedType>
95 MappedType defaultValueForKey(const String&);
98 String defaultValueForKey(const String& key)
100 static HashMap<String, String>& defaults = *new HashMap<String, String>;
101 if (defaults.isEmpty()) {
102 #define DEFINE_STRING_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
103 FOR_EACH_WEBKIT_STRING_PREFERENCE(DEFINE_STRING_DEFAULTS)
104 #undef DEFINE_STRING_DEFAULTS
107 return defaults.get(key);
111 bool defaultValueForKey(const String& key)
113 static HashMap<String, bool>& defaults = *new HashMap<String, bool>;
114 if (defaults.isEmpty()) {
115 #define DEFINE_BOOL_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
116 FOR_EACH_WEBKIT_BOOL_PREFERENCE(DEFINE_BOOL_DEFAULTS)
117 #if ENABLE(TIZEN_PREFERENCE)
118 FOR_EACH_WEBKIT_TIZEN_BOOL_PREFERENCE(DEFINE_BOOL_DEFAULTS)
120 #undef DEFINE_BOOL_DEFAULTS
123 return defaults.get(key);
127 uint32_t defaultValueForKey(const String& key)
129 static HashMap<String, uint32_t>& defaults = *new HashMap<String, uint32_t>;
130 if (defaults.isEmpty()) {
131 #define DEFINE_UINT32_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
132 FOR_EACH_WEBKIT_UINT32_PREFERENCE(DEFINE_UINT32_DEFAULTS)
133 #undef DEFINE_UINT32_DEFAULTS
136 return defaults.get(key);
140 double defaultValueForKey(const String& key)
142 static HashMap<String, double>& defaults = *new HashMap<String, double>;
143 if (defaults.isEmpty()) {
144 #define DEFINE_DOUBLE_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
145 FOR_EACH_WEBKIT_DOUBLE_PREFERENCE(DEFINE_DOUBLE_DEFAULTS)
146 #if ENABLE(TIZEN_PREFERENCE)
147 FOR_EACH_WEBKIT_TIZEN_DOUBLE_PREFERENCE(DEFINE_DOUBLE_DEFAULTS)
149 #undef DEFINE_DOUBLE_DEFAULTS
152 return defaults.get(key);
155 template<typename MapType>
156 static typename MapType::MappedType valueForKey(const MapType& map, const typename MapType::KeyType& key)
158 typename MapType::const_iterator it = map.find(key);
162 return defaultValueForKey<typename MapType::MappedType>(key);
165 template<typename MapType>
166 static bool setValueForKey(MapType& map, const typename MapType::KeyType& key, const typename MapType::MappedType& value)
168 typename MapType::MappedType existingValue = valueForKey(map, key);
169 if (existingValue == value)
176 bool WebPreferencesStore::setStringValueForKey(const String& key, const String& value)
178 return setValueForKey(m_stringValues, key, value);
181 String WebPreferencesStore::getStringValueForKey(const String& key) const
183 return valueForKey(m_stringValues, key);
186 bool WebPreferencesStore::setBoolValueForKey(const String& key, bool value)
188 return setValueForKey(m_boolValues, key, value);
191 bool WebPreferencesStore::getBoolValueForKey(const String& key) const
193 // FIXME: Extend overriding to other key types used from LayoutTestController.
194 BoolOverridesMap::const_iterator it = boolTestRunnerOverridesMap().find(key);
195 if (it != boolTestRunnerOverridesMap().end())
197 return valueForKey(m_boolValues, key);
200 bool WebPreferencesStore::setUInt32ValueForKey(const String& key, uint32_t value)
202 return setValueForKey(m_uint32Values, key, value);
205 uint32_t WebPreferencesStore::getUInt32ValueForKey(const String& key) const
207 return valueForKey(m_uint32Values, key);
210 bool WebPreferencesStore::setDoubleValueForKey(const String& key, double value)
212 return setValueForKey(m_doubleValues, key, value);
215 double WebPreferencesStore::getDoubleValueForKey(const String& key) const
217 return valueForKey(m_doubleValues, key);
220 } // namespace WebKit