queue popup requests on ios

This commit is contained in:
ivan grachev 2023-10-25 16:48:58 +03:00
parent 547ab3549b
commit a3ba43b905
4 changed files with 74 additions and 4 deletions

View File

@ -1,17 +1,26 @@
// Copyright © 2022 Tokenary. All rights reserved.
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.subject === "message-to-wallet") {
if (request.subject === "POPUP_DID_PROCEED" && request.id === pendingPopupId) {
pendingPopupId = null;
pendingPopupRequest = null;
sendPopupCancelResponse = null;
} else if (request.subject === "message-to-wallet") {
if (isMobile) {
popupQueue.push({pendingPopupRequest: request.message, sendPopupCancelResponse: sendResponse});
processPopupQueue();
}
sendNativeMessage(request, sender, sendResponse);
} else if (request.subject === "getResponse") {
browser.runtime.sendNativeMessage("mac.tokenary.io", request, function(response) {
sendResponse(response);
storeConfigurationIfNeeded(request.host, response);
if (isMobile) {
setTimeout( function() { processPopupQueue(); }, 500);
}
});
} else if (request.subject === "getLatestConfiguration") {
getLatestConfiguration(request.host, sendResponse);
} else if (request.subject === "cancelRequest") {
browser.runtime.sendNativeMessage("mac.tokenary.io", request);
} else if (request.subject === "disconnect") {
const provider = request.provider;
const host = request.host;
@ -44,6 +53,9 @@ function sendNativeMessage(request, sender, sendResponse) {
sendResponse(response);
didCompleteRequest(request.message.id, sender.tab.id);
storeConfigurationIfNeeded(request.host, response);
if (isMobile) {
setTimeout( function() { processPopupQueue(); }, 500);
}
});
}
@ -154,6 +166,60 @@ function genId() {
return new Date().getTime() + Math.floor(Math.random() * 1000);
}
// MARK: - iOS extension popup
var pendingPopupRequest = null;
var pendingPopupId = null;
var sendPopupCancelResponse = null;
var popupQueue = [];
function processPopupQueue() {
if (popupQueue.length && pendingPopupId == null && !hasVisiblePopup()) {
const next = popupQueue[0];
popupQueue.shift();
pendingPopupRequest = next.pendingPopupRequest;
const id = pendingPopupRequest.id;
pendingPopupId = id;
sendPopupCancelResponse = next.sendPopupCancelResponse;
browser.browserAction.openPopup();
setTimeout( function() { pollPopupStatus(id); }, 1000);
}
}
function pollPopupStatus(id) {
if (hasVisiblePopup() && pendingPopupId === id) {
setTimeout( function() { pollPopupStatus(id); }, 1000);
} else if (pendingPopupId === id) {
pendingPopupId = null;
didDismissPopup();
}
}
function didDismissPopup() {
cancelPopupRequest(pendingPopupRequest, sendPopupCancelResponse);
pendingPopupRequest = null;
sendPopupCancelResponse = null;
if (popupQueue.length) {
for (let item of popupQueue) {
cancelPopupRequest(item.pendingPopupRequest, item.sendPopupCancelResponse);
}
popupQueue = [];
}
}
function cancelPopupRequest(request, sendResponse) {
const cancelResponse = {
id: request.id,
provider: request.provider,
name: request.name,
error: "canceled",
subject: "cancelRequest",
};
browser.runtime.sendNativeMessage("mac.tokenary.io", cancelResponse);
sendResponse(cancelResponse);
}
function hasVisiblePopup() {
const popup = browser.extension.getViews({ type: 'popup' });
if (popup.length === 0) {

View File

@ -1,5 +1,7 @@
// Copyright © 2022 Tokenary. All rights reserved.
const isMobile = true;
function didCompleteRequest(id, tabId) {
browser.tabs.update(tabId, { active: true });
const request = {id: id, subject: "didCompleteRequest"};

View File

@ -13,7 +13,7 @@ button.addEventListener('click', () => {
window.close();
}, 200);
browser.runtime.sendMessage({subject: 'POPUP_DID_PROCEED'});
browser.runtime.sendMessage({subject: 'POPUP_DID_PROCEED', id: message.id});
});
var title = "proceed\nin tokenary";

View File

@ -1,5 +1,7 @@
// Copyright © 2022 Tokenary. All rights reserved.
const isMobile = false;
function didCompleteRequest(id, tabId) {
}