@@ -3,13 +3,18 @@ package embedder
3
3
// #include "embedder.h"
4
4
// FlutterEngineResult runFlutter(void *user_data, FlutterEngine *engine, FlutterProjectArgs * Args,
5
5
// const char *const * vmArgs, int nVmAgrs);
6
+ // FlutterEngineResult
7
+ // createMessageResponseHandle(FlutterEngine engine, void *user_data,
8
+ // FlutterPlatformMessageResponseHandle **reply);
6
9
// char** makeCharArray(int size);
7
10
// void setArrayString(char **a, char *s, int n);
8
11
// const int32_t kFlutterSemanticsNodeIdBatchEnd = -1;
9
12
// const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1;
10
13
import "C"
11
14
import (
15
+ "errors"
12
16
"fmt"
17
+ "runtime"
13
18
"runtime/debug"
14
19
"sync"
15
20
"unsafe"
@@ -256,8 +261,11 @@ type PlatformMessage struct {
256
261
Channel string
257
262
Message []byte
258
263
259
- // ResponseHandle is only set when receiving a platform message.
260
- // https://github.com/flutter/flutter/issues/18852
264
+ // ResponseHandle is set on some recieved platform message. All
265
+ // PlatformMessage recieved with this attribute must send a response with
266
+ // `SendPlatformMessageResponse`.
267
+ // ResponseHandle can also be created from the embedder side when a
268
+ // platform(golang) message needs native callback.
261
269
ResponseHandle PlatformMessageResponseHandle
262
270
}
263
271
@@ -357,8 +365,42 @@ func (flu *FlutterEngine) MarkExternalTextureFrameAvailable(textureID int64) Res
357
365
return (Result )(res )
358
366
}
359
367
360
- // FlutterEngineGetCurrentTime gets the current time in nanoseconds from the
361
- // clock used by the flutter engine.
368
+ // DataCallback is a function called when a PlatformMessage response send back
369
+ // to the embedder.
370
+ type DataCallback func (binaryReply []byte )
371
+
372
+ // CreatePlatformMessageResponseHandle creates a platform message response
373
+ // handle that allows the embedder to set a native callback for a response to a
374
+ // message.
375
+ // Must be collected via `ReleasePlatformMessageResponseHandle` after the call
376
+ // to `SendPlatformMessage`.
377
+ func (flu * FlutterEngine ) CreatePlatformMessageResponseHandle (callback DataCallback ) (PlatformMessageResponseHandle , error ) {
378
+ var responseHandle * C.FlutterPlatformMessageResponseHandle
379
+
380
+ callbackPointer := uintptr (unsafe .Pointer (& callback ))
381
+ defer func () {
382
+ runtime .KeepAlive (callbackPointer )
383
+ }()
384
+
385
+ res := C .createMessageResponseHandle (flu .Engine , unsafe .Pointer (& callbackPointer ), & responseHandle )
386
+ if (Result )(res ) != ResultSuccess {
387
+ return 0 , errors .New ("failed to create a response handle" )
388
+ }
389
+ return PlatformMessageResponseHandle (unsafe .Pointer (responseHandle )), nil
390
+ }
391
+
392
+ // ReleasePlatformMessageResponseHandle collects a platform message response
393
+ // handle.
394
+ func (flu * FlutterEngine ) ReleasePlatformMessageResponseHandle (responseHandle PlatformMessageResponseHandle ) {
395
+ cResponseHandle := (* C .FlutterPlatformMessageResponseHandle )(unsafe .Pointer (responseHandle ))
396
+ res := C .FlutterPlatformMessageReleaseResponseHandle (flu .Engine , cResponseHandle )
397
+ if (Result )(res ) != ResultSuccess {
398
+ fmt .Printf ("go-flutter: failed to collect platform response message handle" )
399
+ }
400
+ }
401
+
402
+ // FlutterEngineGetCurrentTime gets the current time in nanoseconds from the clock used by the flutter
403
+ // engine.
362
404
func FlutterEngineGetCurrentTime () uint64 {
363
405
return uint64 (C .FlutterEngineGetCurrentTime ())
364
406
}
0 commit comments