Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

View File

@ -0,0 +1,92 @@
// This file is generated by Forward_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef {{"_".join(config.protocol.namespace)}}_Forward_h
#define {{"_".join(config.protocol.namespace)}}_Forward_h
{% if config.lib.export_header %}
#include {{format_include(config.lib.export_header)}}
{% endif %}
#include <memory>
#include <vector>
#include "{{config.crdtp.dir}}/error_support.h"
#include "{{config.crdtp.dir}}/dispatch.h"
#include "{{config.crdtp.dir}}/frontend_channel.h"
#include "{{config.crdtp.dir}}/protocol_core.h"
{% if config.use_embedder_types %}
#include {{format_include(config.lib.protocol_traits)}}
{% else %}
#include {{format_include(config.lib.string_header)}}
{% endif %}
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
using DispatchResponse = {{config.crdtp.namespace}}::DispatchResponse;
using ErrorSupport = {{config.crdtp.namespace}}::ErrorSupport;
using Serializable = {{config.crdtp.namespace}}::Serializable;
using FrontendChannel = {{config.crdtp.namespace}}::FrontendChannel;
using DomainDispatcher = {{config.crdtp.namespace}}::DomainDispatcher;
using UberDispatcher = {{config.crdtp.namespace}}::UberDispatcher;
using Response = DispatchResponse;
{% if config.use_embedder_types %}
using DictionaryValue = crdtp::traits::DictionaryValue;
using Object = crdtp::traits::DictionaryValue;
using ListValue = crdtp::traits::ListValue;
using Value = crdtp::traits::Value;
using String = crdtp::traits::String;
using Binary = crdtp::Binary;
{% else %}
class DictionaryValue;
class FundamentalValue;
class ListValue;
class Object;
class SerializedValue;
class StringValue;
class Value;
{% endif %}
using {{config.crdtp.namespace}}::detail::PtrMaybe;
using {{config.crdtp.namespace}}::detail::ValueMaybe;
template<typename T>
using Maybe = {{config.crdtp.namespace}}::Maybe<T>;
namespace detail {
template <typename T>
struct ArrayTypedef { typedef std::vector<std::unique_ptr<T>> type; };
template <>
struct ArrayTypedef<String> { typedef std::vector<String> type; };
template <>
struct ArrayTypedef<int> { typedef std::vector<int> type; };
template <>
struct ArrayTypedef<double> { typedef std::vector<double> type; };
template <>
struct ArrayTypedef<bool> { typedef std::vector<bool> type; };
template <>
struct ArrayTypedef<Binary> { typedef std::vector<Binary> type; };
} // namespace detail
template <typename T>
using Array = typename detail::ArrayTypedef<T>::type;
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Forward_h)

View File

@ -0,0 +1,45 @@
// This file is generated by Object_cpp.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//#include "Object.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
std::unique_ptr<Object> Object::fromValue(protocol::Value* value, ErrorSupport* errors)
{
protocol::DictionaryValue* dictionary = DictionaryValue::cast(value);
if (!dictionary) {
errors->AddError("object expected");
return nullptr;
}
dictionary = static_cast<protocol::DictionaryValue*>(dictionary->clone().release());
return std::unique_ptr<Object>(new Object(std::unique_ptr<DictionaryValue>(dictionary)));
}
// Implements Serializable.
void Object::AppendSerialized(std::vector<uint8_t>* out) const {
m_object->AppendSerialized(out);
}
std::unique_ptr<protocol::DictionaryValue> Object::toValue() const
{
return DictionaryValue::cast(m_object->clone());
}
std::unique_ptr<Object> Object::clone() const
{
return std::unique_ptr<Object>(new Object(DictionaryValue::cast(m_object->clone())));
}
Object::Object(std::unique_ptr<protocol::DictionaryValue> object) : m_object(std::move(object)) { }
Object::~Object() { }
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}

View File

@ -0,0 +1,43 @@
// This file is generated by Object_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef {{"_".join(config.protocol.namespace)}}_Object_h
#define {{"_".join(config.protocol.namespace)}}_Object_h
//#include "ErrorSupport.h"
//#include "Forward.h"
//#include "Values.h"
#include "{{config.crdtp.dir}}/serializable.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class {{config.lib.export_macro}} Object : public {{config.crdtp.namespace}}::Serializable {
public:
static std::unique_ptr<Object> fromValue(protocol::Value*, ErrorSupport*);
explicit Object(std::unique_ptr<protocol::DictionaryValue>);
~Object();
// Implements Serializable.
void AppendSerialized(std::vector<uint8_t>* out) const override;
std::unique_ptr<protocol::DictionaryValue> toValue() const;
std::unique_ptr<Object> clone() const;
private:
Object() = default;
friend struct {{config.crdtp.namespace}}::ProtocolTypeTraits<std::unique_ptr<Object>, void>;
std::unique_ptr<protocol::DictionaryValue> m_object;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // !defined({{"_".join(config.protocol.namespace)}}_Object_h)

