From 9169388849d8e9209bf751c185d7b1fe097df471 Mon Sep 17 00:00:00 2001 From: Deathamns Date: Sun, 28 Dec 2014 10:56:09 +0100 Subject: [PATCH] Firefox: ownsWeak=true for observers, +minor mods --- platform/firefox/frameModule.js | 37 ++++++++++-------- platform/firefox/vapi-background.js | 59 +++++++++++++++++++---------- platform/safari/vapi-background.js | 2 +- tools/make-safari.sh | 2 +- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/platform/firefox/frameModule.js b/platform/firefox/frameModule.js index 01f922bcb..3a09710db 100644 --- a/platform/firefox/frameModule.js +++ b/platform/firefox/frameModule.js @@ -98,7 +98,12 @@ const contentPolicy = { }, // https://bugzil.la/612921 shouldLoad: function(type, location, origin, context) { - if (!context || !/^https?$/.test(location.scheme)) { + // If we don't know what initiated the request, probably it's not a tab + if ( !context ) { + return this.ACCEPT; + } + + if ( location.scheme !== 'http' && location.scheme !== 'https' ) { return this.ACCEPT; } @@ -106,7 +111,7 @@ const contentPolicy = { ? context.contentWindow || context : (context.ownerDocument || context).defaultView; - if (win) { + if ( win ) { getMessageManager(win).sendSyncMessage(this.messageName, { url: location.spec, type: type, @@ -122,7 +127,11 @@ const contentPolicy = { /******************************************************************************/ const docObserver = { - contentBaseURI: 'chrome://' + appName + '/content/', + contentBaseURI: 'chrome://' + appName + '/content/js/', + QueryInterface: XPCOMUtils.generateQI([ + Ci.nsIObserver, + Ci.nsISupportsWeakReference + ]), initContext: function(win, sandbox) { let messager = getMessageManager(win); @@ -143,10 +152,7 @@ const docObserver = { return; } - Services.scriptloader.loadSubScript( - docObserver.contentBaseURI + script, - win - ); + Services.scriptloader.loadSubScript(script, win); }, win ); @@ -159,7 +165,7 @@ const docObserver = { return win; }, register: function() { - Services.obs.addObserver(this, 'document-element-inserted', false); + Services.obs.addObserver(this, 'document-element-inserted', true); }, unregister: function() { Services.obs.removeObserver(this, 'document-element-inserted'); @@ -171,9 +177,10 @@ const docObserver = { return; } - if (!/^https?:$/.test(win.location.protocol)) { - if (win.location.protocol === 'chrome:' - && win.location.host === appName) { + let loc = win.location; + + if (loc.protocol !== 'http:' && loc.protocol !== 'https:') { + if (loc.protocol === 'chrome:' && loc.host === appName) { this.initContext(win); } @@ -183,15 +190,15 @@ const docObserver = { let lss = Services.scriptloader.loadSubScript; win = this.initContext(win, true); - lss(this.contentBaseURI + 'js/vapi-client.js', win); - lss(this.contentBaseURI + 'js/contentscript-start.js', win); + lss(this.contentBaseURI + 'vapi-client.js', win); + lss(this.contentBaseURI + 'contentscript-start.js', win); let docReady = function(e) { this.removeEventListener(e.type, docReady, true); - lss(docObserver.contentBaseURI + 'js/contentscript-end.js', win); + lss(docObserver.contentBaseURI + 'contentscript-end.js', win); }; - doc.addEventListener('DOMContentLoaded', docReady, true); + win.document.addEventListener('DOMContentLoaded', docReady, true); } }; diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 150d61d06..56c94b2c4 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -321,7 +321,6 @@ vAPI.tabs.registerListeners = function() { // onClosed - handled in windowWatcher.onTabClose // onPopup ? - for (var win of this.getWindows()) { windowWatcher.onReady.call(win); } @@ -561,6 +560,10 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { return; } + if (details.file) { + details.file = vAPI.getURL(details.file); + } + tab.linkedBrowser.messageManager.sendAsyncMessage( location.host + ':broadcast', JSON.stringify({ @@ -580,7 +583,7 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { /******************************************************************************/ -vAPI.tabIcons = { /*tabId: {badge: 0, img: ''}*/ }; +vAPI.tabIcons = { /*tabId: {badge: 0, img: boolean}*/ }; vAPI.setIcon = function(tabId, iconStatus, badge) { // If badge is undefined, then setIcon was called from the TabSelect event var curWin = badge === undefined @@ -718,6 +721,11 @@ vAPI.toolbarButton.register = function(doc) { if (!this.styleURI) { this.styleURI = 'data:text/css,' + encodeURIComponent([ + '#' + this.widgetId + ' {', + 'list-style-image: url(', + vAPI.getURL('img/browsericons/icon16-off.svg'), + ');', + '}', '#' + this.widgetId + '[badge]:not([badge=""])::after {', 'position: absolute;', 'margin-left: -16px;', @@ -856,7 +864,7 @@ vAPI.messaging.setup = function(defaultHandler) { this.onMessage ); - this.globalMessageManager.loadFrameScript(vAPI.messaging.frameScript, true); + this.globalMessageManager.loadFrameScript(this.frameScript, true); vAPI.unload.push(function() { var gmm = vAPI.messaging.globalMessageManager; @@ -880,10 +888,6 @@ vAPI.messaging.broadcast = function(message) { /******************************************************************************/ -vAPI.net = {}; - -/******************************************************************************/ - var httpObserver = { ABORT: Components.results.NS_BINDING_ABORTED, lastRequest: { @@ -893,6 +897,23 @@ var httpObserver = { frameId: null, parentFrameId: null }, + QueryInterface: (function() { + var {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {}); + return XPCOMUtils.generateQI([ + Ci.nsIObserver, + Ci.nsISupportsWeakReference + ]); + })(), + register: function() { + Services.obs.addObserver(httpObserver, 'http-on-opening-request', true); + // Services.obs.addObserver(httpObserver, 'http-on-modify-request', true); + Services.obs.addObserver(httpObserver, 'http-on-examine-response', true); + }, + unregister: function() { + Services.obs.removeObserver(httpObserver, 'http-on-opening-request'); + // Services.obs.removeObserver(httpObserver, 'http-on-modify-request'); + Services.obs.removeObserver(httpObserver, 'http-on-examine-response'); + }, observe: function(httpChannel, topic) { // No need for QueryInterface if this check is performed? if (!(httpChannel instanceof Ci.nsIHttpChannel)) { @@ -994,14 +1015,18 @@ var httpObserver = { /******************************************************************************/ +vAPI.net = {}; + +/******************************************************************************/ + vAPI.net.registerListeners = function() { var typeMap = { - 2: 'script', - 3: 'image', - 4: 'stylesheet', - 5: 'object', - 6: 'main_frame', - 7: 'sub_frame', + 2: 'script', + 3: 'image', + 4: 'stylesheet', + 5: 'object', + 6: 'main_frame', + 7: 'sub_frame', 11: 'xmlhttprequest' }; @@ -1022,9 +1047,7 @@ vAPI.net.registerListeners = function() { shouldLoadListener ); - Services.obs.addObserver(httpObserver, 'http-on-opening-request', false); - // Services.obs.addObserver(httpObserver, 'http-on-modify-request', false); - Services.obs.addObserver(httpObserver, 'http-on-examine-response', false); + httpObserver.register(); vAPI.unload.push(function() { vAPI.messaging.globalMessageManager.removeMessageListener( @@ -1032,9 +1055,7 @@ vAPI.net.registerListeners = function() { shouldLoadListener ); - Services.obs.removeObserver(httpObserver, 'http-on-opening-request'); - // Services.obs.removeObserver(httpObserver, 'http-on-modify-request'); - Services.obs.removeObserver(httpObserver, 'http-on-examine-response'); + httpObserver.unregister(); }); }; diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 34187bf16..217f54b6b 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -427,7 +427,7 @@ safari.application.addEventListener('popover', function(e) { /******************************************************************************/ -vAPI.tabIcons = { /*tabId: {badge: 0, img: dict}*/ }; +vAPI.tabIcons = { /*tabId: {badge: 0, img: suffix}*/ }; vAPI.setIcon = function(tabId, iconStatus, badge) { var curTabId = vAPI.tabs.getTabId( safari.application.activeBrowserWindow.activeTab diff --git a/tools/make-safari.sh b/tools/make-safari.sh index 6fdc566fa..4e95e28f9 100755 --- a/tools/make-safari.sh +++ b/tools/make-safari.sh @@ -22,7 +22,7 @@ cp platform/safari/Info.plist $DES/ cp platform/safari/Settings.plist $DES/ cp LICENSE.txt $DES/ -echo "*** uBlock_xpi: Generating meta..." +echo "*** uBlock.safariextension: Generating meta..." python tools/make-safari-meta.py $DES/ echo "*** uBlock.safariextension: Package done."