Flutter iOS Embedder
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

◆ 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 432 of file standard_codec.cc.

435  {
436  ByteBufferStreamReader stream(response, response_size);
437  uint8_t flag = stream.ReadByte();
438  switch (flag) {
439  case 0: {
440  EncodableValue value = serializer_->ReadValue(&stream);
441  if (value.IsNull()) {
442  result->Success();
443  } else {
444  result->Success(value);
445  }
446  return true;
447  }
448  case 1: {
449  EncodableValue code = serializer_->ReadValue(&stream);
450  EncodableValue message = serializer_->ReadValue(&stream);
451  EncodableValue details = serializer_->ReadValue(&stream);
452  const std::string& message_string =
453  message.IsNull() ? "" : std::get<std::string>(message);
454  if (details.IsNull()) {
455  result->Error(std::get<std::string>(code), message_string);
456  } else {
457  result->Error(std::get<std::string>(code), message_string, details);
458  }
459  return true;
460  }
461  default:
462  return false;
463  }
464 }

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

◆ 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 366 of file standard_codec.cc.

367  {
368  ByteBufferStreamReader stream(message, message_size);
369  EncodableValue method_name_value = serializer_->ReadValue(&stream);
370  const auto* method_name = std::get_if<std::string>(&method_name_value);
371  if (!method_name) {
372  std::cerr << "Invalid method call; method name is not a string."
373  << std::endl;
374  return nullptr;
375  }
376  auto arguments =
377  std::make_unique<EncodableValue>(serializer_->ReadValue(&stream));
378  return std::make_unique<MethodCall<EncodableValue>>(*method_name,
379  std::move(arguments));
380 }

References flutter::StandardCodecSerializer::ReadValue().

◆ 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 411 of file standard_codec.cc.

414  {
415  auto encoded = std::make_unique<std::vector<uint8_t>>();
416  ByteBufferStreamWriter stream(encoded.get());
417  stream.WriteByte(1);
418  serializer_->WriteValue(EncodableValue(error_code), &stream);
419  if (error_message.empty()) {
420  serializer_->WriteValue(EncodableValue(), &stream);
421  } else {
422  serializer_->WriteValue(EncodableValue(error_message), &stream);
423  }
424  if (error_details) {
425  serializer_->WriteValue(*error_details, &stream);
426  } else {
427  serializer_->WriteValue(EncodableValue(), &stream);
428  }
429  return encoded;
430 }

References flutter::StandardCodecSerializer::WriteValue().

◆ 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 383 of file standard_codec.cc.

384  {
385  auto encoded = std::make_unique<std::vector<uint8_t>>();
386  ByteBufferStreamWriter stream(encoded.get());
387  serializer_->WriteValue(EncodableValue(method_call.method_name()), &stream);
388  if (method_call.arguments()) {
389  serializer_->WriteValue(*method_call.arguments(), &stream);
390  } else {
391  serializer_->WriteValue(EncodableValue(), &stream);
392  }
393  return encoded;
394 }

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

◆ EncodeSuccessEnvelopeInternal()

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

Implements flutter::MethodCodec< EncodableValue >.

Definition at line 397 of file standard_codec.cc.

398  {
399  auto encoded = std::make_unique<std::vector<uint8_t>>();
400  ByteBufferStreamWriter stream(encoded.get());
401  stream.WriteByte(0);
402  if (result) {
403  serializer_->WriteValue(*result, &stream);
404  } else {
405  serializer_->WriteValue(EncodableValue(), &stream);
406  }
407  return encoded;
408 }

References flutter::StandardCodecSerializer::WriteValue().

◆ GetInstance()

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

Definition at line 340 of file standard_codec.cc.

341  {
342  if (!serializer) {
343  serializer = &StandardCodecSerializer::GetInstance();
344  }
345  static auto* sInstances = new std::map<const StandardCodecSerializer*,
346  std::unique_ptr<StandardMethodCodec>>;
347  auto it = sInstances->find(serializer);
348  if (it == sInstances->end()) {
349  // Uses new due to private constructor (to prevent API clients from
350  // accidentally passing temporary codec instances to channels).
351  auto emplace_result = sInstances->emplace(
352  serializer, std::unique_ptr<StandardMethodCodec>(
353  new StandardMethodCodec(serializer)));
354  it = emplace_result.first;
355  }
356  return *(it->second);
357 }

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

Referenced by flutter::internal::ResizeChannel(), flutter::internal::SetChannelWarnsOnOverflow(), and flutter::TEST().

◆ operator=()

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

The documentation for this class was generated from the following files:
flutter::StandardCodecSerializer::ReadValue
EncodableValue ReadValue(ByteStreamReader *stream) const
Definition: standard_codec.cc:92
flutter::EncodableValue::IsNull
bool IsNull() const
Definition: encodable_value.h:203
flutter::StandardCodecSerializer::WriteValue
virtual void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const
Definition: standard_codec.cc:98
flutter::StandardMethodCodec::StandardMethodCodec
StandardMethodCodec(StandardMethodCodec const &)=delete
flutter::StandardCodecSerializer::GetInstance
static const StandardCodecSerializer & GetInstance()
Definition: standard_codec.cc:87
flutter::MethodResult::Error
void Error(const std::string &error_code, const std::string &error_message, const T &error_details)
Definition: method_result.h:41
flutter::MethodResult::Success
void Success(const T &result)
Definition: method_result.h:29