Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Refactor context-keeping
Browse files Browse the repository at this point in the history
Improves reliability and thoroughness of blocking on all
browsers.
  • Loading branch information
chrisaljoudi committed Apr 14, 2015
1 parent edd30ec commit fa3666f
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 898 deletions.
16 changes: 11 additions & 5 deletions platform/chromium/vapi-background.js
Expand Up @@ -79,7 +79,7 @@ vAPI.tabs = {};

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

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

Expand All @@ -102,8 +102,8 @@ vAPI.tabs.registerListeners = function() {
var popupCandidates = Object.create(null);

var PopupCandidate = function(details) {
this.targetTabId = details.tabId;
this.openerTabId = details.sourceTabId;
this.targetTabId = details.tabId.toString();
this.openerTabId = details.sourceTabId.toString();
this.targetURL = details.url;
this.selfDestructionTimer = null;
};
Expand Down Expand Up @@ -222,7 +222,12 @@ vAPI.tabs.get = function(tabId, callback) {
if ( typeof tabId === 'string' ) {
tabId = parseInt(tabId, 10);
}
chrome.tabs.get(tabId, onTabReady);
if ( typeof tabId !== 'number' || isNaN(tabId) ) {
onTabReady(null);
}
else {
chrome.tabs.get(tabId, onTabReady);
}
return;
}
var onTabReceived = function(tabs) {
Expand Down Expand Up @@ -719,7 +724,8 @@ vAPI.onLoadAllCompleted = function() {
var i = tabs.length, tab;
while ( i-- ) {
tab = tabs[i];
µb.bindTabToPageStats(tab.id, tab.url);
µb.tabContextManager.commit(tab.id, tab.url);
µb.bindTabToPageStats(tab.id);
// https://github.com/chrisaljoudi/uBlock/issues/129
scriptStart(tab.id);
}
Expand Down
3 changes: 2 additions & 1 deletion platform/firefox/vapi-background.js
Expand Up @@ -333,7 +333,7 @@ var tabWatcher = {

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

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

Expand Down Expand Up @@ -1986,6 +1986,7 @@ vAPI.onLoadAllCompleted = function() {

var tabId = this.tabs.getTabId(tab);
var browser = getBrowserForTab(tab);
µb.tabContextManager.commit(tabId, browser.currentURI.asciiSpec);
µb.bindTabToPageStats(tabId, browser.currentURI.asciiSpec);
browser.messageManager.sendAsyncMessage(
location.host + '-load-completed'
Expand Down
23 changes: 12 additions & 11 deletions platform/safari/vapi-background.js
Expand Up @@ -239,7 +239,7 @@

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

vAPI.isNoTabId = function(tabId) {
vAPI.isBehindTheSceneTabId = function(tabId) {
return tabId.toString() === this.noTabId;
};

Expand All @@ -256,13 +256,13 @@
tabId = vAPI.tabs.getTabId(e.target);
var details = {
targetURL: url,
targetTabId: tabId,
targetTabId: tabId.toString(),
openerTabId: vAPI.tabs.popupCandidate
};
vAPI.tabs.popupCandidate = false;
if(vAPI.tabs.onPopup(details)) {
e.preventDefault();
if(vAPI.tabs.stack[details.openerTabId]) {
vAPI.tabs.popupCandidate = false;
vAPI.tabs.stack[details.openerTabId].activate();
}
}
Expand Down Expand Up @@ -731,17 +731,18 @@
}
switch(e.message.type) {
case "popup":
vAPI.tabs.popupCandidate = vAPI.tabs.getTabId(e.target);
if(e.message.url === "about:blank") {
var openerTabId = vAPI.tabs.getTabId(e.target).toString();
var shouldBlock = !!vAPI.tabs.onPopup({
targetURL: e.message.url,
targetTabId: "preempt",
openerTabId: openerTabId
});
if(shouldBlock) {
e.message = false;
return;
}
else {
e.message = !vAPI.tabs.onPopup({
targetURL: e.message.url,
targetTabId: 0,
openerTabId: vAPI.tabs.getTabId(e.target)
});
vAPI.tabs.popupCandidate = openerTabId;
e.message = true;
}
break;
case "popstate":
Expand Down
2 changes: 1 addition & 1 deletion platform/safari/vapi-client.js
Expand Up @@ -260,7 +260,7 @@ x.setAttribute('src', block(val, 'image') ? 'data:image/gif;base64,R0lGODlhAQABA
return x;\
};\
open = function(u) {\
return block(u, 'popup') ? null : wo.apply(this, arguments);\
if(block(u, 'popup')) return {}; else return wo.apply(this, arguments);\
};\
XMLHttpRequest.prototype.open = function(m, u) {\
if(block(u, 'xmlhttprequest')) {throw 'InvalidAccessError'; return;}\
Expand Down
1 change: 0 additions & 1 deletion src/background.html
Expand Up @@ -19,7 +19,6 @@
<script src="js/dynamic-net-filtering.js"></script>
<script src="js/static-net-filtering.js"></script>
<script src="js/cosmetic-filtering.js"></script>
<script src="js/hnswitches.js"></script>
<script src="js/ublock.js"></script>
<script src="js/messaging.js"></script>
<script src="js/profiler.js"></script>
Expand Down
81 changes: 0 additions & 81 deletions src/document-blocked.html

This file was deleted.

2 changes: 1 addition & 1 deletion src/js/async.js
Expand Up @@ -187,7 +187,7 @@ return asyncJobManager;
};

var updateBadgeAsync = function(tabId) {
if ( vAPI.isNoTabId(tabId) ) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return;
}
µb.asyncJobs.add('updateBadge-' + tabId, tabId, updateBadge, 250);
Expand Down
103 changes: 0 additions & 103 deletions src/js/document-blocked.js

This file was deleted.

5 comments on commit fa3666f

@gorhill
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just pull in the changes from gorhill/uBlock@6441161 for the context stuff? (and respect git history)

@chrisaljoudi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gorhill I tried using the .patch-formatted version of your commit, but that wasn't merge-able due to other slight differences in the files.

Did some more research, and I could't figure out how to merge in the changes without having a pull request already there.

@chrisaljoudi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gorhill any idea how to simply import the commit?

@davidrenne
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @chrisaljoudi and @gorhill. I see in platform that the uBlock product supports safari and opera. I was wondering how big of a market that is with installs compared to firefox and chrome. Those seem to be more of a hidden feature for those two browsers. But I don't use them much. I was just wondering what percentage of installs you have on those browsers.

Great job on the vAPI abstraction. I think I see why you are doing that now reading some of your source code.

@chrisaljoudi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidrenne I don't have stats for Opera, but Safari has about 10% of the total user base.

Please sign in to comment.