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_PUBLIC_FLUTTER_MESSENGER_H_ 6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_PUBLIC_FLUTTER_MESSENGER_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include "flutter_export.h" 12 13 #if defined(__cplusplus) 14 extern "C" { 15 #endif 16 17 // Opaque reference to a Flutter engine messenger. 18 typedef struct FlutterDesktopMessenger* FlutterDesktopMessengerRef; 19 20 // Opaque handle for tracking responses to messages. 21 typedef struct _FlutterPlatformMessageResponseHandle 22 FlutterDesktopMessageResponseHandle; 23 24 // The callback expected as a response of a binary message. 25 typedef void (*FlutterDesktopBinaryReply)(const uint8_t* data, 26 size_t data_size, 27 void* user_data); 28 29 // A message received from Flutter. 30 typedef struct { 31 // Size of this struct as created by Flutter. 32 size_t struct_size; 33 // The name of the channel used for this message. 34 const char* channel; 35 // The raw message data. 36 const uint8_t* message; 37 // The length of |message|. 38 size_t message_size; 39 // The response handle. If non-null, the receiver of this message must call 40 // FlutterDesktopSendMessageResponse exactly once with this handle. 41 const FlutterDesktopMessageResponseHandle* response_handle; 42 } FlutterDesktopMessage; 43 44 // Function pointer type for message handler callback registration. 45 // 46 // The user data will be whatever was passed to FlutterDesktopSetMessageHandler 47 // for the channel the message is received on. 48 typedef void (*FlutterDesktopMessageCallback)( 49 FlutterDesktopMessengerRef /* messenger */, 50 const FlutterDesktopMessage* /* message*/, 51 void* /* user data */); 52 53 // Sends a binary message to the Flutter side on the specified channel. 54 FLUTTER_EXPORT bool FlutterDesktopMessengerSend( 55 FlutterDesktopMessengerRef messenger, 56 const char* channel, 57 const uint8_t* message, 58 const size_t message_size); 59 60 FLUTTER_EXPORT bool FlutterDesktopMessengerSendWithReply( 61 FlutterDesktopMessengerRef messenger, 62 const char* channel, 63 const uint8_t* message, 64 const size_t message_size, 65 const FlutterDesktopBinaryReply reply, 66 void* user_data); 67 68 // Sends a reply to a FlutterDesktopMessage for the given response handle. 69 // 70 // Once this has been called, |handle| is invalid and must not be used again. 71 FLUTTER_EXPORT void FlutterDesktopMessengerSendResponse( 72 FlutterDesktopMessengerRef messenger, 73 const FlutterDesktopMessageResponseHandle* handle, 74 const uint8_t* data, 75 size_t data_length); 76 77 // Registers a callback function for incoming binary messages from the Flutter 78 // side on the specified channel. 79 // 80 // Replaces any existing callback. Provide a null handler to unregister the 81 // existing callback. 82 // 83 // If |user_data| is provided, it will be passed in |callback| calls. 84 FLUTTER_EXPORT void FlutterDesktopMessengerSetCallback( 85 FlutterDesktopMessengerRef messenger, 86 const char* channel, 87 FlutterDesktopMessageCallback callback, 88 void* user_data); 89 90 #if defined(__cplusplus) 91 } // extern "C" 92 #endif 93 94 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_PUBLIC_FLUTTER_MESSENGER_H_ 95