Flutter Engine
flutter::StandardMethodCodec Class Reference

#include <standard_method_codec.h>

Inheritance diagram for flutter::StandardMethodCodec:
flutter::MethodCodec< EncodableValue >

Public Member Functions

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

Static Public Member Functions

static const StandardMethodCodecGetInstance (const StandardCodecSerializer *serializer=nullptr)
 

Protected Member Functions

std::unique_ptr< MethodCall< EncodableValue > > DecodeMethodCallInternal (const uint8_t *message, size_t message_size) const override
 
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCallInternal (const MethodCall< EncodableValue > &method_call) const override
 
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelopeInternal (const EncodableValue *result) const override
 
std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelopeInternal (const std::string &error_code, const std::string &error_message, const EncodableValue *error_details) const override
 
bool DecodeAndProcessResponseEnvelopeInternal (const uint8_t *response, size_t response_size, MethodResult< EncodableValue > *result) const override
 

Detailed Description

Definition at line 18 of file standard_method_codec.h.

Constructor & Destructor Documentation

◆ ~StandardMethodCodec()

flutter::StandardMethodCodec::~StandardMethodCodec ( )
default

Referenced by GetInstance().

◆ StandardMethodCodec()

flutter::StandardMethodCodec::StandardMethodCodec ( StandardMethodCodec const &  )
delete

Referenced by GetInstance().

Member Function Documentation

◆ DecodeAndProcessResponseEnvelopeInternal()

bool flutter::StandardMethodCodec::DecodeAndProcessResponseEnvelopeInternal ( const uint8_t *  response,
size_t  response_size,
MethodResult< EncodableValue > *  result 
) const
overrideprotectedvirtual

Implements flutter::MethodCodec< EncodableValue >.

Definition at line 419 of file standard_codec.cc.

References flutter::MethodResult< T >::Error(), flutter::EncodableValue::IsNull(), flutter::ByteBufferStreamReader::ReadByte(), flutter::MethodResult< T >::Success(), and value.

422  {
423  ByteBufferStreamReader stream(response, response_size);
424  uint8_t flag = stream.ReadByte();
425  switch (flag) {
426  case 0: {
427  EncodableValue value = serializer_->ReadValue(&stream);
428  if (value.IsNull()) {
429  result->Success();
430  } else {
431  result->Success(value);
432  }
433  return true;
434  }
435  case 1: {
436  EncodableValue code = serializer_->ReadValue(&stream);
437  EncodableValue message = serializer_->ReadValue(&stream);
438  EncodableValue details = serializer_->ReadValue(&stream);
439  const std::string& message_string =
440  message.IsNull() ? "" : std::get<std::string>(message);
441  if (details.IsNull()) {
442  result->Error(std::get<std::string>(code), message_string);
443  } else {
444  result->Error(std::get<std::string>(code), message_string, details);
445  }
446  return true;
447  }
448  default:
449  return false;
450  }
451 }
EncodableValue ReadValue(ByteStreamReader *stream) const
uint8_t value

◆ DecodeMethodCallInternal()

std::unique_ptr< MethodCall< EncodableValue > > flutter::StandardMethodCodec::DecodeMethodCallInternal ( const uint8_t *  message,
size_t  message_size 
) const
overrideprotectedvirtual

Implements flutter::MethodCodec< EncodableValue >.

Definition at line 353 of file standard_codec.cc.

354  {
355  ByteBufferStreamReader stream(message, message_size);
356  EncodableValue method_name_value = serializer_->ReadValue(&stream);
357  const auto* method_name = std::get_if<std::string>(&method_name_value);
358  if (!method_name) {
359  std::cerr << "Invalid method call; method name is not a string."
360  << std::endl;
361  return nullptr;
362  }
363  auto arguments =
364  std::make_unique<EncodableValue>(serializer_->ReadValue(&stream));
365  return std::make_unique<MethodCall<EncodableValue>>(*method_name,
366  std::move(arguments));
367 }
EncodableValue ReadValue(ByteStreamReader *stream) const

◆ EncodeErrorEnvelopeInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::StandardMethodCodec::EncodeErrorEnvelopeInternal ( const std::string &  error_code,
const std::string &  error_message,
const EncodableValue error_details 
) const
overrideprotectedvirtual

Implements flutter::MethodCodec< EncodableValue >.

Definition at line 398 of file standard_codec.cc.

References flutter::ByteStreamWriter::WriteByte().

401  {
402  auto encoded = std::make_unique<std::vector<uint8_t>>();
403  ByteBufferStreamWriter stream(encoded.get());
404  stream.WriteByte(1);
405  serializer_->WriteValue(EncodableValue(error_code), &stream);
406  if (error_message.empty()) {
407  serializer_->WriteValue(EncodableValue(), &stream);
408  } else {
409  serializer_->WriteValue(EncodableValue(error_message), &stream);
410  }
411  if (error_details) {
412  serializer_->WriteValue(*error_details, &stream);
413  } else {
414  serializer_->WriteValue(EncodableValue(), &stream);
415  }
416  return encoded;
417 }
virtual void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const

◆ EncodeMethodCallInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::StandardMethodCodec::EncodeMethodCallInternal ( const MethodCall< EncodableValue > &  method_call) const
overrideprotectedvirtual

Implements flutter::MethodCodec< EncodableValue >.

Definition at line 370 of file standard_codec.cc.

References flutter::MethodCall< T >::arguments(), and flutter::MethodCall< T >::method_name().

371  {
372  auto encoded = std::make_unique<std::vector<uint8_t>>();
373  ByteBufferStreamWriter stream(encoded.get());
374  serializer_->WriteValue(EncodableValue(method_call.method_name()), &stream);
375  if (method_call.arguments()) {
376  serializer_->WriteValue(*method_call.arguments(), &stream);
377  } else {
378  serializer_->WriteValue(EncodableValue(), &stream);
379  }
380  return encoded;
381 }
G_BEGIN_DECLS FlMethodCall * method_call
virtual void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const

◆ EncodeSuccessEnvelopeInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::StandardMethodCodec::EncodeSuccessEnvelopeInternal ( const EncodableValue result) const
overrideprotectedvirtual

Implements flutter::MethodCodec< EncodableValue >.

Definition at line 384 of file standard_codec.cc.

References flutter::ByteStreamWriter::WriteByte().

385  {
386  auto encoded = std::make_unique<std::vector<uint8_t>>();
387  ByteBufferStreamWriter stream(encoded.get());
388  stream.WriteByte(0);
389  if (result) {
390  serializer_->WriteValue(*result, &stream);
391  } else {
392  serializer_->WriteValue(EncodableValue(), &stream);
393  }
394  return encoded;
395 }
virtual void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const

◆ GetInstance()

const StandardMethodCodec & flutter::StandardMethodCodec::GetInstance ( const StandardCodecSerializer serializer = nullptr)
static

Definition at line 327 of file standard_codec.cc.

References flutter::StandardCodecSerializer::GetInstance(), StandardMethodCodec(), and ~StandardMethodCodec().

Referenced by flutter::TEST().

328  {
329  if (!serializer) {
330  serializer = &StandardCodecSerializer::GetInstance();
331  }
332  auto* sInstances = new std::map<const StandardCodecSerializer*,
333  std::unique_ptr<StandardMethodCodec>>;
334  auto it = sInstances->find(serializer);
335  if (it == sInstances->end()) {
336  // Uses new due to private constructor (to prevent API clients from
337  // accidentally passing temporary codec instances to channels).
338  auto emplace_result = sInstances->emplace(
339  serializer, std::unique_ptr<StandardMethodCodec>(
340  new StandardMethodCodec(serializer)));
341  it = emplace_result.first;
342  }
343  return *(it->second);
344 }
static const StandardCodecSerializer & GetInstance()
StandardMethodCodec(StandardMethodCodec const &)=delete

◆ operator=()

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

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