WebPreferencesStore.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "config.h"
  26. #include "WebPreferencesStore.h"
  27. #include "FontSmoothingLevel.h"
  28. #include "WebCoreArgumentCoders.h"
  29. #include <WebCore/Settings.h>
  30. namespace WebKit {
  31. namespace WebPreferencesKey {
  32. #define DEFINE_KEY_GETTERS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) \
  33. const String& KeyLower##Key() \
  34. { \
  35. DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral(#KeyUpper))); \
  36. return key; \
  37. }
  38. FOR_EACH_WEBKIT_PREFERENCE(DEFINE_KEY_GETTERS)
  39. #undef DEFINE_KEY_GETTERS
  40. } // namespace WebPreferencesKey
  41. typedef HashMap<String, bool> BoolOverridesMap;
  42. static BoolOverridesMap& boolTestRunnerOverridesMap()
  43. {
  44. DEFINE_STATIC_LOCAL(BoolOverridesMap, map, ());
  45. return map;
  46. }
  47. WebPreferencesStore::WebPreferencesStore()
  48. {
  49. }
  50. void WebPreferencesStore::encode(CoreIPC::ArgumentEncoder& encoder) const
  51. {
  52. encoder << m_stringValues;
  53. encoder << m_boolValues;
  54. encoder << m_uint32Values;
  55. encoder << m_doubleValues;
  56. encoder << m_floatValues;
  57. }
  58. bool WebPreferencesStore::decode(CoreIPC::ArgumentDecoder& decoder, WebPreferencesStore& result)
  59. {
  60. if (!decoder.decode(result.m_stringValues))
  61. return false;
  62. if (!decoder.decode(result.m_boolValues))
  63. return false;
  64. if (!decoder.decode(result.m_uint32Values))
  65. return false;
  66. if (!decoder.decode(result.m_doubleValues))
  67. return false;
  68. if (!decoder.decode(result.m_floatValues))
  69. return false;
  70. return true;
  71. }
  72. void WebPreferencesStore::overrideBoolValueForKey(const String& key, bool value)
  73. {
  74. boolTestRunnerOverridesMap().set(key, value);
  75. }
  76. void WebPreferencesStore::removeTestRunnerOverrides()
  77. {
  78. boolTestRunnerOverridesMap().clear();
  79. }
  80. template<typename MappedType>
  81. MappedType defaultValueForKey(const String&);
  82. template<>
  83. String defaultValueForKey(const String& key)
  84. {
  85. static HashMap<String, String>& defaults = *new HashMap<String, String>;
  86. if (defaults.isEmpty()) {
  87. #define DEFINE_STRING_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
  88. FOR_EACH_WEBKIT_STRING_PREFERENCE(DEFINE_STRING_DEFAULTS)
  89. FOR_EACH_WEBKIT_STRING_PREFERENCE_NOT_IN_WEBCORE(DEFINE_STRING_DEFAULTS)
  90. #undef DEFINE_STRING_DEFAULTS
  91. }
  92. return defaults.get(key);
  93. }
  94. template<>
  95. bool defaultValueForKey(const String& key)
  96. {
  97. static HashMap<String, bool>& defaults = *new HashMap<String, bool>;
  98. if (defaults.isEmpty()) {
  99. #define DEFINE_BOOL_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
  100. FOR_EACH_WEBKIT_BOOL_PREFERENCE(DEFINE_BOOL_DEFAULTS)
  101. #undef DEFINE_BOOL_DEFAULTS
  102. }
  103. return defaults.get(key);
  104. }
  105. template<>
  106. uint32_t defaultValueForKey(const String& key)
  107. {
  108. static HashMap<String, uint32_t>& defaults = *new HashMap<String, uint32_t>;
  109. if (defaults.isEmpty()) {
  110. #define DEFINE_UINT32_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
  111. FOR_EACH_WEBKIT_UINT32_PREFERENCE(DEFINE_UINT32_DEFAULTS)
  112. #undef DEFINE_UINT32_DEFAULTS
  113. }
  114. return defaults.get(key);
  115. }
  116. template<>
  117. double defaultValueForKey(const String& key)
  118. {
  119. static HashMap<String, double>& defaults = *new HashMap<String, double>;
  120. if (defaults.isEmpty()) {
  121. #define DEFINE_DOUBLE_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
  122. FOR_EACH_WEBKIT_DOUBLE_PREFERENCE(DEFINE_DOUBLE_DEFAULTS)
  123. #undef DEFINE_DOUBLE_DEFAULTS
  124. }
  125. return defaults.get(key);
  126. }
  127. template<>
  128. float defaultValueForKey(const String& key)
  129. {
  130. static HashMap<String, float>& defaults = *new HashMap<String, float>;
  131. if (defaults.isEmpty()) {
  132. #define DEFINE_FLOAT_DEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) defaults.set(WebPreferencesKey::KeyLower##Key(), DefaultValue);
  133. FOR_EACH_WEBKIT_FLOAT_PREFERENCE(DEFINE_FLOAT_DEFAULTS)
  134. #undef DEFINE_FLOAT_DEFAULTS
  135. }
  136. return defaults.get(key);
  137. }
  138. template<typename MapType>
  139. static typename MapType::MappedType valueForKey(const MapType& map, const typename MapType::KeyType& key)
  140. {
  141. typename MapType::const_iterator it = map.find(key);
  142. if (it != map.end())
  143. return it->value;
  144. return defaultValueForKey<typename MapType::MappedType>(key);
  145. }
  146. template<typename MapType>
  147. static bool setValueForKey(MapType& map, const typename MapType::KeyType& key, const typename MapType::MappedType& value)
  148. {
  149. typename MapType::MappedType existingValue = valueForKey(map, key);
  150. if (existingValue == value)
  151. return false;
  152. map.set(key, value);
  153. return true;
  154. }
  155. bool WebPreferencesStore::setStringValueForKey(const String& key, const String& value)
  156. {
  157. return setValueForKey(m_stringValues, key, value);
  158. }
  159. String WebPreferencesStore::getStringValueForKey(const String& key) const
  160. {
  161. return valueForKey(m_stringValues, key);
  162. }
  163. bool WebPreferencesStore::setBoolValueForKey(const String& key, bool value)
  164. {
  165. return setValueForKey(m_boolValues, key, value);
  166. }
  167. bool WebPreferencesStore::getBoolValueForKey(const String& key) const
  168. {
  169. // FIXME: Extend overriding to other key types used from TestRunner.
  170. BoolOverridesMap::const_iterator it = boolTestRunnerOverridesMap().find(key);
  171. if (it != boolTestRunnerOverridesMap().end())
  172. return it->value;
  173. return valueForKey(m_boolValues, key);
  174. }
  175. bool WebPreferencesStore::setUInt32ValueForKey(const String& key, uint32_t value)
  176. {
  177. return setValueForKey(m_uint32Values, key, value);
  178. }
  179. uint32_t WebPreferencesStore::getUInt32ValueForKey(const String& key) const
  180. {
  181. return valueForKey(m_uint32Values, key);
  182. }
  183. bool WebPreferencesStore::setDoubleValueForKey(const String& key, double value)
  184. {
  185. return setValueForKey(m_doubleValues, key, value);
  186. }
  187. double WebPreferencesStore::getDoubleValueForKey(const String& key) const
  188. {
  189. return valueForKey(m_doubleValues, key);
  190. }
  191. bool WebPreferencesStore::setFloatValueForKey(const String& key, float value)
  192. {
  193. return setValueForKey(m_floatValues, key, value);
  194. }
  195. float WebPreferencesStore::getFloatValueForKey(const String& key) const
  196. {
  197. return valueForKey(m_floatValues, key);
  198. }
  199. } // namespace WebKit