View File

@ -0,0 +1,12 @@
// This file is generated by Protocol_cpp.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include {{format_include(config.protocol.package, "Protocol")}}
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>

View File

@ -0,0 +1,127 @@
// This file is generated by ValueConversions_cpp.template.
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include {{format_include(config.protocol.package, "Protocol")}}
#include <algorithm>
#include <climits>
#include <string>
//#include "ValueConversions.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
{% for namespace in config.protocol.namespace %}
} // namespace
{% endfor %}
namespace {{config.crdtp.namespace}} {
namespace {
using {{"::".join(config.protocol.namespace)}}::Binary;
using {{"::".join(config.protocol.namespace)}}::Object;
using {{"::".join(config.protocol.namespace)}}::Value;
using {{"::".join(config.protocol.namespace)}}::String;
using {{"::".join(config.protocol.namespace)}}::DictionaryValue;
using {{"::".join(config.protocol.namespace)}}::FundamentalValue;
using {{"::".join(config.protocol.namespace)}}::StringValue;
using {{"::".join(config.protocol.namespace)}}::StringUtil;
//using {{"::".join(config.protocol.namespace)}}::EncodeString;
std::unique_ptr<Value> ReadValue(DeserializerState* state) {
cbor::CBORTokenizer* tokenizer = state->tokenizer();
switch (tokenizer->TokenTag()) {
case cbor::CBORTokenTag::TRUE_VALUE:
return FundamentalValue::create(true);
case cbor::CBORTokenTag::FALSE_VALUE:
return FundamentalValue::create(false);
case cbor::CBORTokenTag::NULL_VALUE:
return Value::null();
case cbor::CBORTokenTag::INT32:
return FundamentalValue::create(tokenizer->GetInt32());
case cbor::CBORTokenTag::DOUBLE:
return FundamentalValue::create(tokenizer->GetDouble());
case cbor::CBORTokenTag::STRING8: {
const auto str = tokenizer->GetString8();
return StringValue::create(StringUtil::fromUTF8(str.data(), str.size()));
}
case cbor::CBORTokenTag::STRING16: {
const auto str = tokenizer->GetString16WireRep();
return StringValue::create(StringUtil::fromUTF16LE(reinterpret_cast<const uint16_t*>(str.data()), str.size() / 2));
}
case cbor::CBORTokenTag::ENVELOPE: {
const auto env = tokenizer->GetEnvelope();
return Value::parseBinary(env.data(), env.size());
}
// Intentionally not supported.
case cbor::CBORTokenTag::BINARY:
// Should not be encountered outside of envelope.
case cbor::CBORTokenTag::MAP_START:
case cbor::CBORTokenTag::ARRAY_START:
default:
state->RegisterError(Error::CBOR_UNSUPPORTED_VALUE);
return nullptr;
}
}
} // namespace
// static
bool ProtocolTypeTraits<std::unique_ptr<Value>>::Deserialize(
DeserializerState* state, std::unique_ptr<Value>* value) {
auto result = ReadValue(state);
if (!result)
return false;
*value = std::move(result);
return true;
}
// static
void ProtocolTypeTraits<std::unique_ptr<Value>>::Serialize(
const std::unique_ptr<Value>& value, std::vector<uint8_t>* bytes) {
value->AppendSerialized(bytes);
}
// static
bool ProtocolTypeTraits<std::unique_ptr<DictionaryValue>>::Deserialize(
DeserializerState* state, std::unique_ptr<DictionaryValue>* value) {
std::unique_ptr<Value> res;
if (!ProtocolTypeTraits<std::unique_ptr<Value>>::Deserialize(state, &res))
return false;
if (res->type() != Value::TypeObject) {
state->RegisterError(Error::BINDINGS_DICTIONARY_VALUE_EXPECTED);
return false;
}
*value = DictionaryValue::cast(std::move(res));
return true;
}
// static
void ProtocolTypeTraits<std::unique_ptr<DictionaryValue>>::Serialize(
const std::unique_ptr<DictionaryValue>& value, std::vector<uint8_t>* bytes) {
value->AppendSerialized(bytes);
}
// static
bool ProtocolTypeTraits<std::unique_ptr<Object>>::Deserialize(DeserializerState* state, std::unique_ptr<Object>* value) {
auto res = DictionaryValue::create();
if (ProtocolTypeTraits<std::unique_ptr<DictionaryValue>>::Deserialize(state, &res)) {
*value = std::make_unique<Object>(std::move(res));
return true;
}
return false;
}
void ProtocolTypeTraits<std::unique_ptr<Object>>::Serialize(const std::unique_ptr<Object>& value, std::vector<uint8_t>* bytes) {
value->AppendSerialized(bytes);
}
} // namespace {{config.crdtp.namespace}}

