Skip to content

Commit 4773050

Browse files
Gustl22suryaKommana2662
authored andcommitted
feat: Expose PostPlatformThreadTask and IsPlatformThread to PluginRegistrarWindows (flutter#191140)
Plugins also should be able to post to the platform thread on windows. This is especially important for platform channel event handling, as Windows platform events are mostly emitted by another thread. Closes flutter#190089 Formerly blocked by flutter#193411 For transparency: Claude assisted code ## 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 documentation (doc comments with `///`). - [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. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 [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 fdb892a commit 4773050

7 files changed

Lines changed: 298 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <flutter_windows.h>
99
#include <windows.h>
1010

11+
#include <functional>
1112
#include <memory>
1213
#include <optional>
1314

@@ -113,6 +114,35 @@ class PluginRegistrarWindows : public PluginRegistrar {
113114
adapter_out);
114115
}
115116

117+
// Returns true if the current thread is the platform thread.
118+
// This can be called on any thread.
119+
bool IsPlatformThread() const {
120+
return FlutterDesktopPluginRegistrarIsPlatformThread(registrar());
121+
}
122+
123+
// Schedule a callback to be called on the platform thread.
124+
//
125+
// This can be called on any thread. The callback is executed only
126+
// once on the platform thread.
127+
void PostPlatformThreadTask(std::function<void()> callback) {
128+
if (!callback) {
129+
return;
130+
}
131+
FlutterDesktopPluginRegistrarPostPlatformThreadTask(
132+
registrar(),
133+
/*callback=*/
134+
[](void* user_data) {
135+
std::unique_ptr<std::function<void()>> cb{
136+
static_cast<std::function<void()>*>(user_data)};
137+
(*cb)();
138+
},
139+
/*on_cancel=*/
140+
[](void* user_data) {
141+
delete static_cast<std::function<void()>*>(user_data);
142+
},
143+
/*user_data=*/new std::function<void()>(std::move(callback)));
144+
}
145+
116146
private:
117147
// A FlutterDesktopWindowProcCallback implementation that forwards back to
118148
// a PluginRegistarWindows instance provided as |user_data|.

‎engine/src/flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,32 @@ class TestWindowsApi : public testing::StubFlutterWindowsApi {
5151
return true;
5252
}
5353

54+
bool PluginRegistrarIsPlatformThread() override {
55+
return is_platform_thread_;
56+
}
57+
58+
void PluginRegistrarPostPlatformThreadTask(VoidCallback callback,
59+
VoidCallback on_cancel,
60+
void* user_data) override {
61+
post_task_callback_ = callback;
62+
post_task_on_cancel_ = on_cancel;
63+
post_task_user_data_ = user_data;
64+
}
65+
66+
void set_is_platform_thread(bool value) { is_platform_thread_ = value; }
67+
68+
VoidCallback post_task_callback() { return post_task_callback_; }
69+
VoidCallback post_task_on_cancel() { return post_task_on_cancel_; }
70+
void* post_task_user_data() { return post_task_user_data_; }
71+
5472
private:
5573
int registered_delegate_count_ = 0;
5674
FlutterDesktopWindowProcCallback last_registered_delegate_ = nullptr;
5775
void* last_registered_user_data_ = nullptr;
76+
bool is_platform_thread_ = false;
77+
VoidCallback post_task_callback_ = nullptr;
78+
VoidCallback post_task_on_cancel_ = nullptr;
79+
void* post_task_user_data_ = nullptr;
5880
};
5981

6082
// A test plugin that tries to access registrar state during destruction and
@@ -249,4 +271,81 @@ TEST(PluginRegistrarWindowsTest, StopsOnceHandled) {
249271
EXPECT_EQ(result, 7);
250272
}
251273

