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_BINARY_MESSENGER_H_ 6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BINARY_MESSENGER_H_ 7 8 #include <functional> 9 #include <string> 10 11 // TODO: Consider adding absl as a dependency and using absl::Span for all of 12 // the message/message_size pairs. 13 namespace flutter { 14 15 // A binary message reply callback. 16 // 17 // Used for submitting a binary reply back to a Flutter message sender. 18 typedef std::function<void(const uint8_t* reply, const size_t reply_size)> 19 BinaryReply; 20 21 // A message handler callback. 22 // 23 // Used for receiving messages from Flutter and providing an asynchronous reply. 24 typedef std::function< 25 void(const uint8_t* message, const size_t message_size, BinaryReply reply)> 26 BinaryMessageHandler; 27 28 // A protocol for a class that handles communication of binary data on named 29 // channels to and from the Flutter engine. 30 class BinaryMessenger { 31 public: 32 virtual ~BinaryMessenger() = default; 33 34 // Sends a binary message to the Flutter side on the specified channel, 35 // expecting no reply. 36 virtual void Send(const std::string& channel, 37 const uint8_t* message, 38 const size_t message_size) const = 0; 39 40 // Sends a binary message to the Flutter side on the specified channel, 41 // expecting a reply. 42 virtual void Send(const std::string& channel, 43 const uint8_t* message, 44 const size_t message_size, 45 BinaryReply reply) const = 0; 46 47 // Registers a message handler for incoming binary messages from the Flutter 48 // side on the specified channel. 49 // 50 // Replaces any existing handler. Provide a null handler to unregister the 51 // existing handler. 52 virtual void SetMessageHandler(const std::string& channel, 53 BinaryMessageHandler handler) = 0; 54 }; 55 56 } // namespace flutter 57 58 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BINARY_MESSENGER_H_ 59