View File

@ -0,0 +1,319 @@
// This file is generated by ValueConversions_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef {{"_".join(config.protocol.namespace)}}_ValueConversions_h
#define {{"_".join(config.protocol.namespace)}}_ValueConversions_h
//#include "ErrorSupport.h"
//#include "Forward.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
template<typename T>
struct ValueConversions {
static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors)
{
return T::fromValue(value, errors);
}
static std::unique_ptr<protocol::Value> toValue(T* value)
{
return value->toValue();
}
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value)
{
return value->toValue();
}
};
template<>
struct ValueConversions<bool> {
static bool fromValue(protocol::Value* value, ErrorSupport* errors)
{
bool result = false;
bool success = value ? value->asBoolean(&result) : false;
if (!success)
errors->AddError("boolean value expected");
return result;
}
static std::unique_ptr<protocol::Value> toValue(bool value)
{
return FundamentalValue::create(value);
}
};
template<>
struct ValueConversions<int> {
static int fromValue(protocol::Value* value, ErrorSupport* errors)
{
int result = 0;
bool success = value ? value->asInteger(&result) : false;
if (!success)
errors->AddError("integer value expected");
return result;
}
static std::unique_ptr<protocol::Value> toValue(int value)
{
return FundamentalValue::create(value);
}
};
template<>
struct ValueConversions<double> {
static double fromValue(protocol::Value* value, ErrorSupport* errors)
{
double result = 0;
bool success = value ? value->asDouble(&result) : false;
if (!success)
errors->AddError("double value expected");
return result;
}
static std::unique_ptr<protocol::Value> toValue(double value)
{
return FundamentalValue::create(value);
}
};
template<>
struct ValueConversions<String> {
static String fromValue(protocol::Value* value, ErrorSupport* errors)
{
String result;
bool success = value ? value->asString(&result) : false;
if (!success)
errors->AddError("string value expected");
return result;
}
static std::unique_ptr<protocol::Value> toValue(const String& value)
{
return StringValue::create(value);
}
};
template<>
struct ValueConversions<Binary> {
static Binary fromValue(protocol::Value* value, ErrorSupport* errors)
{
if (!value ||
(value->type() != Value::TypeBinary && value->type() != Value::TypeString)) {
errors->AddError("Either string base64 or binary value expected");
return Binary();
}
Binary binary;
if (value->asBinary(&binary))
return binary;
String result;
value->asString(&result);
bool success;
Binary out = Binary::fromBase64(result, &success);
if (!success)
errors->AddError("base64 decoding error");
return out;
}
static std::unique_ptr<protocol::Value> toValue(const Binary& value)
{
return BinaryValue::create(value);
}
};
template<typename T>
struct ValueConversions<std::vector<std::unique_ptr<T>>> {
static std::unique_ptr<std::vector<std::unique_ptr<T>>> fromValue(protocol::Value* value, ErrorSupport* errors) {
protocol::ListValue* array = ListValue::cast(value);
if (!array) {
errors->AddError("array expected");
return nullptr;
}
errors->Push();
std::unique_ptr<std::vector<std::unique_ptr<T>>> result(
new std::vector<std::unique_ptr<T>>());
result->reserve(array->size());
for (size_t i = 0; i < array->size(); ++i) {
errors->SetIndex(i);
auto item = ValueConversions<T>::fromValue(array->at(i), errors);
result->emplace_back(std::move(item));
}
errors->Pop();
if (!errors->Errors().empty())
return nullptr;
return result;
}
static std::unique_ptr<protocol::ListValue> toValue(std::vector<std::unique_ptr<T>>* v)
{
std::unique_ptr<protocol::ListValue> result = ListValue::create();
result->reserve(v->size());
for (auto& item : *v)
result->pushValue(ValueConversions<T>::toValue(item.get()));
return result;
}
};
template<typename T>
struct ValueConversions<std::vector<T>> {
static std::unique_ptr<std::vector<T>> fromValue(protocol::Value* value, ErrorSupport* errors) {
protocol::ListValue* array = ListValue::cast(value);
if (!array) {
errors->AddError("array expected");
return nullptr;
}
errors->Push();
std::unique_ptr<std::vector<T>> result(new std::vector<T>());
result->reserve(array->size());
for (size_t i = 0; i < array->size(); ++i) {
errors->SetIndex(i);
auto item = ValueConversions<T>::fromValue(array->at(i), errors);
result->emplace_back(std::move(item));
}
errors->Pop();
if (!errors->Errors().empty())
return nullptr;
return result;
}
static std::unique_ptr<protocol::ListValue> toValue(std::vector<T>* v)
{
std::unique_ptr<protocol::ListValue> result = ListValue::create();
result->reserve(v->size());
for (auto& item : *v)
result->pushValue(ValueConversions<T>::toValue(item));
return result;
}
};
template<>
struct ValueConversions<Value> {
static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors)
{
bool success = !!value;
if (!success) {
errors->AddError("value expected");
return nullptr;
}
return value->clone();
}
static std::unique_ptr<protocol::Value> toValue(Value* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value)
{
return value->clone();
}
};
template<>
struct ValueConversions<DictionaryValue> {
static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors)
{
bool success = value && value->type() == protocol::Value::TypeObject;
if (!success)
errors->AddError("object expected");
return DictionaryValue::cast(value->clone());
}
static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value)
{
return value->clone();
}
};
template<>
struct ValueConversions<ListValue> {
static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors)
{
bool success = value && value->type() == protocol::Value::TypeArray;
if (!success)
errors->AddError("list expected");
return ListValue::cast(value->clone());
}
static std::unique_ptr<protocol::Value> toValue(ListValue* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value)
{
return value->clone();
}
};
template<typename T> struct ValueTypeConverter {
static std::unique_ptr<T> FromValue(const protocol::Value& value) {
std::vector<uint8_t> bytes;
value.AppendSerialized(&bytes);
return T::FromBinary(bytes.data(), bytes.size());
}
static std::unique_ptr<protocol::DictionaryValue> ToValue(const T& obj) {
std::vector<uint8_t> bytes;
obj.AppendSerialized(&bytes);
auto result = Value::parseBinary(bytes.data(), bytes.size());
return DictionaryValue::cast(std::move(result));
}
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
namespace {{config.crdtp.namespace}} {
template<typename T>
struct ProtocolTypeTraits<T,
typename std::enable_if<std::is_base_of<{{"::".join(config.protocol.namespace)}}::Value, T>::value>::type> {
static void Serialize(const {{"::".join(config.protocol.namespace)}}::Value& value, std::vector<uint8_t>* bytes) {
value.AppendSerialized(bytes);
}
};
template <>
struct ProtocolTypeTraits<std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Value>> {
static bool Deserialize(DeserializerState* state, std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Value>* value);
static void Serialize(const std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Value>& value, std::vector<uint8_t>* bytes);
};
template <>
struct ProtocolTypeTraits<std::unique_ptr<{{"::".join(config.protocol.namespace)}}::DictionaryValue>> {
static bool Deserialize(DeserializerState* state, std::unique_ptr<{{"::".join(config.protocol.namespace)}}::DictionaryValue>* value);
static void Serialize(const std::unique_ptr<{{"::".join(config.protocol.namespace)}}::DictionaryValue>& value, std::vector<uint8_t>* bytes);
};
// TODO(caseq): get rid of it, it's just a DictionaryValue really.
template <>
struct ProtocolTypeTraits<std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Object>> {
static bool Deserialize(DeserializerState* state, std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Object>* value);
static void Serialize(const std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Object>& value, std::vector<uint8_t>* bytes);
};
template<>
struct ProtocolTypeTraits<{{"::".join(config.protocol.namespace)}}::Object> {
static void Serialize(const {{"::".join(config.protocol.namespace)}}::Object& value, std::vector<uint8_t>* bytes) {
value.AppendSerialized(bytes);
}
};
} // namespace {{config.crdtp.namespace}}
#endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h)

View File

@ -0,0 +1,542 @@
// This file is generated by Values_cpp.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//#include "Values.h"
#include "{{config.crdtp.dir}}/cbor.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
namespace {
using {{config.crdtp.namespace}}::Status;
using {{config.crdtp.namespace}}::ParserHandler;
using {{config.crdtp.namespace}}::span;
namespace cbor {
using {{config.crdtp.namespace}}::cbor::ParseCBOR;
using {{config.crdtp.namespace}}::cbor::EncodeBinary;
using {{config.crdtp.namespace}}::cbor::EncodeDouble;
using {{config.crdtp.namespace}}::cbor::EncodeFalse;
using {{config.crdtp.namespace}}::cbor::EncodeFromLatin1;
using {{config.crdtp.namespace}}::cbor::EncodeFromUTF16;
using {{config.crdtp.namespace}}::cbor::EncodeIndefiniteLengthArrayStart;
using {{config.crdtp.namespace}}::cbor::EncodeIndefiniteLengthMapStart;
using {{config.crdtp.namespace}}::cbor::EncodeInt32;
using {{config.crdtp.namespace}}::cbor::EncodeNull;
using {{config.crdtp.namespace}}::cbor::EncodeStop;
using {{config.crdtp.namespace}}::cbor::EncodeString8;
using {{config.crdtp.namespace}}::cbor::EncodeTrue;
using {{config.crdtp.namespace}}::cbor::EnvelopeEncoder;
} // namespace cbor
// Uses the parsing events received from driver of |ParserHandler|
// (e.g. cbor::ParseCBOR) into a protocol::Value instance.
class ValueParserHandler : public ParserHandler {
public:
// Provides the parsed protocol::Value.
std::unique_ptr<Value> ReleaseRoot() { return std::move(root_); }
// The first parsing error encountered; |status().ok()| is the default.
Status status() const { return status_; }
private:
//
// Implementation of ParserHandler.
//
void HandleMapBegin() override {
if (!status_.ok()) return;
std::unique_ptr<DictionaryValue> dict = DictionaryValue::create();
DictionaryValue* dict_ptr = dict.get();
AddValueToParent(std::move(dict));
stack_.emplace_back(dict_ptr);
}
void HandleMapEnd() override {
if (!status_.ok()) return;
DCHECK(!stack_.empty());
DCHECK(stack_.back().is_dict);
stack_.pop_back();
}
void HandleArrayBegin() override {
if (!status_.ok()) return;
std::unique_ptr<ListValue> list = ListValue::create();
ListValue* list_ptr = list.get();
AddValueToParent(std::move(list));
stack_.emplace_back(list_ptr);
}
void HandleArrayEnd() override {
if (!status_.ok()) return;
DCHECK(!stack_.empty());
DCHECK(!stack_.back().is_dict);
stack_.pop_back();
}
void HandleString8(span<uint8_t> chars) override {
AddStringToParent(StringUtil::fromUTF8(chars.data(), chars.size()));
}
void HandleString16(span<uint16_t> chars) override {
AddStringToParent(
StringUtil::fromUTF16LE(chars.data(), chars.size()));
}
void HandleBinary(span<uint8_t> bytes) override {
AddValueToParent(
BinaryValue::create(Binary::fromSpan(bytes.data(), bytes.size())));
}
void HandleDouble(double value) override {
AddValueToParent(FundamentalValue::create(value));
}
void HandleInt32(int32_t value) override {
AddValueToParent(FundamentalValue::create(value));
}
void HandleBool(bool value) override {
AddValueToParent(FundamentalValue::create(value));
}
void HandleNull() override {
AddValueToParent(Value::null());
}
void HandleError(Status error) override {
status_ = error;
}
//
// Adding strings and values to the parent value.
// Strings are handled separately because they can be keys for
// dictionary values.
//
void AddStringToParent(String str) {
if (!status_.ok()) return;
if (!root_) {
DCHECK(!key_is_pending_);
root_ = StringValue::create(str);
} else if (stack_.back().is_dict) {
// If we already have a pending key, then this is the value of the
// key/value pair. Otherwise, it's the new pending key.
if (key_is_pending_) {
stack_.back().dict->setString(pending_key_, str);
key_is_pending_ = false;
} else {
pending_key_ = std::move(str);
key_is_pending_ = true;
}
} else { // Top of the stack is a list.
DCHECK(!key_is_pending_);
stack_.back().list->pushValue(StringValue::create(str));
}
}
void AddValueToParent(std::unique_ptr<Value> value) {
if (!status_.ok()) return;
if (!root_) {
DCHECK(!key_is_pending_);
root_ = std::move(value);
} else if (stack_.back().is_dict) {
DCHECK(key_is_pending_);
stack_.back().dict->setValue(pending_key_, std::move(value));
key_is_pending_ = false;
} else { // Top of the stack is a list.
DCHECK(!key_is_pending_);
stack_.back().list->pushValue(std::move(value));
}
}
// |status_.ok()| is the default; if we receive an error event
// we keep the first one and stop modifying any other state.
Status status_;
// The root of the parsed protocol::Value tree.
std::unique_ptr<Value> root_;
// If root_ is a list or a dictionary, this stack keeps track of
// the container we're currently parsing as well as its ancestors.
struct ContainerState {
ContainerState(DictionaryValue* dict) : is_dict(true), dict(dict) {}
ContainerState(ListValue* list) : is_dict(false), list(list) {}
bool is_dict;
union {
DictionaryValue* dict;
ListValue* list;
};
};
std::vector<ContainerState> stack_;
// For maps, keys and values are alternating events, so we keep the
// key around and process it when the value arrives.
bool key_is_pending_ = false;
String pending_key_;
};
} // anonymous namespace
// static
std::unique_ptr<Value> Value::parseBinary(const uint8_t* data, size_t size) {
ValueParserHandler handler;
cbor::ParseCBOR(span<uint8_t>(data, size), &handler);
// TODO(johannes): We have decent error info in handler.status(); provide
// a richer interface that makes this available to client code.
if (handler.status().ok())
return handler.ReleaseRoot();
return nullptr;
}
bool Value::asBoolean(bool*) const
{
return false;
}
bool Value::asDouble(double*) const
{
return false;
}
bool Value::asInteger(int*) const
{
return false;
}
bool Value::asString(String*) const
{
return false;
}
bool Value::asBinary(Binary*) const
{
return false;
}
void Value::AppendSerialized(std::vector<uint8_t>* bytes) const {
DCHECK(m_type == TypeNull);
bytes->push_back(cbor::EncodeNull());
}
std::unique_ptr<Value> Value::clone() const
{
return Value::null();
}
bool FundamentalValue::asBoolean(bool* output) const
{
if (type() != TypeBoolean)
return false;
*output = m_boolValue;
return true;
}
bool FundamentalValue::asDouble(double* output) const
{
if (type() == TypeDouble) {
*output = m_doubleValue;
return true;
}
if (type() == TypeInteger) {
*output = m_integerValue;
return true;
}
return false;
}
bool FundamentalValue::asInteger(int* output) const
{
if (type() != TypeInteger)
return false;
*output = m_integerValue;
return true;
}
void FundamentalValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
switch (type()) {
case TypeDouble:
cbor::EncodeDouble(m_doubleValue, bytes);
return;
case TypeInteger:
cbor::EncodeInt32(m_integerValue, bytes);
return;
case TypeBoolean:
bytes->push_back(m_boolValue ? cbor::EncodeTrue() : cbor::EncodeFalse());
return;
default:
DCHECK(false);
}
}
std::unique_ptr<Value> FundamentalValue::clone() const
{
switch (type()) {
case TypeDouble: return FundamentalValue::create(m_doubleValue);
case TypeInteger: return FundamentalValue::create(m_integerValue);
case TypeBoolean: return FundamentalValue::create(m_boolValue);
default:
DCHECK(false);
}
return nullptr;
}
bool StringValue::asString(String* output) const
{
*output = m_stringValue;
return true;
}
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::AppendSerialized(std::vector<uint8_t>* bytes) const {
EncodeString(m_stringValue, bytes);
}
std::unique_ptr<Value> StringValue::clone() const
{
return StringValue::create(m_stringValue);
}
bool BinaryValue::asBinary(Binary* output) const
{
*output = m_binaryValue;
return true;
}
void BinaryValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
cbor::EncodeBinary(span<uint8_t>(m_binaryValue.data(),
m_binaryValue.size()), bytes);
}
std::unique_ptr<Value> BinaryValue::clone() const
{
return BinaryValue::create(m_binaryValue);
}
DictionaryValue::~DictionaryValue()
{
}
void DictionaryValue::setBoolean(const String& name, bool value)
{
setValue(name, FundamentalValue::create(value));
}
void DictionaryValue::setInteger(const String& name, int value)
{
setValue(name, FundamentalValue::create(value));
}
void DictionaryValue::setDouble(const String& name, double value)
{
setValue(name, FundamentalValue::create(value));
}
void DictionaryValue::setString(const String& name, const String& value)
{
setValue(name, StringValue::create(value));
}
void DictionaryValue::setValue(const String& name, std::unique_ptr<Value> value)
{
set(name, value);
}
void DictionaryValue::setObject(const String& name, std::unique_ptr<DictionaryValue> value)
{
set(name, value);
}
void DictionaryValue::setArray(const String& name, std::unique_ptr<ListValue> value)
{
set(name, value);
}
bool DictionaryValue::getBoolean(const String& name, bool* output) const
{
protocol::Value* value = get(name);
if (!value)
return false;
return value->asBoolean(output);
}
bool DictionaryValue::getInteger(const String& name, int* output) const
{
Value* value = get(name);
if (!value)
return false;
return value->asInteger(output);
}
bool DictionaryValue::getDouble(const String& name, double* output) const
{
Value* value = get(name);
if (!value)
return false;
return value->asDouble(output);
}
bool DictionaryValue::getString(const String& name, String* output) const
{
protocol::Value* value = get(name);
if (!value)
return false;
return value->asString(output);
}
DictionaryValue* DictionaryValue::getObject(const String& name) const
{
return DictionaryValue::cast(get(name));
}
protocol::ListValue* DictionaryValue::getArray(const String& name) const
{
return ListValue::cast(get(name));
}
protocol::Value* DictionaryValue::get(const String& name) const
{
Dictionary::const_iterator it = m_data.find(name);
if (it == m_data.end())
return nullptr;
return it->second.get();
}
DictionaryValue::Entry DictionaryValue::at(size_t index) const
{
const String key = m_order[index];
return std::make_pair(key, m_data.find(key)->second.get());
}
bool DictionaryValue::booleanProperty(const String& name, bool defaultValue) const
{
bool result = defaultValue;
getBoolean(name, &result);
return result;
}
int DictionaryValue::integerProperty(const String& name, int defaultValue) const
{
int result = defaultValue;
getInteger(name, &result);
return result;
}
double DictionaryValue::doubleProperty(const String& name, double defaultValue) const
{
double result = defaultValue;
getDouble(name, &result);
return result;
}
void DictionaryValue::remove(const String& name)
{
m_data.erase(name);
m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end());
}
void DictionaryValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
cbor::EnvelopeEncoder encoder;
encoder.EncodeStart(bytes);
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);
EncodeString(key, bytes);
value->second->AppendSerialized(bytes);
}
bytes->push_back(cbor::EncodeStop());
encoder.EncodeStop(bytes);
}
std::unique_ptr<Value> DictionaryValue::clone() const
{
std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
for (size_t i = 0; i < m_order.size(); ++i) {
String key = m_order[i];
Dictionary::const_iterator value = m_data.find(key);
DCHECK(value != m_data.cend() && value->second);
result->setValue(key, value->second->clone());
}
return result;
}
DictionaryValue::DictionaryValue()
: Value(TypeObject)
{
}
ListValue::~ListValue()
{
}
void ListValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
cbor::EnvelopeEncoder encoder;
encoder.EncodeStart(bytes);
bytes->push_back(cbor::EncodeIndefiniteLengthArrayStart());
for (size_t i = 0; i < m_data.size(); ++i) {
m_data[i]->AppendSerialized(bytes);
}
bytes->push_back(cbor::EncodeStop());
encoder.EncodeStop(bytes);
}
std::unique_ptr<Value> ListValue::clone() const
{
std::unique_ptr<ListValue> result = ListValue::create();
for (const std::unique_ptr<protocol::Value>& value : m_data)
result->pushValue(value->clone());
return result;
}
ListValue::ListValue()
: Value(TypeArray)
{
}
void ListValue::pushValue(std::unique_ptr<protocol::Value> value)
{
DCHECK(value);
m_data.push_back(std::move(value));
}
protocol::Value* ListValue::at(size_t index)
{
DCHECK_LT(index, m_data.size());
return m_data[index].get();
}
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}

