1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

code review

This commit is contained in:
gorhill 2015-12-02 00:59:51 -05:00
parent c32de1dfc4
commit 2705432f43
5 changed files with 51 additions and 64 deletions

View File

@ -1923,47 +1923,41 @@ var httpObserver = {
// Also: // Also:
// https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/Limitations_of_chrome_scripts // https://developer.mozilla.org/en-US/Firefox/Multiprocess_Firefox/Limitations_of_chrome_scripts
tabIdFromChannel: function(channel) { tabIdFromChannel: function(channel) {
var aWindow; var ncbs = channel.notificationCallbacks;
if ( channel.notificationCallbacks ) { if ( !ncbs && channel.loadGroup ) {
try { ncbs = channel.loadGroup.notificationCallbacks;
var loadContext = channel
.notificationCallbacks
.getInterface(Ci.nsILoadContext);
if ( loadContext.topFrameElement ) {
return tabWatcher.tabIdFromTarget(loadContext.topFrameElement);
}
aWindow = loadContext.associatedWindow;
} catch (ex) {
//console.error(ex.toString());
}
} }
var gBrowser; if ( !ncbs ) { return vAPI.noTabId; }
var lc;
try { try {
if ( !aWindow && channel.loadGroup && channel.loadGroup.notificationCallbacks ) { lc = ncbs.getInterface(Ci.nsILoadContext);
aWindow = channel } catch (ex) { }
.loadGroup if ( !lc ) { return vAPI.noTabId; }
.notificationCallbacks if ( lc.topFrameElement ) {
.getInterface(Ci.nsILoadContext) return tabWatcher.tabIdFromTarget(lc.topFrameElement);
.associatedWindow;
}
if ( aWindow ) {
gBrowser = aWindow
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow)
.gBrowser;
}
} catch (ex) {
//console.error(ex.toString());
} }
if ( !gBrowser || !gBrowser._getTabForContentWindow ) { var win = lc.associatedWindow;
return vAPI.noTabId; if ( !win ) { return vAPI.noTabId; }
if ( win.top ) {
win = win.top;
} }
// Using `_getTabForContentWindow` ensure older versions of Firefox var tabBrowser;
// work well. try {
return tabWatcher.tabIdFromTarget(gBrowser._getTabForContentWindow(aWindow)); tabBrowser = getTabBrowser(
win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell).rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow)
);
} catch (ex) { }
if ( !tabBrowser ) { return vAPI.noTabId; }
if ( tabBrowser.getBrowserForContentWindow ) {
return tabWatcher.tabIdFromTarget(tabBrowser.getBrowserForContentWindow(win));
}
// Falling back onto _getTabForContentWindow to ensure older versions
// of Firefox work well.
return tabBrowser._getTabForContentWindow ?
tabWatcher.tabIdFromTarget(tabBrowser._getTabForContentWindow(win)) :
vAPI.noTabId;
}, },
// https://github.com/gorhill/uBlock/issues/959 // https://github.com/gorhill/uBlock/issues/959

View File

