summaryrefslogtreecommitdiff
path: root/tools/inspector_protocol/lib/Values_cpp.template
diff options
context:
space:
mode:
Diffstat (limited to 'tools/inspector_protocol/lib/Values_cpp.template')
-rw-r--r--tools/inspector_protocol/lib/Values_cpp.template143
1 files changed, 87 insertions, 56 deletions
diff --git a/tools/inspector_protocol/lib/Values_cpp.template b/tools/inspector_protocol/lib/Values_cpp.template
index 4b4ba99415..764b4d37d9 100644
--- a/tools/inspector_protocol/lib/Values_cpp.template
+++ b/tools/inspector_protocol/lib/Values_cpp.template
@@ -66,21 +66,21 @@ static constexpr int kStackLimitValues = 1000;
// Below are three parsing routines for CBOR, which cover enough
// to roundtrip JSON messages.
-std::unique_ptr<DictionaryValue> parseMap(int32_t stack_depth, CBORTokenizer* tokenizer);
-std::unique_ptr<ListValue> parseArray(int32_t stack_depth, CBORTokenizer* tokenizer);
-std::unique_ptr<Value> parseValue(int32_t stack_depth, CBORTokenizer* tokenizer);
+std::unique_ptr<DictionaryValue> parseMap(int32_t stack_depth, cbor::CBORTokenizer* tokenizer);
+std::unique_ptr<ListValue> parseArray(int32_t stack_depth, cbor::CBORTokenizer* tokenizer);
+std::unique_ptr<Value> parseValue(int32_t stack_depth, cbor::CBORTokenizer* tokenizer);
// |bytes| must start with the indefinite length array byte, so basically,
// ParseArray may only be called after an indefinite length array has been
// detected.
-std::unique_ptr<ListValue> parseArray(int32_t stack_depth, CBORTokenizer* tokenizer) {
- DCHECK(tokenizer->TokenTag() == CBORTokenTag::ARRAY_START);
+std::unique_ptr<ListValue> parseArray(int32_t stack_depth, cbor::CBORTokenizer* tokenizer) {
+ DCHECK(tokenizer->TokenTag() == cbor::CBORTokenTag::ARRAY_START);
tokenizer->Next();
auto list = ListValue::create();
- while (tokenizer->TokenTag() != CBORTokenTag::STOP) {
+ while (tokenizer->TokenTag() != cbor::CBORTokenTag::STOP) {
// Error::CBOR_UNEXPECTED_EOF_IN_ARRAY
- if (tokenizer->TokenTag() == CBORTokenTag::DONE) return nullptr;
- if (tokenizer->TokenTag() == CBORTokenTag::ERROR_VALUE) return nullptr;
+ if (tokenizer->TokenTag() == cbor::CBORTokenTag::DONE) return nullptr;
+ if (tokenizer->TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
// Parse value.
auto value = parseValue(stack_depth, tokenizer);
if (!value) return nullptr;
@@ -91,60 +91,66 @@ std::unique_ptr<ListValue> parseArray(int32_t stack_depth, CBORTokenizer* tokeni
}
std::unique_ptr<Value> parseValue(
- int32_t stack_depth, CBORTokenizer* tokenizer) {
+ int32_t stack_depth, cbor::CBORTokenizer* tokenizer) {
// Error::CBOR_STACK_LIMIT_EXCEEDED
if (stack_depth > kStackLimitValues) return nullptr;
// Skip past the envelope to get to what's inside.
- if (tokenizer->TokenTag() == CBORTokenTag::ENVELOPE)
+ if (tokenizer->TokenTag() == cbor::CBORTokenTag::ENVELOPE)
tokenizer->EnterEnvelope();
switch (tokenizer->TokenTag()) {
- case CBORTokenTag::ERROR_VALUE:
+ case cbor::CBORTokenTag::ERROR_VALUE:
return nullptr;
- case CBORTokenTag::DONE:
+ case cbor::CBORTokenTag::DONE:
// Error::CBOR_UNEXPECTED_EOF_EXPECTED_VALUE
return nullptr;
- case CBORTokenTag::TRUE_VALUE: {
+ case cbor::CBORTokenTag::TRUE_VALUE: {
std::unique_ptr<Value> value = FundamentalValue::create(true);
tokenizer->Next();
return value;
}
- case CBORTokenTag::FALSE_VALUE: {
+ case cbor::CBORTokenTag::FALSE_VALUE: {
std::unique_ptr<Value> value = FundamentalValue::create(false);
tokenizer->Next();
return value;
}
- case CBORTokenTag::NULL_VALUE: {
+ case cbor::CBORTokenTag::NULL_VALUE: {
std::unique_ptr<Value> value = FundamentalValue::null();
tokenizer->Next();
return value;
}
- case CBORTokenTag::INT32: {
+ case cbor::CBORTokenTag::INT32: {
std::unique_ptr<Value> value = FundamentalValue::create(tokenizer->GetInt32());
tokenizer->Next();
return value;
}
- case CBORTokenTag::DOUBLE: {
+ case cbor::CBORTokenTag::DOUBLE: {
std::unique_ptr<Value> value = FundamentalValue::create(tokenizer->GetDouble());
tokenizer->Next();
return value;
}
- case CBORTokenTag::STRING8: {
+ case cbor::CBORTokenTag::STRING8: {
span<uint8_t> str = tokenizer->GetString8();
- std::unique_ptr<Value> value = StringValue::create(StringUtil::fromUTF8(str.data(), str.size()));
+ std::unique_ptr<Value> value =
+ StringValue::create(StringUtil::fromUTF8(str.data(), str.size()));
tokenizer->Next();
return value;
}
- case CBORTokenTag::STRING16:
- // NOT SUPPORTED YET.
- return nullptr;
- case CBORTokenTag::BINARY: {
+ case cbor::CBORTokenTag::STRING16: {
+ span<uint8_t> wire = tokenizer->GetString16WireRep();
+ DCHECK_EQ(wire.size() & 1, 0u);
+ std::unique_ptr<Value> value = StringValue::create(StringUtil::fromUTF16(
+ reinterpret_cast<const uint16_t*>(wire.data()), wire.size() / 2));
+ tokenizer->Next();
+ return value;
+ }
+ case cbor::CBORTokenTag::BINARY: {
span<uint8_t> payload = tokenizer->GetBinary();
tokenizer->Next();
return BinaryValue::create(Binary::fromSpan(payload.data(), payload.size()));
}
- case CBORTokenTag::MAP_START:
+ case cbor::CBORTokenTag::MAP_START:
return parseMap(stack_depth + 1, tokenizer);
- case CBORTokenTag::ARRAY_START:
+ case cbor::CBORTokenTag::ARRAY_START:
return parseArray(stack_depth + 1, tokenizer);
default:
// Error::CBOR_UNSUPPORTED_VALUE
@@ -156,22 +162,22 @@ std::unique_ptr<Value> parseValue(
// ParseArray may only be called after an indefinite length array has been
// detected.
std::unique_ptr<DictionaryValue> parseMap(
- int32_t stack_depth, CBORTokenizer* tokenizer) {
+ int32_t stack_depth, cbor::CBORTokenizer* tokenizer) {
auto dict = DictionaryValue::create();
tokenizer->Next();
- while (tokenizer->TokenTag() != CBORTokenTag::STOP) {
- if (tokenizer->TokenTag() == CBORTokenTag::DONE) {
+ while (tokenizer->TokenTag() != cbor::CBORTokenTag::STOP) {
+ if (tokenizer->TokenTag() == cbor::CBORTokenTag::DONE) {
// Error::CBOR_UNEXPECTED_EOF_IN_MAP
return nullptr;
}
- if (tokenizer->TokenTag() == CBORTokenTag::ERROR_VALUE) return nullptr;
+ if (tokenizer->TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
// Parse key.
String key;
- if (tokenizer->TokenTag() == CBORTokenTag::STRING8) {
+ if (tokenizer->TokenTag() == cbor::CBORTokenTag::STRING8) {
span<uint8_t> key_span = tokenizer->GetString8();
key = StringUtil::fromUTF8(key_span.data(), key_span.size());
tokenizer->Next();
- } else if (tokenizer->TokenTag() == CBORTokenTag::STRING16) {
+ } else if (tokenizer->TokenTag() == cbor::CBORTokenTag::STRING16) {
return nullptr; // STRING16 not supported yet.
} else {
// Error::CBOR_INVALID_MAP_KEY
@@ -196,22 +202,21 @@ std::unique_ptr<Value> Value::parseBinary(const uint8_t* data, size_t size) {
if (bytes.empty()) return nullptr;
// Error::CBOR_INVALID_START_BYTE
- // TODO(johannes): EncodeInitialByteForEnvelope() method.
- if (bytes[0] != 0xd8) return nullptr;
+ if (bytes[0] != cbor::InitialByteForEnvelope()) return nullptr;
- CBORTokenizer tokenizer(bytes);
- if (tokenizer.TokenTag() == CBORTokenTag::ERROR_VALUE) return nullptr;
+ cbor::CBORTokenizer tokenizer(bytes);
+ if (tokenizer.TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
// We checked for the envelope start byte above, so the tokenizer
// must agree here, since it's not an error.
- DCHECK(tokenizer.TokenTag() == CBORTokenTag::ENVELOPE);
+ DCHECK(tokenizer.TokenTag() == cbor::CBORTokenTag::ENVELOPE);
tokenizer.EnterEnvelope();
// Error::MAP_START_EXPECTED
- if (tokenizer.TokenTag() != CBORTokenTag::MAP_START) return nullptr;
+ if (tokenizer.TokenTag() != cbor::CBORTokenTag::MAP_START) return nullptr;
std::unique_ptr<Value> result = parseMap(/*stack_depth=*/1, &tokenizer);
if (!result) return nullptr;
- if (tokenizer.TokenTag() == CBORTokenTag::DONE) return result;
- if (tokenizer.TokenTag() == CBORTokenTag::ERROR_VALUE) return nullptr;
+ if (tokenizer.TokenTag() == cbor::CBORTokenTag::DONE) return result;
+ if (tokenizer.TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
// Error::CBOR_TRAILING_JUNK
return nullptr;
}
@@ -249,7 +254,7 @@ void Value::writeJSON(StringBuilder* output) const
void Value::writeBinary(std::vector<uint8_t>* bytes) const {
DCHECK(m_type == TypeNull);
- bytes->push_back(EncodeNull());
+ bytes->push_back(cbor::EncodeNull());
}
std::unique_ptr<Value> Value::clone() const
@@ -326,13 +331,13 @@ void FundamentalValue::writeJSON(StringBuilder* output) const
void FundamentalValue::writeBinary(std::vector<uint8_t>* bytes) const {
switch (type()) {
case TypeDouble:
- EncodeDouble(m_doubleValue, bytes);
+ cbor::EncodeDouble(m_doubleValue, bytes);
return;
case TypeInteger:
- EncodeInt32(m_integerValue, bytes);
+ cbor::EncodeInt32(m_integerValue, bytes);
return;
case TypeBoolean:
- bytes->push_back(m_boolValue ? EncodeTrue() : EncodeFalse());
+ bytes->push_back(m_boolValue ? cbor::EncodeTrue() : cbor::EncodeFalse());
return;
default:
DCHECK(false);
@@ -363,10 +368,37 @@ void StringValue::writeJSON(StringBuilder* output) const
StringUtil::builderAppendQuotedString(*output, m_stringValue);
}
+namespace {
+// This routine distinguishes between the current encoding for a given
+// string |s|, and calls encoding routines that will
+// - Ensure that all ASCII strings end up being encoded as UTF8 in
+// the wire format - e.g., EncodeFromUTF16 will detect ASCII and
+// do the (trivial) transcode to STRING8 on the wire, but if it's
+// not ASCII it'll do STRING16.
+// - Select a format that's cheap to convert to. E.g., we don't
+// have LATIN1 on the wire, so we call EncodeFromLatin1 which
+// transcodes to UTF8 if needed.
+void EncodeString(const String& s, std::vector<uint8_t>* out) {
+ if (StringUtil::CharacterCount(s) == 0) {
+ cbor::EncodeString8(span<uint8_t>(nullptr, 0), out); // Empty string.
+ } else if (StringUtil::CharactersLatin1(s)) {
+ cbor::EncodeFromLatin1(span<uint8_t>(StringUtil::CharactersLatin1(s),
+ StringUtil::CharacterCount(s)),
+ out);
+ } else if (StringUtil::CharactersUTF16(s)) {
+ cbor::EncodeFromUTF16(span<uint16_t>(StringUtil::CharactersUTF16(s),
+ StringUtil::CharacterCount(s)),
+ out);
+ } else if (StringUtil::CharactersUTF8(s)) {
+ cbor::EncodeString8(span<uint8_t>(StringUtil::CharactersUTF8(s),
+ StringUtil::CharacterCount(s)),
+ out);
+ }
+}
+} // namespace
+
void StringValue::writeBinary(std::vector<uint8_t>* bytes) const {
- StringUTF8Adapter utf8(m_stringValue);
- EncodeString8(span<uint8_t>(reinterpret_cast<const uint8_t*>(utf8.Data()),
- utf8.length()), bytes);
+ EncodeString(m_stringValue, bytes);
}
std::unique_ptr<Value> StringValue::clone() const
@@ -387,7 +419,8 @@ void BinaryValue::writeJSON(StringBuilder* output) const
}
void BinaryValue::writeBinary(std::vector<uint8_t>* bytes) const {
- EncodeBinary(span<uint8_t>(m_binaryValue.data(), m_binaryValue.size()), bytes);
+ cbor::EncodeBinary(span<uint8_t>(m_binaryValue.data(),
+ m_binaryValue.size()), bytes);
}
std::unique_ptr<Value> BinaryValue::clone() const
@@ -550,19 +583,17 @@ void DictionaryValue::writeJSON(StringBuilder* output) const
}
void DictionaryValue::writeBinary(std::vector<uint8_t>* bytes) const {
- EnvelopeEncoder encoder;
+ cbor::EnvelopeEncoder encoder;
encoder.EncodeStart(bytes);
- bytes->push_back(EncodeIndefiniteLengthMapStart());
+ bytes->push_back(cbor::EncodeIndefiniteLengthMapStart());
for (size_t i = 0; i < m_order.size(); ++i) {
const String& key = m_order[i];
Dictionary::const_iterator value = m_data.find(key);
DCHECK(value != m_data.cend() && value->second);
- StringUTF8Adapter utf8(key);
- EncodeString8(span<uint8_t>(reinterpret_cast<const uint8_t*>(utf8.Data()),
- utf8.length()), bytes);
+ EncodeString(key, bytes);
value->second->writeBinary(bytes);
}
- bytes->push_back(EncodeStop());
+ bytes->push_back(cbor::EncodeStop());
encoder.EncodeStop(bytes);
}
@@ -601,13 +632,13 @@ void ListValue::writeJSON(StringBuilder* output) const
}
void ListValue::writeBinary(std::vector<uint8_t>* bytes) const {
- EnvelopeEncoder encoder;
+ cbor::EnvelopeEncoder encoder;
encoder.EncodeStart(bytes);
- bytes->push_back(EncodeIndefiniteLengthArrayStart());
+ bytes->push_back(cbor::EncodeIndefiniteLengthArrayStart());
for (size_t i = 0; i < m_data.size(); ++i) {
m_data[i]->writeBinary(bytes);
}
- bytes->push_back(EncodeStop());
+ bytes->push_back(cbor::EncodeStop());
encoder.EncodeStop(bytes);
}