Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 098761f

Browse files
authored
Revert "Improve C++ plugin lifetime handling (#17489)" (#17563)
Seems to have triggered flaky failures on the Windows bot since landing. Example failure: [ RUN ] PluginRegistrarTest.ManagerRemovesOnDestruction c:\b\s\w\ir\cache\builder\src\flutter\shell\platform\common\cpp\client_wrapper\plugin_registrar_unittests.cc(149): error: Expected: (manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle)) != (first_wrapper), actual: 000002400A90E3D0 vs 000002400A90E3D0 This reverts commit faf44fe.
1 parent d1c90b4 commit 098761f

File tree

11 files changed

+10
-175
lines changed

11 files changed

+10
-175
lines changed

shell/platform/common/cpp/client_wrapper/BUILD.gn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,4 @@ executable("client_wrapper_unittests") {
6565
# https://github.com/flutter/flutter/issues/41414.
6666
"//third_party/dart/runtime:libdart_jit",
6767
]
68-
69-
defines = [ "FLUTTER_DESKTOP_LIBRARY" ]
7068
}

shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
66
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
77

8-
#include <map>
98
#include <memory>
109
#include <set>
1110
#include <string>
@@ -70,48 +69,6 @@ class Plugin {
7069
virtual ~Plugin() = default;
7170
};
7271

73-
// A singleton to own PluginRegistrars. This is intended for use in plugins,
74-
// where there is no higher-level object to own a PluginRegistrar that can
75-
// own plugin instances and ensure that they live as long as the engine they
76-
// are registered with.
77-
class PluginRegistrarManager {
78-
public:
79-
static PluginRegistrarManager* GetInstance();
80-
81-
// Prevent copying.
82-
PluginRegistrarManager(PluginRegistrarManager const&) = delete;
83-
PluginRegistrarManager& operator=(PluginRegistrarManager const&) = delete;
84-
85-
// Returns a plugin registrar wrapper of type T, which must be a kind of
86-
// PluginRegistrar, creating it if necessary. The returned registrar will
87-
// live as long as the underlying FlutterDesktopPluginRegistrarRef, so
88-
// can be used to own plugin instances.
89-
//
90-
// Calling this multiple times for the same registrar_ref with different
91-
// template types results in undefined behavior.
92-
template <class T>
93-
T* GetRegistrar(FlutterDesktopPluginRegistrarRef registrar_ref) {
94-
auto insert_result =
95-
registrars_.emplace(registrar_ref, std::make_unique<T>(registrar_ref));
96-
auto& registrar_pair = *(insert_result.first);
97-
FlutterDesktopRegistrarSetDestructionHandler(registrar_pair.first,
98-
OnRegistrarDestroyed);
99-
return static_cast<T*>(registrar_pair.second.get());
100-
}
101-
102-
private:
103-
PluginRegistrarManager();
104-
105-
using WrapperMap = std::map<FlutterDesktopPluginRegistrarRef,
106-
std::unique_ptr<PluginRegistrar>>;
107-
108-
static void OnRegistrarDestroyed(FlutterDesktopPluginRegistrarRef registrar);
109-
110-
WrapperMap* registrars() { return &registrars_; }
111-
112-
WrapperMap registrars_;
113-
};
114-
11572
} // namespace flutter
11673

11774
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_

shell/platform/common/cpp/client_wrapper/plugin_registrar.cc

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void BinaryMessengerImpl::SetMessageHandler(const std::string& channel,
138138
ForwardToHandler, message_handler);
139139
}
140140

141-
// ===== PluginRegistrar =====
141+
// PluginRegistrar:
142142

143143
PluginRegistrar::PluginRegistrar(FlutterDesktopPluginRegistrarRef registrar)
144144
: registrar_(registrar) {
@@ -157,20 +157,4 @@ void PluginRegistrar::EnableInputBlockingForChannel(
157157
FlutterDesktopRegistrarEnableInputBlocking(registrar_, channel.c_str());
158158
}
159159

160-
// ===== PluginRegistrarManager =====
161-
162-
// static
163-
PluginRegistrarManager* PluginRegistrarManager::GetInstance() {
164-
static PluginRegistrarManager* instance = new PluginRegistrarManager();
165-
return instance;
166-
}
167-
168-
PluginRegistrarManager::PluginRegistrarManager() = default;
169-
170-
// static
171-
void PluginRegistrarManager::OnRegistrarDestroyed(
172-
FlutterDesktopPluginRegistrarRef registrar) {
173-
GetInstance()->registrars()->erase(registrar);
174-
}
175-
176160
} // namespace flutter

shell/platform/common/cpp/client_wrapper/plugin_registrar_unittests.cc

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,28 @@ class TestApi : public testing::StubFlutterApi {
3333
return message_engine_result;
3434
}
3535

