From b247f4bc80da22218969157fd1b9b252841a00ef Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 16 Jun 2024 18:22:23 +0200 Subject: [PATCH 1/6] refactor: cleanup browser extension --- src/BrowserExtension.cpp | 113 +++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 51 deletions(-) diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index 3ac8dc2dded..68df87f5a99 100644 --- a/src/BrowserExtension.cpp +++ b/src/BrowserExtension.cpp @@ -2,13 +2,6 @@ #include "singletons/NativeMessaging.hpp" -#include -#include -#include -#include - -#include -#include #include #include #include @@ -16,69 +9,87 @@ #ifdef Q_OS_WIN # include # include -# include -#endif -namespace chatterino { +# include + +#endif namespace { - void initFileMode() - { + +using namespace chatterino; + +void initFileMode() +{ #ifdef Q_OS_WIN - _setmode(_fileno(stdin), _O_BINARY); - _setmode(_fileno(stdout), _O_BINARY); + _setmode(_fileno(stdin), _O_BINARY); + _setmode(_fileno(stdout), _O_BINARY); #endif - } - - void runLoop() - { - auto received_message = std::make_shared(true); +} - auto thread = std::thread([=]() { - while (true) - { - using namespace std::chrono_literals; - if (!received_message->exchange(false)) - { - _Exit(1); - } - std::this_thread::sleep_for(5s); - } - }); +// TODO(Qt6): Use QUtf8String +void sendToBrowser(QLatin1String str) +{ + auto len = static_cast(str.length()); + std::cout.write(std::bit_cast(&len), sizeof(len)); + std::cout.write(str.data(), str.length()); + std::cout.flush(); +} - while (true) - { - char size_c[4]; - std::cin.read(size_c, 4); +QByteArray receiveFromBrowser() +{ + uint32_t size = 0; + std::cin.read(std::bit_cast(&size), sizeof(size)); - if (std::cin.eof()) - { - break; - } + if (std::cin.eof()) + { + return {}; + } - auto size = *reinterpret_cast(size_c); + QByteArray buffer{size, Qt::Uninitialized}; + std::cin.read(buffer.data(), size); - std::unique_ptr buffer(new char[size + 1]); - std::cin.read(buffer.get(), size); - *(buffer.get() + size) = '\0'; + return buffer; +} - auto data = QByteArray::fromRawData(buffer.get(), - static_cast(size)); - auto doc = QJsonDocument(); +void runLoop() +{ + auto receivedMessage = std::make_shared(true); - if (doc.object().value("type") == "nm_pong") + auto thread = std::thread([=]() { + while (true) + { + using namespace std::chrono_literals; + if (!receivedMessage->exchange(false)) { - received_message->store(true); + sendToBrowser(QLatin1String{ + R"({"type":"status","status":"exiting-host","reason":"no message was received in 10s"})"}); + _Exit(1); } + std::this_thread::sleep_for(10s); + } + }); - received_message->store(true); - - nm::client::sendMessage(data); + while (true) + { + auto buffer = receiveFromBrowser(); + if (buffer.isNull()) + { + break; } - _Exit(0); + + receivedMessage->store(true); + + nm::client::sendMessage(buffer); } + + sendToBrowser(QLatin1String{ + R"({"type":"status","status":"exiting-host","reason":"received EOF"})"}); + _Exit(0); +} } // namespace +namespace chatterino { + void runBrowserExtensionHost() { initFileMode(); From ebb5da8fff98dfc6f717ad5ea3026b68be810355 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 16 Jun 2024 18:41:29 +0200 Subject: [PATCH 2/6] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b43a309698..55c35351a01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - Dev: Qt Creator now auto-configures Conan when loading the project and skips vcpkg. (#5305) - Dev: The MSVC CRT is now bundled with Chatterino as it depends on having a recent version installed. (#5447) - Dev: Refactor/unsingletonize `UserDataController`. (#5459) +- Dev: Cleanup `BrowserExtension`. (#5465) ## 2.5.1 From c0a34778f790605b3c3676321b0dd9ddd00bd196 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 16 Jun 2024 18:46:57 +0200 Subject: [PATCH 3/6] fix: use reinterpret_cast for ubuntu --- src/BrowserExtension.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index 68df87f5a99..2e82759cc70 100644 --- a/src/BrowserExtension.cpp +++ b/src/BrowserExtension.cpp @@ -30,7 +30,7 @@ void initFileMode() void sendToBrowser(QLatin1String str) { auto len = static_cast(str.length()); - std::cout.write(std::bit_cast(&len), sizeof(len)); + std::cout.write(reinterpret_cast(&len), sizeof(len)); std::cout.write(str.data(), str.length()); std::cout.flush(); } @@ -38,7 +38,7 @@ void sendToBrowser(QLatin1String str) QByteArray receiveFromBrowser() { uint32_t size = 0; - std::cin.read(std::bit_cast(&size), sizeof(size)); + std::cin.read(reinterpret_cast(&size), sizeof(size)); if (std::cin.eof()) { From a32246a196813f1d746eef5d29ac65c5710b2aa1 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 16 Jun 2024 18:53:43 +0200 Subject: [PATCH 4/6] fix: clang and qt5 fixes --- src/BrowserExtension.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index 2e82759cc70..e41be8de07a 100644 --- a/src/BrowserExtension.cpp +++ b/src/BrowserExtension.cpp @@ -29,7 +29,7 @@ void initFileMode() // TODO(Qt6): Use QUtf8String void sendToBrowser(QLatin1String str) { - auto len = static_cast(str.length()); + auto len = static_cast(str.size()); std::cout.write(reinterpret_cast(&len), sizeof(len)); std::cout.write(str.data(), str.length()); std::cout.flush(); @@ -45,7 +45,7 @@ QByteArray receiveFromBrowser() return {}; } - QByteArray buffer{size, Qt::Uninitialized}; + QByteArray buffer{static_cast(size), Qt::Uninitialized}; std::cin.read(buffer.data(), size); return buffer; From 45ab15009c04b6af5cbcf8fa2d7e73079aa8686f Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 16 Jun 2024 18:58:58 +0200 Subject: [PATCH 5/6] fix: more qt5 man this is old --- src/BrowserExtension.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index e41be8de07a..1176975b2ad 100644 --- a/src/BrowserExtension.cpp +++ b/src/BrowserExtension.cpp @@ -45,7 +45,8 @@ QByteArray receiveFromBrowser() return {}; } - QByteArray buffer{static_cast(size), Qt::Uninitialized}; + QByteArray buffer{static_cast(size), + Qt::Uninitialized}; std::cin.read(buffer.data(), size); return buffer; From 95f40852afa162f4a57d643a2821c3692a715496 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 16 Jun 2024 19:19:05 +0200 Subject: [PATCH 6/6] fix: even more qt5 --- src/BrowserExtension.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index 1176975b2ad..8511cbcb078 100644 --- a/src/BrowserExtension.cpp +++ b/src/BrowserExtension.cpp @@ -31,7 +31,7 @@ void sendToBrowser(QLatin1String str) { auto len = static_cast(str.size()); std::cout.write(reinterpret_cast(&len), sizeof(len)); - std::cout.write(str.data(), str.length()); + std::cout.write(str.data(), str.size()); std::cout.flush(); }