mirror-chatterino2/src/BrowserExtension.cpp
nerix 378aee7ab1
Refactor Native Messages (#4738)
* refactor: move ipc queue into its own class

* refactor: move windows.h related functions to AW

* refactor: make NM-Client methods static

* refactor: json access

* refactor: use struct initializer

* refactor: move `handleMessage` to anon-namespace

* refactor: clean-up includes

* refactor: move action handler to functions

* refactor: cleanup `handleSelect`

* fix: cleanup clang-tidy warnings

* chore: simplify json

* revert: keep handlers as methods

This is more readable and extensible.

* fix: typo

* fix: namespace

* fix: rename define

* refactor: `IpcQueue` to be simpler

* fix: rename cmake option

* fix: use variant when constructing

* fix: make it a ref

* fix: its a pair now
2023-07-30 11:14:58 +00:00

89 lines
1.9 KiB
C++

#include "BrowserExtension.hpp"
#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 {
namespace {
void initFileMode()
{
#ifdef Q_OS_WIN
_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);
}
});
while (true)
{
char size_c[4];
std::cin.read(size_c, 4);
if (std::cin.eof())
{
break;
}
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);
}
} // namespace
void runBrowserExtensionHost()
{
initFileMode();
runLoop();
}
} // namespace chatterino