274+
TEST(PluginRegistrarWindowsTest, IsPlatformThread) {
275+
auto windows_api = std::make_unique<TestWindowsApi>();
276+
EXPECT_CALL(*windows_api, PluginRegistrarGetView)
277+
.WillRepeatedly(Return(nullptr));
278+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
279+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
280+
PluginRegistrarWindows registrar(
281+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1));
282+
283+
test_api->set_is_platform_thread(false);
284+
EXPECT_FALSE(registrar.IsPlatformThread());
285+
286+
test_api->set_is_platform_thread(true);
287+
EXPECT_TRUE(registrar.IsPlatformThread());
288+
}
289+
290+
TEST(PluginRegistrarWindowsTest, PostPlatformThreadTask) {
291+
auto windows_api = std::make_unique<TestWindowsApi>();
292+
EXPECT_CALL(*windows_api, PluginRegistrarGetView)
293+
.WillRepeatedly(Return(nullptr));
294+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
295+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
296+
PluginRegistrarWindows registrar(
297+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1));
298+
299+
bool called = false;
300+
registrar.PostPlatformThreadTask([&called]() { called = true; });
301+
302+
// The stub should have received the task.
303+
EXPECT_NE(test_api->post_task_callback(), nullptr);
304+
EXPECT_NE(test_api->post_task_user_data(), nullptr);
305+
EXPECT_FALSE(called);
306+
307+
// Simulate executing the callback.
308+
test_api->post_task_callback()(test_api->post_task_user_data());
309+
EXPECT_TRUE(called);
310+
}
311+
312+
TEST(PluginRegistrarWindowsTest, PostPlatformThreadTaskNullCallback) {
313+
auto windows_api = std::make_unique<TestWindowsApi>();
314+
EXPECT_CALL(*windows_api, PluginRegistrarGetView)
315+
.WillRepeatedly(Return(nullptr));
316+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
317+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
318+
PluginRegistrarWindows registrar(
319+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1));
320+
321+
// Posting a null callback should be a no-op.
322+
registrar.PostPlatformThreadTask(nullptr);
323+
EXPECT_EQ(test_api->post_task_callback(), nullptr);
324+
}
325+
326+
TEST(PluginRegistrarWindowsTest, PostPlatformThreadTaskOnCancel) {
327+
auto windows_api = std::make_unique<TestWindowsApi>();
328+
EXPECT_CALL(*windows_api, PluginRegistrarGetView)
329+
.WillRepeatedly(Return(nullptr));
330+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
331+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
332+
PluginRegistrarWindows registrar(
333+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1));
334+
335+
bool called = false;
336+
// Captured by the callback to detect whether the callback is freed.
337+
auto captured = std::make_shared<int>(0);
338+
std::weak_ptr<int> weak_token = captured;
339+
registrar.PostPlatformThreadTask(
340+
[&called, token = std::move(captured)]() { called = true; });
341+
EXPECT_FALSE(weak_token.expired());
342+
343+
// Simulate the engine discarding the task: on_cancel must free the
344+
// callback without running it.
345+
EXPECT_NE(test_api->post_task_on_cancel(), nullptr);
346+
test_api->post_task_on_cancel()(test_api->post_task_user_data());
347+
EXPECT_FALSE(called);
348+
EXPECT_TRUE(weak_token.expired());
349+
}
350+
252351
} // namespace flutter

‎engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,25 @@ bool FlutterDesktopPluginRegistrarGetGraphicsAdapter(
244244
return false;
245245
}
246246

