| 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 "Values.h" | 5 //#include "Values.h" |
| 6 | 6 |
| 7 {% for namespace in config.protocol.namespace %} | 7 {% for namespace in config.protocol.namespace %} |
| 8 namespace {{namespace}} { | 8 namespace {{namespace}} { |
| 9 {% endfor %} | 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(uint16_t c, StringBuilder* 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': StringUtil::builderAppend(*dst, "\\b"); break; |
| 21 case '\f': dst->append("\\f"); break; | 21 case '\f': StringUtil::builderAppend(*dst, "\\f"); break; |
| 22 case '\n': dst->append("\\n"); break; | 22 case '\n': StringUtil::builderAppend(*dst, "\\n"); break; |
| 23 case '\r': dst->append("\\r"); break; | 23 case '\r': StringUtil::builderAppend(*dst, "\\r"); break; |
| 24 case '\t': dst->append("\\t"); break; | 24 case '\t': StringUtil::builderAppend(*dst, "\\t"); break; |
| 25 case '\\': dst->append("\\\\"); break; | 25 case '\\': StringUtil::builderAppend(*dst, "\\\\"); break; |
| 26 case '"': dst->append("\\\""); break; | 26 case '"': StringUtil::builderAppend(*dst, "\\\""); 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(uint16_t number, StringBuilder* dst) | 35 void appendUnsignedAsHex(uint16_t number, StringBuilder* dst) |
| 36 { | 36 { |
| 37 dst->append("\\u"); | 37 StringUtil::builderAppend(*dst, "\\u"); |
| 38 for (size_t i = 0; i < 4; ++i) { | 38 for (size_t i = 0; i < 4; ++i) { |
| 39 uint16_t c = hexDigits[(number & 0xF000) >> 12]; | 39 uint16_t c = hexDigits[(number & 0xF000) >> 12]; |
| 40 dst->append(c); | 40 StringUtil::builderAppend(*dst, c); |
| 41 number <<= 4; | 41 number <<= 4; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 void escapeStringForJSON(const String& str, StringBuilder* 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 uint16_t 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 StringUtil::builderAppend(*dst, c); |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) | 62 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) |
| 63 { | 63 { |
| 64 dst->append('"'); | 64 StringUtil::builderAppend(*dst, '"'); |
| 65 escapeStringForJSON(str, dst); | 65 escapeStringForJSON(str, dst); |
| 66 dst->append('"'); | 66 StringUtil::builderAppend(*dst, '"'); |
| 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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool Value::asSerialized(String*) const | 91 bool Value::asSerialized(String*) const |
| 92 { | 92 { |
| 93 return false; | 93 return false; |
| 94 } | 94 } |
| 95 | 95 |
| 96 void Value::writeJSON(StringBuilder* output) const | 96 void Value::writeJSON(StringBuilder* output) const |
| 97 { | 97 { |
| 98 DCHECK(m_type == TypeNull); | 98 DCHECK(m_type == TypeNull); |
| 99 output->append(nullValueString, 4); | 99 StringUtil::builderAppend(*output, nullValueString, 4); |
| 100 } | 100 } |
| 101 | 101 |
| 102 std::unique_ptr<Value> Value::clone() const | 102 std::unique_ptr<Value> Value::clone() const |
| 103 { | 103 { |
| 104 return Value::null(); | 104 return Value::null(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 String Value::serialize() | 107 String Value::serialize() |
| 108 { | 108 { |
| 109 StringBuilder result; | 109 StringBuilder result; |
| 110 StringUtil::builderReserve(result, 512); | 110 StringUtil::builderReserve(result, 512); |
| 111 writeJSON(&result); | 111 writeJSON(&result); |
| 112 return result.toString(); | 112 return StringUtil::builderToString(result); |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool FundamentalValue::asBoolean(bool* output) const | 115 bool FundamentalValue::asBoolean(bool* output) const |
| 116 { | 116 { |
| 117 if (type() != TypeBoolean) | 117 if (type() != TypeBoolean) |
| 118 return false; | 118 return false; |
| 119 *output = m_boolValue; | 119 *output = m_boolValue; |
| 120 return true; | 120 return true; |
| 121 } | 121 } |
| 122 | 122 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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(StringBuilder* 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 StringUtil::builderAppend(*output, trueValueString, 4); |
| 150 else | 150 else |
| 151 output->append(falseValueString, 5); | 151 StringUtil::builderAppend(*output, 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 StringUtil::builderAppend(*output, nullValueString, 4); |
| 155 return; | 155 return; |
| 156 } | 156 } |
| 157 output->append(StringUtil::fromDouble(m_doubleValue)); | 157 StringUtil::builderAppend(*output, StringUtil::fromDouble(m_doubleValue)
); |
| 158 } else if (type() == TypeInteger) { | 158 } else if (type() == TypeInteger) { |
| 159 output->append(StringUtil::fromInteger(m_integerValue)); | 159 StringUtil::builderAppend(*output, StringUtil::fromInteger(m_integerValu
e)); |
| 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: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 191 | 191 |
| 192 bool SerializedValue::asSerialized(String* 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(StringBuilder* 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 StringUtil::builderAppend(*output, 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 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 323 } |
| 324 | 324 |
| 325 void DictionaryValue::remove(const String& 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(StringBuilder* output) const | 331 void DictionaryValue::writeJSON(StringBuilder* output) const |
| 332 { | 332 { |
| 333 output->append('{'); | 333 StringUtil::builderAppend(*output, '{'); |
| 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 StringUtil::builderAppend(*output, ','); |
| 339 doubleQuoteStringForJSON(it->first, output); | 339 doubleQuoteStringForJSON(it->first, output); |
| 340 output->append(':'); | 340 StringUtil::builderAppend(*output, ':'); |
| 341 it->second->writeJSON(output); | 341 it->second->writeJSON(output); |
| 342 } | 342 } |
| 343 output->append('}'); | 343 StringUtil::builderAppend(*output, '}'); |
| 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 String 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(StringBuilder* output) const | 367 void ListValue::writeJSON(StringBuilder* output) const |
| 368 { | 368 { |
| 369 output->append('['); | 369 StringUtil::builderAppend(*output, '['); |
| 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 StringUtil::builderAppend(*output, ','); |
| 374 value->writeJSON(output); | 374 value->writeJSON(output); |
| 375 first = false; | 375 first = false; |
| 376 } | 376 } |
| 377 output->append(']'); | 377 StringUtil::builderAppend(*output, ']'); |
| 378 } | 378 } |
| 379 | 379 |
| 380 std::unique_ptr<Value> ListValue::clone() const | 380 std::unique_ptr<Value> ListValue::clone() const |
| 381 { | 381 { |
| 382 std::unique_ptr<ListValue> result = ListValue::create(); | 382 std::unique_ptr<ListValue> result = ListValue::create(); |
| 383 for (const std::unique_ptr<protocol::Value>& value : m_data) | 383 for (const std::unique_ptr<protocol::Value>& value : m_data) |
| 384 result->pushValue(value->clone()); | 384 result->pushValue(value->clone()); |
| 385 return std::move(result); | 385 return std::move(result); |
| 386 } | 386 } |
| 387 | 387 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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 {% for namespace in config.protocol.namespace %} | 405 {% for namespace in config.protocol.namespace %} |
| 406 } // namespace {{namespace}} | 406 } // namespace {{namespace}} |
| 407 {% endfor %} | 407 {% endfor %} |
| OLD | NEW |