Flutter iOS Embedder
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 118 of file json_method_codec.cc.

121  {
122  std::unique_ptr<rapidjson::Document> json_response =
123  JsonMessageCodec::GetInstance().DecodeMessage(response, response_size);
124  if (!json_response) {
125  return false;
126  }
127  if (!json_response->IsArray()) {
128  return false;
129  }
130  switch (json_response->Size()) {
131  case 1: {
132  std::unique_ptr<rapidjson::Document> value =
133  ExtractElement(json_response.get(), &((*json_response)[0]));
134  if (value->IsNull()) {
135  result->Success();
136  } else {
137  result->Success(*value);
138  }
139  return true;
140  }
141  case 3: {
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()) {
147  result->Error(code, message);
148  } else {
149  result->Error(code, message, *details);
150  }
151  return true;
152  }
153  default:
154  return false;
155  }
156 }

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

◆ 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.

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 }

References flutter::MessageCodec< T >::DecodeMessage(), and flutter::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.

103  {
104  // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
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;
110  if (error_details) {
111  details_value.CopyFrom(*error_details, allocator);
112  }
113  envelope.PushBack(details_value, allocator);
114 
115  return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
116 }

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

◆ 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.

68  {
69  // TODO(stuartmorgan): Consider revisiting the codec APIs to avoid the need
70  // to copy everything when doing encoding (e.g., by having a version that
71  // takes 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 }

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

◆ 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.

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 }

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

◆ GetInstance()

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

Definition at line 36 of file json_method_codec.cc.

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

Referenced by flutter::TEST().

◆ operator=()

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

The documentation for this class was generated from the following files:
flutter::JsonMessageCodec::GetInstance
static const JsonMessageCodec & GetInstance()
Definition: json_message_codec.cc:17
flutter::MessageCodec::DecodeMessage
std::unique_ptr< T > DecodeMessage(const uint8_t *binary_message, const size_t message_size) const
Definition: message_codec.h:29
flutter::MessageCodec::EncodeMessage
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45
flutter::JsonMethodCodec::JsonMethodCodec
JsonMethodCodec()=default