Skip to content

Commit 7639550

Browse files
authored
Expose engineId on native plugin bindings and registrars on Android, iOS, and macOS (flutter#195881)
Exposes the engine identifier (corresponding to `PlatformDispatcher.instance.engineId` in Dart) directly on native plugin bindings and registrars across Android, iOS, and macOS so that FFI/JNI plugins can associate and clean up per-engine native state during plugin registration and detachment without access to the underlying engine instance: - **Android**: Adds `FlutterPluginBinding.getEngineId()` returning `flutterEngine.getEngineId()`. - **iOS**: Adds `@property(readonly) int64_t engineIdentifier` to `FlutterBaseRegistrar` (inherited by `FlutterPluginRegistrar` and `FlutterApplicationRegistrar`), cached at registrar initialization so it remains valid during `detachFromEngineForRegistrar:` when the weak engine reference is already `nil`. - **macOS**: Adds `@property(readonly) int64_t engineIdentifier` to `FlutterPluginRegistrar`, cached at registrar initialization. work towards flutter#195876 (linux and windows will follow in another pr) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent d756cdd commit 7639550

10 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/FlutterPlugin.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ public FlutterEngine getFlutterEngine() {
143143
return flutterEngine;
144144
}
145145

146+
/**
147+
* Returns the unique identifier for the {@link FlutterEngine} associated with this binding.
148+
*
149+
* <p>This corresponds to {@code PlatformDispatcher.instance.engineId} in Dart.
150+
*/
151+
public long getEngineId() {
152+
return flutterEngine.getEngineId();
153+
}
154+
146155
@NonNull
147156
public BinaryMessenger getBinaryMessenger() {
148157
return binaryMessenger;

‎engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/PluginComponentTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,56 @@ public void pluginsCanAccessOtherPlugins() {
106106
assertEquals(accessedPlugin, plugin.getAccessedPlugin());
107107
}
108108

109+
@Test
110+
public void pluginsCanAccessEngineId() {
111+
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
112+
FlutterJNI flutterJNI = mock(FlutterJNI.class);
113+
jniAttached = false;
114+
when(flutterJNI.isAttached()).thenAnswer(invocation -> jniAttached);
115+
doAnswer(invocation -> jniAttached = true).when(flutterJNI).attachToNative();
116+
117+
FlutterLoader flutterLoader = new FlutterLoader(mockFlutterJNI);
118+
119+
FlutterEngine flutterEngine1 =
120+
new FlutterEngine(ApplicationProvider.getApplicationContext(), flutterLoader, flutterJNI);
121+
FlutterEngine flutterEngine2 =
122+
new FlutterEngine(ApplicationProvider.getApplicationContext(), flutterLoader, flutterJNI);
123+
124+
PluginThatRecordsEngineId plugin1 = new PluginThatRecordsEngineId();
125+
PluginThatRecordsEngineId plugin2 = new PluginThatRecordsEngineId();
126+
flutterEngine1.getPlugins().add(plugin1);
127+
flutterEngine2.getPlugins().add(plugin2);
128+
129+
assertEquals(flutterEngine1.getEngineId(), plugin1.getAttachedEngineId());
130+
assertEquals(flutterEngine2.getEngineId(), plugin2.getAttachedEngineId());
131+
132+
flutterEngine1.getPlugins().remove(PluginThatRecordsEngineId.class);
133+
assertEquals(flutterEngine1.getEngineId(), plugin1.getDetachedEngineId());
134+
}
135+
136+
private static class PluginThatRecordsEngineId implements FlutterPlugin {
137+
private long attachedEngineId;
138+
private long detachedEngineId;
139+
140+
public long getAttachedEngineId() {
141+
return attachedEngineId;
142+
}
143+
144+
public long getDetachedEngineId() {
145+
return detachedEngineId;
146+
}
147+
148+
@Override
149+
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
150+
attachedEngineId = binding.getEngineId();
151+
}
152+
153+
@Override
154+
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
155+
detachedEngineId = binding.getEngineId();
156+
}
157+
}
158+
109159
private static class PluginThatAccessesAssets implements FlutterPlugin {
110160
private String assetPathBasedOnName;
111161
private String assetPathBasedOnNameAndPackage;

‎engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ typedef enum {
288288
* Provides registration context for the application or plugins.
289289
*/
290290
@protocol FlutterBaseRegistrar <NSObject>
291+
292+
/**
293+
* The unique identifier for the `FlutterEngine` associated with this registrar.
294+
*
295+
* This corresponds to `PlatformDispatcher.instance.engineId` in Dart.
296+
*/
297+
@property(nonatomic, readonly) int64_t engineIdentifier;
298+
291299
/**
292300
* Returns a `FlutterBinaryMessenger` for creating Dart/iOS communication
293301
* channels to be used by the application or a plugin.

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,10 @@ - (instancetype)initWithKey:(NSString*)key flutterEngine:(FlutterEngine*)flutter
17601760
return self;
17611761
}
17621762

1763+
- (int64_t)engineIdentifier {
1764+
return _flutterEngine.engineIdentifier;
1765+
}
1766+
17631767
- (NSObject<FlutterBinaryMessenger>*)messenger {
17641768
return _flutterEngine.binaryMessenger;
17651769
}

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,15 @@ - (void)testEngineId {
421421
[engine run];
422422
int64_t id1 = engine.engineIdentifier;
423423
XCTAssertTrue(id1 != 0);
424+
NSObject<FlutterPluginRegistrar>* registrar1 = [engine registrarForPlugin:@"foo"];
425+
XCTAssertEqual(registrar1.engineIdentifier, id1);
424426
FlutterEngine* spawn = [engine spawnWithEntrypoint:nil
425427
libraryURI:nil
426428
initialRoute:nil
427429
entrypointArgs:nil];
428430
int64_t id2 = spawn.engineIdentifier;
431+
NSObject<FlutterPluginRegistrar>* registrar2 = [spawn registrarForPlugin:@"foo"];
432+
XCTAssertEqual(registrar2.engineIdentifier, id2);
429433
XCTAssertEqual([FlutterEngine engineForIdentifier:id1], engine);
430434
XCTAssertEqual([FlutterEngine engineForIdentifier:id2], spawn);
431435
}
@@ -1170,6 +1174,8 @@ - (void)testRegistrarForApplication {
11701174

11711175
NSObject<FlutterApplicationRegistrar>* registrar = [mockEngine registrarForApplication:pluginKey];
11721176

1177+
XCTAssertTrue([registrar respondsToSelector:@selector(engineIdentifier)]);
1178+
XCTAssertEqual(registrar.engineIdentifier, engine.engineIdentifier);
11731179
XCTAssertTrue([registrar respondsToSelector:@selector(messenger)]);
11741180
XCTAssertTrue([registrar respondsToSelector:@selector(textures)]);
11751181
XCTAssertTrue([registrar respondsToSelector:@selector(registerViewFactory:withId:)]);

‎engine/src/flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
FLUTTER_DARWIN_EXPORT
2828
@protocol FlutterPluginRegistrar <NSObject>
2929

30+
/**
31+
* The unique identifier for the `FlutterEngine` associated with this registrar.
32+
*
33+
* This corresponds to `PlatformDispatcher.instance.engineId` in Dart.
34+
*/
35+
@property(nonatomic, readonly) int64_t engineIdentifier;
36+
3037
/**
3138
* The binary messenger used for creating channels to communicate with the Flutter engine.
3239
*/

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ - (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine
340340

341341
#pragma mark - FlutterPluginRegistrar
342342

343+
- (int64_t)engineIdentifier {
344+
return _flutterEngine.engineIdentifier;
345+
}
346+
343347
- (id<FlutterBinaryMessenger>)messenger {
344348
return _flutterEngine.binaryMessenger;
345349
}
@@ -739,7 +743,7 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
739743
[FlutterLogger logDirect:[NSString stringWithUTF8String:log.c_str()]];
740744
};
741745

742-
flutterArguments.engine_id = reinterpret_cast<int64_t>((__bridge void*)self);
746+
flutterArguments.engine_id = self.engineIdentifier;
743747
BOOL enableWideGamut = _project.enableWideGamut;
744748
if (std::find(switches.begin(), switches.end(), "--enable-impeller=false") != switches.end()) {
745749
enableWideGamut = NO;
@@ -1383,6 +1387,10 @@ - (void)shutDownEngine {
13831387
_engine = nullptr;
13841388
}
13851389

1390+
- (int64_t)engineIdentifier {
1391+
return reinterpret_cast<int64_t>((__bridge void*)self);
1392+
}
1393+
13861394
+ (FlutterEngine*)engineForIdentifier:(int64_t)identifier {
13871395
NSAssert([[NSThread currentThread] isMainThread], @"Must be called on the main thread.");
13881396
return (__bridge FlutterEngine*)reinterpret_cast<void*>(identifier);

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,9 @@ @implementation MockableFlutterEngine
951951
if (!engineId.has_value()) {
952952
return;
953953
}
954+
id<FlutterPluginRegistrar> registrar = [engine registrarForPlugin:@"MyPlugin"];
955+
EXPECT_EQ(engine.engineIdentifier, *engineId);
956+
EXPECT_EQ(registrar.engineIdentifier, *engineId);
954957
EXPECT_EQ(engine, [FlutterEngine engineForIdentifier:*engineId]);
955958
ShutDownEngine();
956959
}

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ typedef NS_ENUM(NSInteger, FlutterAppExitResponse) {
262262
*/
263263
- (NSArray<NSScreen*>*)screens;
264264

265+
/**
266+
* The unique identifier for this engine.
267+
*/
268+
@property(nonatomic, readonly) int64_t engineIdentifier;
269+
265270
/**
266271
* Returns engine for the identifier. The identifier must be valid for an engine
267272
* that is currently running, otherwise the behavior is undefined.

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPluginTest.mm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ @interface FakePluginRegistrar : NSObject <FlutterPluginRegistrar>
2727
@implementation FakePluginRegistrar
2828

2929
// Synthesize properties declared in FlutterPluginRegistrar protocol.
30+
@synthesize engineIdentifier;
3031
@synthesize messenger;
3132
@synthesize textures;
3233
@synthesize view;

0 commit comments

Comments
 (0)