Skip to content

Commit

Permalink
fix #3546, #3428
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Feb 26, 2018
1 parent 64682ab commit 2c90158
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 188 deletions.
45 changes: 25 additions & 20 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,29 @@ vAPI.tabs = {};

/******************************************************************************/

// https://github.com/gorhill/uBlock/issues/3546
// Added a new flavor of behind-the-scene tab id: vAPI.anyTabId.
// vAPI.anyTabId will be used for network requests which can be filtered,
// because they comes with enough contextual information. It's just not
// possible to pinpoint exactly from which tab it comes from. For example,
// with Firefox/webext, the `documentUrl` property is available for every
// network requests.

vAPI.isBehindTheSceneTabId = function(tabId) {
return tabId.toString() === '-1';
if ( typeof tabId === 'string' ) { debugger; }
return tabId < 0;
};

vAPI.noTabId = '-1';
vAPI.unsetTabId = 0;
vAPI.noTabId = -1; // definitely not any existing tab
vAPI.anyTabId = -2; // one of the existing tab

/******************************************************************************/

// To remove when tabId-as-integer has been tested enough.

var toChromiumTabId = function(tabId) {
if ( typeof tabId === 'string' ) {
tabId = parseInt(tabId, 10);
}
if ( typeof tabId === 'string' ) { debugger; }
if ( typeof tabId !== 'number' || isNaN(tabId) || tabId === -1 ) {
return 0;
}
Expand Down Expand Up @@ -329,8 +340,8 @@ vAPI.tabs.registerListeners = function() {
}
if ( typeof vAPI.tabs.onPopupCreated === 'function' ) {
vAPI.tabs.onPopupCreated(
details.tabId.toString(),
details.sourceTabId.toString()
details.tabId,
details.sourceTabId
);
}
};
Expand Down Expand Up @@ -364,7 +375,7 @@ vAPI.tabs.registerListeners = function() {
if ( changeInfo.url ) {
changeInfo.url = sanitizeURL(changeInfo.url);
}
onUpdatedClient(tabId.toString(), changeInfo, tab);
onUpdatedClient(tabId, changeInfo, tab);
};

chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate);
Expand Down Expand Up @@ -542,9 +553,7 @@ vAPI.tabs.open = function(details) {

vAPI.tabs.replace = function(tabId, url) {
tabId = toChromiumTabId(tabId);
if ( tabId === 0 ) {
return;
}
if ( tabId === 0 ) { return; }

var targetURL = url;

Expand All @@ -565,9 +574,7 @@ vAPI.tabs.replace = function(tabId, url) {

vAPI.tabs.remove = function(tabId) {
tabId = toChromiumTabId(tabId);
if ( tabId === 0 ) {
return;
}
if ( tabId === 0 ) { return; }

var onTabRemoved = function() {
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
Expand Down Expand Up @@ -605,9 +612,7 @@ vAPI.tabs.reload = function(tabId, bypassCache) {

vAPI.tabs.select = function(tabId) {
tabId = toChromiumTabId(tabId);
if ( tabId === 0 ) {
return;
}
if ( tabId === 0 ) { return; }

chrome.tabs.update(tabId, { active: true }, function(tab) {
if ( chrome.runtime.lastError ) {
Expand Down Expand Up @@ -789,15 +794,15 @@ vAPI.messaging.onPortMessage = (function() {
case 'connectionRefused':
toPort = messaging.ports.get(msg.fromToken);
if ( toPort !== undefined ) {
msg.tabId = tabId && tabId.toString();
msg.tabId = tabId;
toPort.postMessage(request);
} else {
msg.what = 'connectionBroken';
port.postMessage(request);
}
break;
case 'connectionRequested':
msg.tabId = tabId && tabId.toString();
msg.tabId = tabId;
for ( toPort of messaging.ports.values() ) {
toPort.postMessage(request);
}
Expand All @@ -809,7 +814,7 @@ vAPI.messaging.onPortMessage = (function() {
port.name === msg.fromToken ? msg.toToken : msg.fromToken
);
if ( toPort !== undefined ) {
msg.tabId = tabId && tabId.toString();
msg.tabId = tabId;
toPort.postMessage(request);
} else {
msg.what = 'connectionBroken';
Expand Down
4 changes: 1 addition & 3 deletions platform/chromium/vapi-webrequest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2017 Raymond Hill
Copyright (C) 2017-2018 Raymond Hill
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
Expand Down Expand Up @@ -105,8 +105,6 @@ vAPI.net.registerListeners = function() {
};

var normalizeRequestDetails = function(details) {
details.tabId = details.tabId.toString();

var type = details.type;

// https://github.com/gorhill/uBlock/issues/1493
Expand Down
26 changes: 13 additions & 13 deletions platform/firefox/vapi-background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2107 The uBlock Origin authors
Copyright (C) 2014-2018 The uBlock Origin authors
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
Expand Down Expand Up @@ -833,10 +833,11 @@ var getOwnerWindow = function(target) {
/******************************************************************************/

vAPI.isBehindTheSceneTabId = function(tabId) {
return tabId.toString() === '-1';
return tabId < 0;
};

vAPI.noTabId = '-1';
vAPI.noTabId = -1;
vAPI.anyTabId = -2;

/******************************************************************************/

Expand Down Expand Up @@ -1283,7 +1284,7 @@ var tabWatcher = (function() {
}
var tabId = browserToTabIdMap.get(browser);
if ( tabId === undefined ) {
tabId = '' + tabIdGenerator++;
tabId = tabIdGenerator++;
browserToTabIdMap.set(browser, tabId);
tabIdToBrowserMap.set(tabId, Cu.getWeakReference(browser));
}
Expand Down Expand Up @@ -1316,7 +1317,7 @@ var tabWatcher = (function() {
var removeBrowserEntry = function(tabId, browser) {
if ( tabId && tabId !== vAPI.noTabId ) {
vAPI.tabs.onClosed(tabId);
delete vAPI.toolbarButton.tabs[tabId];
vAPI.toolbarButton.tabs.delete(tabId);
tabIdToBrowserMap.delete(tabId);
}
if ( browser ) {
Expand Down Expand Up @@ -1539,7 +1540,7 @@ vAPI.setIcon = function(tabId, iconStatus, badge) {
if ( tabId === undefined ) {
tabId = curTabId;
} else if ( badge !== undefined ) {
tb.tabs[tabId] = { badge: badge, img: iconStatus === 'on' };
tb.tabs.set(tabId, { badge: badge, img: iconStatus === 'on' });
}

if ( curTabId && tabId === curTabId ) {
Expand Down Expand Up @@ -2028,7 +2029,7 @@ var httpObserver = {
},

// https://github.com/gorhill/uBlock/issues/959
syntheticPendingRequest: { frameId: 0, parentFrameId: -1, tabId: '', rawtype: 1 },
syntheticPendingRequest: { frameId: 0, parentFrameId: -1, tabId: 0, rawtype: 1 },

handleRequest: function(channel, URI, details) {
var type = this.typeMap[details.rawtype] || 'other';
Expand Down Expand Up @@ -2437,7 +2438,7 @@ vAPI.toolbarButton = {
viewId: location.host + '-panel',
label: vAPI.app.name,
tooltiptext: vAPI.app.name,
tabs: {/*tabId: {badge: 0, img: boolean}*/},
tabs: new Map(/* tabId: { badge: 0, img: boolean } */),
init: null,
codePath: ''
};
Expand Down Expand Up @@ -2473,8 +2474,8 @@ vAPI.toolbarButton = {
if ( tabId === undefined ) {
return label;
}
var tabDetails = this.tabs[tabId];
if ( !tabDetails ) {
var tabDetails = this.tabs.get(tabId);
if ( tabDetails === undefined ) {
return label;
}
if ( !tabDetails.img ) {
Expand Down Expand Up @@ -2563,7 +2564,7 @@ vAPI.toolbarButton = {
return;
}

var icon = this.tabs[tabId];
var icon = this.tabs.get(tabId);

button.setAttribute('badge', icon && icon.badge || '');
button.classList.toggle('off', !icon || !icon.img);
Expand Down Expand Up @@ -3502,9 +3503,8 @@ vAPI.onLoadAllCompleted = function() {
// TODO: vAPI shouldn't know about uBlock. Just like in uMatrix, uBlock
// should collect on its side all the opened tabs whenever it is ready.
var µb = µBlock;
var tabId;
for ( var browser of tabWatcher.browsers() ) {
tabId = tabWatcher.tabIdFromTarget(browser);
var tabId = tabWatcher.tabIdFromTarget(browser);
µb.tabContextManager.commit(tabId, browser.currentURI.asciiSpec);
µb.bindTabToPageStats(tabId);
}
Expand Down
10 changes: 8 additions & 2 deletions platform/webext/vapi-webrequest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2017 Raymond Hill
Copyright (C) 2017-2018 Raymond Hill
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
Expand Down Expand Up @@ -100,10 +100,16 @@ vAPI.net.registerListeners = function() {

var punycode = self.punycode;
var reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
var reNetworkURI = /^(?:ftps?|https?|wss?)/;
var parsedURL = new URL('about:blank');

var normalizeRequestDetails = function(details) {
details.tabId = details.tabId.toString();
if (
details.tabId === vAPI.noTabId &&
reNetworkURI.test(details.documentUrl)
) {
details.tabId = vAPI.anyTabId;
}

if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
parsedURL.href = details.url;
Expand Down
2 changes: 1 addition & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var µBlock = (function() { // jshint ignore:line

selfieAfter: 17 * oneMinute,

pageStores: {},
pageStores: new Map(),
pageStoresToken: 0,

storageQuota: vAPI.storage.QUOTA_BYTES,
Expand Down
14 changes: 7 additions & 7 deletions src/js/logger-ui-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if (

var logger = self.logger;
var inspectorConnectionId;
var inspectedTabId = '';
var inspectedTabId = 0;
var inspectedURL = '';
var inspectedHostname = '';
var inspector = uDom.nodeFromId('domInspector');
Expand Down Expand Up @@ -468,7 +468,7 @@ var startDialog = (function() {
var onClicked = function(ev) {
ev.stopPropagation();

if ( inspectedTabId === '' ) { return; }
if ( inspectedTabId === 0 ) { return; }

var target = ev.target;
var parent = target.parentElement;
Expand Down Expand Up @@ -540,7 +540,7 @@ var onMouseOver = (function() {
};

return function(ev) {
if ( inspectedTabId === '' ) { return; }
if ( inspectedTabId === 0 ) { return; }
// Convenience: skip real-time highlighting if shift key is pressed.
if ( ev.shiftKey ) { return; }
// Find closest `li`
Expand All @@ -560,15 +560,15 @@ var onMouseOver = (function() {
/******************************************************************************/

var currentTabId = function() {
if ( showdomButton.classList.contains('active') === false ) { return ''; }
if ( showdomButton.classList.contains('active') === false ) { return 0; }
return logger.tabIdFromPageSelector();
};

/******************************************************************************/

var injectInspector = function() {
var tabId = currentTabId();
if ( tabId === '' ) { return; }
if ( tabId === 0 ) { return; }
inspectedTabId = tabId;
messaging.send('loggerUI', {
what: 'scriptlet',
Expand All @@ -586,7 +586,7 @@ var shutdownInspector = function() {
}
logger.removeAllChildren(domTree);
inspector.classList.add('vCompact');
inspectedTabId = '';
inspectedTabId = 0;
};

/******************************************************************************/
Expand Down Expand Up @@ -658,7 +658,7 @@ var toggleOff = function() {
uDom.nodeFromSelector('#domInspector .permatoolbar .highlightMode').removeEventListener('click', toggleHighlightMode);
uDom.nodeFromSelector('#domInspector .permatoolbar .revert').removeEventListener('click', revert);
uDom.nodeFromSelector('#domInspector .permatoolbar .commit').removeEventListener('click', startDialog);
inspectedTabId = '';
inspectedTabId = 0;
};

/******************************************************************************/
Expand Down
Loading

0 comments on commit 2c90158

Please sign in to comment.