diff --git a/CHANGELOG.md b/CHANGELOG.md index e77706f91d5..03a676036c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,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 diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index 3ac8dc2dded..8511cbcb078 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,88 @@ #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.size()); + std::cout.write(reinterpret_cast(&len), sizeof(len)); + std::cout.write(str.data(), str.size()); + std::cout.flush(); +} - while (true) - { - char size_c[4]; - std::cin.read(size_c, 4); +QByteArray receiveFromBrowser() +{ + uint32_t size = 0; + std::cin.read(reinterpret_cast(&size), sizeof(size)); - if (std::cin.eof()) - { - break; - } + if (std::cin.eof()) + { + return {}; + } - auto size = *reinterpret_cast(size_c); + QByteArray buffer{static_cast(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();