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