From 89a737419d54d31fb6d312f1ac87b4ff4f2de75a Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sat, 22 Jun 2019 00:35:25 +0200 Subject: Put window management into its own file --- js/vapi-background.js | 245 ++------------------------------------------------ 1 file changed, 7 insertions(+), 238 deletions(-) (limited to 'js/vapi-background.js') diff --git a/js/vapi-background.js b/js/vapi-background.js index a809b48..d03a205 100644 --- a/js/vapi-background.js +++ b/js/vapi-background.js @@ -57,79 +57,6 @@ }, }; - - // List of things that needs to be destroyed when disabling the extension - // Only functions should be added to it - // eMatrix: taken care by vAPI.addCleanUpTask --- use that function - let cleanupTasks = []; - - // This must be updated manually, every time a new task is added/removed - // eMatrix: do we? - let expectedNumberOfCleanups = 7; - - vAPI.addCleanUpTask = function (task) { - if (typeof task !== 'function') { - return; - } - - cleanupTasks.push(task); - } - - vAPI.deferUntil = function (testFn, mainFn, details) { - let dtls = (typeof details !== 'object') ? {} : details; - let now = 0; - let next = dtls.next || 200; - let until = dtls.until || 2000; - - let check = function () { - if (testFn() === true || now >= until) { - mainFn(); - return; - } - now += next; - vAPI.setTimeout(check, next); - }; - - if ('sync' in dtls && dtls.sync === true) { - check(); - } else { - vAPI.setTimeout(check, 1); - } - }; - - window.addEventListener('unload', function () { - // if (typeof vAPI.app.onShutdown === 'function') { - // vAPI.app.onShutdown(); - // } - - // IMPORTANT: cleanup tasks must be executed using LIFO order. - for (let i=cleanupTasks.length-1; i>=0; --i) { - try { - cleanupTasks[i](); - } catch (e) { - // Just in case a clean up task ends up throwing for - // no reason - console.error(e); - } - } - - // eMatrix: temporarily disabled - // if (cleanupTasks.length < expectedNumberOfCleanups) { - // console.error - // ('eMatrix> Cleanup tasks performed: %s (out of %s)', - // cleanupTasks.length, - // expectedNumberOfCleanups); - // } - - // frameModule needs to be cleared too - let frameModuleURL = vAPI.getURL('frameModule.js'); - let frameModule = {}; - - Cu.import(frameModuleURL, frameModule); - frameModule.contentObserver.unregister(); - Cu.unload(frameModuleURL); - }); - vAPI.browserSettings = { // For now, only booleans. originalValues: {}, @@ -606,164 +533,6 @@ vAPI.cacheStorage = vAPI.storage; - // This must be executed/setup early. - let winWatcher = (function () { - let windowToIdMap = new Map(); - let windowIdGenerator = 1; - let api = { - onOpenWindow: null, - onCloseWindow: null - }; - - // https://github.com/gorhill/uMatrix/issues/586 This is - // necessary hack because on SeaMonkey 2.40, for unknown - // reasons private windows do not have the attribute - // `windowtype` set to `navigator:browser`. As a fallback, the - // code here will also test whether the id attribute is - // `main-window`. - api.toBrowserWindow = function (win) { - let docElement = win && win.document - && win.document.documentElement; - - if (!docElement) { - return null; - } - if (vAPI.thunderbird) { - return docElement.getAttribute('windowtype') === 'mail:3pane' - ? win - : null; - } - - return docElement.getAttribute('windowtype') === 'navigator:browser' - || docElement.getAttribute('id') === 'main-window' - ? win - : null; - }; - - api.getWindows = function () { - return windowToIdMap.keys(); - }; - - api.idFromWindow = function (win) { - return windowToIdMap.get(win) || 0; - }; - - api.getCurrentWindow = function () { - return this.toBrowserWindow(Services.wm.getMostRecentWindow(null)); - }; - - let addWindow = function (win) { - if (!win || windowToIdMap.has(win)) { - return; - } - - windowToIdMap.set(win, windowIdGenerator++); - - if (typeof api.onOpenWindow === 'function') { - api.onOpenWindow(win); - } - }; - - let removeWindow = function (win) { - if (!win || windowToIdMap.delete(win) !== true) { - return; - } - - if (typeof api.onCloseWindow === 'function') { - api.onCloseWindow(win); - } - }; - - // https://github.com/gorhill/uMatrix/issues/357 - // Use nsIWindowMediator for being notified of opened/closed windows. - let listeners = { - onOpenWindow: function (aWindow) { - let win; - try { - win = aWindow.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow); - } catch (e) { - // Ignore - } - - addWindow(win); - }, - onCloseWindow: function (aWindow) { - let win; - try { - win = aWindow.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow); - } catch (e) { - // Ignore - } - - removeWindow(win); - }, - observe: function (aSubject, topic) { - let win; - try { - win = aSubject.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow); - } catch (e) { - // Ignore - } - - if (!win) { - return; - } - - switch (topic) { - case 'domwindowopened': - addWindow(win); - break; - case 'domwindowclosed': - removeWindow(win); - break; - default: - console.error('unknown observer topic'); - break; - } - } - }; - - (function() { - let winumerator; - - winumerator = Services.wm.getEnumerator(null); - while (winumerator.hasMoreElements()) { - let win = winumerator.getNext(); - - if (!win.closed) { - windowToIdMap.set(win, windowIdGenerator++); - } - } - - winumerator = Services.ww.getWindowEnumerator(); - while (winumerator.hasMoreElements()) { - let win = winumerator.getNext() - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow); - - if (!win.closed) { - windowToIdMap.set(win, windowIdGenerator++); - } - } - - Services.wm.addListener(listeners); - Services.ww.registerNotification(listeners); - })(); - - vAPI.addCleanUpTask(function() { - Services.wm.removeListener(listeners); - Services.ww.unregisterNotification(listeners); - windowToIdMap.clear(); - }); - - return api; - })(); - - vAPI.window = winWatcher; - // browser-handling utilities vAPI.browser = {}; @@ -793,7 +562,7 @@ if (badge === undefined) { win = iconId; } else { - win = winWatcher.getCurrentWindow(); + win = vAPI.window.getCurrentWindow(); } let tabBrowser = vAPI.browser.getTabBrowser(win); @@ -1949,7 +1718,7 @@ }; let shutdown = function () { - for (let win of winWatcher.getWindows()) { + for (let win of vAPI.window.getWindows()) { let toolbarButton = win.document.getElementById(tbb.id); if (toolbarButton) { toolbarButton.parentNode.removeChild(toolbarButton); @@ -2024,7 +1793,7 @@ let badgeCSSRules = 'background: #000;color: #fff'; let updateBadgeStyle = function () { - for (let win of winWatcher.getWindows()) { + for (let win of vAPI.window.getWindows()) { let button = win.document.getElementById(tbb.id); if (button === null) { continue; @@ -2047,7 +1816,7 @@ CustomizableUI.getWidget(wId).areaType === CustomizableUI.TYPE_MENU_PANEL; - for (let win of winWatcher.getWindows()) { + for (let win of vAPI.window.getWindows()) { let button = win.document.getElementById(wId); if (button === null) { continue; @@ -2080,7 +1849,7 @@ }; let shutdown = function () { - for (let win of winWatcher.getWindows()) { + for (let win of vAPI.window.getWindows()) { let panel = win.document.getElementById(tbb.viewId); if (panel !== null && panel.parentNode !== null) { panel.parentNode.removeChild(panel); @@ -2360,13 +2129,13 @@ }); }; - for (let win of winWatcher.getWindows()) { + for (let win of vAPI.window.getWindows()) { this.register(win.document); } }; vAPI.contextMenu.remove = function () { - for (let win of winWatcher.getWindows()) { + for (let win of vAPI.window.getWindows()) { this.unregister(win.document); } -- cgit v1.2.3