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 "WebCoreArgumentCoders.h" |
30 | #include "WebPreferencesKeys.h" |
31 | #include <wtf/NeverDestroyed.h> |
32 | |
33 | namespace WebKit { |
34 | |
35 | typedef HashMap<String, bool> BoolOverridesMap; |
36 | |
37 | static BoolOverridesMap& boolTestRunnerOverridesMap() |
38 | { |
39 | static NeverDestroyed<BoolOverridesMap> map; |
40 | return map; |
41 | } |
42 | |
43 | WebPreferencesStore::WebPreferencesStore() |
44 | { |
45 | } |
46 | |
47 | void WebPreferencesStore::encode(IPC::Encoder& encoder) const |
48 | { |
49 | encoder << m_values; |
50 | encoder << m_overriddenDefaults; |
51 | } |
52 | |
53 | bool WebPreferencesStore::decode(IPC::Decoder& decoder, WebPreferencesStore& result) |
54 | { |
55 | Optional<HashMap<String, Value>> values; |
56 | decoder >> values; |
57 | if (!values) |
58 | return false; |
59 | result.m_values = WTFMove(*values); |
60 | |
61 | Optional<HashMap<String, Value>> overriddenDefaults; |
62 | decoder >> overriddenDefaults; |
63 | if (!overriddenDefaults) |
64 | return false; |
65 | result.m_overriddenDefaults = WTFMove(*overriddenDefaults); |
66 | return true; |
67 | } |
68 | |
69 | void WebPreferencesStore::overrideBoolValueForKey(const String& key, bool value) |
70 | { |
71 | boolTestRunnerOverridesMap().set(key, value); |
72 | } |
73 | |
74 | void WebPreferencesStore::removeTestRunnerOverrides() |
75 | { |
76 | boolTestRunnerOverridesMap().clear(); |
77 | } |
78 | |
79 | template<typename MappedType> |
80 | static MappedType valueForKey(const WebPreferencesStore::ValueMap& values, const WebPreferencesStore::ValueMap& overriddenDefaults, const String& key) |
81 | { |
82 | auto valuesIt = values.find(key); |
83 | if (valuesIt != values.end() && WTF::holds_alternative<MappedType>(valuesIt->value)) |
84 | return WTF::get<MappedType>(valuesIt->value); |
85 | |
86 | auto overriddenDefaultsIt = overriddenDefaults.find(key); |
87 | if (overriddenDefaultsIt != overriddenDefaults.end() && WTF::holds_alternative<MappedType>(overriddenDefaultsIt->value)) |
88 | return WTF::get<MappedType>(overriddenDefaultsIt->value); |
89 | |
90 | auto& defaultsMap = WebPreferencesStore::defaults(); |
91 | auto defaultsIt = defaultsMap.find(key); |
92 | if (defaultsIt != defaultsMap.end() && WTF::holds_alternative<MappedType>(defaultsIt->value)) |
93 | return WTF::get<MappedType>(defaultsIt->value); |
94 | |
95 | return MappedType(); |
96 | } |
97 | |
98 | template<typename MappedType> |
99 | static bool setValueForKey(WebPreferencesStore::ValueMap& map, const WebPreferencesStore::ValueMap& overriddenDefaults, const String& key, const MappedType& value) |
100 | { |
101 | MappedType existingValue = valueForKey<MappedType>(map, overriddenDefaults, key); |
102 | if (existingValue == value) |
103 | return false; |
104 | |
105 | map.set(key, WebPreferencesStore::Value(value)); |
106 | return true; |
107 | } |
108 | |
109 | bool WebPreferencesStore::setStringValueForKey(const String& key, const String& value) |
110 | { |
111 | return setValueForKey<String>(m_values, m_overriddenDefaults, key, value); |
112 | } |
113 | |
114 | String WebPreferencesStore::getStringValueForKey(const String& key) const |
115 | { |
116 | return valueForKey<String>(m_values, m_overriddenDefaults, key); |
117 | } |
118 | |
119 | bool WebPreferencesStore::setBoolValueForKey(const String& key, bool value) |
120 | { |
121 | return setValueForKey<bool>(m_values, m_overriddenDefaults, key, value); |
122 | } |
123 | |
124 | bool WebPreferencesStore::getBoolValueForKey(const String& key) const |
125 | { |
126 | // FIXME: Extend overriding to other key types used from TestRunner. |
127 | auto it = boolTestRunnerOverridesMap().find(key); |
128 | if (it != boolTestRunnerOverridesMap().end()) |
129 | return it->value; |
130 | |
131 | return valueForKey<bool>(m_values, m_overriddenDefaults, key); |
132 | } |
133 | |
134 | bool WebPreferencesStore::setUInt32ValueForKey(const String& key, uint32_t value) |
135 | { |
136 | return setValueForKey<uint32_t>(m_values, m_overriddenDefaults, key, value); |
137 | } |
138 | |
139 | uint32_t WebPreferencesStore::getUInt32ValueForKey(const String& key) const |
140 | { |
141 | return valueForKey<uint32_t>(m_values, m_overriddenDefaults, key); |
142 | } |
143 | |
144 | bool WebPreferencesStore::setDoubleValueForKey(const String& key, double value) |
145 | { |
146 | return setValueForKey<double>(m_values, m_overriddenDefaults, key, value); |
147 | } |
148 | |
149 | double WebPreferencesStore::getDoubleValueForKey(const String& key) const |
150 | { |
151 | return valueForKey<double>(m_values, m_overriddenDefaults, key); |
152 | } |
153 | |
154 | // Overridden Defaults |
155 | |
156 | void WebPreferencesStore::setOverrideDefaultsStringValueForKey(const String& key, String value) |
157 | { |
158 | m_overriddenDefaults.set(key, Value(value)); |
159 | } |
160 | |
161 | void WebPreferencesStore::setOverrideDefaultsBoolValueForKey(const String& key, bool value) |
162 | { |
163 | m_overriddenDefaults.set(key, Value(value)); |
164 | } |
165 | |
166 | void WebPreferencesStore::setOverrideDefaultsUInt32ValueForKey(const String& key, uint32_t value) |
167 | { |
168 | m_overriddenDefaults.set(key, Value(value)); |
169 | } |
170 | |
171 | void WebPreferencesStore::setOverrideDefaultsDoubleValueForKey(const String& key, double value) |
172 | { |
173 | m_overriddenDefaults.set(key, Value(value)); |
174 | } |
175 | |
176 | } // namespace WebKit |
177 | |