refactor: cleanup browser extension (#5465)

This commit is contained in:
nerix 2024-06-22 13:38:12 +02:00 committed by GitHub
parent 2ef3306d1d
commit 0b54b0b8f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 53 deletions

View file

@ -39,6 +39,7 @@
- Dev: Qt Creator now auto-configures Conan when loading the project and skips vcpkg. (#5305) - 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: The MSVC CRT is now bundled with Chatterino as it depends on having a recent version installed. (#5447)
- Dev: Refactor/unsingletonize `UserDataController`. (#5459) - Dev: Refactor/unsingletonize `UserDataController`. (#5459)
- Dev: Cleanup `BrowserExtension`. (#5465)
## 2.5.1 ## 2.5.1

View file

@ -2,13 +2,6 @@
#include "singletons/NativeMessaging.hpp" #include "singletons/NativeMessaging.hpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
#include <QTimer>
#include <chrono>
#include <fstream>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <thread> #include <thread>
@ -16,69 +9,88 @@
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# include <fcntl.h> # include <fcntl.h>
# include <io.h> # include <io.h>
# include <stdio.h>
#endif
namespace chatterino { # include <cstdio>
#endif
namespace { namespace {
void initFileMode()
{ using namespace chatterino;
void initFileMode()
{
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
_setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY);
#endif #endif
}
// TODO(Qt6): Use QUtf8String
void sendToBrowser(QLatin1String str)
{
auto len = static_cast<uint32_t>(str.size());
std::cout.write(reinterpret_cast<const char *>(&len), sizeof(len));
std::cout.write(str.data(), str.size());
std::cout.flush();
}
QByteArray receiveFromBrowser()
{
uint32_t size = 0;
std::cin.read(reinterpret_cast<char *>(&size), sizeof(size));
if (std::cin.eof())
{
return {};
} }
void runLoop() QByteArray buffer{static_cast<QByteArray::size_type>(size),
{ Qt::Uninitialized};
auto received_message = std::make_shared<std::atomic_bool>(true); std::cin.read(buffer.data(), size);
auto thread = std::thread([=]() { return buffer;
while (true) }
{
using namespace std::chrono_literals;
if (!received_message->exchange(false))
{
_Exit(1);
}
std::this_thread::sleep_for(5s);
}
});
void runLoop()
{
auto receivedMessage = std::make_shared<std::atomic_bool>(true);
auto thread = std::thread([=]() {
while (true) while (true)
{ {
char size_c[4]; using namespace std::chrono_literals;
std::cin.read(size_c, 4); if (!receivedMessage->exchange(false))
if (std::cin.eof())
{ {
break; sendToBrowser(QLatin1String{
R"({"type":"status","status":"exiting-host","reason":"no message was received in 10s"})"});
_Exit(1);
} }
std::this_thread::sleep_for(10s);
auto size = *reinterpret_cast<uint32_t *>(size_c);
std::unique_ptr<char[]> buffer(new char[size + 1]);
std::cin.read(buffer.get(), size);
*(buffer.get() + size) = '\0';
auto data = QByteArray::fromRawData(buffer.get(),
static_cast<int32_t>(size));
auto doc = QJsonDocument();
if (doc.object().value("type") == "nm_pong")
{
received_message->store(true);
}
received_message->store(true);
nm::client::sendMessage(data);
} }
_Exit(0); });
while (true)
{
auto buffer = receiveFromBrowser();
if (buffer.isNull())
{
break;
}
receivedMessage->store(true);
nm::client::sendMessage(buffer);
} }
sendToBrowser(QLatin1String{
R"({"type":"status","status":"exiting-host","reason":"received EOF"})"});
_Exit(0);
}
} // namespace } // namespace
namespace chatterino {
void runBrowserExtensionHost() void runBrowserExtensionHost()
{ {
initFileMode(); initFileMode();