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,12 +9,15 @@
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# include <fcntl.h> # include <fcntl.h>
# include <io.h> # include <io.h>
# include <stdio.h>
# include <cstdio>
#endif #endif
namespace chatterino {
namespace { namespace {
using namespace chatterino;
void initFileMode() void initFileMode()
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
@ -30,55 +26,71 @@ namespace {
#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 {};
}
QByteArray buffer{static_cast<QByteArray::size_type>(size),
Qt::Uninitialized};
std::cin.read(buffer.data(), size);
return buffer;
}
void runLoop() 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([=]() { auto thread = std::thread([=]() {
while (true) while (true)
{ {
using namespace std::chrono_literals; 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); _Exit(1);
} }
std::this_thread::sleep_for(5s); std::this_thread::sleep_for(10s);
} }
}); });
while (true) while (true)
{ {
char size_c[4]; auto buffer = receiveFromBrowser();
std::cin.read(size_c, 4); if (buffer.isNull())
if (std::cin.eof())
{ {
break; break;
} }
auto size = *reinterpret_cast<uint32_t *>(size_c); receivedMessage->store(true);
std::unique_ptr<char[]> buffer(new char[size + 1]); nm::client::sendMessage(buffer);
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); sendToBrowser(QLatin1String{
R"({"type":"status","status":"exiting-host","reason":"received EOF"})"});
nm::client::sendMessage(data);
}
_Exit(0); _Exit(0);
} }
} // namespace } // namespace
namespace chatterino {
void runBrowserExtensionHost() void runBrowserExtensionHost()
{ {
initFileMode(); initFileMode();