Flutter Engine
flutter::JsonMethodCodec Class Reference

#include <json_method_codec.h>

Inheritance diagram for flutter::JsonMethodCodec:
flutter::MethodCodec< rapidjson::Document >

Public Member Functions

 ~JsonMethodCodec ()=default
 
 JsonMethodCodec (JsonMethodCodec const &)=delete
 
JsonMethodCodecoperator= (JsonMethodCodec const &)=delete
 
- Public Member Functions inherited from flutter::MethodCodec< rapidjson::Document >
 MethodCodec ()=default
 
 MethodCodec (MethodCodec< rapidjson::Document > const &)=delete
 
virtual ~MethodCodec ()=default
 
MethodCodecoperator= (MethodCodec< rapidjson::Document > const &)=delete
 
std::unique_ptr< MethodCall< rapidjson::Document > > DecodeMethodCall (const uint8_t *message, size_t message_size) const
 
std::unique_ptr< MethodCall< rapidjson::Document > > DecodeMethodCall (const std::vector< uint8_t > &message) const
 
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCall (const MethodCall< rapidjson::Document > &method_call) const
 
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelope (const rapidjson::Document *result=nullptr) const
 
std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelope (const std::string &error_code, const std::string &error_message="", const rapidjson::Document *error_details=nullptr) const
 
bool DecodeAndProcessResponseEnvelope (const uint8_t *response, size_t response_size, MethodResult< rapidjson::Document > *result) const
 

Static Public Member Functions

static const JsonMethodCodecGetInstance ()
 

Protected Member Functions

 JsonMethodCodec ()=default
 
std::unique_ptr< MethodCall< rapidjson::Document > > DecodeMethodCallInternal (const uint8_t *message, const size_t message_size) const override
 
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCallInternal (const MethodCall< rapidjson::Document > &method_call) const override
 
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelopeInternal (const 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
 
bool DecodeAndProcessResponseEnvelopeInternal (const uint8_t *response, const size_t response_size, MethodResult< rapidjson::Document > *result) const override
 

Detailed Description

Definition at line 16 of file json_method_codec.h.

Constructor & Destructor Documentation

◆ ~JsonMethodCodec()

flutter::JsonMethodCodec::~JsonMethodCodec ( )
default

◆ JsonMethodCodec() [1/2]

flutter::JsonMethodCodec::JsonMethodCodec ( JsonMethodCodec const &  )
delete

◆ JsonMethodCodec() [2/2]

flutter::JsonMethodCodec::JsonMethodCodec ( )
protecteddefault

Member Function Documentation

◆ DecodeAndProcessResponseEnvelopeInternal()

bool flutter::JsonMethodCodec::DecodeAndProcessResponseEnvelopeInternal ( const uint8_t *  response,
const size_t  response_size,
MethodResult< rapidjson::Document > *  result 
) const
overrideprotectedvirtual

Implements flutter::MethodCodec< rapidjson::Document >.

Definition at line 117 of file json_method_codec.cc.

References flutter::MessageCodec< T >::DecodeMessage(), flutter::MethodResult< T >::Error(), flutter::JsonMessageCodec::GetInstance(), flutter::MethodResult< T >::Success(), and value.

120  {
121  std::unique_ptr<rapidjson::Document> json_response =
122  JsonMessageCodec::GetInstance().DecodeMessage(response, response_size);
123  if (!json_response) {
124  return false;
125  }
126  if (!json_response->IsArray()) {
127  return false;
128  }
129  switch (json_response->Size()) {
130  case 1: {
131  std::unique_ptr<rapidjson::Document> value =
132  ExtractElement(json_response.get(), &((*json_response)[0]));
133  if (value->IsNull()) {
134  result->Success();
135  } else {
136  result->Success(*value);
137  }
138  return true;
139  }
140  case 3: {
141  std::string code = (*json_response)[0].GetString();
142  std::string message = (*json_response)[1].GetString();
143  std::unique_ptr<rapidjson::Document> details =
144  ExtractElement(json_response.get(), &((*json_response)[2]));
145  if (details->IsNull()) {
146  result->Error(code, message);
147  } else {
148  result->Error(code, message, *details);
149  }
150  return true;
151  }
152  default:
153  return false;
154  }
155 }
uint8_t value
std::unique_ptr< T > DecodeMessage(const uint8_t *binary_message, const size_t message_size) const
Definition: message_codec.h:29
static const JsonMessageCodec & GetInstance()

◆ DecodeMethodCallInternal()

std::unique_ptr< MethodCall< rapidjson::Document > > flutter::JsonMethodCodec::DecodeMethodCallInternal ( const uint8_t *  message,
const size_t  message_size 
) const
overrideprotectedvirtual

Implements flutter::MethodCodec< rapidjson::Document >.

Definition at line 42 of file json_method_codec.cc.

References flutter::MessageCodec< T >::DecodeMessage(), and flutter::JsonMessageCodec::GetInstance().

43  {
44  std::unique_ptr<rapidjson::Document> json_message =
45  JsonMessageCodec::GetInstance().DecodeMessage(message, message_size);
46  if (!json_message) {
47  return nullptr;
48  }
49 
50  auto method_name_iter = json_message->FindMember(kMessageMethodKey);
51  if (method_name_iter == json_message->MemberEnd()) {
52  return nullptr;
53  }
54  if (!method_name_iter->value.IsString()) {
55  return nullptr;
56  }
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));
62  }
63  return std::make_unique<MethodCall<rapidjson::Document>>(
64  method_name, std::move(arguments));
65 }
std::unique_ptr< T > DecodeMessage(const uint8_t *binary_message, const size_t message_size) const
Definition: message_codec.h:29
static const JsonMessageCodec & GetInstance()

◆ EncodeErrorEnvelopeInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::JsonMethodCodec::EncodeErrorEnvelopeInternal ( const std::string &  error_code,
const std::string &  error_message,
const rapidjson::Document *  error_details 
) const
overrideprotectedvirtual

Implements flutter::MethodCodec< rapidjson::Document >.

Definition at line 100 of file json_method_codec.cc.

References flutter::MessageCodec< T >::EncodeMessage(), and flutter::JsonMessageCodec::GetInstance().

103  {
104  rapidjson::Document envelope(rapidjson::kArrayType);
105  auto& allocator = envelope.GetAllocator();
106  envelope.PushBack(rapidjson::Value(error_code, allocator), allocator);
107  envelope.PushBack(rapidjson::Value(error_message, allocator), allocator);
108  rapidjson::Value details_value;
109  if (error_details) {
110  details_value.CopyFrom(*error_details, allocator);
111  }
112  envelope.PushBack(details_value, allocator);
113 
114  return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
115 }
static const JsonMessageCodec & GetInstance()
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45

◆ EncodeMethodCallInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::JsonMethodCodec::EncodeMethodCallInternal ( const MethodCall< rapidjson::Document > &  method_call) const
overrideprotectedvirtual

Implements flutter::MethodCodec< rapidjson::Document >.

Definition at line 67 of file json_method_codec.cc.

References flutter::MethodCall< T >::arguments(), flutter::MessageCodec< T >::EncodeMessage(), flutter::JsonMessageCodec::GetInstance(), flutter::MethodCall< T >::method_name(), and name.

68  {
69  // TODO: Consider revisiting the codec APIs to avoid the need to copy
70  // everything when doing encoding (e.g., by having a version that takes
71  // owership of the object to encode, so that it can be moved instead).
72  rapidjson::Document message(rapidjson::kObjectType);
73  auto& allocator = message.GetAllocator();
74  rapidjson::Value name(method_call.method_name(), allocator);
75  rapidjson::Value arguments;
76  if (method_call.arguments()) {
77  arguments.CopyFrom(*method_call.arguments(), allocator);
78  }
79  message.AddMember(kMessageMethodKey, name, allocator);
80  message.AddMember(kMessageArgumentsKey, arguments, allocator);
81 
83 }
G_BEGIN_DECLS FlMethodCall * method_call
static const JsonMessageCodec & GetInstance()
const char * name
Definition: fuchsia.cc:50
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45

◆ EncodeSuccessEnvelopeInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::JsonMethodCodec::EncodeSuccessEnvelopeInternal ( const rapidjson::Document *  result) const
overrideprotectedvirtual

Implements flutter::MethodCodec< rapidjson::Document >.

Definition at line 86 of file json_method_codec.cc.

References flutter::MessageCodec< T >::EncodeMessage(), and flutter::JsonMessageCodec::GetInstance().

87  {
88  rapidjson::Document envelope;
89  envelope.SetArray();
90  rapidjson::Value result_value;
91  if (result) {
92  result_value.CopyFrom(*result, envelope.GetAllocator());
93  }
94  envelope.PushBack(result_value, envelope.GetAllocator());
95 
97 }
static const JsonMessageCodec & GetInstance()
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45

◆ GetInstance()

const JsonMethodCodec & flutter::JsonMethodCodec::GetInstance ( )
static

Definition at line 36 of file json_method_codec.cc.

Referenced by flutter::TextInputPlugin::KeyboardHook(), and flutter::TEST().

36  {
37  static JsonMethodCodec sInstance;
38  return sInstance;
39 }

◆ operator=()

JsonMethodCodec& flutter::JsonMethodCodec::operator= ( JsonMethodCodec const &  )
delete

The documentation for this class was generated from the following files: