Skip to content

fix: support multi plugin instance like desktop_mult_window plugin #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions windows/webview_windows_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ class WebviewWindowsPlugin : public flutter::Plugin {
virtual ~WebviewWindowsPlugin();

private:
std::unique_ptr<WebviewPlatform> platform_;
// CreateDispatcherQueueController can only be called once per thread.
// In the desktop_mult_window plugin, the plugin creates multiple instances,
// which can cause issues due to this constraint
// Therefore, we made it a singleton
static std::unique_ptr<WebviewPlatform> platform_;
static int plugin_count_ = 0;
std::unique_ptr<WebviewHost> webview_host_;
std::unordered_map<int64_t, std::unique_ptr<WebviewBridge>> instances_;

Expand All @@ -73,6 +78,8 @@ class WebviewWindowsPlugin : public flutter::Plugin {
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
};

std::unique_ptr<WebviewPlatform> WebviewWindowsPlugin::platform_;

// static
void WebviewWindowsPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarWindows* registrar) {
Expand All @@ -97,12 +104,20 @@ WebviewWindowsPlugin::WebviewWindowsPlugin(flutter::TextureRegistrar* textures,
: textures_(textures), messenger_(messenger) {
window_class_.lpszClassName = L"FlutterWebviewMessage";
window_class_.lpfnWndProc = &DefWindowProc;
RegisterClass(&window_class_);
plugin_count_ ++;
if (plugin_count_ == 1){
RegisterClass(&window_class_);
}
}

WebviewWindowsPlugin::~WebviewWindowsPlugin() {
instances_.clear();
UnregisterClass(window_class_.lpszClassName, nullptr);
plugin_count_ --;
// To prevent the accidental creation of other windows caused by deregistration
// in multi-plugin-instance environments like desktop_mult_window
if (plugin_count_ == 0){
UnregisterClass(window_class_.lpszClassName, nullptr);
}
}

void WebviewWindowsPlugin::HandleMethodCall(
Expand Down