| 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 namespace internal {
18 // Internal helper functions used by BasicMessageChannel and MethodChannel.
19
20 // Adjusts the number of messages that will get buffered when sending messages
21 // to channels that aren't fully set up yet. For example, the engine isn't
22 // running yet or the channel's message handler isn't set up on the Dart side
23 // yet.
24 void ResizeChannel(BinaryMessenger* messenger, std::string name, int new_size);
25
26 // Defines whether the channel should show warning messages when discarding
27 // messages due to overflow.
28 //
29 // When |warns| is false, the channel is expected to overflow and warning
30 // messages will not be shown.
31 void SetChannelWarnsOnOverflow(BinaryMessenger* messenger,
32 std::string name,
33 bool warns);
34
35 } // namespace internal
36
37 class EncodableValue;
38
39 // A message reply callback.
40 //
41 // Used for submitting a reply back to a Flutter message sender.
42 template <typename T>
43 using MessageReply = std::function<void(const T& reply)>;
44
45 // A handler for receiving a message from the Flutter engine.
46 //
47 // Implementations must asynchronously call reply exactly once with the reply
48 // to the message.
49 template <typename T>
50 using MessageHandler =
51 std::function<void(const T& message, const MessageReply<T>& reply)>;
52
53 // A channel for communicating with the Flutter engine by sending asynchronous
54 // messages.
55 template <typename T = EncodableValue>
56 class BasicMessageChannel {
57 public:
58 // Creates an instance that sends and receives method calls on the channel
59 // named |name|, encoded with |codec| and dispatched via |messenger|.
60 BasicMessageChannel(BinaryMessenger* messenger,
61 const std::string& name,
62 const MessageCodec<T>* codec)
63 : messenger_(messenger), name_(name), codec_(codec) {}
64
65 ~BasicMessageChannel() = default;
66
67 // Prevent copying.
68 BasicMessageChannel(BasicMessageChannel const&) = delete;
69 BasicMessageChannel& operator=(BasicMessageChannel const&) = delete;
70
71 // Sends a message to the Flutter engine on this channel.
72 void Send(const T& message) {
73 std::unique_ptr<std::vector<uint8_t>> raw_message =
74 codec_->EncodeMessage(message);
75 messenger_->Send(name_, raw_message->data(), raw_message->size());
76 }
77
78 // Sends a message to the Flutter engine on this channel expecting a reply.
79 void Send(const T& message, BinaryReply reply) {
80 std::unique_ptr<std::vector<uint8_t>> raw_message =
81 codec_->EncodeMessage(message);
82 messenger_->Send(name_, raw_message->data(), raw_message->size(),
83 std::move(reply));
84 }
85
86 // Registers a handler that should be called any time a message is
87 // received on this channel. A null handler will remove any previous handler.
88 //
89 // Note that the BasicMessageChannel does not own the handler, and will not
90 // unregister it on destruction, so the caller is responsible for
91 // unregistering explicitly if it should no longer be called.
92 void SetMessageHandler(const MessageHandler<T>& handler) const {
93 if (!handler) {
94 messenger_->SetMessageHandler(name_, nullptr);
95 return;
96 }
97 const auto* codec = codec_;
98 std::string channel_name = name_;
99 BinaryMessageHandler binary_handler = [handler, codec, channel_name](
100 const uint8_t* binary_message,
101 const size_t binary_message_size,
102 const BinaryReply& binary_reply) {
103 // Use this channel's codec to decode the message and build a reply
104 // handler.
105 std::unique_ptr<T> message =
106 codec->DecodeMessage(binary_message, binary_message_size);
107 if (!message) {
108 std::cerr << "Unable to decode message on channel " << channel_name
109 << std::endl;
110 binary_reply(nullptr, 0);
111 return;
112 }
113
114 MessageReply<T> unencoded_reply = [binary_reply,
115 codec](const T& unencoded_response) {
116 auto binary_response = codec->EncodeMessage(unencoded_response);
117 binary_reply(binary_response->data(), binary_response->size());
118 };
119 handler(*message, std::move(unencoded_reply));
120 };
121 messenger_->SetMessageHandler(name_, std::move(binary_handler));
122 }
123
124 // Adjusts the number of messages that will get buffered when sending messages
125 // to channels that aren't fully set up yet. For example, the engine isn't
126 // running yet or the channel's message handler isn't set up on the Dart side
127 // yet.
128 void Resize(int new_size) {
129 internal::ResizeChannel(messenger_, name_, new_size);
130 }
131
132 // Defines whether the channel should show warning messages when discarding
133 // messages due to overflow.
134 //
135 // When |warns| is false, the channel is expected to overflow and warning
136 // messages will not be shown.
137 void SetWarnsOnOverflow(bool warns) {
138 internal::SetChannelWarnsOnOverflow(messenger_, name_, warns);
139 }
140
141 private:
142 BinaryMessenger* messenger_;
143 std::string name_;
144 const MessageCodec<T>* codec_;
145 };
146
147 } // namespace flutter
148
149 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_BASIC_MESSAGE_CHANNEL_H_
binary_messenger.h
flutter::BasicMessageChannel
Definition: basic_message_channel.h:56
flutter::BasicMessageChannel::Send
void Send(const T &message)
Definition: basic_message_channel.h:72
flutter::BasicMessageChannel::BasicMessageChannel
BasicMessageChannel(BasicMessageChannel const &)=delete
flutter::BasicMessageChannel::~BasicMessageChannel
~BasicMessageChannel()=default
flutter::BasicMessageChannel::Send
void Send(const T &message, BinaryReply reply)
Definition: basic_message_channel.h:79
flutter::BasicMessageChannel::Resize
void Resize(int new_size)
Definition: basic_message_channel.h:128
flutter::BasicMessageChannel::operator=
BasicMessageChannel & operator=(BasicMessageChannel const &)=delete
flutter::BasicMessageChannel::SetMessageHandler
void SetMessageHandler(const MessageHandler< T > &handler) const
Definition: basic_message_channel.h:92
flutter::BasicMessageChannel::SetWarnsOnOverflow
void SetWarnsOnOverflow(bool warns)
Definition: basic_message_channel.h:137
flutter::BasicMessageChannel::BasicMessageChannel
BasicMessageChannel(BinaryMessenger *messenger, const std::string &name, const MessageCodec< T > *codec)
Definition: basic_message_channel.h:60
flutter::BinaryMessenger
Definition: binary_messenger.h:28
flutter::BinaryMessenger::Send
virtual void Send(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply=nullptr) const =0
flutter::BinaryMessenger::SetMessageHandler
virtual void SetMessageHandler(const std::string &channel, BinaryMessageHandler handler)=0
flutter::MessageCodec
Definition: message_codec.h:17
message
Win32Message message
Definition: keyboard_unittests.cc:137
message_codec.h
flutter::internal::SetChannelWarnsOnOverflow
void SetChannelWarnsOnOverflow(BinaryMessenger *messenger, std::string name, bool warns)
Definition: core_implementations.cc:194
flutter::internal::ResizeChannel
void ResizeChannel(BinaryMessenger *messenger, std::string name, int new_size)
Definition: core_implementations.cc:181
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::MessageHandler
std::function< void(const T &message, const MessageReply< T > &reply)> MessageHandler
Definition: basic_message_channel.h:51
flutter::BinaryMessageHandler
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
Definition: binary_messenger.h:24
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:17
flutter::MessageReply
std::function< void(const T &reply)> MessageReply
Definition: basic_message_channel.h:43