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: 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

View file

@ -2,13 +2,6 @@
#include "singletons/NativeMessaging.hpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
#include <QTimer>
#include <chrono>
#include <fstream>
#include <iostream>
#include <memory>
#include <thread>
@ -16,12 +9,15 @@
#ifdef Q_OS_WIN
# include <fcntl.h>
# include <io.h>
# include <stdio.h>
# include <cstdio>
#endif
namespace chatterino {
namespace {
using namespace chatterino;
void initFileMode()
{
#ifdef Q_OS_WIN
@ -30,55 +26,71 @@ namespace {
#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 {};
}
QByteArray buffer{static_cast<QByteArray::size_type>(size),
Qt::Uninitialized};
std::cin.read(buffer.data(), size);
return buffer;
}
void runLoop()
{
auto received_message = std::make_shared<std::atomic_bool>(true);
auto receivedMessage = std::make_shared<std::atomic_bool>(true);
auto thread = std::thread([=]() {
while (true)
{
using namespace std::chrono_literals;
if (!received_message->exchange(false))
if (!receivedMessage->exchange(false))
{
sendToBrowser(QLatin1String{
R"({"type":"status","status":"exiting-host","reason":"no message was received in 10s"})"});
_Exit(1);
}
std::this_thread::sleep_for(5s);
std::this_thread::sleep_for(10s);
}
});
while (true)
{
char size_c[4];
std::cin.read(size_c, 4);
if (std::cin.eof())
auto buffer = receiveFromBrowser();
if (buffer.isNull())
{
break;
}
auto size = *reinterpret_cast<uint32_t *>(size_c);
receivedMessage->store(true);
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);
nm::client::sendMessage(buffer);
}
received_message->store(true);
nm::client::sendMessage(data);
}
sendToBrowser(QLatin1String{
R"({"type":"status","status":"exiting-host","reason":"received EOF"})"});
_Exit(0);
}
} // namespace
namespace chatterino {
void runBrowserExtensionHost()
{
initFileMode();