OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 //#include "Values.h" |
6 #include <cmath> | |
7 | 6 |
8 namespace blink { | 7 {% for namespace in config.protocol.namespace %} |
9 namespace protocol { | 8 namespace {{namespace}} { |
| 9 {% endfor %} |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 const char* const nullValueString = "null"; | 13 const char* const nullValueString = "null"; |
14 const char* const trueValueString = "true"; | 14 const char* const trueValueString = "true"; |
15 const char* const falseValueString = "false"; | 15 const char* const falseValueString = "false"; |
16 | 16 |
17 inline bool escapeChar(UChar c, String16Builder* dst) | 17 inline bool escapeChar(uint16_t c, StringBuilder* dst) |
18 { | 18 { |
19 switch (c) { | 19 switch (c) { |
20 case '\b': dst->append("\\b"); break; | 20 case '\b': dst->append("\\b"); break; |
21 case '\f': dst->append("\\f"); break; | 21 case '\f': dst->append("\\f"); break; |
22 case '\n': dst->append("\\n"); break; | 22 case '\n': dst->append("\\n"); break; |
23 case '\r': dst->append("\\r"); break; | 23 case '\r': dst->append("\\r"); break; |
24 case '\t': dst->append("\\t"); break; | 24 case '\t': dst->append("\\t"); break; |
25 case '\\': dst->append("\\\\"); break; | 25 case '\\': dst->append("\\\\"); break; |
26 case '"': dst->append("\\\""); break; | 26 case '"': dst->append("\\\""); break; |
27 default: | 27 default: |
28 return false; | 28 return false; |
29 } | 29 } |
30 return true; | 30 return true; |
31 } | 31 } |
32 | 32 |
33 const char hexDigits[17] = "0123456789ABCDEF"; | 33 const char hexDigits[17] = "0123456789ABCDEF"; |
34 | 34 |
35 void appendUnsignedAsHex(UChar number, String16Builder* dst) | 35 void appendUnsignedAsHex(uint16_t number, StringBuilder* dst) |
36 { | 36 { |
37 dst->append("\\u"); | 37 dst->append("\\u"); |
38 for (size_t i = 0; i < 4; ++i) { | 38 for (size_t i = 0; i < 4; ++i) { |
39 UChar c = hexDigits[(number & 0xF000) >> 12]; | 39 uint16_t c = hexDigits[(number & 0xF000) >> 12]; |
40 dst->append(c); | 40 dst->append(c); |
41 number <<= 4; | 41 number <<= 4; |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 void escapeStringForJSON(const String16& str, String16Builder* dst) | 45 void escapeStringForJSON(const String& str, StringBuilder* dst) |
46 { | 46 { |
47 for (unsigned i = 0; i < str.length(); ++i) { | 47 for (unsigned i = 0; i < str.length(); ++i) { |
48 UChar c = str[i]; | 48 uint16_t c = str[i]; |
49 if (!escapeChar(c, dst)) { | 49 if (!escapeChar(c, dst)) { |
50 if (c < 32 || c > 126 || c == '<' || c == '>') { | 50 if (c < 32 || c > 126 || c == '<' || c == '>') { |
51 // 1. Escaping <, > to prevent script execution. | 51 // 1. Escaping <, > to prevent script execution. |
52 // 2. Technically, we could also pass through c > 126 as UTF8, b
ut this | 52 // 2. Technically, we could also pass through c > 126 as UTF8, b
ut this |
53 // is also optional. It would also be a pain to implement her
e. | 53 // is also optional. It would also be a pain to implement her
e. |
54 appendUnsignedAsHex(c, dst); | 54 appendUnsignedAsHex(c, dst); |
55 } else { | 55 } else { |
56 dst->append(c); | 56 dst->append(c); |
57 } | 57 } |
58 } | 58 } |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 void doubleQuoteStringForJSON(const String16& str, String16Builder* dst) | 62 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) |
63 { | 63 { |
64 dst->append('"'); | 64 dst->append('"'); |
65 escapeStringForJSON(str, dst); | 65 escapeStringForJSON(str, dst); |
66 dst->append('"'); | 66 dst->append('"'); |
67 } | 67 } |
68 | 68 |
69 } // anonymous namespace | 69 } // anonymous namespace |
70 | 70 |
71 bool Value::asBoolean(bool*) const | 71 bool Value::asBoolean(bool*) const |
72 { | 72 { |
73 return false; | 73 return false; |
74 } | 74 } |
75 | 75 |
76 bool Value::asDouble(double*) const | 76 bool Value::asDouble(double*) const |
77 { | 77 { |
78 return false; | 78 return false; |
79 } | 79 } |
80 | 80 |
81 bool Value::asInteger(int*) const | 81 bool Value::asInteger(int*) const |
82 { | 82 { |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 bool Value::asString(String16*) const | 86 bool Value::asString(String*) const |
87 { | 87 { |
88 return false; | 88 return false; |
89 } | 89 } |
90 | 90 |
91 bool Value::asSerialized(String16*) const | 91 bool Value::asSerialized(String*) const |
92 { | 92 { |
93 return false; | 93 return false; |
94 } | 94 } |
95 | 95 |
96 String16 Value::toJSONString() const | 96 String Value::toJSONString() const |
97 { | 97 { |
98 String16Builder result; | 98 StringBuilder result; |
99 result.reserveCapacity(512); | 99 StringUtil::builderReserve(result, 512); |
100 writeJSON(&result); | 100 writeJSON(&result); |
101 return result.toString(); | 101 return result.toString(); |
102 } | 102 } |
103 | 103 |
104 void Value::writeJSON(String16Builder* output) const | 104 void Value::writeJSON(StringBuilder* output) const |
105 { | 105 { |
106 DCHECK(m_type == TypeNull); | 106 DCHECK(m_type == TypeNull); |
107 output->append(nullValueString, 4); | 107 output->append(nullValueString, 4); |
108 } | 108 } |
109 | 109 |
110 std::unique_ptr<Value> Value::clone() const | 110 std::unique_ptr<Value> Value::clone() const |
111 { | 111 { |
112 return Value::null(); | 112 return Value::null(); |
113 } | 113 } |
114 | 114 |
(...skipping 19 matching lines...) Expand all Loading... |
134 } | 134 } |
135 | 135 |
136 bool FundamentalValue::asInteger(int* output) const | 136 bool FundamentalValue::asInteger(int* output) const |
137 { | 137 { |
138 if (type() != TypeInteger) | 138 if (type() != TypeInteger) |
139 return false; | 139 return false; |
140 *output = m_integerValue; | 140 *output = m_integerValue; |
141 return true; | 141 return true; |
142 } | 142 } |
143 | 143 |
144 void FundamentalValue::writeJSON(String16Builder* output) const | 144 void FundamentalValue::writeJSON(StringBuilder* output) const |
145 { | 145 { |
146 DCHECK(type() == TypeBoolean || type() == TypeInteger || type() == TypeDoubl
e); | 146 DCHECK(type() == TypeBoolean || type() == TypeInteger || type() == TypeDoubl
e); |
147 if (type() == TypeBoolean) { | 147 if (type() == TypeBoolean) { |
148 if (m_boolValue) | 148 if (m_boolValue) |
149 output->append(trueValueString, 4); | 149 output->append(trueValueString, 4); |
150 else | 150 else |
151 output->append(falseValueString, 5); | 151 output->append(falseValueString, 5); |
152 } else if (type() == TypeDouble) { | 152 } else if (type() == TypeDouble) { |
153 if (!std::isfinite(m_doubleValue)) { | 153 if (!std::isfinite(m_doubleValue)) { |
154 output->append(nullValueString, 4); | 154 output->append(nullValueString, 4); |
155 return; | 155 return; |
156 } | 156 } |
157 output->append(String16::fromDouble(m_doubleValue)); | 157 output->append(StringUtil::fromDouble(m_doubleValue)); |
158 } else if (type() == TypeInteger) { | 158 } else if (type() == TypeInteger) { |
159 output->append(String16::fromInteger(m_integerValue)); | 159 output->append(StringUtil::fromInteger(m_integerValue)); |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 std::unique_ptr<Value> FundamentalValue::clone() const | 163 std::unique_ptr<Value> FundamentalValue::clone() const |
164 { | 164 { |
165 switch (type()) { | 165 switch (type()) { |
166 case TypeDouble: return FundamentalValue::create(m_doubleValue); | 166 case TypeDouble: return FundamentalValue::create(m_doubleValue); |
167 case TypeInteger: return FundamentalValue::create(m_integerValue); | 167 case TypeInteger: return FundamentalValue::create(m_integerValue); |
168 case TypeBoolean: return FundamentalValue::create(m_boolValue); | 168 case TypeBoolean: return FundamentalValue::create(m_boolValue); |
169 default: | 169 default: |
170 NOTREACHED(); | 170 NOTREACHED(); |
171 } | 171 } |
172 return nullptr; | 172 return nullptr; |
173 } | 173 } |
174 | 174 |
175 bool StringValue::asString(String16* output) const | 175 bool StringValue::asString(String* output) const |
176 { | 176 { |
177 *output = m_stringValue; | 177 *output = m_stringValue; |
178 return true; | 178 return true; |
179 } | 179 } |
180 | 180 |
181 void StringValue::writeJSON(String16Builder* output) const | 181 void StringValue::writeJSON(StringBuilder* output) const |
182 { | 182 { |
183 DCHECK(type() == TypeString); | 183 DCHECK(type() == TypeString); |
184 doubleQuoteStringForJSON(m_stringValue, output); | 184 doubleQuoteStringForJSON(m_stringValue, output); |
185 } | 185 } |
186 | 186 |
187 std::unique_ptr<Value> StringValue::clone() const | 187 std::unique_ptr<Value> StringValue::clone() const |
188 { | 188 { |
189 return StringValue::create(m_stringValue); | 189 return StringValue::create(m_stringValue); |
190 } | 190 } |
191 | 191 |
192 bool SerializedValue::asSerialized(String16* output) const | 192 bool SerializedValue::asSerialized(String* output) const |
193 { | 193 { |
194 *output = m_serializedValue; | 194 *output = m_serializedValue; |
195 return true; | 195 return true; |
196 } | 196 } |
197 | 197 |
198 void SerializedValue::writeJSON(String16Builder* output) const | 198 void SerializedValue::writeJSON(StringBuilder* output) const |
199 { | 199 { |
200 DCHECK(type() == TypeSerialized); | 200 DCHECK(type() == TypeSerialized); |
201 output->append(m_serializedValue); | 201 output->append(m_serializedValue); |
202 } | 202 } |
203 | 203 |
204 std::unique_ptr<Value> SerializedValue::clone() const | 204 std::unique_ptr<Value> SerializedValue::clone() const |
205 { | 205 { |
206 return SerializedValue::create(m_serializedValue); | 206 return SerializedValue::create(m_serializedValue); |
207 } | 207 } |
208 | 208 |
209 DictionaryValue::~DictionaryValue() | 209 DictionaryValue::~DictionaryValue() |
210 { | 210 { |
211 } | 211 } |
212 | 212 |
213 void DictionaryValue::setBoolean(const String16& name, bool value) | 213 void DictionaryValue::setBoolean(const String& name, bool value) |
214 { | 214 { |
215 setValue(name, FundamentalValue::create(value)); | 215 setValue(name, FundamentalValue::create(value)); |
216 } | 216 } |
217 | 217 |
218 void DictionaryValue::setInteger(const String16& name, int value) | 218 void DictionaryValue::setInteger(const String& name, int value) |
219 { | 219 { |
220 setValue(name, FundamentalValue::create(value)); | 220 setValue(name, FundamentalValue::create(value)); |
221 } | 221 } |
222 | 222 |
223 void DictionaryValue::setDouble(const String16& name, double value) | 223 void DictionaryValue::setDouble(const String& name, double value) |
224 { | 224 { |
225 setValue(name, FundamentalValue::create(value)); | 225 setValue(name, FundamentalValue::create(value)); |
226 } | 226 } |
227 | 227 |
228 void DictionaryValue::setString(const String16& name, const String16& value) | 228 void DictionaryValue::setString(const String& name, const String& value) |
229 { | 229 { |
230 setValue(name, StringValue::create(value)); | 230 setValue(name, StringValue::create(value)); |
231 } | 231 } |
232 | 232 |
233 void DictionaryValue::setValue(const String16& name, std::unique_ptr<Value> valu
e) | 233 void DictionaryValue::setValue(const String& name, std::unique_ptr<Value> value) |
234 { | 234 { |
235 set(name, value); | 235 set(name, value); |
236 } | 236 } |
237 | 237 |
238 void DictionaryValue::setObject(const String16& name, std::unique_ptr<Dictionary
Value> value) | 238 void DictionaryValue::setObject(const String& name, std::unique_ptr<DictionaryVa
lue> value) |
239 { | 239 { |
240 set(name, value); | 240 set(name, value); |
241 } | 241 } |
242 | 242 |
243 void DictionaryValue::setArray(const String16& name, std::unique_ptr<ListValue>
value) | 243 void DictionaryValue::setArray(const String& name, std::unique_ptr<ListValue> va
lue) |
244 { | 244 { |
245 set(name, value); | 245 set(name, value); |
246 } | 246 } |
247 | 247 |
248 bool DictionaryValue::getBoolean(const String16& name, bool* output) const | 248 bool DictionaryValue::getBoolean(const String& name, bool* output) const |
249 { | 249 { |
250 protocol::Value* value = get(name); | 250 protocol::Value* value = get(name); |
251 if (!value) | 251 if (!value) |
252 return false; | 252 return false; |
253 return value->asBoolean(output); | 253 return value->asBoolean(output); |
254 } | 254 } |
255 | 255 |
256 bool DictionaryValue::getInteger(const String16& name, int* output) const | 256 bool DictionaryValue::getInteger(const String& name, int* output) const |
257 { | 257 { |
258 Value* value = get(name); | 258 Value* value = get(name); |
259 if (!value) | 259 if (!value) |
260 return false; | 260 return false; |
261 return value->asInteger(output); | 261 return value->asInteger(output); |
262 } | 262 } |
263 | 263 |
264 bool DictionaryValue::getDouble(const String16& name, double* output) const | 264 bool DictionaryValue::getDouble(const String& name, double* output) const |
265 { | 265 { |
266 Value* value = get(name); | 266 Value* value = get(name); |
267 if (!value) | 267 if (!value) |
268 return false; | 268 return false; |
269 return value->asDouble(output); | 269 return value->asDouble(output); |
270 } | 270 } |
271 | 271 |
272 bool DictionaryValue::getString(const String16& name, String16* output) const | 272 bool DictionaryValue::getString(const String& name, String* output) const |
273 { | 273 { |
274 protocol::Value* value = get(name); | 274 protocol::Value* value = get(name); |
275 if (!value) | 275 if (!value) |
276 return false; | 276 return false; |
277 return value->asString(output); | 277 return value->asString(output); |
278 } | 278 } |
279 | 279 |
280 DictionaryValue* DictionaryValue::getObject(const String16& name) const | 280 DictionaryValue* DictionaryValue::getObject(const String& name) const |
281 { | 281 { |
282 return DictionaryValue::cast(get(name)); | 282 return DictionaryValue::cast(get(name)); |
283 } | 283 } |
284 | 284 |
285 protocol::ListValue* DictionaryValue::getArray(const String16& name) const | 285 protocol::ListValue* DictionaryValue::getArray(const String& name) const |
286 { | 286 { |
287 return ListValue::cast(get(name)); | 287 return ListValue::cast(get(name)); |
288 } | 288 } |
289 | 289 |
290 protocol::Value* DictionaryValue::get(const String16& name) const | 290 protocol::Value* DictionaryValue::get(const String& name) const |
291 { | 291 { |
292 Dictionary::const_iterator it = m_data.find(name); | 292 Dictionary::const_iterator it = m_data.find(name); |
293 if (it == m_data.end()) | 293 if (it == m_data.end()) |
294 return nullptr; | 294 return nullptr; |
295 return it->second.get(); | 295 return it->second.get(); |
296 } | 296 } |
297 | 297 |
298 DictionaryValue::Entry DictionaryValue::at(size_t index) const | 298 DictionaryValue::Entry DictionaryValue::at(size_t index) const |
299 { | 299 { |
300 const String16 key = m_order[index]; | 300 const String key = m_order[index]; |
301 return std::make_pair(key, m_data.find(key)->second.get()); | 301 return std::make_pair(key, m_data.find(key)->second.get()); |
302 } | 302 } |
303 | 303 |
304 bool DictionaryValue::booleanProperty(const String16& name, bool defaultValue) c
onst | 304 bool DictionaryValue::booleanProperty(const String& name, bool defaultValue) con
st |
305 { | 305 { |
306 bool result = defaultValue; | 306 bool result = defaultValue; |
307 getBoolean(name, &result); | 307 getBoolean(name, &result); |
308 return result; | 308 return result; |
309 } | 309 } |
310 | 310 |
311 int DictionaryValue::integerProperty(const String16& name, int defaultValue) con
st | 311 int DictionaryValue::integerProperty(const String& name, int defaultValue) const |
312 { | 312 { |
313 int result = defaultValue; | 313 int result = defaultValue; |
314 getInteger(name, &result); | 314 getInteger(name, &result); |
315 return result; | 315 return result; |
316 } | 316 } |
317 | 317 |
318 double DictionaryValue::doubleProperty(const String16& name, double defaultValue
) const | 318 double DictionaryValue::doubleProperty(const String& name, double defaultValue)
const |
319 { | 319 { |
320 double result = defaultValue; | 320 double result = defaultValue; |
321 getDouble(name, &result); | 321 getDouble(name, &result); |
322 return result; | 322 return result; |
323 } | 323 } |
324 | 324 |
325 void DictionaryValue::remove(const String16& name) | 325 void DictionaryValue::remove(const String& name) |
326 { | 326 { |
327 m_data.erase(name); | 327 m_data.erase(name); |
328 m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end
()); | 328 m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end
()); |
329 } | 329 } |
330 | 330 |
331 void DictionaryValue::writeJSON(String16Builder* output) const | 331 void DictionaryValue::writeJSON(StringBuilder* output) const |
332 { | 332 { |
333 output->append('{'); | 333 output->append('{'); |
334 for (size_t i = 0; i < m_order.size(); ++i) { | 334 for (size_t i = 0; i < m_order.size(); ++i) { |
335 Dictionary::const_iterator it = m_data.find(m_order[i]); | 335 Dictionary::const_iterator it = m_data.find(m_order[i]); |
336 CHECK(it != m_data.end()); | 336 CHECK(it != m_data.end()); |
337 if (i) | 337 if (i) |
338 output->append(','); | 338 output->append(','); |
339 doubleQuoteStringForJSON(it->first, output); | 339 doubleQuoteStringForJSON(it->first, output); |
340 output->append(':'); | 340 output->append(':'); |
341 it->second->writeJSON(output); | 341 it->second->writeJSON(output); |
342 } | 342 } |
343 output->append('}'); | 343 output->append('}'); |
344 } | 344 } |
345 | 345 |
346 std::unique_ptr<Value> DictionaryValue::clone() const | 346 std::unique_ptr<Value> DictionaryValue::clone() const |
347 { | 347 { |
348 std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); | 348 std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); |
349 for (size_t i = 0; i < m_order.size(); ++i) { | 349 for (size_t i = 0; i < m_order.size(); ++i) { |
350 String16 key = m_order[i]; | 350 String key = m_order[i]; |
351 Dictionary::const_iterator value = m_data.find(key); | 351 Dictionary::const_iterator value = m_data.find(key); |
352 DCHECK(value != m_data.cend() && value->second); | 352 DCHECK(value != m_data.cend() && value->second); |
353 result->setValue(key, value->second->clone()); | 353 result->setValue(key, value->second->clone()); |
354 } | 354 } |
355 return std::move(result); | 355 return std::move(result); |
356 } | 356 } |
357 | 357 |
358 DictionaryValue::DictionaryValue() | 358 DictionaryValue::DictionaryValue() |
359 : Value(TypeObject) | 359 : Value(TypeObject) |
360 { | 360 { |
361 } | 361 } |
362 | 362 |
363 ListValue::~ListValue() | 363 ListValue::~ListValue() |
364 { | 364 { |
365 } | 365 } |
366 | 366 |
367 void ListValue::writeJSON(String16Builder* output) const | 367 void ListValue::writeJSON(StringBuilder* output) const |
368 { | 368 { |
369 output->append('['); | 369 output->append('['); |
370 bool first = true; | 370 bool first = true; |
371 for (const std::unique_ptr<protocol::Value>& value : m_data) { | 371 for (const std::unique_ptr<protocol::Value>& value : m_data) { |
372 if (!first) | 372 if (!first) |
373 output->append(','); | 373 output->append(','); |
374 value->writeJSON(output); | 374 value->writeJSON(output); |
375 first = false; | 375 first = false; |
376 } | 376 } |
377 output->append(']'); | 377 output->append(']'); |
(...skipping 17 matching lines...) Expand all Loading... |
395 DCHECK(value); | 395 DCHECK(value); |
396 m_data.push_back(std::move(value)); | 396 m_data.push_back(std::move(value)); |
397 } | 397 } |
398 | 398 |
399 protocol::Value* ListValue::at(size_t index) | 399 protocol::Value* ListValue::at(size_t index) |
400 { | 400 { |
401 DCHECK_LT(index, m_data.size()); | 401 DCHECK_LT(index, m_data.size()); |
402 return m_data[index].get(); | 402 return m_data[index].get(); |
403 } | 403 } |
404 | 404 |
405 } // namespace protocol | 405 {% for namespace in config.protocol.namespace %} |
406 } // namespace blink | 406 } // namespace {{namespace}} |
| 407 {% endfor %} |
OLD | NEW |