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