2018-04-09 22:59:19 +02:00
|
|
|
#include "nativemessagingmanager.hpp"
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include "application.hpp"
|
2018-04-20 19:54:45 +02:00
|
|
|
#include "providers/twitch/twitchserver.hpp"
|
2018-04-09 22:59:19 +02:00
|
|
|
#include "singletons/pathmanager.hpp"
|
2018-04-20 19:54:45 +02:00
|
|
|
#include "util/posttothread.hpp"
|
2018-04-09 22:59:19 +02:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
|
|
#include <boost/interprocess/ipc/message_queue.hpp>
|
2018-04-12 00:40:18 +02:00
|
|
|
|
|
|
|
namespace ipc = boost::interprocess;
|
2018-04-09 22:59:19 +02:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <QProcess>
|
2018-04-25 14:49:30 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include <Windows.h>
|
2018-04-25 14:49:30 +02:00
|
|
|
#include "singletons/windowmanager.hpp"
|
|
|
|
#include "widgets/attachedwindow.hpp"
|
2018-04-09 22:59:19 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-12 00:09:16 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2018-04-09 22:59:19 +02:00
|
|
|
#define EXTENSION_ID "aeicjepmjkgmbeohnchmpfjbpchogmjn"
|
|
|
|
#define MESSAGE_SIZE 1024
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace singletons {
|
|
|
|
|
|
|
|
NativeMessagingManager::NativeMessagingManager()
|
|
|
|
{
|
2018-04-26 18:10:26 +02:00
|
|
|
qDebug() << "init NativeMessagingManager";
|
2018-04-09 22:59:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-12 00:09:16 +02:00
|
|
|
void NativeMessagingManager::writeByteArray(QByteArray a)
|
|
|
|
{
|
|
|
|
char *data = a.data();
|
|
|
|
uint32_t size;
|
|
|
|
size = a.size();
|
|
|
|
std::cout.write(reinterpret_cast<char *>(&size), 4);
|
|
|
|
std::cout.write(data, a.size());
|
|
|
|
std::cout.flush();
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:59:19 +02:00
|
|
|
void NativeMessagingManager::registerHost()
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2018-05-28 18:25:19 +02:00
|
|
|
if (app->paths->isPortable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:59:19 +02:00
|
|
|
// create manifest
|
|
|
|
QJsonDocument document;
|
|
|
|
QJsonObject root_obj;
|
|
|
|
root_obj.insert("name", "com.chatterino.chatterino");
|
|
|
|
root_obj.insert("description", "Browser interaction with chatterino.");
|
|
|
|
root_obj.insert("path", QCoreApplication::applicationFilePath());
|
|
|
|
root_obj.insert("type", "stdio");
|
|
|
|
|
2018-04-25 14:49:30 +02:00
|
|
|
// chrome
|
2018-04-09 22:59:19 +02:00
|
|
|
QJsonArray allowed_origins_arr = {"chrome-extension://aeicjepmjkgmbeohnchmpfjbpchogmjn/"};
|
|
|
|
root_obj.insert("allowed_origins", allowed_origins_arr);
|
|
|
|
|
2018-04-25 14:49:30 +02:00
|
|
|
// firefox
|
|
|
|
QJsonArray allowed_extensions = {"585a153c7e1ac5463478f25f8f12220e9097e716@temporary-addon"};
|
|
|
|
root_obj.insert("allowed_extensions", allowed_extensions);
|
|
|
|
|
2018-04-09 22:59:19 +02:00
|
|
|
// save the manifest
|
2018-04-27 22:11:19 +02:00
|
|
|
QString manifestPath = app->paths->settingsFolderPath + "/native-messaging-manifest.json";
|
2018-04-09 22:59:19 +02:00
|
|
|
|
|
|
|
document.setObject(root_obj);
|
|
|
|
|
|
|
|
QFile file(manifestPath);
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
|
|
|
file.write(document.toJson());
|
|
|
|
file.flush();
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// clang-format off
|
|
|
|
QProcess::execute("REG ADD \"HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\com.chatterino.chatterino\" /ve /t REG_SZ /d \"" + manifestPath + "\" /f");
|
|
|
|
// clang-format on
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingManager::openGuiMessageQueue()
|
|
|
|
{
|
|
|
|
static ReceiverThread thread;
|
|
|
|
|
|
|
|
if (thread.isRunning()) {
|
|
|
|
thread.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingManager::sendToGuiProcess(const QByteArray &array)
|
|
|
|
{
|
2018-04-12 00:33:55 +02:00
|
|
|
ipc::message_queue messageQueue(ipc::open_or_create, "chatterino_gui", 100, MESSAGE_SIZE);
|
2018-04-09 22:59:19 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
messageQueue.try_send(array.data(), array.size(), 1);
|
|
|
|
} catch (ipc::interprocess_exception &ex) {
|
2018-04-12 00:09:16 +02:00
|
|
|
qDebug() << "send to gui process:" << ex.what();
|
2018-04-09 22:59:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeMessagingManager::ReceiverThread::run()
|
|
|
|
{
|
|
|
|
ipc::message_queue::remove("chatterino_gui");
|
|
|
|
|
2018-04-12 00:33:55 +02:00
|
|
|
ipc::message_queue messageQueue(ipc::open_or_create, "chatterino_gui", 100, MESSAGE_SIZE);
|
2018-04-09 22:59:19 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
2018-05-28 08:34:54 +02:00
|
|
|
std::unique_ptr<char> buf(static_cast<char *>(malloc(MESSAGE_SIZE)));
|
2018-04-09 22:59:19 +02:00
|
|
|
ipc::message_queue::size_type retSize;
|
|
|
|
unsigned int priority;
|
|
|
|
|
2018-05-28 08:34:54 +02:00
|
|
|
messageQueue.receive(buf.get(), MESSAGE_SIZE, retSize, priority);
|
2018-04-09 22:59:19 +02:00
|
|
|
|
2018-05-28 08:34:54 +02:00
|
|
|
QJsonDocument document =
|
|
|
|
QJsonDocument::fromJson(QByteArray::fromRawData(buf.get(), retSize));
|
2018-04-12 01:17:25 +02:00
|
|
|
|
|
|
|
this->handleMessage(document.object());
|
2018-04-09 22:59:19 +02:00
|
|
|
} catch (ipc::interprocess_exception &ex) {
|
2018-04-12 00:09:16 +02:00
|
|
|
qDebug() << "received from gui process:" << ex.what();
|
2018-04-09 22:59:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 01:17:25 +02:00
|
|
|
|
|
|
|
void NativeMessagingManager::ReceiverThread::handleMessage(const QJsonObject &root)
|
|
|
|
{
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2018-04-20 19:54:45 +02:00
|
|
|
QString action = root.value("action").toString();
|
|
|
|
|
|
|
|
if (action.isNull()) {
|
|
|
|
qDebug() << "NM action was null";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == "select") {
|
|
|
|
QString _type = root.value("type").toString();
|
2018-04-25 14:49:30 +02:00
|
|
|
bool attach = root.value("attach").toBool();
|
2018-04-20 19:54:45 +02:00
|
|
|
QString name = root.value("name").toString();
|
|
|
|
|
2018-05-28 19:49:37 +02:00
|
|
|
#ifdef USEWINSDK
|
2018-05-28 18:25:19 +02:00
|
|
|
widgets::AttachedWindow::GetArgs args;
|
|
|
|
args.winId = root.value("winId").toString();
|
|
|
|
args.yOffset = root.value("yOffset").toInt(-1);
|
|
|
|
args.width = root.value("size").toObject().value("width").toInt(-1);
|
|
|
|
args.height = root.value("size").toObject().value("height").toInt(-1);
|
|
|
|
|
|
|
|
if (_type.isNull() || args.winId.isNull()) {
|
2018-04-25 14:49:30 +02:00
|
|
|
qDebug() << "NM type, name or winId missing";
|
|
|
|
attach = false;
|
2018-04-20 19:54:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-05-28 19:49:37 +02:00
|
|
|
#endif
|
2018-04-20 19:54:45 +02:00
|
|
|
|
|
|
|
if (_type == "twitch") {
|
2018-05-28 18:25:19 +02:00
|
|
|
util::postToThread([=] {
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
app->twitch.server->watchingChannel.update(
|
|
|
|
app->twitch.server->getOrAddChannel(name));
|
|
|
|
}
|
2018-04-25 14:49:30 +02:00
|
|
|
|
|
|
|
if (attach) {
|
2018-04-26 20:58:32 +02:00
|
|
|
#ifdef USEWINSDK
|
2018-05-29 23:58:11 +02:00
|
|
|
if (args.height != -1) {
|
|
|
|
auto *window = widgets::AttachedWindow::get(::GetForegroundWindow(), args);
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
window->setChannel(app->twitch.server->getOrAddChannel(name));
|
|
|
|
}
|
2018-05-28 18:25:19 +02:00
|
|
|
}
|
|
|
|
// window->show();
|
2018-04-26 20:58:32 +02:00
|
|
|
#endif
|
2018-04-25 14:49:30 +02:00
|
|
|
}
|
2018-04-20 19:54:45 +02:00
|
|
|
});
|
2018-04-25 14:49:30 +02:00
|
|
|
|
2018-04-20 19:54:45 +02:00
|
|
|
} else {
|
|
|
|
qDebug() << "NM unknown channel type";
|
|
|
|
}
|
2018-04-25 14:49:30 +02:00
|
|
|
} else if (action == "detach") {
|
|
|
|
QString winId = root.value("winId").toString();
|
|
|
|
|
|
|
|
if (winId.isNull()) {
|
|
|
|
qDebug() << "NM winId missing";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-26 20:58:32 +02:00
|
|
|
#ifdef USEWINSDK
|
2018-04-25 14:49:30 +02:00
|
|
|
util::postToThread([winId] { widgets::AttachedWindow::detach(winId); });
|
2018-04-26 20:58:32 +02:00
|
|
|
#endif
|
2018-04-25 14:49:30 +02:00
|
|
|
} else {
|
|
|
|
qDebug() << "NM unknown action " + action;
|
2018-04-20 19:54:45 +02:00
|
|
|
}
|
2018-05-28 18:25:19 +02:00
|
|
|
} // namespace singletons
|
2018-04-12 01:17:25 +02:00
|
|
|
|
2018-04-09 22:59:19 +02:00
|
|
|
} // namespace singletons
|
|
|
|
} // namespace chatterino
|