From 3e033975a4b6a8c5d966eb67002ab6eff5fb2e12 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Mon, 31 Jul 2023 14:33:16 +0200 Subject: [PATCH 1/4] feat: keep channels from browser tabs alive --- src/singletons/NativeMessaging.cpp | 49 ++++++++++++++++++++++++++++++ src/singletons/NativeMessaging.hpp | 22 ++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/singletons/NativeMessaging.cpp b/src/singletons/NativeMessaging.cpp index ef42e706da1..cd0a08318ef 100644 --- a/src/singletons/NativeMessaging.cpp +++ b/src/singletons/NativeMessaging.cpp @@ -3,6 +3,7 @@ #include "Application.hpp" #include "common/Literals.hpp" #include "common/QLogging.hpp" +#include "debug/AssertInGuiThread.hpp" #include "providers/twitch/TwitchIrcServer.hpp" #include "singletons/Paths.hpp" #include "util/IpcQueue.hpp" @@ -129,12 +130,22 @@ namespace nm::client { } // namespace nm::client // SERVER +NativeMessagingServer::NativeMessagingServer() + : thread(*this) +{ +} void NativeMessagingServer::start() { this->thread.start(); } +NativeMessagingServer::ReceiverThread::ReceiverThread( + NativeMessagingServer &parent) + : parent_(parent) +{ +} + void NativeMessagingServer::ReceiverThread::run() { auto [messageQueue, error] = @@ -177,6 +188,11 @@ void NativeMessagingServer::ReceiverThread::handleMessage( this->handleDetach(root); return; } + if (action == "sync") + { + this->handleSync(root); + return; + } qCDebug(chatterinoNativeMessage) << "NM unknown action" << action; } @@ -263,6 +279,39 @@ void NativeMessagingServer::ReceiverThread::handleDetach( } // NOLINTEND(readability-convert-member-functions-to-static) +void NativeMessagingServer::ReceiverThread::handleSync(const QJsonObject &root) +{ + // Structure: + // { action: 'sync', twitch?: string[] } + postToThread( + [&parent = this->parent_, twitch = root["twitch"_L1].toArray()] { + parent.syncChannels(twitch); + }); +} + +void NativeMessagingServer::syncChannels(const QJsonArray &twitchChannels) +{ + assertInGuiThread(); + + auto *app = getApp(); + + std::vector updated; + updated.reserve(twitchChannels.size()); + for (const auto &value : twitchChannels) + { + auto name = value.toString(); + if (name.isEmpty()) + { + continue; + } + // the deduping is done on the extension side + updated.emplace_back(app->twitch->getOrAddChannel(name)); + } + + // This will destroy channels that aren't used anymore. + this->channelWarmer_ = std::move(updated); +} + Atomic> &nmIpcError() { static Atomic> x; diff --git a/src/singletons/NativeMessaging.hpp b/src/singletons/NativeMessaging.hpp index 4f39057c020..bb20ab76266 100644 --- a/src/singletons/NativeMessaging.hpp +++ b/src/singletons/NativeMessaging.hpp @@ -6,10 +6,15 @@ #include #include +#include + namespace chatterino { class Application; class Paths; +class Channel; + +using ChannelPtr = std::shared_ptr; void registerNmHost(Paths &paths); std::string &getNmQueueName(Paths &paths); @@ -26,21 +31,38 @@ namespace nm::client { class NativeMessagingServer final { public: + NativeMessagingServer(); + NativeMessagingServer(const NativeMessagingServer &) = delete; + NativeMessagingServer(NativeMessagingServer &&) = delete; + NativeMessagingServer &operator=(const NativeMessagingServer &) = delete; + NativeMessagingServer &operator=(NativeMessagingServer &&) = delete; + void start(); private: class ReceiverThread : public QThread { public: + ReceiverThread(NativeMessagingServer &parent); + void run() override; private: void handleMessage(const QJsonObject &root); void handleSelect(const QJsonObject &root); void handleDetach(const QJsonObject &root); + void handleSync(const QJsonObject &root); + + NativeMessagingServer &parent_; }; + void syncChannels(const QJsonArray &twitchChannels); + ReceiverThread thread; + + std::vector channelWarmer_; + + friend ReceiverThread; }; } // namespace chatterino From edfdd1bd912e7ad9625ab5c17e216366b84ef595 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Mon, 31 Jul 2023 14:53:22 +0200 Subject: [PATCH 2/4] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62f7f4ee9cd..cf0051af1bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Minor: 7TV badges now automatically update upon changing. (#4512) - Minor: Stream status requests are now batched. (#4713) - Minor: Added `/c2-theme-autoreload` command to automatically reload a custom theme. This is useful for when you're developing your own theme. (#4718) +- Minor: All channels opened in browser tabs are synced when using the extension for quicker switching between tabs. (#4741) - Bugfix: Increased amount of blocked users loaded from 100 to 1,000. (#4721) - Bugfix: Fixed generation of crashdumps by the browser-extension process when the browser was closed. (#4667) - Bugfix: Fix spacing issue with mentions inside RTL text. (#4677) From fbefd8da7e2585993b3c6024e9a06dfc957ec15d Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 5 Aug 2023 13:20:21 +0200 Subject: [PATCH 3/4] fix: add comment --- src/singletons/NativeMessaging.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/singletons/NativeMessaging.hpp b/src/singletons/NativeMessaging.hpp index bb20ab76266..4ed46ad0dad 100644 --- a/src/singletons/NativeMessaging.hpp +++ b/src/singletons/NativeMessaging.hpp @@ -60,6 +60,8 @@ class NativeMessagingServer final ReceiverThread thread; + /// This vector contains all channels that are open the user's browser. + /// These channels are joined to be able to switch channels more quickly. std::vector channelWarmer_; friend ReceiverThread; From 415dc318759433c033687ad97f44066203d8c247 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 5 Aug 2023 13:20:58 +0200 Subject: [PATCH 4/4] fix: rename key --- src/singletons/NativeMessaging.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/singletons/NativeMessaging.cpp b/src/singletons/NativeMessaging.cpp index cd0a08318ef..a7165c1f766 100644 --- a/src/singletons/NativeMessaging.cpp +++ b/src/singletons/NativeMessaging.cpp @@ -282,11 +282,11 @@ void NativeMessagingServer::ReceiverThread::handleDetach( void NativeMessagingServer::ReceiverThread::handleSync(const QJsonObject &root) { // Structure: - // { action: 'sync', twitch?: string[] } - postToThread( - [&parent = this->parent_, twitch = root["twitch"_L1].toArray()] { - parent.syncChannels(twitch); - }); + // { action: 'sync', twitchChannels?: string[] } + postToThread([&parent = this->parent_, + twitch = root["twitchChannels"_L1].toArray()] { + parent.syncChannels(twitch); + }); } void NativeMessagingServer::syncChannels(const QJsonArray &twitchChannels)