From ece178965b43e19e93c05fb6d0d020348516636f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:19:19 -0800 Subject: [PATCH] [Windows] Allow plugins to get views (flutter/engine#51096) Allow Flutter Windows plugins to get views by their ID. Design doc: https://flutter.dev/go/desktop-multi-view-runner-apis Part of https://github.com/flutter/flutter/issues/143767 Part of https://github.com/flutter/flutter/issues/142845 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../include/flutter/plugin_registrar.h | 2 +- .../include/flutter/flutter_view.h | 1 + .../flutter/plugin_registrar_windows.h | 33 +++++++++-- .../plugin_registrar_windows_unittests.cc | 59 ++++++++++++++++--- .../testing/stub_flutter_windows_api.cc | 15 ++++- .../testing/stub_flutter_windows_api.h | 9 +++ .../shell/platform/windows/flutter_windows.cc | 9 ++- .../windows/flutter_windows_unittests.cc | 58 ++++++++++++++++++ .../platform/windows/public/flutter_windows.h | 14 ++++- 9 files changed, 180 insertions(+), 20 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h b/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h index 15cf99689a..e21f472b6b 100644 --- a/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h +++ b/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h @@ -54,7 +54,7 @@ class PluginRegistrar { void AddPlugin(std::unique_ptr plugin); protected: - FlutterDesktopPluginRegistrarRef registrar() { return registrar_; } + FlutterDesktopPluginRegistrarRef registrar() const { return registrar_; } // Destroys all owned plugins. Subclasses should call this at the beginning of // their destructors to prevent the possibility of an owned plugin trying to diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h index 8f1752a9f5..48e7607c96 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h @@ -17,6 +17,7 @@ class FlutterView { public: explicit FlutterView(FlutterDesktopViewRef view) : view_(view) {} + // Destroys this reference to the view. The underlying view is not destroyed. virtual ~FlutterView() = default; // Prevent copying. diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h index 851971eae9..7655b31ff0 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h @@ -32,22 +32,45 @@ class PluginRegistrarWindows : public PluginRegistrar { explicit PluginRegistrarWindows( FlutterDesktopPluginRegistrarRef core_registrar) : PluginRegistrar(core_registrar) { - view_ = std::make_unique( - FlutterDesktopPluginRegistrarGetView(core_registrar)); + FlutterDesktopViewRef implicit_view = + FlutterDesktopPluginRegistrarGetView(core_registrar); + if (implicit_view) { + implicit_view_ = std::make_unique(implicit_view); + } } virtual ~PluginRegistrarWindows() { // Must be the first call. ClearPlugins(); // Explicitly cleared to facilitate destruction order testing. - view_.reset(); + implicit_view_.reset(); } // Prevent copying. PluginRegistrarWindows(PluginRegistrarWindows const&) = delete; PluginRegistrarWindows& operator=(PluginRegistrarWindows const&) = delete; - FlutterView* GetView() { return view_.get(); } + // Returns the implicit view, or nullptr if there is no implicit view. + // + // See: + // https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html + // + // DEPRECATED: Use |GetViewById| instead. + FlutterView* GetView() { return implicit_view_.get(); } + + // Returns the view with the given ID, or nullptr if the view does not exist. + // + // Destroying the shared pointer destroys the reference to the view; it does + // not destroy the underlying view. + std::shared_ptr GetViewById(FlutterViewId view_id) const { + FlutterDesktopViewRef view = + FlutterDesktopPluginRegistrarGetViewById(registrar(), view_id); + if (!view) { + return nullptr; + } + + return std::make_shared(view); + } // Registers |delegate| to receive WindowProc callbacks for the top-level // window containing this Flutter instance. Returns an ID that can be used to @@ -115,7 +138,7 @@ class PluginRegistrarWindows : public PluginRegistrar { } // The associated FlutterView, if any. - std::unique_ptr view_; + std::unique_ptr implicit_view_; // The next ID to return from RegisterWindowProcDelegate. int next_window_proc_delegate_id_ = 1; diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc index 0c9bdb9618..f1d08c5fb9 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc @@ -7,12 +7,15 @@ #include "flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h" #include "flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace flutter { namespace { +using ::testing::Return; + // Stub implementation to validate calls to the API. class TestWindowsApi : public testing::StubFlutterWindowsApi { public: @@ -24,6 +27,12 @@ class TestWindowsApi : public testing::StubFlutterWindowsApi { last_registered_user_data_ = user_data; } + MOCK_METHOD(FlutterDesktopViewRef, PluginRegistrarGetView, (), (override)); + MOCK_METHOD(FlutterDesktopViewRef, + PluginRegistrarGetViewById, + (FlutterDesktopViewId), + (override)); + void PluginRegistrarUnregisterTopLevelWindowProcDelegate( FlutterDesktopWindowProcCallback delegate) override { --registered_delegate_count_; @@ -65,16 +74,47 @@ class TestPlugin : public Plugin { } // namespace TEST(PluginRegistrarWindowsTest, GetView) { - testing::ScopedStubFlutterWindowsApi scoped_api_stub( - std::make_unique()); + auto windows_api = std::make_unique(); + EXPECT_CALL(*windows_api, PluginRegistrarGetView) + .WillOnce(Return(reinterpret_cast(1))); + + testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api)); auto test_api = static_cast(scoped_api_stub.stub()); PluginRegistrarWindows registrar( reinterpret_cast(1)); + EXPECT_NE(registrar.GetView(), nullptr); } +TEST(PluginRegistrarWindowsTest, GetViewById) { + auto windows_api = std::make_unique(); + EXPECT_CALL(*windows_api, PluginRegistrarGetView) + .WillRepeatedly(Return(nullptr)); + EXPECT_CALL(*windows_api, PluginRegistrarGetViewById(123)) + .WillOnce(Return(reinterpret_cast(1))); + EXPECT_CALL(*windows_api, PluginRegistrarGetViewById(456)) + .WillOnce(Return(nullptr)); + + testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api)); + auto test_api = static_cast(scoped_api_stub.stub()); + PluginRegistrarWindows registrar( + reinterpret_cast(1)); + + EXPECT_EQ(registrar.GetView(), nullptr); + EXPECT_NE(registrar.GetViewById(123).get(), nullptr); + EXPECT_EQ(registrar.GetViewById(456).get(), nullptr); +} + // Tests that the registrar runs plugin destructors before its own teardown. TEST(PluginRegistrarWindowsTest, PluginDestroyedBeforeRegistrar) { + auto windows_api = std::make_unique(); + EXPECT_CALL(*windows_api, PluginRegistrarGetView) + .WillRepeatedly(Return(reinterpret_cast(1))); + testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api)); + auto test_api = static_cast(scoped_api_stub.stub()); + PluginRegistrarWindows registrar( + reinterpret_cast(1)); + auto dummy_registrar_handle = reinterpret_cast(1); bool registrar_valid_at_destruction = false; @@ -89,8 +129,9 @@ TEST(PluginRegistrarWindowsTest, PluginDestroyedBeforeRegistrar) { } TEST(PluginRegistrarWindowsTest, RegisterUnregister) { - testing::ScopedStubFlutterWindowsApi scoped_api_stub( - std::make_unique()); + auto windows_api = std::make_unique(); + EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(nullptr)); + testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api)); auto test_api = static_cast(scoped_api_stub.stub()); PluginRegistrarWindows registrar( reinterpret_cast(1)); @@ -118,8 +159,9 @@ TEST(PluginRegistrarWindowsTest, RegisterUnregister) { } TEST(PluginRegistrarWindowsTest, CallsRegisteredDelegates) { - testing::ScopedStubFlutterWindowsApi scoped_api_stub( - std::make_unique()); + auto windows_api = std::make_unique(); + EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(nullptr)); + testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api)); auto test_api = static_cast(scoped_api_stub.stub()); PluginRegistrarWindows registrar( reinterpret_cast(1)); @@ -154,8 +196,9 @@ TEST(PluginRegistrarWindowsTest, CallsRegisteredDelegates) { } TEST(PluginRegistrarWindowsTest, StopsOnceHandled) { - testing::ScopedStubFlutterWindowsApi scoped_api_stub( - std::make_unique()); + auto windows_api = std::make_unique(); + EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(nullptr)); + testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api)); auto test_api = static_cast(scoped_api_stub.stub()); PluginRegistrarWindows registrar( reinterpret_cast(1)); diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc index 384e0c3cfa..f51d7f14ad 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc @@ -194,8 +194,19 @@ void FlutterDesktopEngineRegisterPlatformViewType( FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( FlutterDesktopPluginRegistrarRef controller) { - // The stub ignores this, so just return an arbitrary non-zero value. - return reinterpret_cast(1); + if (s_stub_implementation) { + return s_stub_implementation->PluginRegistrarGetView(); + } + return nullptr; +} + +FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( + FlutterDesktopPluginRegistrarRef controller, + FlutterDesktopViewId view_id) { + if (s_stub_implementation) { + return s_stub_implementation->PluginRegistrarGetViewById(view_id); + } + return nullptr; } void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h index 43eaa50074..8f3eb0905a 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h @@ -86,6 +86,15 @@ class StubFlutterWindowsApi { return reinterpret_cast(2); } + // Called for FlutterDesktopPluginRegistrarGetView. + virtual FlutterDesktopViewRef PluginRegistrarGetView() { return nullptr; } + + // Called for FlutterDesktopPluginRegistrarGetViewById. + virtual FlutterDesktopViewRef PluginRegistrarGetViewById( + FlutterDesktopViewId view_id) { + return nullptr; + } + // Called for FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate. virtual void PluginRegistrarRegisterTopLevelWindowProcDelegate( FlutterDesktopWindowProcCallback delegate, diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index df25772582..2629b42f15 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -261,12 +261,15 @@ void FlutterDesktopEngineRegisterPlatformViewType( FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( FlutterDesktopPluginRegistrarRef registrar) { - // TODO(loicsharma): Add |FlutterDesktopPluginRegistrarGetViewById| and - // deprecate this API as it makes single view assumptions. - // https://github.com/flutter/flutter/issues/143767 return HandleForView(registrar->engine->view(flutter::kImplicitViewId)); } +FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( + FlutterDesktopPluginRegistrarRef registrar, + FlutterDesktopViewId view_id) { + return HandleForView(registrar->engine->view(view_id)); +} + void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc index 55e5b65a2f..58b5b50b1d 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc @@ -333,6 +333,64 @@ TEST_F(WindowsTest, GetGraphicsAdapter) { ASSERT_TRUE(SUCCEEDED(dxgi_adapter->GetDesc(&desc))); } +// Implicit view has the implicit view ID. +TEST_F(WindowsTest, PluginRegistrarGetImplicitView) { + auto& context = GetContext(); + WindowsConfigBuilder builder(context); + ViewControllerPtr controller{builder.Run()}; + ASSERT_NE(controller, nullptr); + + FlutterDesktopEngineRef engine = + FlutterDesktopViewControllerGetEngine(controller.get()); + FlutterDesktopPluginRegistrarRef registrar = + FlutterDesktopEngineGetPluginRegistrar(engine, "foo_bar"); + FlutterDesktopViewRef implicit_view = + FlutterDesktopPluginRegistrarGetView(registrar); + + ASSERT_NE(implicit_view, nullptr); +} + +TEST_F(WindowsTest, PluginRegistrarGetView) { + auto& context = GetContext(); + WindowsConfigBuilder builder(context); + ViewControllerPtr controller{builder.Run()}; + ASSERT_NE(controller, nullptr); + + FlutterDesktopEngineRef engine = + FlutterDesktopViewControllerGetEngine(controller.get()); + FlutterDesktopPluginRegistrarRef registrar = + FlutterDesktopEngineGetPluginRegistrar(engine, "foo_bar"); + + FlutterDesktopViewId view_id = + FlutterDesktopViewControllerGetViewId(controller.get()); + FlutterDesktopViewRef view = + FlutterDesktopPluginRegistrarGetViewById(registrar, view_id); + + FlutterDesktopViewRef view_123 = FlutterDesktopPluginRegistrarGetViewById( + registrar, static_cast(123)); + + ASSERT_NE(view, nullptr); + ASSERT_EQ(view_123, nullptr); +} + +TEST_F(WindowsTest, PluginRegistrarGetViewHeadless) { + auto& context = GetContext(); + WindowsConfigBuilder builder(context); + EnginePtr engine{builder.RunHeadless()}; + ASSERT_NE(engine, nullptr); + + FlutterDesktopPluginRegistrarRef registrar = + FlutterDesktopEngineGetPluginRegistrar(engine.get(), "foo_bar"); + + FlutterDesktopViewRef implicit_view = + FlutterDesktopPluginRegistrarGetView(registrar); + FlutterDesktopViewRef view_123 = FlutterDesktopPluginRegistrarGetViewById( + registrar, static_cast(123)); + + ASSERT_EQ(implicit_view, nullptr); + ASSERT_EQ(view_123, nullptr); +} + // Verify the app does not crash if EGL initializes successfully but // the rendering surface cannot be created. TEST_F(WindowsTest, SurfaceOptional) { diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index aa74241fdb..80d78766f9 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -249,10 +249,22 @@ typedef bool (*FlutterDesktopWindowProcCallback)(HWND /* hwnd */, void* /* user data */, LRESULT* result); -// Returns the view associated with this registrar's engine instance. +// Returns the implicit view associated with this registrar's engine instance, +// or null if there is no implicit view. +// +// See: +// https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html +// +// DEPRECATED: Use |FlutterDesktopPluginRegistrarGetViewById| instead. FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( FlutterDesktopPluginRegistrarRef registrar); +// Returns the view associated with the registrar's engine instance, or null if +// the view does not exist. +FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( + FlutterDesktopPluginRegistrarRef registrar, + FlutterDesktopViewId view_id); + FLUTTER_EXPORT void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( FlutterDesktopPluginRegistrarRef registrar,