From b978977e7a4592806fe7519104b823a156ce9183 Mon Sep 17 00:00:00 2001 From: fourtf Date: Thu, 12 Apr 2018 01:17:25 +0200 Subject: [PATCH] fished base communication between browser and chatterino --- browser_ext/background.js | 65 +++++++++++++++-------- src/main.cpp | 4 -- src/singletons/nativemessagingmanager.cpp | 13 +++-- src/singletons/nativemessagingmanager.hpp | 3 ++ 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/browser_ext/background.js b/browser_ext/background.js index f45230551..d0e0680f3 100644 --- a/browser_ext/background.js +++ b/browser_ext/background.js @@ -10,41 +10,37 @@ const ignoredPages = { const appName = "com.chatterino.chatterino"; -let port; +/// Connect to port -function matchUrl(url) { - if (!url) - return; - - const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9]+)\/?$/); - - if (match) { - const channelName = match[2]; - - if (!ignoredPages[channelName]) { - selectChannel(channelName); - } - } -} +let port = null; function connectPort() { - let port = chrome.runtime.connectNative("com.chatterino.chatterino"); + port = chrome.runtime.connectNative("com.chatterino.chatterino"); + console.log("port connected"); port.onMessage.addListener(function(msg) { console.log(msg); }); port.onDisconnect.addListener(function() { - console.log("Disconnected"); + console.log("port disconnected"); + + port = null; }); } -function selectChannel(channelName) { - console.log(channelName); +function getPort() { + if (port) { + return port; + } else { + // TODO: add cooldown + connectPort(); - port.postMessage({channelName: channelName}); + return port; + } } -/// add listeners +/// Tab listeners + chrome.tabs.onActivated.addListener((activeInfo) => { chrome.tabs.get(activeInfo.tabId, (tab) => { if (!tab) @@ -63,3 +59,30 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { matchUrl(changeInfo.url); }); + + +/// Misc + +function matchUrl(url) { + if (!url) + return; + + const match = url.match(/^https?:\/\/(www\.)?twitch.tv\/([a-zA-Z0-9]+)\/?$/); + + if (match) { + const channelName = match[2]; + + if (!ignoredPages[channelName]) { + selectChannel(channelName); + } + } +} + +function selectChannel(channelName) { + console.log("select" + channelName); + + let port = getPort(); + if (port) { + port.postMessage({action: "select", channelName: channelName}); + } +} diff --git a/src/main.cpp b/src/main.cpp index 36c1998f0..4c1659cc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -114,12 +114,8 @@ void runNativeMessagingHost() std::cin.read(b, size); *(b + size) = '\0'; - nmm.writeByteArray("{\"a\":1}"); - nmm.sendToGuiProcess(QByteArray(b, size)); - nmm.writeByteArray("{\"a\":2}"); - free(b); } } diff --git a/src/singletons/nativemessagingmanager.cpp b/src/singletons/nativemessagingmanager.cpp index b674326bd..b6b4768a7 100644 --- a/src/singletons/nativemessagingmanager.cpp +++ b/src/singletons/nativemessagingmanager.cpp @@ -95,9 +95,7 @@ void NativeMessagingManager::openGuiMessageQueue() void NativeMessagingManager::sendToGuiProcess(const QByteArray &array) { #ifdef BOOSTLIBS - writeByteArray("{\"b\": 1}"); ipc::message_queue messageQueue(ipc::open_or_create, "chatterino_gui", 100, MESSAGE_SIZE); - writeByteArray("{\"b\": 2}"); try { messageQueue.try_send(array.data(), array.size(), 1); @@ -122,13 +120,20 @@ void NativeMessagingManager::ReceiverThread::run() messageQueue.receive(buf, MESSAGE_SIZE, retSize, priority); - QString text = QString::fromUtf8(buf, retSize); - qDebug() << text; + QJsonDocument document = QJsonDocument::fromRawData(buf, retSize); + + this->handleMessage(document.object()); } catch (ipc::interprocess_exception &ex) { qDebug() << "received from gui process:" << ex.what(); } } #endif } + +void NativeMessagingManager::ReceiverThread::handleMessage(const QJsonObject &root) +{ + // TODO: add code xD +} + } // namespace singletons } // namespace chatterino diff --git a/src/singletons/nativemessagingmanager.hpp b/src/singletons/nativemessagingmanager.hpp index 96908f526..0604caddb 100644 --- a/src/singletons/nativemessagingmanager.hpp +++ b/src/singletons/nativemessagingmanager.hpp @@ -16,6 +16,9 @@ public: { public: void run() override; + + private: + void handleMessage(const QJsonObject &root); }; void writeByteArray(QByteArray a);