@ -276,7 +276,7 @@ var getFirewallRules = function(srcHostname, desHostnames) {
/******************************************************************************/ /******************************************************************************/
var popupDataFromTabId = function(tabId, tabTitle) { var popupDataFromTabId = function(tabId, tabTitle) {
var tabContext = µb.tabContextManager.lookup(tabId); var tabContext = µb.tabContextManager.mustLookup(tabId);
var r = { var r = {
advancedUserEnabled: µb.userSettings.advancedUserEnabled, advancedUserEnabled: µb.userSettings.advancedUserEnabled,
appName: vAPI.app.name, appName: vAPI.app.name,

View File

@ -292,7 +292,7 @@ PageStore.factory = function(tabId) {
/******************************************************************************/ /******************************************************************************/
PageStore.prototype.init = function(tabId) { PageStore.prototype.init = function(tabId) {
var tabContext = µb.tabContextManager.lookup(tabId); var tabContext = µb.tabContextManager.mustLookup(tabId);
this.tabId = tabId; this.tabId = tabId;
this.tabHostname = tabContext.rootHostname; this.tabHostname = tabContext.rootHostname;
this.title = tabContext.rawURL; this.title = tabContext.rawURL;
@ -335,7 +335,7 @@ PageStore.prototype.reuse = function(context) {
// When force refreshing a page, the page store data needs to be reset. // When force refreshing a page, the page store data needs to be reset.
// If the hostname changes, we can't merely just update the context. // If the hostname changes, we can't merely just update the context.
var tabContext = µb.tabContextManager.lookup(this.tabId); var tabContext = µb.tabContextManager.mustLookup(this.tabId);
if ( tabContext.rootHostname !== this.tabHostname ) { if ( tabContext.rootHostname !== this.tabHostname ) {
context = ''; context = '';
} }
@ -441,7 +441,7 @@ PageStore.prototype.createContextFromFrameHostname = function(frameHostname) {
/******************************************************************************/ /******************************************************************************/
PageStore.prototype.getNetFilteringSwitch = function() { PageStore.prototype.getNetFilteringSwitch = function() {
return µb.tabContextManager.lookup(this.tabId).getNetFilteringSwitch(); return µb.tabContextManager.mustLookup(this.tabId).getNetFilteringSwitch();
}; };
/******************************************************************************/ /******************************************************************************/
@ -451,7 +451,7 @@ PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
return false; return false;
} }
var tabContext = µb.tabContextManager.lookup(this.tabId); var tabContext = µb.tabContextManager.mustLookup(this.tabId);
if ( µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) ) { if ( µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) ) {
return false; return false;

View File

@ -346,15 +346,15 @@ housekeep itself.
return entry; return entry;
}; };
// Find a tab context for a specific tab.
var lookup = function(tabId) {
return tabContexts[tabId] || null;
};
// Find a tab context for a specific tab. If none is found, attempt to // Find a tab context for a specific tab. If none is found, attempt to
// fix this. When all fail, the behind-the-scene context is returned. // fix this. When all fail, the behind-the-scene context is returned.
var lookup = function(tabId, url) { var mustLookup = function(tabId) {
var entry; var entry = tabContexts[tabId];
if ( url !== undefined ) {
entry = push(tabId, url);
} else {
entry = tabContexts[tabId];
}
if ( entry !== undefined ) { if ( entry !== undefined ) {
return entry; return entry;
} }
@ -426,6 +426,7 @@ housekeep itself.
push: push, push: push,
commit: commit, commit: commit,
lookup: lookup, lookup: lookup,
mustLookup: mustLookup,
exists: exists, exists: exists,
createContext: createContext createContext: createContext
}; };
@ -576,23 +577,15 @@ vAPI.tabs.onPopupUpdated = (function() {
return function(targetTabId, openerTabId) { return function(targetTabId, openerTabId) {
// Opener details. // Opener details.
var tabContext = µb.tabContextManager.lookup(openerTabId); var tabContext = µb.tabContextManager.lookup(openerTabId);
var openerURL = ''; if ( tabContext === null ) { return; }
if ( tabContext.tabId === openerTabId ) { var openerURL = tabContext.rawURL;
openerURL = tabContext.rawURL; if ( openerURL === '' ) { return; }
if ( openerURL === '' ) {
return;
}
}
// Popup details. // Popup details.
tabContext = µb.tabContextManager.lookup(targetTabId); tabContext = µb.tabContextManager.lookup(targetTabId);
var targetURL = ''; if ( tabContext === null ) { return; }
if ( tabContext.tabId === targetTabId ) { var targetURL = tabContext.rawURL;
targetURL = tabContext.rawURL; if ( targetURL === '' ) { return; }
if ( targetURL === '' ) {
return;
}
}
// https://github.com/gorhill/uBlock/issues/341 // https://github.com/gorhill/uBlock/issues/341
// Allow popups if uBlock is turned off in opener's context. // Allow popups if uBlock is turned off in opener's context.

View File

@ -60,7 +60,7 @@ var onBeforeRequest = function(details) {
var µb = µBlock; var µb = µBlock;
var pageStore = µb.pageStoreFromTabId(tabId); var pageStore = µb.pageStoreFromTabId(tabId);
if ( !pageStore ) { if ( !pageStore ) {
var tabContext = µb.tabContextManager.lookup(tabId); var tabContext = µb.tabContextManager.mustLookup(tabId);
if ( vAPI.isBehindTheSceneTabId(tabContext.tabId) ) { if ( vAPI.isBehindTheSceneTabId(tabContext.tabId) ) {
return onBeforeBehindTheSceneRequest(details); return onBeforeBehindTheSceneRequest(details);
} }