Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleanup browser extension #5465

Merged
merged 8 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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));
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
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));
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved

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
Loading