Repository navigation
Expose engineId on native plugin registrars on Linux and Windows - #196032
tarrinneal wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces APIs to retrieve the unique engine identifier from the plugin registrar on both Linux and Windows platforms, along with corresponding unit tests. The review feedback points out a portability issue in fl_engine.cc, fl_view.cc, and flutter_windows.cc, where pointers are directly cast to int64_t using reinterpret_cast. To align with the Google C++ Style Guide, these pointers should be cast to intptr_t or uintptr_t first to avoid compilation issues on 32-bit platforms.
| return fl_plugin_registrar_new(nullptr, self->binary_messenger, | ||
| self->texture_registrar); | ||
| self->texture_registrar, | ||
| reinterpret_cast<int64_t>(self)); |
There was a problem hiding this comment.
According to the Google C++ Style Guide (referenced by the Repository Style Guide), when casting a pointer to an integer, you should only cast to/from intptr_t or uintptr_t. Direct casting to int64_t using reinterpret_cast can lead to compilation errors or warnings on 32-bit platforms where pointer sizes differ from 64-bit integers. Casting to intptr_t first is portable and safe, and it will be implicitly promoted to int64_t.
reinterpret_cast<intptr_t>(self));References
- Google C++ Style Guide: When casting a pointer to an integer, use
reinterpret_casttointptr_toruintptr_t. (link)
| fl_engine_get_binary_messenger(self->engine), | ||
| fl_engine_get_texture_registrar(self->engine)); | ||
| fl_engine_get_texture_registrar(self->engine), | ||
| reinterpret_cast<int64_t>(self->engine)); |
There was a problem hiding this comment.
According to the Google C++ Style Guide (referenced by the Repository Style Guide), when casting a pointer to an integer, you should only cast to/from intptr_t or uintptr_t. Direct casting to int64_t using reinterpret_cast can lead to compilation errors or warnings on 32-bit platforms where pointer sizes differ from 64-bit integers. Casting to intptr_t first is portable and safe, and it will be implicitly promoted to int64_t.
reinterpret_cast<intptr_t>(self->engine));References
- Google C++ Style Guide: When casting a pointer to an integer, use
reinterpret_casttointptr_toruintptr_t. (link)
| if (!registrar) { | ||
| return 0; | ||
| } | ||
| return reinterpret_cast<int64_t>(registrar->engine); |
There was a problem hiding this comment.
According to the Google C++ Style Guide (referenced by the Repository Style Guide), when casting a pointer to an integer, you should only cast to/from intptr_t or uintptr_t. Direct casting to int64_t using reinterpret_cast can lead to compilation errors or warnings on 32-bit platforms where pointer sizes differ from 64-bit integers. Casting to intptr_t first is portable and safe, and it will be implicitly promoted to int64_t.
return reinterpret_cast<intptr_t>(registrar->engine);References
- Google C++ Style Guide: When casting a pointer to an integer, use
reinterpret_casttointptr_toruintptr_t. (link)
| if (!registrar) { | ||
| return 0; | ||
| } | ||
| return reinterpret_cast<intptr_t>(registrar->engine); |
There was a problem hiding this comment.
Could we centralize the casting logic to ensure it doesn't drift? Currently both FlutterDesktopPluginRegistrarGetEngineId and FlutterWindowsEngine have logic to convert a FlutterWindowsEngine* into an ID. I'd consider:
- Adding a
int64_t FlutterWindowsEngine::id() - Updating this to call
registrar->engine->id() - Updating this line to call
id():
| // | ||
| // This corresponds to PlatformDispatcher.instance.engineId in Dart. | ||
| FLUTTER_EXPORT int64_t FlutterDesktopPluginRegistrarGetEngineId( | ||
| FlutterDesktopPluginRegistrarRef registrar); |
There was a problem hiding this comment.
Nit: could you introduce a FlutterDesktopEngineId typedef, similar to the existing FlutterDesktopViewId above:
You'll get some type safety benefits if we ever add an API that accepts an engine ID.
| return fl_plugin_registrar_new(nullptr, self->binary_messenger, | ||
| self->texture_registrar); | ||
| self->texture_registrar, | ||
| reinterpret_cast<intptr_t>(self)); |
There was a problem hiding this comment.
I'd consider adding a private int64_t fl_engine_get_id(FlEngine*) helper function so that we can keep this logic unified. This would be a good spot for it:
|
|
||
| // Returns the unique identifier for the engine associated with this registrar. | ||
| // | ||
| // This corresponds to PlatformDispatcher.instance.engineId in Dart. |
There was a problem hiding this comment.
Let's call out thread-safety here since this can be used from any thread:
| // This corresponds to PlatformDispatcher.instance.engineId in Dart. | |
| // This corresponds to PlatformDispatcher.instance.engineId in Dart. | |
| // This can be called on any thread. |
mattkae
left a comment
There was a problem hiding this comment.
One nit on Linux, and I +1 Loic's Windows review
|
|
||
| FlPluginRegistrarInterface* iface = FL_PLUGIN_REGISTRAR_GET_IFACE(self); | ||
| if (iface->get_engine_id == nullptr) { | ||
| return 0; |
There was a problem hiding this comment.
Is there a constant that we can pin this too somewhere, e.g. kDefaultEngineId if that exists in the repo?
Follow-up to #195881, completing platform parity by exposing the engine identifier (corresponding to
PlatformDispatcher.instance.engineIdin Dart) directly on native plugin registrars on Linux and Windows so that FFI plugins can associate and clean up per-engine native state during plugin registration and detachment without access to internal engine APIs:fl_plugin_registrar_get_engine_id(FlPluginRegistrar*)toFlPluginRegistrar(fl_plugin_registrar.h).FlutterDesktopPluginRegistrarGetEngineId(FlutterDesktopPluginRegistrarRef)to the C API (flutter_windows.h) andPluginRegistrarWindows::GetEngineId()to the C++ client wrapper (plugin_registrar_windows.h).Fixes #195876
Breaking Change Assessment
Unlike the
@requiredObjective-C protocol property added on iOS and macOS in #195881 (which required updating customFlutterPluginRegistrartest stubs in flutter/packages#13574 and has been taggedc: API breakon #195881 / #195876 for release notes), this PR is non-breaking for both production plugins and custom test fakes/mocks:FlPluginRegistrar):get_engine_idis appended to_FlPluginRegistrarInterface. GLib zero-initializesGTypeInterfacestructs prior toiface_init, andfl_plugin_registrar_get_engine_idguards againstiface->get_engine_id == nullptr(returning0), so existing GObject mock/fakeFlPluginRegistrarimplementations that do not assigniface->get_engine_idcontinue to compile and run without changes (verified byFlPluginRegistrarTest.LegacyFakeWithoutGetEngineId).FlutterDesktopPluginRegistrarRef/PluginRegistrarWindows):FlutterDesktopPluginRegistrarGetEngineIdis a standalone C function andPluginRegistrarWindows::GetEngineId()is a concrete, non-virtual method on a concrete class, requiring no overrides in existing test doubles.Pre-launch Checklist
///).