From 937e1da321def25435f52856b547b02c4a6762ce Mon Sep 17 00:00:00 2001 From: Shamil Yessenkulov Date: Mon, 30 Sep 2024 04:44:50 -0700 Subject: [PATCH] Modifying addPlugin() based on the isDuplicationAllowed flag Summary: Adding the flag if the FlipperPlugin is allowed for duplication. This will be set as False by default. Since there is already existing Multi Plugin and it is possible to find if the current plugin allowed for duplication, modifying the addPlugin() function. The behaviour will stay the same for all plugins that has the newly added flag by default. If the plugin with the same identifier is already present and allowed for duplication, then instead of skipping - will create an instance of the multi plugin. Also cleaning the already existing one by disconnecting and erasing it. The newly changed logic can still cause the tree-shape structure, however this might be possible only for the users that will modify the flag and call the addPlugin() few times with the same identifier. As for now, I presume this might be not a problem Differential Revision: D63325554 fbshipit-source-id: affbc9f4bac1f71c37c5f284e707a324298031f5 --- xplat/Flipper/FlipperClient.cpp | 17 +++++++++++++++++ xplat/Flipper/FlipperPlugin.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/xplat/Flipper/FlipperClient.cpp b/xplat/Flipper/FlipperClient.cpp index 8fbf6f58e76..7728e46a3a9 100644 --- a/xplat/Flipper/FlipperClient.cpp +++ b/xplat/Flipper/FlipperClient.cpp @@ -15,6 +15,7 @@ #include "FireAndForgetBasedFlipperResponder.h" #include "FlipperConnectionImpl.h" #include "FlipperConnectionManagerImpl.h" +#include "FlipperMultiPlugin.h" #include "FlipperState.h" #include "FlipperStep.h" #include "Log.h" @@ -64,6 +65,22 @@ void FlipperClient::addPlugin(std::shared_ptr plugin) { std::lock_guard lock(mutex_); if (plugins_.find(plugin->identifier()) != plugins_.end()) { log("[client] Plugin " + plugin->identifier() + " already added"); + // If the plugin is allowed to create a multi plugin, then instead of + // skipping, create a multi plugin and replace the existing plugin. + if (plugin->isDuplicationAllowed) { + log("[client] Plugin is allowed for duplication. Creating multiplugin"); + auto existingPlugin = plugins_[plugin->identifier()]; + std::vector> plugins = { + existingPlugin, plugin}; + auto newPlugin = + std::static_pointer_cast( + std::make_shared( + std::move(plugins))); + disconnect(std::move(existingPlugin)); + plugins_.erase(plugin->identifier()); + plugins_[plugin->identifier()] = std::move(newPlugin); + needs_refresh = true; + } } else { plugins_[plugin->identifier()] = plugin; needs_refresh = true; diff --git a/xplat/Flipper/FlipperPlugin.h b/xplat/Flipper/FlipperPlugin.h index e1495a6d732..f44815875f5 100644 --- a/xplat/Flipper/FlipperPlugin.h +++ b/xplat/Flipper/FlipperPlugin.h @@ -44,6 +44,8 @@ class FlipperPlugin { virtual bool runInBackground() { return false; } + + bool isDuplicationAllowed = false; }; } // namespace flipper