36+
// Called for FlutterDesktopMessengerSetCallback.
3637
void MessengerSetCallback(const char* channel,
3738
FlutterDesktopMessageCallback callback,
3839
void* user_data) override {
39-
last_message_callback_set_ = callback;
40-
}
41-
42-
void RegistrarSetDestructionHandler(
43-
FlutterDesktopOnRegistrarDestroyed callback) override {
44-
last_destruction_callback_set_ = callback;
40+
last_callback_set_ = callback;
4541
}
4642

4743
const uint8_t* last_data_sent() { return last_data_sent_; }
48-
FlutterDesktopMessageCallback last_message_callback_set() {
49-
return last_message_callback_set_;
50-
}
51-
FlutterDesktopOnRegistrarDestroyed last_destruction_callback_set() {
52-
return last_destruction_callback_set_;
44+
FlutterDesktopMessageCallback last_callback_set() {
45+
return last_callback_set_;
5346
}
5447

5548
private:
5649
const uint8_t* last_data_sent_ = nullptr;
57-
FlutterDesktopMessageCallback last_message_callback_set_ = nullptr;
58-
FlutterDesktopOnRegistrarDestroyed last_destruction_callback_set_ = nullptr;
50+
FlutterDesktopMessageCallback last_callback_set_ = nullptr;
5951
};
6052

6153
} // namespace
6254

6355
// Tests that the registrar returns a messenger that passes Send through to the
6456
// C API.
65-
TEST(PluginRegistrarTest, MessengerSend) {
57+
TEST(MethodCallTest, MessengerSend) {
6658
testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
6759
auto test_api = static_cast<TestApi*>(scoped_api_stub.stub());
6860

@@ -78,7 +70,7 @@ TEST(PluginRegistrarTest, MessengerSend) {
7870

7971
// Tests that the registrar returns a messenger that passes callback
8072
// registration and unregistration through to the C API.
81-
TEST(PluginRegistrarTest, MessengerSetMessageHandler) {
73+
TEST(MethodCallTest, MessengerSetMessageHandler) {
8274
testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
8375
auto test_api = static_cast<TestApi*>(scoped_api_stub.stub());
8476

@@ -93,60 +85,11 @@ TEST(PluginRegistrarTest, MessengerSetMessageHandler) {
9385
const size_t message_size,
9486
BinaryReply reply) {};
9587
messenger->SetMessageHandler(channel_name, std::move(binary_handler));
96-
EXPECT_NE(test_api->last_message_callback_set(), nullptr);
88+
EXPECT_NE(test_api->last_callback_set(), nullptr);
9789

9890
// Unregister.
9991
messenger->SetMessageHandler(channel_name, nullptr);
100-
EXPECT_EQ(test_api->last_message_callback_set(), nullptr);
101-
}
102-
103-
// Tests that the registrar manager returns the same instance when getting
104-
// the wrapper for the same reference.
105-
TEST(PluginRegistrarTest, ManagerSameInstance) {
106-
testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
107-
108-
auto dummy_registrar_handle =
109-
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
110-
111-
PluginRegistrarManager* manager = PluginRegistrarManager::GetInstance();
112-
EXPECT_EQ(manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle),
113-
manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle));
114-
}
115-
116-
// Tests that the registrar manager returns different objects for different
117-
// references.
118-
TEST(PluginRegistrarTest, ManagerDifferentInstances) {
119-
testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
120-
121-
auto dummy_registrar_handle_a =
122-
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
123-
auto dummy_registrar_handle_b =
124-
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(2);
125-
126-
PluginRegistrarManager* manager = PluginRegistrarManager::GetInstance();
127-
EXPECT_NE(manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle_a),
128-
manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle_b));
129-
}
130-
131-
// Tests that the registrar manager deletes wrappers when the underlying
132-
// reference is destroyed.
133-
TEST(PluginRegistrarTest, ManagerRemovesOnDestruction) {
134-
testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
135-
auto test_api = static_cast<TestApi*>(scoped_api_stub.stub());
136-
137-
auto dummy_registrar_handle =
138-
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
139-
PluginRegistrarManager* manager = PluginRegistrarManager::GetInstance();
140-
auto* first_wrapper =
141-
manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle);
142-
143-
// Simulate destruction of the reference.
144-
EXPECT_NE(test_api->last_destruction_callback_set(), nullptr);
145-
test_api->last_destruction_callback_set()(dummy_registrar_handle);
146-
147-
// Requesting the wrapper should now create a new object.
148-
EXPECT_NE(manager->GetRegistrar<PluginRegistrar>(dummy_registrar_handle),
149-
first_wrapper);
92+
EXPECT_EQ(test_api->last_callback_set(), nullptr);
15093
}
15194

15295
} // namespace flutter

shell/platform/common/cpp/client_wrapper/testing/stub_flutter_api.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger(
4444
return reinterpret_cast<FlutterDesktopMessengerRef>(1);
4545
}
4646

