5#include "flutter/shell/platform/common/json_method_codec.h"
7#include "flutter/shell/platform/common/json_message_codec.h"
14constexpr char kMessageMethodKey[] =
"method";
15constexpr char kMessageArgumentsKey[] =
"args";
20std::unique_ptr<rapidjson::Document> ExtractElement(
21 rapidjson::Document* document,
22 rapidjson::Value* subtree) {
23 auto extracted = std::make_unique<rapidjson::Document>();
25 document->Swap(*subtree);
29 extracted->Swap(*document);
41std::unique_ptr<MethodCall<rapidjson::Document>>
43 size_t message_size)
const {
44 std::unique_ptr<rapidjson::Document> json_message =
50 auto method_name_iter = json_message->FindMember(kMessageMethodKey);
51 if (method_name_iter == json_message->MemberEnd()) {
54 if (!method_name_iter->value.IsString()) {
57 std::string method_name(method_name_iter->value.GetString());
58 auto arguments_iter = json_message->FindMember(kMessageArgumentsKey);
59 std::unique_ptr<rapidjson::Document> arguments;
60 if (arguments_iter != json_message->MemberEnd()) {
61 arguments = ExtractElement(json_message.get(), &(arguments_iter->value));
63 return std::make_unique<MethodCall<rapidjson::Document>>(
64 method_name, std::move(arguments));
72 rapidjson::Document
message(rapidjson::kObjectType);
73 auto& allocator =
message.GetAllocator();
75 rapidjson::Value arguments;
77 arguments.CopyFrom(*
method_call.arguments(), allocator);
79 message.AddMember(kMessageMethodKey,
name, allocator);
80 message.AddMember(kMessageArgumentsKey, arguments, allocator);
85std::unique_ptr<std::vector<uint8_t>>
87 const rapidjson::Document*
result)
const {
88 rapidjson::Document envelope;
90 rapidjson::Value result_value;
92 result_value.CopyFrom(*
result, envelope.GetAllocator());
94 envelope.PushBack(result_value, envelope.GetAllocator());
99std::unique_ptr<std::vector<uint8_t>>
101 const std::string& error_code,
102 const std::string& error_message,
103 const rapidjson::Document* error_details)
const {
105 rapidjson::Document envelope(rapidjson::kArrayType);
106 auto& allocator = envelope.GetAllocator();
107 envelope.PushBack(rapidjson::Value(error_code, allocator), allocator);
108 envelope.PushBack(rapidjson::Value(error_message, allocator), allocator);
109 rapidjson::Value details_value;
111 details_value.CopyFrom(*error_details, allocator);
113 envelope.PushBack(details_value, allocator);
119 const uint8_t* response,
120 size_t response_size,
122 std::unique_ptr<rapidjson::Document> json_response =
124 if (!json_response) {
127 if (!json_response->IsArray()) {
130 switch (json_response->Size()) {
132 std::unique_ptr<rapidjson::Document>
value =
133 ExtractElement(json_response.get(), &((*json_response)[0]));
134 if (
value->IsNull()) {
142 std::string
code = (*json_response)[0].GetString();
143 std::string
message = (*json_response)[1].GetString();
144 std::unique_ptr<rapidjson::Document> details =
145 ExtractElement(json_response.get(), &((*json_response)[2]));
146 if (details->IsNull()) {
static const JsonMessageCodec & GetInstance()
static const JsonMethodCodec & GetInstance()
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCallInternal(const MethodCall< rapidjson::Document > &method_call) const override
std::unique_ptr< MethodCall< rapidjson::Document > > DecodeMethodCallInternal(const uint8_t *message, const size_t message_size) const override
bool DecodeAndProcessResponseEnvelopeInternal(const uint8_t *response, const size_t response_size, MethodResult< rapidjson::Document > *result) const override
std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelopeInternal(const std::string &error_code, const std::string &error_message, const rapidjson::Document *error_details) const override
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelopeInternal(const rapidjson::Document *result) const override
std::unique_ptr< T > DecodeMessage(const uint8_t *binary_message, const size_t message_size) const
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
DEF_SWITCHES_START aot vmservice shared library name