uBlock/src/js/tab.js

615 lines
21 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
2015-03-07 19:20:18 +01:00
µBlock - a browser extension to block requests.
2014-06-24 00:42:43 +02:00
Copyright (C) 2014 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
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://github.com/gorhill/uBlock
*/
2015-01-02 19:42:35 +01:00
/* global vAPI, µBlock */
/******************************************************************************/
/******************************************************************************/
(function() {
'use strict';
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2015-01-02 19:42:35 +01:00
var µb = µBlock;
2015-04-09 00:46:08 +02:00
// https://github.com/gorhill/httpswitchboard/issues/303
// Some kind of trick going on here:
// Any scheme other than 'http' and 'https' is remapped into a fake
// URL which trick the rest of µBlock into being able to process an
// otherwise unmanageable scheme. µBlock needs web page to have a proper
// hostname to work properly, so just like the 'chromium-behind-the-scene'
// fake domain name, we map unknown schemes into a fake '{scheme}-scheme'
// hostname. This way, for a specific scheme you can create scope with
// rules which will apply only to that scheme.
/******************************************************************************/
/******************************************************************************/
µb.normalizePageURL = function(tabId, pageURL) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return 'http://behind-the-scene/';
}
var uri = this.URI.set(pageURL);
var scheme = uri.scheme;
if ( scheme === 'https' || scheme === 'http' ) {
return uri.normalizedURI();
}
2015-04-27 16:54:13 +02:00
var fakeHostname = scheme + '-scheme';
2015-04-09 00:46:08 +02:00
if ( uri.hostname !== '' ) {
2015-04-27 16:54:13 +02:00
fakeHostname = uri.hostname + '.' + fakeHostname;
} else if ( scheme === 'about' && uri.path !== '' ) {
fakeHostname = uri.path + '.' + fakeHostname;
2015-04-09 00:46:08 +02:00
}
2015-04-27 16:54:13 +02:00
return 'http://' + fakeHostname + '/';
2015-04-09 00:46:08 +02:00
};
/******************************************************************************/
/******************************************************************************
To keep track from which context *exactly* network requests are made. This is
often tricky for various reasons, and the challenge is not specific to one
browser.
The time at which a URL is assigned to a tab and the time when a network
request for a root document is made must be assumed to be unrelated: it's all
asynchronous. There is no guaranteed order in which the two events are fired.
Also, other "anomalies" can occur:
- a network request for a root document is fired without the corresponding
tab being really assigned a new URL
<https://github.com/chrisaljoudi/uBlock/issues/516>
- a network request for a secondary resource is labeled with a tab id for
which no root document was pulled for that tab.
<https://github.com/chrisaljoudi/uBlock/issues/1001>
- a network request for a secondary resource is made without the root
document to which it belongs being formally bound yet to the proper tab id,
causing a bad scope to be used for filtering purpose.
<https://github.com/chrisaljoudi/uBlock/issues/1205>
<https://github.com/chrisaljoudi/uBlock/issues/1140>
So the solution here is to keep a lightweight data structure which only
purpose is to keep track as accurately as possible of which root document
belongs to which tab. That's the only purpose, and because of this, there are
no restrictions for when the URL of a root document can be associated to a tab.
Before, the PageStore object was trying to deal with this, but it had to
enforce some restrictions so as to not descend into one of the above issues, or
other issues. The PageStore object can only be associated with a tab for which
a definitive navigation event occurred, because it collects information about
what occurred in the tab (for example, the number of requests blocked for a
page).
The TabContext objects do not suffer this restriction, and as a result they
offer the most reliable picture of which root document URL is really associated
to which tab. Moreover, the TabObject can undo an association from a root
document, and automatically re-associate with the next most recent. This takes
care of <https://github.com/chrisaljoudi/uBlock/issues/516>.
The PageStore object no longer cache the various information about which
root document it is currently bound. When it needs to find out, it will always
defer to the TabContext object, which will provide the real answer. This takes
case of <https://github.com/chrisaljoudi/uBlock/issues/1205>. In effect, the
master switch and dynamic filtering rules can be evaluated now properly even
in the absence of a PageStore object, this was not the case before.
Also, the TabContext object will try its best to find a good candidate root
document URL for when none exists. This takes care of
<https://github.com/chrisaljoudi/uBlock/issues/1001>.
The TabContext manager is self-contained, and it takes care to properly
housekeep itself.
*/
µb.tabContextManager = (function() {
var tabContexts = Object.create(null);
// https://github.com/chrisaljoudi/uBlock/issues/1001
// This is to be used as last-resort fallback in case a tab is found to not
// be bound while network requests are fired for the tab.
var mostRecentRootDocURL = '';
var mostRecentRootDocURLTimestamp = 0;
var gcPeriod = 10 * 60 * 1000;
var TabContext = function(tabId) {
2015-04-13 01:55:10 +02:00
this.tabId = tabId.toString();
2015-04-09 00:46:08 +02:00
this.stack = [];
this.rawURL =
this.normalURL =
this.rootHostname =
this.rootDomain = '';
this.timer = null;
this.onTabCallback = null;
this.onTimerCallback = null;
tabContexts[tabId] = this;
};
TabContext.prototype.destroy = function() {
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
if ( this.timer !== null ) {
clearTimeout(this.timer);
this.timer = null;
}
delete tabContexts[this.tabId];
};
TabContext.prototype.onTab = function(tab) {
if ( tab ) {
this.timer = setTimeout(this.onTimerCallback, gcPeriod);
} else {
this.destroy();
}
};
TabContext.prototype.onTimer = function() {
this.timer = null;
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
vAPI.tabs.get(this.tabId, this.onTabCallback);
};
// This takes care of orphanized tab contexts. Can't be started for all
// contexts, as the behind-the-scene context is permanent -- so we do not
// want to slush it.
TabContext.prototype.autodestroy = function() {
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
this.onTabCallback = this.onTab.bind(this);
this.onTimerCallback = this.onTimer.bind(this);
this.timer = setTimeout(this.onTimerCallback, gcPeriod);
};
// Update just force all properties to be updated to match the most current
// root URL.
TabContext.prototype.update = function() {
if ( this.stack.length === 0 ) {
this.rawURL = this.normalURL = this.rootHostname = this.rootDomain = '';
} else {
this.rawURL = this.stack[this.stack.length - 1];
this.normalURL = µb.normalizePageURL(this.tabId, this.rawURL);
this.rootHostname = µb.URI.hostnameFromURI(this.normalURL);
this.rootDomain = µb.URI.domainFromHostname(this.rootHostname);
}
};
// Called whenever a candidate root URL is spotted for the tab.
TabContext.prototype.push = function(url) {
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
var count = this.stack.length;
if ( count !== 0 && this.stack[count - 1] === url ) {
return;
}
2015-04-09 00:46:08 +02:00
this.stack.push(url);
this.update();
};
// Called when a former push is a false positive:
// https://github.com/chrisaljoudi/uBlock/issues/516
TabContext.prototype.unpush = function(url) {
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
// We are not going to unpush if there is no other candidate, the
// point of unpush is to make space for a better candidate.
if ( this.stack.length === 1 ) {
return;
}
var pos = this.stack.indexOf(url);
if ( pos === -1 ) {
return;
}
this.stack.splice(pos, 1);
if ( this.stack.length === 0 ) {
this.destroy();
return;
}
if ( pos !== this.stack.length ) {
return;
}
this.update();
};
// This tells that the url is definitely the one to be associated with the
// tab, there is no longer any ambiguity about which root URL is really
// sitting in which tab.
TabContext.prototype.commit = function(url) {
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
this.stack = [url];
this.update();
};
// These are to be used for the API of the tab context manager.
var push = function(tabId, url) {
var entry = tabContexts[tabId];
if ( entry === undefined ) {
entry = new TabContext(tabId);
entry.autodestroy();
}
entry.push(url);
mostRecentRootDocURL = url;
mostRecentRootDocURLTimestamp = Date.now();
return entry;
};
// 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.
var lookup = function(tabId, url) {
var entry;
if ( url !== undefined ) {
entry = push(tabId, url);
} else {
entry = tabContexts[tabId];
}
if ( entry !== undefined ) {
return entry;
}
// https://github.com/chrisaljoudi/uBlock/issues/1025
// Google Hangout popup opens without a root frame. So for now we will
// just discard that best-guess root frame if it is too far in the
// future, at which point it ceases to be a "best guess".
if ( mostRecentRootDocURL !== '' && mostRecentRootDocURLTimestamp + 500 < Date.now() ) {
mostRecentRootDocURL = '';
}
// https://github.com/chrisaljoudi/uBlock/issues/1001
// Not a behind-the-scene request, yet no page store found for the
// tab id: we will thus bind the last-seen root document to the
// unbound tab. It's a guess, but better than ending up filtering
// nothing at all.
if ( mostRecentRootDocURL !== '' ) {
return push(tabId, mostRecentRootDocURL);
}
// If all else fail at finding a page store, re-categorize the
// request as behind-the-scene. At least this ensures that ultimately
// the user can still inspect/filter those net requests which were
// about to fall through the cracks.
// Example: Chromium + case #12 at
// http://raymondhill.net/ublock/popup.html
return tabContexts[vAPI.noTabId];
};
var commit = function(tabId, url) {
var entry = tabContexts[tabId];
if ( entry === undefined ) {
entry = push(tabId, url);
} else {
entry.commit(url);
}
return entry;
};
var unpush = function(tabId, url) {
var entry = tabContexts[tabId];
if ( entry !== undefined ) {
entry.unpush(url);
}
};
var exists = function(tabId) {
return tabContexts[tabId] !== undefined;
};
// Behind-the-scene tab context
(function() {
var entry = new TabContext(vAPI.noTabId);
entry.stack.push('');
entry.rawURL = '';
entry.normalURL = µb.normalizePageURL(entry.tabId);
entry.rootHostname = µb.URI.hostnameFromURI(entry.normalURL);
entry.rootDomain = µb.URI.domainFromHostname(entry.rootHostname);
})();
// Context object, typically to be used to feed filtering engines.
var Context = function(tabId) {
var tabContext = lookup(tabId);
this.rootHostname = tabContext.rootHostname;
this.rootDomain = tabContext.rootDomain;
this.pageHostname =
this.pageDomain =
this.requestURL =
this.requestHostname =
this.requestDomain = '';
};
var createContext = function(tabId) {
return new Context(tabId);
};
return {
push: push,
unpush: unpush,
commit: commit,
lookup: lookup,
exists: exists,
createContext: createContext
};
})();
2015-01-02 19:42:35 +01:00
/******************************************************************************/
/******************************************************************************/
// When the DOM content of root frame is loaded, this means the tab
// content has changed.
2015-03-08 16:06:36 +01:00
vAPI.tabs.onNavigation = function(details) {
if ( details.frameId !== 0 ) {
return;
}
2015-04-09 00:46:08 +02:00
var tabContext = µb.tabContextManager.commit(details.tabId, details.url);
var pageStore = µb.bindTabToPageStats(details.tabId, 'afterNavigate');
2015-01-28 22:19:46 +01:00
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/630
2015-01-28 22:19:46 +01:00
// The hostname of the bound document must always be present in the
// mini-matrix. That's the best place I could find for the fix, all other
// options had bad side-effects or complications.
2015-03-15 18:14:52 +01:00
// TODO: Eventually, we will have to use an API to check whether a scheme
2015-01-28 22:19:46 +01:00
// is supported as I suspect we are going to start to see `ws`, `wss`
// as well soon.
2015-04-09 00:46:08 +02:00
if ( pageStore && tabContext.rawURL.lastIndexOf('http', 0) === 0 ) {
pageStore.hostnameToCountMap[tabContext.rootHostname] = 0;
2015-01-28 22:19:46 +01:00
}
};
2014-09-08 19:45:03 +02:00
2015-03-08 16:06:36 +01:00
/******************************************************************************/
// It may happen the URL in the tab changes, while the page's document
// stays the same (for instance, Google Maps). Without this listener,
// the extension icon won't be properly refreshed.
2015-03-08 16:06:36 +01:00
vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
if ( !tab.url || tab.url === '' ) {
return;
}
if ( !changeInfo.url ) {
return;
}
2015-04-09 00:46:08 +02:00
µb.tabContextManager.commit(tabId, changeInfo.url);
µb.bindTabToPageStats(tabId, 'tabUpdated');
};
2014-06-24 00:42:43 +02:00
2015-03-08 16:06:36 +01:00
/******************************************************************************/
vAPI.tabs.onClosed = function(tabId) {
if ( tabId < 0 ) {
return;
}
2015-01-02 19:42:35 +01:00
µb.unbindTabFromPageStats(tabId);
};
2014-10-15 19:14:25 +02:00
2015-03-08 16:06:36 +01:00
/******************************************************************************/
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/297
2015-03-08 16:06:36 +01:00
vAPI.tabs.onPopup = function(details) {
2015-03-09 15:19:00 +01:00
//console.debug('vAPI.tabs.onPopup: details = %o', details);
var tabContext = µb.tabContextManager.lookup(details.openerTabId);
2015-04-13 01:55:10 +02:00
var openerURL = '';
if ( tabContext.tabId === details.openerTabId ) {
openerURL = tabContext.normalURL;
2015-03-08 16:06:36 +01:00
}
if ( openerURL === '' ) {
return;
}
2015-03-08 16:06:36 +01:00
var µburi = µb.URI;
var openerHostname = µburi.hostnameFromURI(openerURL);
var openerDomain = µburi.domainFromHostname(openerHostname);
var targetURL = details.targetURL;
2015-03-27 18:00:55 +01:00
// If the page URL is that of our "blocked page" URL, extract the URL of
// the page which was blocked.
if ( targetURL.lastIndexOf(vAPI.getURL('document-blocked.html'), 0) === 0 ) {
var matches = /details=([^&]+)/.exec(targetURL);
if ( matches !== null ) {
targetURL = JSON.parse(atob(matches[1])).url;
}
}
2015-03-27 19:59:17 +01:00
var context = {
pageHostname: openerHostname,
pageDomain: openerDomain,
rootHostname: openerHostname,
rootDomain: openerDomain,
requestURL: targetURL,
requestHostname: µb.URI.hostnameFromURI(targetURL),
requestType: 'popup'
};
var result = '';
2015-03-27 18:00:55 +01:00
// Check user switch first
if ( µb.hnSwitches.evaluateZ('no-popups', openerHostname) ) {
result = 'ub:no-popups: ' + µb.hnSwitches.z + ' true';
2015-03-27 18:00:55 +01:00
}
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/323
// https://github.com/chrisaljoudi/uBlock/issues/1142
2015-03-31 22:40:52 +02:00
// If popup OR opener URL is whitelisted, do not block the popup
2015-03-31 22:38:47 +02:00
if (
result === '' &&
µb.getNetFilteringSwitch(openerURL) &&
µb.getNetFilteringSwitch(targetURL)
) {
2015-03-08 16:06:36 +01:00
result = µb.staticNetFilteringEngine.matchStringExactType(context, targetURL, 'popup');
}
2014-10-15 19:14:25 +02:00
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/91
var pageStore = µb.pageStoreFromTabId(details.openerTabId);
2015-03-08 16:06:36 +01:00
if ( pageStore ) {
2015-03-27 19:59:17 +01:00
pageStore.logRequest(context, result);
}
µb.logger.writeOne(details.openerTabId, context, result);
2014-10-15 19:14:25 +02:00
// Not blocked
2015-01-22 19:00:59 +01:00
if ( µb.isAllowResult(result) ) {
return;
}
// Blocked
if ( µb.userSettings.showIconBadge ) {
µb.updateBadgeAsync(details.openerTabId);
}
// It is a popup, block and remove the tab.
2015-03-08 16:06:36 +01:00
µb.unbindTabFromPageStats(details.targetTabId);
vAPI.tabs.remove(details.targetTabId);
return true;
};
vAPI.tabs.registerListeners();
2014-10-15 19:14:25 +02:00
2014-06-24 00:42:43 +02:00
/******************************************************************************/
/******************************************************************************/
// Create an entry for the tab if it doesn't exist.
2015-04-09 00:46:08 +02:00
µb.bindTabToPageStats = function(tabId, context) {
if ( vAPI.isBehindTheSceneTabId(tabId) === false ) {
this.updateBadgeAsync(tabId);
}
2014-07-07 01:14:32 +02:00
2014-07-14 17:24:59 +02:00
// Do not create a page store for URLs which are of no interests
2015-04-09 00:46:08 +02:00
if ( µb.tabContextManager.exists(tabId) === false ) {
2014-07-14 17:24:59 +02:00
this.unbindTabFromPageStats(tabId);
2014-06-24 00:42:43 +02:00
return null;
}
2015-01-17 13:19:48 +01:00
// Reuse page store if one exists: this allows to guess if a tab is a popup
2014-06-24 00:42:43 +02:00
var pageStore = this.pageStores[tabId];
2014-08-02 17:40:27 +02:00
2015-01-17 13:19:48 +01:00
// Tab is not bound
if ( !pageStore ) {
2015-04-09 00:46:08 +02:00
return this.pageStores[tabId] = this.PageStore.factory(tabId);
2015-01-17 13:19:48 +01:00
}
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/516
// Never rebind behind-the-scene scope
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return pageStore;
}
2015-01-17 13:19:48 +01:00
// https://github.com/gorhill/uBlock/issues/516
2015-04-09 00:46:08 +02:00
// If context if 'beforeRequest', do not rebind, wait for confirmation.
2015-01-17 13:19:48 +01:00
if ( context === 'beforeRequest' ) {
return pageStore;
}
2015-01-24 18:34:36 +01:00
// Rebind according to context. We rebind even if the URL did not change,
// as maybe the tab was force-reloaded, in which case the page stats must
// be all reset.
2015-04-09 00:46:08 +02:00
pageStore.reuse(context);
2014-06-24 00:42:43 +02:00
return pageStore;
};
2015-04-09 00:46:08 +02:00
/******************************************************************************/
2015-01-02 19:42:35 +01:00
µb.unbindTabFromPageStats = function(tabId) {
2014-07-16 23:20:11 +02:00
//console.debug('µBlock> unbindTabFromPageStats(%d)', tabId);
2014-09-14 22:20:40 +02:00
var pageStore = this.pageStores[tabId];
if ( pageStore !== undefined ) {
pageStore.dispose();
delete this.pageStores[tabId];
}
2014-06-24 00:42:43 +02:00
};
/******************************************************************************/
2015-01-02 19:42:35 +01:00
µb.pageStoreFromTabId = function(tabId) {
2014-06-24 00:42:43 +02:00
return this.pageStores[tabId];
};
/******************************************************************************/
// Permanent page store for behind-the-scene requests. Must never be removed.
2015-04-09 00:46:08 +02:00
µb.pageStores[vAPI.noTabId] = µb.PageStore.factory(vAPI.noTabId);
2015-01-02 19:42:35 +01:00
/******************************************************************************/
/******************************************************************************/
// Stale page store entries janitor
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/455
2015-01-02 19:42:35 +01:00
var pageStoreJanitorPeriod = 15 * 60 * 1000;
var pageStoreJanitorSampleAt = 0;
var pageStoreJanitorSampleSize = 10;
var pageStoreJanitor = function() {
var vapiTabs = vAPI.tabs;
var tabIds = Object.keys(µb.pageStores).sort();
var checkTab = function(tabId) {
vapiTabs.get(tabId, function(tab) {
if ( !tab ) {
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µtabId);
2015-01-02 19:42:35 +01:00
µb.unbindTabFromPageStats(tabId);
}
});
};
if ( pageStoreJanitorSampleAt >= tabIds.length ) {
pageStoreJanitorSampleAt = 0;
}
var tabId;
2015-01-02 19:42:35 +01:00
var n = Math.min(pageStoreJanitorSampleAt + pageStoreJanitorSampleSize, tabIds.length);
for ( var i = pageStoreJanitorSampleAt; i < n; i++ ) {
tabId = tabIds[i];
// Do not remove behind-the-scene page store
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
continue;
}
checkTab(tabId);
2015-01-02 19:42:35 +01:00
}
pageStoreJanitorSampleAt = n;
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
};
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
/******************************************************************************/
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2015-01-02 19:42:35 +01:00
})();
/******************************************************************************/