2018-12-30 23:41:44 +01:00
|
|
|
import Debug from '../../conf/Debug';
|
|
|
|
import BrowserDetect from '../../conf/BrowserDetect';
|
2021-03-06 02:06:46 +01:00
|
|
|
import Logger from '../Logger';
|
|
|
|
import { browser } from 'webextension-polyfill-ts';
|
|
|
|
import Settings from '../Settings';
|
2022-07-31 00:15:28 +02:00
|
|
|
import EventBus from '../EventBus';
|
2018-12-30 23:41:44 +01:00
|
|
|
|
2020-04-13 15:20:29 +02:00
|
|
|
if (process.env.CHANNEL !== 'stable'){
|
2020-12-03 01:16:57 +01:00
|
|
|
console.info("Loading CommsClient");
|
2020-04-13 15:20:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
/**
|
|
|
|
* Ultrawidify communication spans a few different "domains" that require a few different
|
|
|
|
* means of communication. The four isolated domains are:
|
|
|
|
*
|
|
|
|
* > content script event bus (CS)
|
|
|
|
* > player UI event bus (UI)
|
|
|
|
* > UWServer event bus (BG)
|
|
|
|
* > popup event bus
|
|
|
|
*
|
|
|
|
* It is our goal to route messages between various domains. It is our goal that eventBus
|
|
|
|
* instances in different parts of our script are at least somewhat interoperable between
|
|
|
|
* each other. As such, scripts sending commands should be unaware that Comms object even
|
|
|
|
* exists.
|
|
|
|
*
|
|
|
|
* EventBus is started first. Other components (including commsClient) follow later.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* fig 0. ULTRAWIDIFY COMMUNICATION MAP
|
|
|
|
*
|
|
|
|
* CS EVENT BUS
|
|
|
|
* (accessible within tab scripts)
|
|
|
|
* | NOT EVENT BUS
|
|
|
|
* PageInfo x (accessible within popup)
|
|
|
|
* x |
|
|
|
|
* : : x UWServer
|
|
|
|
* x CommsClient <---------------x CommsServer x
|
|
|
|
* | (Connect to popup)
|
|
|
|
* |
|
|
|
|
* x eventBus.sendToTunnel()
|
|
|
|
* <iframe tunnel>
|
|
|
|
* A
|
|
|
|
* |
|
|
|
|
* V
|
|
|
|
* x <iframe tunnel>
|
|
|
|
* |
|
|
|
|
* PlayerUIBase x
|
|
|
|
* : :
|
|
|
|
* |
|
|
|
|
* UI EVENT BUS
|
|
|
|
* (accessible within player UI)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
class CommsClient {
|
2021-03-06 02:06:46 +01:00
|
|
|
commsId: string;
|
|
|
|
|
|
|
|
logger: Logger;
|
|
|
|
settings: any; // sus?
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
eventBus: EventBus;
|
2021-03-06 02:06:46 +01:00
|
|
|
|
|
|
|
_listener: (m: any) => void;
|
|
|
|
port: any;
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
//#region lifecycle
|
|
|
|
constructor(name: string, logger: Logger, eventBus: EventBus) {
|
2021-03-06 02:06:46 +01:00
|
|
|
try {
|
2022-07-31 00:15:28 +02:00
|
|
|
this.logger = logger;
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
this.eventBus.setComms(this);
|
|
|
|
|
|
|
|
this.port = browser.runtime.connect(null, {name: name});
|
|
|
|
|
|
|
|
this.logger.onLogEnd(
|
|
|
|
(history) => {
|
|
|
|
this.logger.log('info', 'comms', 'Sending logging-stop-and-save to background script ...');
|
|
|
|
try {
|
|
|
|
this.port.postMessage({cmd: 'logging-stop-and-save', host: window.location.hostname, history})
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.log('error', 'comms', 'Failed to send message to background script. Error:', e);
|
|
|
|
}
|
2020-01-30 23:17:43 +01:00
|
|
|
}
|
2022-07-31 00:15:28 +02:00
|
|
|
);
|
2020-01-30 01:07:00 +01:00
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
this._listener = m => this.processReceivedMessage(m);
|
|
|
|
this.port.onMessage.addListener(this._listener);
|
2018-12-30 23:41:44 +01:00
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
this.commsId = (Math.random() * 20).toFixed(0);
|
2020-01-21 00:40:56 +01:00
|
|
|
|
2021-03-06 02:06:46 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error("CONSTRUCOTR FAILED:", e)
|
|
|
|
}
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
2022-07-31 00:15:28 +02:00
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
destroy() {
|
|
|
|
if (!BrowserDetect.edge) { // edge is a very special browser made by outright morons.
|
|
|
|
this.port.onMessage.removeListener(this._listener);
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 00:15:28 +02:00
|
|
|
//#endregion
|
2018-12-30 23:41:44 +01:00
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
async sendMessage(message){
|
2019-01-18 00:26:15 +01:00
|
|
|
message = JSON.parse(JSON.stringify(message)); // vue quirk. We should really use vue store instead
|
2021-03-06 02:06:46 +01:00
|
|
|
return browser.runtime.sendMessage(null, message, null);
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 00:15:28 +02:00
|
|
|
processReceivedMessage(message){
|
|
|
|
this.eventBus.send(message.command, message.config, {fromComms: true});
|
2018-12-30 23:41:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:20:29 +02:00
|
|
|
if (process.env.CHANNEL !== 'stable'){
|
2020-12-03 01:16:57 +01:00
|
|
|
console.info("CommsClient loaded");
|
2020-04-13 15:20:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:41:44 +01:00
|
|
|
export default CommsClient;
|