Skip to content

Commit

Permalink
refactor: cleanup browser extension (#5465)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz authored Jun 22, 2024
1 parent 2ef3306 commit 0b54b0b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
114 changes: 63 additions & 51 deletions src/BrowserExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,95 @@

#include "singletons/NativeMessaging.hpp"

#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
#include <QTimer>

#include <chrono>
#include <fstream>
#include <iostream>
#include <memory>
#include <thread>

#ifdef Q_OS_WIN
# include <fcntl.h>
# include <io.h>
# include <stdio.h>
#endif

namespace chatterino {
# include <cstdio>

#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<std::atomic_bool>(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<uint32_t>(str.size());
std::cout.write(reinterpret_cast<const char *>(&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<char *>(&size), sizeof(size));

if (std::cin.eof())
{
break;
}
if (std::cin.eof())
{
return {};
}

auto size = *reinterpret_cast<uint32_t *>(size_c);
QByteArray buffer{static_cast<QByteArray::size_type>(size),
Qt::Uninitialized};
std::cin.read(buffer.data(), size);

std::unique_ptr<char[]> 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<int32_t>(size));
auto doc = QJsonDocument();
void runLoop()
{
auto receivedMessage = std::make_shared<std::atomic_bool>(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();
Expand Down

0 comments on commit 0b54b0b

Please sign in to comment.