diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2019-06-22 00:35:25 +0200 |
---|---|---|
committer | Alessio Vanni <vannilla@firemail.cc> | 2019-06-22 00:35:25 +0200 |
commit | 89a737419d54d31fb6d312f1ac87b4ff4f2de75a (patch) | |
tree | adb290a8818fcb7ae50a7d8fb8675083506b5f9b /js | |
parent | 50aff81e575d92fc3ac4a6194fffa7ea89344e2c (diff) | |
download | ematrix-89a737419d54d31fb6d312f1ac87b4ff4f2de75a.tar.lz ematrix-89a737419d54d31fb6d312f1ac87b4ff4f2de75a.tar.xz ematrix-89a737419d54d31fb6d312f1ac87b4ff4f2de75a.zip |
Put window management into its own file
Diffstat (limited to 'js')
-rw-r--r-- | js/vapi-background.js | 245 | ||||
-rw-r--r-- | js/vapi-window.js | 191 |
2 files changed, 198 insertions, 238 deletions
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); } diff --git a/js/vapi-window.js b/js/vapi-window.js new file mode 100644 index 0000000..ff0e9fe --- /dev/null +++ b/js/vapi-window.js @@ -0,0 +1,191 @@ +/******************************************************************************* + + ηMatrix - a browser extension to black/white list requests. + Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors + Copyright (C) 2019 Alessio Vanni + + 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 {http://www.gnu.org/licenses/}. + + Home: https://gitlab.com/vannilla/ematrix + uMatrix Home: https://github.com/gorhill/uMatrix +*/ + +/* global self, Components */ + +// For background page (windows management) +'use strict'; + +/******************************************************************************/ + +(function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + + let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js + + vAPI.window = (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; + })(); +})(); |