2018-08-02 14:23:27 +02:00
|
|
|
#include "BrowserExtension.hpp"
|
|
|
|
|
|
|
|
#include "singletons/NativeMessaging.hpp"
|
|
|
|
|
2021-05-08 15:57:00 +02:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2018-08-02 14:23:27 +02:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QTimer>
|
2021-05-08 15:57:00 +02:00
|
|
|
|
2021-05-23 23:10:29 +02:00
|
|
|
#include <chrono>
|
2018-08-02 14:23:27 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2021-05-23 23:10:29 +02:00
|
|
|
#include <thread>
|
2018-08-02 14:23:27 +02:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
2018-08-15 22:46:20 +02:00
|
|
|
# include <fcntl.h>
|
|
|
|
# include <io.h>
|
|
|
|
# include <stdio.h>
|
2018-08-02 14:23:27 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
namespace {
|
2018-08-15 22:46:20 +02:00
|
|
|
void initFileMode()
|
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2018-08-15 22:46:20 +02:00
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
|
|
|
_setmode(_fileno(stdout), _O_BINARY);
|
2018-08-02 14:23:27 +02:00
|
|
|
#endif
|
2018-08-15 22:46:20 +02:00
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2023-07-30 13:14:58 +02:00
|
|
|
void runLoop()
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
2019-08-25 21:25:31 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
char size_c[4];
|
|
|
|
std::cin.read(size_c, 4);
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
if (std::cin.eof())
|
2019-08-25 21:25:31 +02:00
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
break;
|
2019-08-25 21:25:31 +02:00
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
auto size = *reinterpret_cast<uint32_t *>(size_c);
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
std::unique_ptr<char[]> buffer(new char[size + 1]);
|
|
|
|
std::cin.read(buffer.get(), size);
|
|
|
|
*(buffer.get() + size) = '\0';
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-25 21:25:31 +02:00
|
|
|
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);
|
|
|
|
|
2023-07-30 13:14:58 +02:00
|
|
|
nm::client::sendMessage(data);
|
2018-08-15 22:46:20 +02:00
|
|
|
}
|
2023-06-04 14:21:16 +02:00
|
|
|
_Exit(0);
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void runBrowserExtensionHost()
|
|
|
|
{
|
|
|
|
initFileMode();
|
|
|
|
|
2023-07-30 13:14:58 +02:00
|
|
|
runLoop();
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|