2020-05-23 17:34:55 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-05-23 17:34:55 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CrossFrameAPIPort extends EventDispatcher {
|
2020-07-18 18:16:35 +00:00
|
|
|
constructor(otherTabId, otherFrameId, port, messageHandlers) {
|
2020-05-23 17:34:55 +00:00
|
|
|
super();
|
2020-07-18 18:16:35 +00:00
|
|
|
this._otherTabId = otherTabId;
|
2020-05-23 17:34:55 +00:00
|
|
|
this._otherFrameId = otherFrameId;
|
|
|
|
this._port = port;
|
|
|
|
this._messageHandlers = messageHandlers;
|
|
|
|
this._activeInvocations = new Map();
|
|
|
|
this._invocationId = 0;
|
|
|
|
this._eventListeners = new EventListenerCollection();
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:16:35 +00:00
|
|
|
get otherTabId() {
|
|
|
|
return this._otherTabId;
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:34:55 +00:00
|
|
|
get otherFrameId() {
|
|
|
|
return this._otherFrameId;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare() {
|
|
|
|
this._eventListeners.addListener(this._port.onDisconnect, this._onDisconnect.bind(this));
|
|
|
|
this._eventListeners.addListener(this._port.onMessage, this._onMessage.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
invoke(action, params, ackTimeout, responseTimeout) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this._port === null) {
|
2021-01-25 23:44:49 +00:00
|
|
|
reject(new Error(`Port is disconnected (${action})`));
|
2020-05-23 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = this._invocationId++;
|
2021-01-25 23:44:49 +00:00
|
|
|
const invocation = {
|
|
|
|
id,
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
responseTimeout,
|
|
|
|
action,
|
|
|
|
ack: false,
|
|
|
|
timer: null
|
|
|
|
};
|
2020-05-23 17:34:55 +00:00
|
|
|
this._activeInvocations.set(id, invocation);
|
|
|
|
|
|
|
|
if (ackTimeout !== null) {
|
|
|
|
try {
|
2021-01-25 23:44:49 +00:00
|
|
|
invocation.timer = setTimeout(() => this._onError(id, 'Acknowledgement timeout'), ackTimeout);
|
2020-05-23 17:34:55 +00:00
|
|
|
} catch (e) {
|
2021-01-25 23:44:49 +00:00
|
|
|
this._onError(id, 'Failed to set timeout');
|
2020-05-23 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._port.postMessage({type: 'invoke', id, data: {action, params}});
|
|
|
|
} catch (e) {
|
|
|
|
this._onError(id, e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this._onDisconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_onDisconnect() {
|
|
|
|
if (this._port === null) { return; }
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
this._port = null;
|
|
|
|
for (const id of this._activeInvocations.keys()) {
|
2021-01-25 23:44:49 +00:00
|
|
|
this._onError(id, 'Disconnected');
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
this.trigger('disconnect', this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessage({type, id, data}) {
|
|
|
|
switch (type) {
|
|
|
|
case 'invoke':
|
|
|
|
this._onInvoke(id, data);
|
|
|
|
break;
|
|
|
|
case 'ack':
|
|
|
|
this._onAck(id);
|
|
|
|
break;
|
|
|
|
case 'result':
|
|
|
|
this._onResult(id, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response handlers
|
|
|
|
|
|
|
|
_onAck(id) {
|
|
|
|
const invocation = this._activeInvocations.get(id);
|
|
|
|
if (typeof invocation === 'undefined') {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.warn(new Error(`Request ${id} not found for acknowledgement`));
|
2020-05-23 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invocation.ack) {
|
2021-01-25 23:44:49 +00:00
|
|
|
this._onError(id, `Request ${id} already acknowledged`);
|
2020-05-23 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
invocation.ack = true;
|
|
|
|
|
|
|
|
if (invocation.timer !== null) {
|
|
|
|
clearTimeout(invocation.timer);
|
|
|
|
invocation.timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseTimeout = invocation.responseTimeout;
|
|
|
|
if (responseTimeout !== null) {
|
|
|
|
try {
|
2021-01-25 23:44:49 +00:00
|
|
|
invocation.timer = setTimeout(() => this._onError(id, 'Response timeout'), responseTimeout);
|
2020-05-23 17:34:55 +00:00
|
|
|
} catch (e) {
|
2021-01-25 23:44:49 +00:00
|
|
|
this._onError(id, 'Failed to set timeout');
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onResult(id, data) {
|
|
|
|
const invocation = this._activeInvocations.get(id);
|
|
|
|
if (typeof invocation === 'undefined') {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.warn(new Error(`Request ${id} not found`));
|
2020-05-23 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!invocation.ack) {
|
2021-01-25 23:44:49 +00:00
|
|
|
this._onError(id, `Request ${id} not acknowledged`);
|
2020-05-23 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._activeInvocations.delete(id);
|
|
|
|
|
|
|
|
if (invocation.timer !== null) {
|
|
|
|
clearTimeout(invocation.timer);
|
|
|
|
invocation.timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const error = data.error;
|
|
|
|
if (typeof error !== 'undefined') {
|
2021-01-08 02:36:20 +00:00
|
|
|
invocation.reject(deserializeError(error));
|
2020-05-23 17:34:55 +00:00
|
|
|
} else {
|
|
|
|
invocation.resolve(data.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onError(id, error) {
|
|
|
|
const invocation = this._activeInvocations.get(id);
|
|
|
|
if (typeof invocation === 'undefined') { return; }
|
|
|
|
|
2021-01-25 23:44:49 +00:00
|
|
|
if (typeof error === 'string') {
|
|
|
|
error = new Error(`${error} (${invocation.action})`);
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:34:55 +00:00
|
|
|
this._activeInvocations.delete(id);
|
|
|
|
if (invocation.timer !== null) {
|
|
|
|
clearTimeout(invocation.timer);
|
|
|
|
invocation.timer = null;
|
|
|
|
}
|
|
|
|
invocation.reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invocation
|
|
|
|
|
|
|
|
_onInvoke(id, {action, params}) {
|
|
|
|
const messageHandler = this._messageHandlers.get(action);
|
2020-09-04 21:59:38 +00:00
|
|
|
this._sendAck(id);
|
2020-05-23 17:34:55 +00:00
|
|
|
if (typeof messageHandler === 'undefined') {
|
2020-09-04 21:59:38 +00:00
|
|
|
this._sendError(id, new Error(`Unknown action: ${action}`));
|
2020-07-11 19:20:00 +00:00
|
|
|
return false;
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
2020-09-04 21:59:38 +00:00
|
|
|
|
|
|
|
const callback = (data) => this._sendResult(id, data);
|
2021-02-14 23:18:02 +00:00
|
|
|
return invokeMessageHandler(messageHandler, params, callback);
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_sendResponse(data) {
|
|
|
|
if (this._port === null) { return; }
|
|
|
|
try {
|
|
|
|
this._port.postMessage(data);
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendAck(id) {
|
|
|
|
this._sendResponse({type: 'ack', id});
|
|
|
|
}
|
|
|
|
|
2020-09-04 21:59:38 +00:00
|
|
|
_sendResult(id, data) {
|
|
|
|
this._sendResponse({type: 'result', id, data});
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_sendError(id, error) {
|
2021-01-08 02:36:20 +00:00
|
|
|
this._sendResponse({type: 'result', id, data: {error: serializeError(error)}});
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CrossFrameAPI {
|
|
|
|
constructor() {
|
|
|
|
this._ackTimeout = 3000; // 3 seconds
|
|
|
|
this._responseTimeout = 10000; // 10 seconds
|
|
|
|
this._commPorts = new Map();
|
|
|
|
this._messageHandlers = new Map();
|
|
|
|
this._onDisconnectBind = this._onDisconnect.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare() {
|
|
|
|
chrome.runtime.onConnect.addListener(this._onConnect.bind(this));
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:16:35 +00:00
|
|
|
invoke(targetFrameId, action, params={}) {
|
|
|
|
return this.invokeTab(null, targetFrameId, action, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
async invokeTab(targetTabId, targetFrameId, action, params={}) {
|
2020-07-19 16:26:57 +00:00
|
|
|
if (typeof targetTabId !== 'number') { targetTabId = null; }
|
2020-07-18 18:16:35 +00:00
|
|
|
const commPort = this._getOrCreateCommPort(targetTabId, targetFrameId);
|
2020-05-23 17:34:55 +00:00
|
|
|
return await commPort.invoke(action, params, this._ackTimeout, this._responseTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerHandlers(messageHandlers) {
|
|
|
|
for (const [key, value] of messageHandlers) {
|
|
|
|
if (this._messageHandlers.has(key)) {
|
|
|
|
throw new Error(`Handler ${key} is already registered`);
|
|
|
|
}
|
|
|
|
this._messageHandlers.set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:19:55 +00:00
|
|
|
unregisterHandler(key) {
|
|
|
|
return this._messageHandlers.delete(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2020-05-23 17:34:55 +00:00
|
|
|
_onConnect(port) {
|
2020-07-18 18:16:35 +00:00
|
|
|
try {
|
|
|
|
let details;
|
|
|
|
try {
|
|
|
|
details = JSON.parse(port.name);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (details.name !== 'cross-frame-communication-port') { return; }
|
2020-05-23 17:34:55 +00:00
|
|
|
|
2020-07-18 18:16:35 +00:00
|
|
|
const otherTabId = details.sourceTabId;
|
|
|
|
const otherFrameId = details.sourceFrameId;
|
|
|
|
this._setupCommPort(otherTabId, otherFrameId, port);
|
|
|
|
} catch (e) {
|
|
|
|
port.disconnect();
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(e);
|
2020-07-18 18:16:35 +00:00
|
|
|
}
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDisconnect(commPort) {
|
|
|
|
commPort.off('disconnect', this._onDisconnectBind);
|
2020-07-18 18:16:35 +00:00
|
|
|
const {otherTabId, otherFrameId} = commPort;
|
|
|
|
const tabPorts = this._commPorts.get(otherTabId);
|
|
|
|
if (typeof tabPorts !== 'undefined') {
|
|
|
|
tabPorts.delete(otherFrameId);
|
|
|
|
if (tabPorts.size === 0) {
|
|
|
|
this._commPorts.delete(otherTabId);
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:16:35 +00:00
|
|
|
_getOrCreateCommPort(otherTabId, otherFrameId) {
|
|
|
|
const tabPorts = this._commPorts.get(otherTabId);
|
|
|
|
if (typeof tabPorts !== 'undefined') {
|
|
|
|
const commPort = tabPorts.get(otherFrameId);
|
|
|
|
if (typeof commPort !== 'undefined') {
|
|
|
|
return commPort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._createCommPort(otherTabId, otherFrameId);
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:16:35 +00:00
|
|
|
_createCommPort(otherTabId, otherFrameId) {
|
|
|
|
const details = {
|
|
|
|
name: 'background-cross-frame-communication-port',
|
|
|
|
targetTabId: otherTabId,
|
|
|
|
targetFrameId: otherFrameId
|
|
|
|
};
|
|
|
|
const port = yomichan.connect(null, {name: JSON.stringify(details)});
|
|
|
|
return this._setupCommPort(otherTabId, otherFrameId, port);
|
2020-05-23 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:16:35 +00:00
|
|
|
_setupCommPort(otherTabId, otherFrameId, port) {
|
|
|
|
const commPort = new CrossFrameAPIPort(otherTabId, otherFrameId, port, this._messageHandlers);
|
|
|
|
let tabPorts = this._commPorts.get(otherTabId);
|
|
|
|
if (typeof tabPorts === 'undefined') {
|
|
|
|
tabPorts = new Map();
|
|
|
|
this._commPorts.set(otherTabId, tabPorts);
|
|
|
|
}
|
|
|
|
tabPorts.set(otherFrameId, commPort);
|
2020-05-23 17:34:55 +00:00
|
|
|
commPort.prepare();
|
|
|
|
commPort.on('disconnect', this._onDisconnectBind);
|
|
|
|
return commPort;
|
|
|
|
}
|
|
|
|
}
|