Flutter Engine
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
 

Detailed Description

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

Definition at line 33 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 37 of file method_channel.h.

References flutter::MethodChannel< T >::operator=(), and flutter::MethodChannel< T >::~MethodChannel().

40  : messenger_(messenger), name_(name), codec_(codec) {}
const char * name
Definition: fuchsia.cc:50

◆ ~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 52 of file method_channel.h.

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

Referenced by flutter::TEST().

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

◆ operator=()

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

◆ SetMethodCallHandler()

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

Definition at line 97 of file method_channel.h.

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

Referenced by flutter::TEST().

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

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