47-
void FlutterDesktopRegistrarSetDestructionHandler(
48-
FlutterDesktopPluginRegistrarRef registrar,
49-
FlutterDesktopOnRegistrarDestroyed callback) {
50-
if (s_stub_implementation) {
51-
s_stub_implementation->RegistrarSetDestructionHandler(callback);
52-
}
53-
}
54-
5547
void FlutterDesktopRegistrarEnableInputBlocking(
5648
FlutterDesktopPluginRegistrarRef registrar,
5749
const char* channel) {

shell/platform/common/cpp/client_wrapper/testing/stub_flutter_api.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class StubFlutterApi {
3434

3535
virtual ~StubFlutterApi() {}
3636

37-
// Called for FlutterDesktopRegistrarSetDestructionHandler.
38-
virtual void RegistrarSetDestructionHandler(
39-
FlutterDesktopOnRegistrarDestroyed callback) {}
40-
4137
// Called for FlutterDesktopRegistrarEnableInputBlocking.
4238
virtual void RegistrarEnableInputBlocking(const char* channel) {}
4339

shell/platform/common/cpp/public/flutter_plugin_registrar.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,10 @@ extern "C" {
1818
// Opaque reference to a plugin registrar.
1919
typedef struct FlutterDesktopPluginRegistrar* FlutterDesktopPluginRegistrarRef;
2020

21-
// Function pointer type for registrar destruction callback.
22-
typedef void (*FlutterDesktopOnRegistrarDestroyed)(
23-
FlutterDesktopPluginRegistrarRef);
24-
2521
// Returns the engine messenger associated with this registrar.
2622
FLUTTER_EXPORT FlutterDesktopMessengerRef
2723
FlutterDesktopRegistrarGetMessenger(FlutterDesktopPluginRegistrarRef registrar);
2824

29-
// Registers a callback to be called when the plugin registrar is destroyed.
30-
FLUTTER_EXPORT void FlutterDesktopRegistrarSetDestructionHandler(
31-
FlutterDesktopPluginRegistrarRef registrar,
32-
FlutterDesktopOnRegistrarDestroyed callback);
33-
3425
// Enables input blocking on the given channel.
3526
//
3627
// If set, then the Flutter window will disable input callbacks

shell/platform/glfw/flutter_glfw.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ struct FlutterDesktopPluginRegistrar {
118118

119119
// The handle for the window associated with this registrar.
120120
FlutterDesktopWindow* window;
121-
122-
// Callback to be called on registrar destruction.
123-
FlutterDesktopOnRegistrarDestroyed destruction_handler;
124121
};
125122

126123
// State associated with the messenger used to communicate with the engine.
@@ -685,11 +682,6 @@ FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow(
685682
}
686683

687684
void FlutterDesktopDestroyWindow(FlutterDesktopWindowControllerRef controller) {
688-
FlutterDesktopPluginRegistrarRef registrar =
689-
controller->plugin_registrar.get();
690-
if (registrar->destruction_handler) {
691-
registrar->destruction_handler(registrar);
692-
}
693685
FlutterEngineShutdown(controller->engine);
694686
delete controller;
695687
}
@@ -840,12 +832,6 @@ FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger(
840832
return registrar->messenger.get();
841833
}
842834

843-
void FlutterDesktopRegistrarSetDestructionHandler(
844-
FlutterDesktopPluginRegistrarRef registrar,
845-
FlutterDesktopOnRegistrarDestroyed callback) {
846-
registrar->destruction_handler = callback;
847-
}
848-
849835
FlutterDesktopWindowRef FlutterDesktopRegistrarGetWindow(
850836
FlutterDesktopPluginRegistrarRef registrar) {
851837
return registrar->window;

shell/platform/windows/flutter_windows.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,6 @@ FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger(
248248
return registrar->messenger.get();
249249
}
250250

251-
void FlutterDesktopRegistrarSetDestructionHandler(
252-
FlutterDesktopPluginRegistrarRef registrar,
253-
FlutterDesktopOnRegistrarDestroyed callback) {
254-
registrar->destruction_handler = callback;
255-
}
256-
257251
FlutterDesktopViewRef FlutterDesktopRegistrarGetView(
258252
FlutterDesktopPluginRegistrarRef registrar) {
259253
return registrar->window;

shell/platform/windows/win32_flutter_window.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ Win32FlutterWindow::Win32FlutterWindow(int width, int height) {
1515

1616
Win32FlutterWindow::~Win32FlutterWindow() {
1717
DestroyRenderSurface();
18-
if (plugin_registrar_ && plugin_registrar_->destruction_handler) {
19-
plugin_registrar_->destruction_handler(plugin_registrar_.get());
20-
}
2118
}
2219

2320
FlutterDesktopViewControllerRef Win32FlutterWindow::CreateWin32FlutterWindow(

0 commit comments

Comments
 (0)