Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: third_party/inspector_protocol/lib/Values_cpp.template

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

Powered by Google App Engine
This is Rietveld 408576698