View File

@ -0,0 +1,266 @@
// This file is generated by Values_h.template.
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef {{"_".join(config.protocol.namespace)}}_Values_h
#define {{"_".join(config.protocol.namespace)}}_Values_h
//#include "Allocator.h"
//#include "Forward.h"
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>
#include {{format_include(config.protocol.package, "Forward")}}
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
class ListValue;
class DictionaryValue;
class Value;
#define PROTOCOL_DISALLOW_COPY(ClassName) \
private: \
ClassName(const ClassName&) = delete; \
ClassName& operator=(const ClassName&) = delete
class {{config.lib.export_macro}} Value : public Serializable {
PROTOCOL_DISALLOW_COPY(Value);
public:
virtual ~Value() override { }
static std::unique_ptr<Value> null()
{
return std::unique_ptr<Value>(new Value());
}
static std::unique_ptr<Value> parseBinary(const uint8_t* data, size_t size);
enum ValueType {
TypeNull = 0,
TypeBoolean,
TypeInteger,
TypeDouble,
TypeString,
TypeBinary,
TypeObject,
TypeArray,
TypeImported
};
ValueType type() const { return m_type; }
bool isNull() const { return m_type == TypeNull; }
virtual bool asBoolean(bool* output) const;
virtual bool asDouble(double* output) const;
virtual bool asInteger(int* output) const;
virtual bool asString(String* output) const;
virtual bool asBinary(Binary* output) const;
virtual void AppendSerialized(std::vector<uint8_t>* bytes) const override;
virtual std::unique_ptr<Value> clone() const;
protected:
Value() : m_type(TypeNull) { }
explicit Value(ValueType type) : m_type(type) { }
private:
friend class DictionaryValue;
friend class ListValue;
ValueType m_type;
};
class {{config.lib.export_macro}} FundamentalValue : public Value {
public:
static std::unique_ptr<FundamentalValue> create(bool value)
{
return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
}
static std::unique_ptr<FundamentalValue> create(int value)
{
return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
}
static std::unique_ptr<FundamentalValue> create(double value)
{
return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
}
bool asBoolean(bool* output) const override;
bool asDouble(double* output) const override;
bool asInteger(int* output) const override;
void AppendSerialized(std::vector<uint8_t>* bytes) const override;
std::unique_ptr<Value> clone() const override;
private:
explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { }
explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { }
explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { }
union {
bool m_boolValue;
double m_doubleValue;
int m_integerValue;
};
};
class {{config.lib.export_macro}} StringValue : public Value {
public:
static std::unique_ptr<StringValue> create(const String& value)
{
return std::unique_ptr<StringValue>(new StringValue(value));
}
static std::unique_ptr<StringValue> create(const char* value)
{
return std::unique_ptr<StringValue>(new StringValue(value));
}
bool asString(String* output) const override;
void AppendSerialized(std::vector<uint8_t>* bytes) const override;
std::unique_ptr<Value> clone() const override;
private:
explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { }
explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { }
String m_stringValue;
};
class {{config.lib.export_macro}} BinaryValue : public Value {
public:
static std::unique_ptr<BinaryValue> create(const Binary& value)
{
return std::unique_ptr<BinaryValue>(new BinaryValue(value));
}
bool asBinary(Binary* output) const override;
void AppendSerialized(std::vector<uint8_t>* bytes) const override;
std::unique_ptr<Value> clone() const override;
private:
explicit BinaryValue(const Binary& value) : Value(TypeBinary), m_binaryValue(value) { }
Binary m_binaryValue;
};
class {{config.lib.export_macro}} DictionaryValue : public Value {
public:
using Entry = std::pair<String, Value*>;
static std::unique_ptr<DictionaryValue> create()
{
return std::unique_ptr<DictionaryValue>(new DictionaryValue());
}
static DictionaryValue* cast(Value* value)
{
if (!value || value->type() != TypeObject)
return nullptr;
return static_cast<DictionaryValue*>(value);
}
static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value)
{
DictionaryValue* dictionaryValue = cast(value.get());
if (dictionaryValue) value.release();
return std::unique_ptr<DictionaryValue>(dictionaryValue);
}
void AppendSerialized(std::vector<uint8_t>* bytes) const override;
std::unique_ptr<Value> clone() const override;
size_t size() const { return m_data.size(); }
void setBoolean(const String& name, bool);
void setInteger(const String& name, int);
void setDouble(const String& name, double);
void setString(const String& name, const String&);
void setValue(const String& name, std::unique_ptr<Value>);
void setObject(const String& name, std::unique_ptr<DictionaryValue>);
void setArray(const String& name, std::unique_ptr<ListValue>);
bool getBoolean(const String& name, bool* output) const;
bool getInteger(const String& name, int* output) const;
bool getDouble(const String& name, double* output) const;
bool getString(const String& name, String* output) const;
DictionaryValue* getObject(const String& name) const;
ListValue* getArray(const String& name) const;
Value* get(const String& name) const;
Entry at(size_t index) const;
bool booleanProperty(const String& name, bool defaultValue) const;
int integerProperty(const String& name, int defaultValue) const;
double doubleProperty(const String& name, double defaultValue) const;
void remove(const String& name);
~DictionaryValue() override;
private:
DictionaryValue();
template<typename T>
void set(const String& key, std::unique_ptr<T>& value)
{
DCHECK(value);
bool isNew = m_data.find(key) == m_data.end();
m_data[key] = std::move(value);
if (isNew)
m_order.push_back(key);
}
using Dictionary = std::unordered_map<String, std::unique_ptr<Value>>;
Dictionary m_data;
std::vector<String> m_order;
};
class {{config.lib.export_macro}} ListValue : public Value {
public:
static std::unique_ptr<ListValue> create()
{
return std::unique_ptr<ListValue>(new ListValue());
}
static ListValue* cast(Value* value)
{
if (!value || value->type() != TypeArray)
return nullptr;
return static_cast<ListValue*>(value);
}
static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value)
{
ListValue* listValue = cast(value.get());
if (listValue) value.release();
return std::unique_ptr<ListValue>(listValue);
}
~ListValue() override;
void AppendSerialized(std::vector<uint8_t>* bytes) const override;
std::unique_ptr<Value> clone() const override;
void pushValue(std::unique_ptr<Value>);
Value* at(size_t index);
size_t size() const { return m_data.size(); }
void reserve(size_t capacity) { m_data.reserve(capacity); }
private:
ListValue();
std::vector<std::unique_ptr<Value>> m_data;
};
{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}
#endif // {{"_".join(config.protocol.namespace)}}_Values_h