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

Commit 0099599

Browse files
committed
this fixes #455
1 parent b114361 commit 0099599

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

platform/chromium/vapi-background.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,22 @@ vAPI.tabs.get = function(tabId, callback) {
8686
var onTabReady = function(tab) {
8787
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
8888
if ( chrome.runtime.lastError ) {
89-
return;
89+
;
9090
}
91+
// Caller must be prepared to deal with nil tab value
9192
callback(tab);
9293
};
9394
if ( tabId !== null ) {
95+
if ( typeof tabId === 'string' ) {
96+
tabId = parseInt(tabId, 10);
97+
}
9498
chrome.tabs.get(tabId, onTabReady);
9599
return;
96100
}
97101
var onTabReceived = function(tabs) {
98102
// https://code.google.com/p/chromium/issues/detail?id=410868#c8
99103
if ( chrome.runtime.lastError ) {
100-
return;
104+
;
101105
}
102106
callback(tabs[0]);
103107
};

src/js/tab.js

+64-21
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,29 @@
1919
Home: https://github.com/gorhill/uBlock
2020
*/
2121

22-
/* global µBlock */
22+
/* global vAPI, µBlock */
23+
24+
/******************************************************************************/
25+
/******************************************************************************/
26+
27+
(function() {
28+
2329
'use strict';
2430

2531
/******************************************************************************/
2632

33+
var µb = µBlock;
34+
35+
/******************************************************************************/
36+
/******************************************************************************/
37+
2738
// When the DOM content of root frame is loaded, this means the tab
2839
// content has changed.
2940
vAPI.tabs.onNavigation = function(details) {
3041
if ( details.frameId !== 0 ) {
3142
return;
3243
}
33-
µBlock.bindTabToPageStats(details.tabId, details.url);
44+
µb.bindTabToPageStats(details.tabId, details.url);
3445
};
3546

3647
// It may happen the URL in the tab changes, while the page's document
@@ -43,19 +54,19 @@ vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
4354
if ( !changeInfo.url ) {
4455
return;
4556
}
46-
µBlock.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated');
57+
µb.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated');
4758
};
4859

4960
vAPI.tabs.onClosed = function(tabId) {
5061
if ( tabId < 0 ) {
5162
return;
5263
}
53-
µBlock.unbindTabFromPageStats(tabId);
64+
µb.unbindTabFromPageStats(tabId);
5465
};
5566

5667
// https://github.com/gorhill/uBlock/issues/297
5768
vAPI.tabs.onPopup = function(details) {
58-
var pageStore = µBlock.pageStoreFromTabId(details.sourceTabId);
69+
var pageStore = µb.pageStoreFromTabId(details.sourceTabId);
5970
if ( !pageStore ) {
6071
return;
6172
}
@@ -64,8 +75,8 @@ vAPI.tabs.onPopup = function(details) {
6475

6576
// https://github.com/gorhill/uBlock/issues/323
6677
// If popup URL is whitelisted, do not block it
67-
if ( µBlock.getNetFilteringSwitch(requestURL) ) {
68-
result = µBlock.netFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup');
78+
if ( µb.getNetFilteringSwitch(requestURL) ) {
79+
result = µb.netFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup');
6980
}
7081

7182
// https://github.com/gorhill/uBlock/issues/91
@@ -81,7 +92,7 @@ vAPI.tabs.onPopup = function(details) {
8192
// Blocked
8293

8394
// It is a popup, block and remove the tab.
84-
µBlock.unbindTabFromPageStats(details.tabId);
95+
µb.unbindTabFromPageStats(details.tabId);
8596
vAPI.tabs.remove(details.tabId);
8697

8798
// for Safari
@@ -90,7 +101,6 @@ vAPI.tabs.onPopup = function(details) {
90101

91102
vAPI.tabs.registerListeners();
92103

93-
94104
/******************************************************************************/
95105
/******************************************************************************/
96106

@@ -104,7 +114,7 @@ vAPI.tabs.registerListeners();
104114
// hostname. This way, for a specific scheme you can create scope with
105115
// rules which will apply only to that scheme.
106116

107-
µBlock.normalizePageURL = function(pageURL) {
117+
µb.normalizePageURL = function(pageURL) {
108118
var uri = this.URI.set(pageURL);
109119
if ( uri.scheme === 'https' || uri.scheme === 'http' ) {
110120
return uri.normalizedURI();
@@ -116,7 +126,7 @@ vAPI.tabs.registerListeners();
116126

117127
// Create an entry for the tab if it doesn't exist.
118128

119-
µBlock.bindTabToPageStats = function(tabId, pageURL, context) {
129+
µb.bindTabToPageStats = function(tabId, pageURL, context) {
120130
this.updateBadgeAsync(tabId);
121131

122132
// https://github.com/gorhill/httpswitchboard/issues/303
@@ -144,7 +154,7 @@ vAPI.tabs.registerListeners();
144154
return pageStore;
145155
};
146156

147-
µBlock.unbindTabFromPageStats = function(tabId) {
157+
µb.unbindTabFromPageStats = function(tabId) {
148158
//console.debug('µBlock> unbindTabFromPageStats(%d)', tabId);
149159
var pageStore = this.pageStores[tabId];
150160
if ( pageStore !== undefined ) {
@@ -155,27 +165,60 @@ vAPI.tabs.registerListeners();
155165

156166
/******************************************************************************/
157167

158-
µBlock.pageUrlFromTabId = function(tabId) {
168+
µb.pageUrlFromTabId = function(tabId) {
159169
var pageStore = this.pageStores[tabId];
160170
return pageStore ? pageStore.pageURL : '';
161171
};
162172

163-
µBlock.pageUrlFromPageStats = function(pageStats) {
173+
µb.pageUrlFromPageStats = function(pageStats) {
164174
if ( pageStats ) {
165175
return pageStats.pageURL;
166176
}
167177
return '';
168178
};
169179

170-
µBlock.pageStoreFromTabId = function(tabId) {
180+
µb.pageStoreFromTabId = function(tabId) {
171181
return this.pageStores[tabId];
172182
};
173183

184+
/******************************************************************************/
185+
/******************************************************************************/
186+
187+
// Stale page store entries janitor
188+
// https://github.com/gorhill/uBlock/issues/455
189+
190+
var pageStoreJanitorPeriod = 15 * 60 * 1000;
191+
var pageStoreJanitorSampleAt = 0;
192+
var pageStoreJanitorSampleSize = 10;
193+
194+
var pageStoreJanitor = function() {
195+
var vapiTabs = vAPI.tabs;
196+
var tabIds = Object.keys(µb.pageStores).sort();
197+
var checkTab = function(tabId) {
198+
vapiTabs.get(tabId, function(tab) {
199+
if ( !tab ) {
200+
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µb.pageUrlFromTabId(tabId));
201+
µb.unbindTabFromPageStats(tabId);
202+
}
203+
});
204+
};
205+
if ( pageStoreJanitorSampleAt >= tabIds.length ) {
206+
pageStoreJanitorSampleAt = 0;
207+
}
208+
var n = Math.min(pageStoreJanitorSampleAt + pageStoreJanitorSampleSize, tabIds.length);
209+
for ( var i = pageStoreJanitorSampleAt; i < n; i++ ) {
210+
checkTab(tabIds[i]);
211+
}
212+
pageStoreJanitorSampleAt = n;
213+
214+
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
215+
};
216+
217+
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
218+
219+
/******************************************************************************/
174220
/******************************************************************************/
175221

176-
// µBlock.forceReload = function(pageURL) {
177-
// var tabId = this.tabIdFromPageUrl(pageURL);
178-
// if ( tabId ) {
179-
// chrome.tabs.reload(tabId, { bypassCache: true });
180-
// }
181-
// };
222+
})();
223+
224+
/******************************************************************************/

0 commit comments

Comments
 (0)