Flutter Engine
basic_message_channel.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BASIC_MESSAGE_CHANNEL_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BASIC_MESSAGE_CHANNEL_H_
7 
8 #include <iostream>
9 #include <string>
10 
11 #include "binary_messenger.h"
12 #include "message_codec.h"
13 
14 namespace flutter {
15 
16 class EncodableValue;
17 
18 // A message reply callback.
19 //
20 // Used for submitting a reply back to a Flutter message sender.
21 template <typename T>
22 using MessageReply = std::function<void(const T& reply)>;
23 
24 // A handler for receiving a message from the Flutter engine.
25 //
26 // Implementations must asynchronously call reply exactly once with the reply
27 // to the message.
28 template <typename T>
29 using MessageHandler =
30  std::function<void(const T& message, const MessageReply<T>& reply)>;
31 
32 // A channel for communicating with the Flutter engine by sending asynchronous
33 // messages.
34 template <typename T = EncodableValue>
36  public:
37  // Creates an instance that sends and receives method calls on the channel
38  // named |name|, encoded with |codec| and dispatched via |messenger|.
40  const std::string& name,
41  const MessageCodec<T>* codec)
42  : messenger_(messenger), name_(name), codec_(codec) {}
43 
44  ~BasicMessageChannel() = default;
45 
46  // Prevent copying.
49 
50  // Sends a message to the Flutter engine on this channel.
51  void Send(const T& message) {
52  std::unique_ptr<std::vector<uint8_t>> raw_message =
53  codec_->EncodeMessage(message);
54  messenger_->Send(name_, raw_message->data(), raw_message->size());
55  }
56 
57  // Sends a message to the Flutter engine on this channel expecting a reply.
58  void Send(const T& message, BinaryReply reply) {
59  std::unique_ptr<std::vector<uint8_t>> raw_message =
60  codec_->EncodeMessage(message);
61  messenger_->Send(name_, raw_message->data(), raw_message->size(), reply);
62  }
63 
64  // Registers a handler that should be called any time a message is
65  // received on this channel. A null handler will remove any previous handler.
66  //
67  // Note that the BasicMessageChannel does not own the handler, and will not
68  // unregister it on destruction, so the caller is responsible for
69  // unregistering explicitly if it should no longer be called.
70  void SetMessageHandler(const MessageHandler<T>& handler) const {
71  if (!handler) {
72  messenger_->SetMessageHandler(name_, nullptr);
73  return;
74  }
75  const auto* codec = codec_;
76  std::string channel_name = name_;
77  BinaryMessageHandler binary_handler = [handler, codec, channel_name](
78  const uint8_t* binary_message,
79  const size_t binary_message_size,
80  BinaryReply binary_reply) {
81  // Use this channel's codec to decode the message and build a reply
82  // handler.
83  std::unique_ptr<T> message =
84  codec->DecodeMessage(binary_message, binary_message_size);
85  if (!message) {
86  std::cerr << "Unable to decode message on channel " << channel_name
87  << std::endl;
88  binary_reply(nullptr, 0);
89  return;
90  }
91 
92  MessageReply<T> unencoded_reply = [binary_reply,
93  codec](const T& unencoded_response) {
94  auto binary_response = codec->EncodeMessage(unencoded_response);
95  binary_reply(binary_response->data(), binary_response->size());
96  };
97  handler(*message, std::move(unencoded_reply));
98  };
99  messenger_->SetMessageHandler(name_, std::move(binary_handler));
100  }
101 
102  private:
103  BinaryMessenger* messenger_;
104  std::string name_;
105  const MessageCodec<T>* codec_;
106 };
107 
108 } // namespace flutter
109 
110 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BASIC_MESSAGE_CHANNEL_H_
BasicMessageChannel(BinaryMessenger *messenger, const std::string &name, const MessageCodec< T > *codec)
void Send(const T &message, BinaryReply reply)
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
void SetMessageHandler(const MessageHandler< T > &handler) const
BasicMessageChannel & operator=(BasicMessageChannel const &)=delete
virtual void SetMessageHandler(const std::string &channel, BinaryMessageHandler handler)=0
const char * name
Definition: fuchsia.cc:50
std::function< void(const T &message, const MessageReply< T > &reply)> MessageHandler
virtual void Send(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply=nullptr) const =0
std::function< void(const T &reply)> MessageReply
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler