This library implements the encoding layer for the inspector protocol. The following facilities are provided:
Some tools for portability, including a simple span implementation. The library itself does not depend on anything except the C++ standard libraries. For the JSON features, dependencies are injected via a json::Platform object. See the json namespace in encoding.h
CBOR-based encoding/decoding functionality for a binary format. See the comment at the top of the cbor namespace in encoding.h for details.
JSON encoder/decoder. See the json namespace in encoding.h for details.
CBOR and JSON parsers use a streaming API. See StreamingParserHandler in encoding.h.
Two-way conversions between CBOR-based binary and JSON. See the json::ConvertCBORToJSON / json::ConvertJSONToCBOR functions in encoding.h
The library is designed for portability and users of the inspector protocol should link it into their programs. We do so in Chromium, V8, and internally at Google.
TODO(johannes): Update the design documentation below.
We're hoping for:
To begin, we wish to make a library for the core part of digesting inspector_protocol messages - the encoding library, in this directory. That is, encoding / decoding as well as extracting some values that are needed for dispatch (method, message id, session id).
Our first input shall be JSON, the existing wire format. It‘s convenient for testing, but also we’ll later be able to use it to convert the existing wire format as early as possible into binary, e.g. in the devtools pipe for headless, and therefore we won't need to maintain much separate code between json and binary inputs.
With the approach described here, we will not parse the JSON into dictionaries or base::Value / protocol::Value nested structures. Instead, we'll convert it into a binary format, probably CBOR.
For this, json_parser{.h,.cc} implements a JSON parser with a SAX style handler interface, which will drive conversion into a binary buffer via a specific handler. We‘ll call the abstraction for the binary format MessageBuffer from here (most likely we’ll have a C++ class with that name).
For parts of the Chromium code that doesn‘t need to inspect deep into the inspector_protocol messages, we’ll be able to extract individual values (message id, session id, method name) from this MessageBuffer. This is because MessageBuffer owns the wire format bytes (e.g. std::vector<uint_t> in a private field) and can return values as string pieces or int64_t as appropriate.
This library does not involve code generation - it‘s just checked in C++. The library will be unittested, and because we have a JSON parser, it’s an excellent way to drive examples into the CBOR buffer and check that they round trip OK. It may be practical to develop a JSON serializer in the process as well, at first to make testing simpler, and then later, to use it for compatibility in production.
There is strtod (for json), there is character encoding, and there is string / vector / etc. libraries. We'll try handle them to maximize portability while making it easy to test the library.
For strtod, clients of the library will need to provide their implementation. We‘ll use virtual method dispatch (abstract class + implementation), they’ll have to pass in an object, e.g. a singleton.
For UTF8 / UTF16 Unicode encoding, we‘ll try to avoid adding dependencies and see how far we get with the approach, much like jsoncpp does it, basically very minimal / light and no dependency on ICU. We’ll depend on mini_chromium for testing, so that gives us ICU there.
strings. We‘ll try to keep this minimal as well, by primarily working with string pieces on the public surface on the library. E.g., for the message id, which can be extracted from the binary message, we’ll return a string piece which is backed by our binary buffer.
The above library should be usable at least from the browser side, blink, and v8, but likely also from other projects (e.g. Google internal).
The code generator (not part of encoding library) will provide:
The code generator is currently implemented in Python / jinja2 templates; we can probably adapt this / bend it to these needs. The templates for the existing functionality should shrink, however. E.g., we will not have a json parser in a template but rather use the one in the new library, and we may also move other supporting code out of the templates and into the library. The benefit of this should be that it's easier to understand / test / maintain, especially it should become possible to unittest within this project and provide sufficient confidence.