Flutter Linux Embedder
flutter::MethodChannel< T > Class Template Reference

#include <method_channel.h>

Public Member Functions

 MethodChannel (BinaryMessenger *messenger, const std::string &name, const MethodCodec< T > *codec)
 
 ~MethodChannel ()=default
 
 MethodChannel (MethodChannel const &)=delete
 
MethodChanneloperator= (MethodChannel const &)=delete
 
void InvokeMethod (const std::string &method, std::unique_ptr< T > arguments, std::unique_ptr< MethodResult< T >> result=nullptr)
 
void SetMethodCallHandler (MethodCallHandler< T > handler) const
 
void Resize (int new_size)
 
void SetWarnsOnOverflow (bool warns)
 

Detailed Description

template<typename T = EncodableValue>
class flutter::MethodChannel< T >

Definition at line 34 of file method_channel.h.

Constructor & Destructor Documentation

◆ MethodChannel() [1/2]

template<typename T = EncodableValue>
flutter::MethodChannel< T >::MethodChannel ( BinaryMessenger messenger,
const std::string &  name,
const MethodCodec< T > *  codec 
)
inline

Definition at line 38 of file method_channel.h.

41  : messenger_(messenger), name_(name), codec_(codec) {}

◆ ~MethodChannel()

template<typename T = EncodableValue>
flutter::MethodChannel< T >::~MethodChannel ( )
default

◆ MethodChannel() [2/2]

template<typename T = EncodableValue>
flutter::MethodChannel< T >::MethodChannel ( MethodChannel< T > const &  )
delete

Member Function Documentation

◆ InvokeMethod()

template<typename T = EncodableValue>
void flutter::MethodChannel< T >::InvokeMethod ( const std::string &  method,
std::unique_ptr< T >  arguments,
std::unique_ptr< MethodResult< T >>  result = nullptr 
)
inline

Definition at line 53 of file method_channel.h.

55  {
56  MethodCall<T> method_call(method, std::move(arguments));
57  std::unique_ptr<std::vector<uint8_t>> message =
58  codec_->EncodeMethodCall(method_call);
59  if (!result) {
60  messenger_->Send(name_, message->data(), message->size(), nullptr);
61  return;
62  }
63 
64  // std::function requires a copyable lambda, so convert to a shared pointer.
65  // This is safe since only one copy of the shared_pointer will ever be
66  // accessed.
67  std::shared_ptr<MethodResult<T>> shared_result(result.release());
68  const auto* codec = codec_;
69  std::string channel_name = name_;
70  BinaryReply reply_handler = [shared_result, codec, channel_name](
71  const uint8_t* reply, size_t reply_size) {
72  if (reply_size == 0) {
73  shared_result->NotImplemented();
74  return;
75  }
76  // Use this channel's codec to decode and handle the
77  // reply.
78  bool decoded = codec->DecodeAndProcessResponseEnvelope(
79  reply, reply_size, shared_result.get());
80  if (!decoded) {
81  std::cerr << "Unable to decode reply to method "
82  "invocation on channel "
83  << channel_name << std::endl;
84  shared_result->NotImplemented();
85  }
86  };
87 
88  messenger_->Send(name_, message->data(), message->size(),
89  std::move(reply_handler));
90  }

References method_call, result, and flutter::BinaryMessenger::Send().

Referenced by flutter::TEST().

◆ operator=()

template<typename T = EncodableValue>
MethodChannel& flutter::MethodChannel< T >::operator= ( MethodChannel< T > const &  )
delete

◆ Resize()

template<typename T = EncodableValue>
void flutter::MethodChannel< T >::Resize ( int  new_size)
inline

Definition at line 130 of file method_channel.h.

130  {
131  internal::ResizeChannel(messenger_, name_, new_size);
132  }

References flutter::internal::ResizeChannel().

Referenced by flutter::TEST().

◆ SetMethodCallHandler()

template<typename T = EncodableValue>
void flutter::MethodChannel< T >::SetMethodCallHandler ( MethodCallHandler< T >  handler) const
inline

Definition at line 99 of file method_channel.h.

99  {
100  if (!handler) {
101  messenger_->SetMessageHandler(name_, nullptr);
102  return;
103  }
104  const auto* codec = codec_;
105  std::string channel_name = name_;
106  BinaryMessageHandler binary_handler = [handler, codec, channel_name](
107  const uint8_t* message,
108  size_t message_size,
109  BinaryReply reply) {
110  // Use this channel's codec to decode the call and build a result handler.
111  auto result =
112  std::make_unique<EngineMethodResult<T>>(std::move(reply), codec);
113  std::unique_ptr<MethodCall<T>> method_call =
114  codec->DecodeMethodCall(message, message_size);
115  if (!method_call) {
116  std::cerr << "Unable to construct method call from message on channel "
117  << channel_name << std::endl;
118  result->NotImplemented();
119  return;
120  }
121  handler(*method_call, std::move(result));
122  };
123  messenger_->SetMessageHandler(name_, std::move(binary_handler));
124  }

References method_call, result, and flutter::BinaryMessenger::SetMessageHandler().

Referenced by flutter::TEST().

◆ SetWarnsOnOverflow()

template<typename T = EncodableValue>
void flutter::MethodChannel< T >::SetWarnsOnOverflow ( bool  warns)
inline

Definition at line 139 of file method_channel.h.

139  {
140  internal::SetChannelWarnsOnOverflow(messenger_, name_, warns);
141  }

References flutter::internal::SetChannelWarnsOnOverflow().

Referenced by flutter::TEST().


The documentation for this class was generated from the following file:
flutter::BinaryMessenger::SetMessageHandler
virtual void SetMessageHandler(const std::string &channel, BinaryMessageHandler handler)=0
method_call
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
Definition: fl_method_channel.h:120
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:17
flutter::internal::ResizeChannel
void ResizeChannel(BinaryMessenger *messenger, std::string name, int new_size)
Definition: core_implementations.cc:181
result
GAsyncResult * result
Definition: fl_text_input_handler.cc:106
flutter::internal::SetChannelWarnsOnOverflow
void SetChannelWarnsOnOverflow(BinaryMessenger *messenger, std::string name, bool warns)
Definition: core_implementations.cc:194
flutter::BinaryMessenger::Send
virtual void Send(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply=nullptr) const =0
flutter::BinaryMessageHandler
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
Definition: binary_messenger.h:24