initial import
[vuplus_webkit] / Source / WebKit2 / Shared / WebPreferencesStore.cpp
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
54 WebPreferencesStore::WebPreferencesStore()
55 {
56 }
57
58 void WebPreferencesStore::encode(CoreIPC::ArgumentEncoder* encoder) const
59 {
60     encoder->encode(m_stringValues);
61     encoder->encode(m_boolValues);
62     encoder->encode(m_uint32Values);
63     encoder->encode(m_doubleValues);
64 }
65
66 bool WebPreferencesStore::decode(CoreIPC::ArgumentDecoder* decoder, WebPreferencesStore& result)
67 {
68     if (!decoder->decode(result.m_stringValues))
69         return false;
70     if (!decoder->decode(result.m_boolValues))
71         return false;
72     if (!decoder->decode(result.m_uint32Values))
73         return false;
74     if (!decoder->decode(result.m_doubleValues))
75         return false;
76
77     if (hasXSSAuditorEnabledTestRunnerOverride)
78         result.m_boolValues.set(WebPreferencesKey::xssAuditorEnabledKey(), xssAuditorEnabledTestRunnerOverride);
79
80     return true;
81 }
82
83 void WebPreferencesStore::overrideXSSAuditorEnabledForTestRunner(bool enabled)
84 {
85     hasXSSAuditorEnabledTestRunnerOverride = true;
86     xssAuditorEnabledTestRunnerOverride = enabled;
87 }
88
89 void WebPreferencesStore::removeTestRunnerOverrides()
90 {
91     hasXSSAuditorEnabledTestRunnerOverride = false;
92 }
93
94
95 template<typename MappedType>
96 MappedType defaultValueForKey(const String&);
97
98 template<>
99 String defaultValueForKey(const String& key)
100 {
101     static HashMap<String, String>& defaults = *new HashMap<String, String>;
102     if (defaults.isEmpty()) {
103 #define DEFINE_STRING_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
104         FOR_EACH_WEBKIT_STRING_PREFERENCE(DEFINE_STRING_DEFAULTS)
105 #undef DEFINE_STRING_DEFAULTS
106     }
107
108     return defaults.get(key);
109 }
110
111 template<>
112 bool defaultValueForKey(const String& key)
113 {
114     static HashMap<String, bool>& defaults = *new HashMap<String, bool>;
115     if (defaults.isEmpty()) {
116 #define DEFINE_BOOL_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
117         FOR_EACH_WEBKIT_BOOL_PREFERENCE(DEFINE_BOOL_DEFAULTS)
118 #undef DEFINE_BOOL_DEFAULTS
119     }
120
121     return defaults.get(key);
122 }
123
124 template<>
125 uint32_t defaultValueForKey(const String& key)
126 {
127     static HashMap<String, uint32_t>& defaults = *new HashMap<String, uint32_t>;
128     if (defaults.isEmpty()) {
129 #define DEFINE_UINT32_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
130         FOR_EACH_WEBKIT_UINT32_PREFERENCE(DEFINE_UINT32_DEFAULTS)
131 #undef DEFINE_UINT32_DEFAULTS
132     }
133
134     return defaults.get(key);
135 }
136
137 template<>
138 double defaultValueForKey(const String& key)
139 {
140     static HashMap<String, double>& defaults = *new HashMap<String, double>;
141     if (defaults.isEmpty()) {
142 #define DEFINE_DOUBLE_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
143         FOR_EACH_WEBKIT_DOUBLE_PREFERENCE(DEFINE_DOUBLE_DEFAULTS)
144 #undef DEFINE_DOUBLE_DEFAULTS
145     }
146
147     return defaults.get(key);
148 }
149
150 template<typename MapType>
151 static typename MapType::MappedType valueForKey(const MapType& map, const typename MapType::KeyType& key)
152 {
153     typename MapType::const_iterator it = map.find(key);
154     if (it != map.end())
155         return it->second;
156
157     return defaultValueForKey<typename MapType::MappedType>(key);
158 }
159
160 template<typename MapType>
161 static bool setValueForKey(MapType& map, const typename MapType::KeyType& key, const typename MapType::MappedType& value)
162 {
163     typename MapType::MappedType existingValue = valueForKey(map, key);
164     if (existingValue == value)
165         return false;
166     
167     map.set(key, value);
168     return true;
169 }
170
171 bool WebPreferencesStore::setStringValueForKey(const String& key, const String& value)
172 {
173     return setValueForKey(m_stringValues, key, value);
174 }
175
176 String WebPreferencesStore::getStringValueForKey(const String& key) const
177 {
178     return valueForKey(m_stringValues, key);
179 }
180
181 bool WebPreferencesStore::setBoolValueForKey(const String& key, bool value)
182 {
183     return setValueForKey(m_boolValues, key, value);
184 }
185
186 bool WebPreferencesStore::getBoolValueForKey(const String& key) const
187 {
188     return valueForKey(m_boolValues, key);
189 }
190
191 bool WebPreferencesStore::setUInt32ValueForKey(const String& key, uint32_t value) 
192 {
193     return setValueForKey(m_uint32Values, key, value);
194 }
195
196 uint32_t WebPreferencesStore::getUInt32ValueForKey(const String& key) const
197 {
198     return valueForKey(m_uint32Values, key);
199 }
200
201 bool WebPreferencesStore::setDoubleValueForKey(const String& key, double value) 
202 {
203     return setValueForKey(m_doubleValues, key, value);
204 }
205
206 double WebPreferencesStore::getDoubleValueForKey(const String& key) const
207 {
208     return valueForKey(m_doubleValues, key);
209 }
210
211 } // namespace WebKit