247+
bool FlutterDesktopPluginRegistrarIsPlatformThread(
248+
FlutterDesktopPluginRegistrarRef registrar) {
249+
if (s_stub_implementation) {
250+
return s_stub_implementation->PluginRegistrarIsPlatformThread();
251+
}
252+
return false;
253+
}
254+
255+
void FlutterDesktopPluginRegistrarPostPlatformThreadTask(
256+
FlutterDesktopPluginRegistrarRef registrar,
257+
VoidCallback callback,
258+
VoidCallback on_cancel,
259+
void* user_data) {
260+
if (s_stub_implementation) {
261+
s_stub_implementation->PluginRegistrarPostPlatformThreadTask(
262+
callback, on_cancel, user_data);
263+
}
264+
}
265+
247266
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(
248267
FlutterDesktopPluginRegistrarRef registrar,
249268
FlutterDesktopWindowProcCallback delegate,

‎engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ class StubFlutterWindowsApi {
124124
return false;
125125
}
126126

127+
// Called for FlutterDesktopPluginRegistrarIsPlatformThread.
128+
virtual bool PluginRegistrarIsPlatformThread() { return false; }
129+
130+
// Called for FlutterDesktopPluginRegistrarPostPlatformThreadTask.
131+
virtual void PluginRegistrarPostPlatformThreadTask(VoidCallback callback,
132+
VoidCallback on_cancel,
133+
void* user_data) {}
134+
127135
// Called for FlutterDesktopEngineProcessExternalWindowMessage.
128136
virtual bool EngineProcessExternalWindowMessage(
129137
FlutterDesktopEngineRef engine,

‎engine/src/flutter/shell/platform/windows/flutter_windows.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ bool FlutterDesktopPluginRegistrarGetGraphicsAdapter(
373373
HandleForEngine(registrar->engine), adapter_out);
374374
}
375375

376+
bool FlutterDesktopPluginRegistrarIsPlatformThread(
377+
FlutterDesktopPluginRegistrarRef registrar) {
378+
return FlutterDesktopEngineIsPlatformThread(
379+
HandleForEngine(registrar->engine));
380+
}
381+
382+
void FlutterDesktopPluginRegistrarPostPlatformThreadTask(
383+
FlutterDesktopPluginRegistrarRef registrar,
384+
VoidCallback callback,
385+
VoidCallback on_cancel,
386+
void* user_data) {
387+
FlutterDesktopEnginePostPlatformThreadTask(HandleForEngine(registrar->engine),
388+
callback, on_cancel, user_data);
389+
}
390+
376391
UINT FlutterDesktopGetDpiForHWND(HWND hwnd) {
377392
return flutter::GetDpiForHWND(hwnd);
378393
}

‎engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,39 @@ TEST_F(WindowsTest, IsNotPlatformThread) {
430430
EXPECT_FALSE(result);
431431
}
432432

433+
// Verify the plugin registrar reports the platform thread correctly.
434+
TEST_F(WindowsTest, PluginRegistrarIsPlatformThread) {
435+
auto& context = GetContext();
436+
WindowsConfigBuilder builder(context);
437+
EnginePtr engine{builder.RunHeadless()};
438+
ASSERT_NE(engine, nullptr);
439+
440+
FlutterDesktopPluginRegistrarRef registrar =
441+
FlutterDesktopEngineGetPluginRegistrar(engine.get(), "foo_bar");
442+
443+
EXPECT_TRUE(FlutterDesktopPluginRegistrarIsPlatformThread(registrar));
444+
}
445+
446+
// Verify the plugin registrar reports a background thread as not the platform
447+
// thread.
448+
TEST_F(WindowsTest, PluginRegistrarIsNotPlatformThread) {
449+
auto& context = GetContext();
450+
WindowsConfigBuilder builder(context);
451+
EnginePtr engine{builder.RunHeadless()};
452+
ASSERT_NE(engine, nullptr);
453+
454+
FlutterDesktopPluginRegistrarRef registrar =
455+
FlutterDesktopEngineGetPluginRegistrar(engine.get(), "foo_bar");
456+
457+
bool result = true;
458+
std::thread background([&]() {
459+
result = FlutterDesktopPluginRegistrarIsPlatformThread(registrar);
460+
});
461+
background.join();
462+
463+
EXPECT_FALSE(result);
464+
}
465+
433466
// Verify a task can be posted to the platform thread while on the platform
434467
// thread.
435468
TEST_F(WindowsTest, PostPlatformThreadTaskFromPlatformThread) {
@@ -572,6 +605,81 @@ TEST_F(WindowsTest, PostPlatformThreadTaskNotCancelledWhenRun) {
572605
EXPECT_FALSE(captures.cancel_called);
573606
}
574607

608+
// Verify a task can be posted by the plugin registrar to the platform thread
609+
// while on the platform thread, and that its cancel callback is not invoked.
610+
TEST_F(WindowsTest, PluginRegistrarPostPlatformThreadTaskFromPlatformThread) {
611+
auto& context = GetContext();
612+
WindowsConfigBuilder builder(context);
613+
EnginePtr engine{builder.RunHeadless()};
614+
ASSERT_NE(engine, nullptr);
615+
616+
FlutterDesktopPluginRegistrarRef registrar =
617+
FlutterDesktopEngineGetPluginRegistrar(engine.get(), "foo_bar");
618+
619+
struct Captures {
620+
std::thread::id thread_id;
621+
bool done = false;
622+
bool cancel_called = false;
623+
} captures;
624+
625+
FlutterDesktopPluginRegistrarPostPlatformThreadTask(
626+
registrar,
627+
[](void* user_data) {
628+
auto captures = static_cast<Captures*>(user_data);
629+
captures->thread_id = std::this_thread::get_id();
630+
captures->done = true;
631+
},
632+
[](void* user_data) {
633+
static_cast<Captures*>(user_data)->cancel_called = true;
634+
},
635+
&captures);
636+
637+
while (!captures.done) {
638+
PumpMessage();
639+
}
640+
641+
// Ensures that the cancel callback is not called,
642+
// in case the engine was shut down after running the task.
643+
engine.reset();
644+
645+
EXPECT_EQ(captures.thread_id, std::this_thread::get_id());
646+
EXPECT_FALSE(captures.cancel_called);
647+
}
648+
649+
// Verify that destroying the engine after posting a task by the plugin
650+
// registrar invokes the cancel callback instead of the task callback.
651+
TEST_F(WindowsTest, PluginRegistrarPostPlatformThreadTaskCancelledOnDestroy) {
652+
auto& context = GetContext();
653+
WindowsConfigBuilder builder(context);
654+
EnginePtr engine{builder.RunHeadless()};
655+
ASSERT_NE(engine, nullptr);
656+
657+
FlutterDesktopPluginRegistrarRef registrar =
658+
FlutterDesktopEngineGetPluginRegistrar(engine.get(), "foo_bar");
659+
660+
struct Captures {
661+
bool callback_called = false;
662+
bool cancel_called = false;
663+
} captures;
664+
665+
FlutterDesktopPluginRegistrarPostPlatformThreadTask(
666+
registrar,
667+
[](void* user_data) {
668+
static_cast<Captures*>(user_data)->callback_called = true;
669+
},
670+
[](void* user_data) {
671+
static_cast<Captures*>(user_data)->cancel_called = true;
672+
},
673+
&captures);
674+
675+
// Destroy the engine before the task has a chance to run. The registrar is
676+
// owned by the engine and must not be used afterwards.
677+
engine.reset();
678+
679+
EXPECT_FALSE(captures.callback_called);
680+
EXPECT_TRUE(captures.cancel_called);
681+
}
682+
575683
// Implicit view has the implicit view ID.
576684
TEST_F(WindowsTest, GetViewId) {
577685
auto& context = GetContext();

‎engine/src/flutter/shell/platform/windows/public/flutter_windows.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,25 @@ FLUTTER_EXPORT bool FlutterDesktopPluginRegistrarGetGraphicsAdapter(
378378
FlutterDesktopPluginRegistrarRef registrar,
379379
IDXGIAdapter** adapter_out);
380380

381+
// Returns true if the current thread is the platform thread.
382+
// This can be called on any thread.
383+
FLUTTER_EXPORT bool FlutterDesktopPluginRegistrarIsPlatformThread(
384+
FlutterDesktopPluginRegistrarRef registrar);
385+
386+
// Schedule a callback to be called on the platform thread.
387+
//
388+
// This can be called on any thread. The callback is executed only
389+
// once on the platform thread.
390+
//
391+
// If the task is discarded without being executed (e.g. during engine
392+
// shutdown), |on_cancel| is called on the platform thread so the caller can
393+
// cleanup allocations. |on_cancel| can be nullptr if no cleanup is needed.
394+
FLUTTER_EXPORT void FlutterDesktopPluginRegistrarPostPlatformThreadTask(
395+
FlutterDesktopPluginRegistrarRef registrar,
396+
VoidCallback callback,
397+
VoidCallback on_cancel,
398+
void* user_data);
399+
381400
// ========== Freestanding Utilities ==========
382401

383402
// Gets the DPI for a given |hwnd|, depending on the supported APIs per

0 commit comments

Comments
 (0)