You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

709 lines
24 KiB

10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
  1. /*******************************************************************************
  2. µMatrix - a Chromium browser extension to black/white list requests.
  3. Copyright (C) 2014-2016 Raymond Hill
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uMatrix
  15. */
  16. /******************************************************************************/
  17. /******************************************************************************/
  18. (function() {
  19. 'use strict';
  20. /******************************************************************************/
  21. var µm = µMatrix;
  22. // https://github.com/gorhill/httpswitchboard/issues/303
  23. // Some kind of trick going on here:
  24. // Any scheme other than 'http' and 'https' is remapped into a fake
  25. // URL which trick the rest of µMatrix into being able to process an
  26. // otherwise unmanageable scheme. µMatrix needs web page to have a proper
  27. // hostname to work properly, so just like the 'behind-the-scene'
  28. // fake domain name, we map unknown schemes into a fake '{scheme}-scheme'
  29. // hostname. This way, for a specific scheme you can create scope with
  30. // rules which will apply only to that scheme.
  31. /******************************************************************************/
  32. /******************************************************************************/
  33. µm.normalizePageURL = function(tabId, pageURL) {
  34. if ( vAPI.isBehindTheSceneTabId(tabId) ) {
  35. return 'http://' + this.behindTheSceneScope + '/';
  36. }
  37. // If the URL is that of our "blocked page" document, return the URL of
  38. // the blocked page.
  39. if ( pageURL.lastIndexOf(vAPI.getURL('main-blocked.html'), 0) === 0 ) {
  40. var matches = /main-blocked\.html\?details=([^&]+)/.exec(pageURL);
  41. if ( matches && matches.length === 2 ) {
  42. try {
  43. var details = JSON.parse(atob(matches[1]));
  44. pageURL = details.url;
  45. } catch (e) {
  46. }
  47. }
  48. }
  49. var uri = this.URI.set(pageURL);
  50. var scheme = uri.scheme;
  51. if ( scheme === 'https' || scheme === 'http' ) {
  52. return uri.normalizedURI();
  53. }
  54. var fakeHostname = scheme + '-scheme';
  55. if ( uri.hostname !== '' ) {
  56. fakeHostname = uri.hostname + '.' + fakeHostname;
  57. } else if ( scheme === 'about' ) {
  58. fakeHostname = uri.path + '.' + fakeHostname;
  59. }
  60. return 'http://' + fakeHostname + '/';
  61. };
  62. /******************************************************************************/
  63. /******************************************************************************
  64. To keep track from which context *exactly* network requests are made. This is
  65. often tricky for various reasons, and the challenge is not specific to one
  66. browser.
  67. The time at which a URL is assigned to a tab and the time when a network
  68. request for a root document is made must be assumed to be unrelated: it's all
  69. asynchronous. There is no guaranteed order in which the two events are fired.
  70. Also, other "anomalies" can occur:
  71. - a network request for a root document is fired without the corresponding
  72. tab being really assigned a new URL
  73. <https://github.com/chrisaljoudi/uBlock/issues/516>
  74. - a network request for a secondary resource is labeled with a tab id for
  75. which no root document was pulled for that tab.
  76. <https://github.com/chrisaljoudi/uBlock/issues/1001>
  77. - a network request for a secondary resource is made without the root
  78. document to which it belongs being formally bound yet to the proper tab id,
  79. causing a bad scope to be used for filtering purpose.
  80. <https://github.com/chrisaljoudi/uBlock/issues/1205>
  81. <https://github.com/chrisaljoudi/uBlock/issues/1140>
  82. So the solution here is to keep a lightweight data structure which only
  83. purpose is to keep track as accurately as possible of which root document
  84. belongs to which tab. That's the only purpose, and because of this, there are
  85. no restrictions for when the URL of a root document can be associated to a tab.
  86. Before, the PageStore object was trying to deal with this, but it had to
  87. enforce some restrictions so as to not descend into one of the above issues, or
  88. other issues. The PageStore object can only be associated with a tab for which
  89. a definitive navigation event occurred, because it collects information about
  90. what occurred in the tab (for example, the number of requests blocked for a
  91. page).
  92. The TabContext objects do not suffer this restriction, and as a result they
  93. offer the most reliable picture of which root document URL is really associated
  94. to which tab. Moreover, the TabObject can undo an association from a root
  95. document, and automatically re-associate with the next most recent. This takes
  96. care of <https://github.com/chrisaljoudi/uBlock/issues/516>.
  97. The PageStore object no longer cache the various information about which
  98. root document it is currently bound. When it needs to find out, it will always
  99. defer to the TabContext object, which will provide the real answer. This takes
  100. case of <https://github.com/chrisaljoudi/uBlock/issues/1205>. In effect, the
  101. master switch and dynamic filtering rules can be evaluated now properly even
  102. in the absence of a PageStore object, this was not the case before.
  103. Also, the TabContext object will try its best to find a good candidate root
  104. document URL for when none exists. This takes care of
  105. <https://github.com/chrisaljoudi/uBlock/issues/1001>.
  106. The TabContext manager is self-contained, and it takes care to properly
  107. housekeep itself.
  108. */
  109. µm.tabContextManager = (function() {
  110. var tabContexts = Object.create(null);
  111. // https://github.com/chrisaljoudi/uBlock/issues/1001
  112. // This is to be used as last-resort fallback in case a tab is found to not
  113. // be bound while network requests are fired for the tab.
  114. var mostRecentRootDocURL = '';
  115. var mostRecentRootDocURLTimestamp = 0;
  116. var gcPeriod = 31 * 60 * 1000; // every 31 minutes
  117. // A pushed entry is removed from the stack unless it is committed with
  118. // a set time.
  119. var StackEntry = function(url, commit) {
  120. this.url = url;
  121. this.committed = commit;
  122. this.tstamp = Date.now();
  123. };
  124. var TabContext = function(tabId) {
  125. this.tabId = tabId;
  126. this.stack = [];
  127. this.rawURL =
  128. this.normalURL =
  129. this.scheme =
  130. this.rootHostname =
  131. this.rootDomain = '';
  132. this.secure = false;
  133. this.commitTimer = null;
  134. this.gcTimer = null;
  135. tabContexts[tabId] = this;
  136. };
  137. TabContext.prototype.destroy = function() {
  138. if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
  139. return;
  140. }
  141. if ( this.gcTimer !== null ) {
  142. clearTimeout(this.gcTimer);
  143. this.gcTimer = null;
  144. }
  145. delete tabContexts[this.tabId];
  146. };
  147. TabContext.prototype.onTab = function(tab) {
  148. if ( tab ) {
  149. this.gcTimer = vAPI.setTimeout(this.onGC.bind(this), gcPeriod);
  150. } else {
  151. this.destroy();
  152. }
  153. };
  154. TabContext.prototype.onGC = function() {
  155. this.gcTimer = null;
  156. if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
  157. return;
  158. }
  159. vAPI.tabs.get(this.tabId, this.onTab.bind(this));
  160. };
  161. // https://github.com/gorhill/uBlock/issues/248
  162. // Stack entries have to be committed to stick. Non-committed stack
  163. // entries are removed after a set delay.
  164. TabContext.prototype.onCommit = function() {
  165. if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
  166. return;
  167. }
  168. this.commitTimer = null;
  169. // Remove uncommitted entries at the top of the stack.
  170. var i = this.stack.length;
  171. while ( i-- ) {
  172. if ( this.stack[i].committed ) {
  173. break;
  174. }
  175. }
  176. // https://github.com/gorhill/uBlock/issues/300
  177. // If no committed entry was found, fall back on the bottom-most one
  178. // as being the committed one by default.
  179. if ( i === -1 && this.stack.length !== 0 ) {
  180. this.stack[0].committed = true;
  181. i = 0;
  182. }
  183. i += 1;
  184. if ( i < this.stack.length ) {
  185. this.stack.length = i;
  186. this.update();
  187. µm.bindTabToPageStats(this.tabId, 'newURL');
  188. }
  189. };
  190. // This takes care of orphanized tab contexts. Can't be started for all
  191. // contexts, as the behind-the-scene context is permanent -- so we do not
  192. // want to flush it.
  193. TabContext.prototype.autodestroy = function() {
  194. if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
  195. return;
  196. }
  197. this.gcTimer = vAPI.setTimeout(this.onGC.bind(this), gcPeriod);
  198. };
  199. // Update just force all properties to be updated to match the most recent
  200. // root URL.
  201. TabContext.prototype.update = function() {
  202. if ( this.stack.length === 0 ) {
  203. this.rawURL = this.normalURL = this.scheme =
  204. this.rootHostname = this.rootDomain = '';
  205. this.secure = false;
  206. return;
  207. }
  208. this.rawURL = this.stack[this.stack.length - 1].url;
  209. this.normalURL = µm.normalizePageURL(this.tabId, this.rawURL);
  210. this.scheme = µm.URI.schemeFromURI(this.rawURL);
  211. this.rootHostname = µm.URI.hostnameFromURI(this.normalURL);
  212. this.rootDomain = µm.URI.domainFromHostname(this.rootHostname) || this.rootHostname;
  213. this.secure = µm.URI.isSecureScheme(this.scheme);
  214. };
  215. // Called whenever a candidate root URL is spotted for the tab.
  216. TabContext.prototype.push = function(url, context) {
  217. if ( vAPI.isBehindTheSceneTabId(this.tabId) ) { return; }
  218. var committed = context !== undefined;
  219. var count = this.stack.length;
  220. var topEntry = this.stack[count - 1];
  221. if ( topEntry && topEntry.url === url ) {
  222. if ( committed ) {
  223. topEntry.committed = true;
  224. }
  225. return;
  226. }
  227. if ( this.commitTimer !== null ) {
  228. clearTimeout(this.commitTimer);
  229. }
  230. if ( committed ) {
  231. this.stack = [new StackEntry(url, true)];
  232. } else {
  233. this.stack.push(new StackEntry(url));
  234. this.commitTimer = vAPI.setTimeout(this.onCommit.bind(this), 1000);
  235. }
  236. this.update();
  237. µm.bindTabToPageStats(this.tabId, context);
  238. };
  239. // These are to be used for the API of the tab context manager.
  240. var push = function(tabId, url, context) {
  241. var entry = tabContexts[tabId];
  242. if ( entry === undefined ) {
  243. entry = new TabContext(tabId);
  244. entry.autodestroy();
  245. }
  246. entry.push(url, context);
  247. mostRecentRootDocURL = url;
  248. mostRecentRootDocURLTimestamp = Date.now();
  249. return entry;
  250. };
  251. // Find a tab context for a specific tab. If none is found, attempt to
  252. // fix this. When all fail, the behind-the-scene context is returned.
  253. var mustLookup = function(tabId, url) {
  254. var entry;
  255. if ( url !== undefined ) {
  256. entry = push(tabId, url);
  257. } else {
  258. entry = tabContexts[tabId];
  259. }
  260. if ( entry !== undefined ) {
  261. return entry;
  262. }
  263. // https://github.com/chrisaljoudi/uBlock/issues/1025
  264. // Google Hangout popup opens without a root frame. So for now we will
  265. // just discard that best-guess root frame if it is too far in the
  266. // future, at which point it ceases to be a "best guess".
  267. if ( mostRecentRootDocURL !== '' && mostRecentRootDocURLTimestamp + 500 < Date.now() ) {
  268. mostRecentRootDocURL = '';
  269. }
  270. // https://github.com/chrisaljoudi/uBlock/issues/1001
  271. // Not a behind-the-scene request, yet no page store found for the
  272. // tab id: we will thus bind the last-seen root document to the
  273. // unbound tab. It's a guess, but better than ending up filtering
  274. // nothing at all.
  275. if ( mostRecentRootDocURL !== '' ) {
  276. return push(tabId, mostRecentRootDocURL);
  277. }
  278. // If all else fail at finding a page store, re-categorize the
  279. // request as behind-the-scene. At least this ensures that ultimately
  280. // the user can still inspect/filter those net requests which were
  281. // about to fall through the cracks.
  282. // Example: Chromium + case #12 at
  283. // http://raymondhill.net/ublock/popup.html
  284. return tabContexts[vAPI.noTabId];
  285. };
  286. var lookup = function(tabId) {
  287. return tabContexts[tabId] || null;
  288. };
  289. // Behind-the-scene tab context
  290. (function() {
  291. var entry = new TabContext(vAPI.noTabId);
  292. entry.stack.push(new StackEntry('', true));
  293. entry.rawURL = '';
  294. entry.normalURL = µm.normalizePageURL(entry.tabId);
  295. entry.rootHostname = µm.URI.hostnameFromURI(entry.normalURL);
  296. entry.rootDomain = µm.URI.domainFromHostname(entry.rootHostname) || entry.rootHostname;
  297. })();
  298. // https://github.com/gorhill/uMatrix/issues/513
  299. // Force a badge update here, it could happen that all the subsequent
  300. // network requests are already in the page store, which would cause
  301. // the badge to no be updated for these network requests.
  302. vAPI.tabs.onNavigation = function(details) {
  303. var tabId = details.tabId;
  304. if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
  305. push(tabId, details.url, 'newURL');
  306. µm.updateBadgeAsync(tabId);
  307. };
  308. // https://github.com/gorhill/uMatrix/issues/872
  309. // `changeInfo.url` may not always be available (Firefox).
  310. vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
  311. if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
  312. if ( typeof tab.url !== 'string' || tab.url === '' ) { return; }
  313. var url = changeInfo.url || tab.url;
  314. if ( url ) {
  315. push(tabId, url, 'updateURL');
  316. }
  317. };
  318. vAPI.tabs.onClosed = function(tabId) {
  319. µm.unbindTabFromPageStats(tabId);
  320. var entry = tabContexts[tabId];
  321. if ( entry instanceof TabContext ) {
  322. entry.destroy();
  323. }
  324. };
  325. return {
  326. push: push,
  327. lookup: lookup,
  328. mustLookup: mustLookup
  329. };
  330. })();
  331. vAPI.tabs.registerListeners();
  332. /******************************************************************************/
  333. /******************************************************************************/
  334. // Create an entry for the tab if it doesn't exist
  335. µm.bindTabToPageStats = function(tabId, context) {
  336. this.updateBadgeAsync(tabId);
  337. // Do not create a page store for URLs which are of no interests
  338. // Example: dev console
  339. var tabContext = this.tabContextManager.lookup(tabId);
  340. if ( tabContext === null ) {
  341. throw new Error('Unmanaged tab id: ' + tabId);
  342. }
  343. // rhill 2013-11-24: Never ever rebind behind-the-scene
  344. // virtual tab.
  345. // https://github.com/gorhill/httpswitchboard/issues/67
  346. if ( vAPI.isBehindTheSceneTabId(tabId) ) {
  347. return this.pageStores[tabId];
  348. }
  349. var normalURL = tabContext.normalURL;
  350. var pageStore = this.pageStores[tabId] || null;
  351. // The previous page URL, if any, associated with the tab
  352. if ( pageStore !== null ) {
  353. // No change, do not rebind
  354. if ( pageStore.pageUrl === normalURL ) {
  355. return pageStore;
  356. }
  357. // https://github.com/gorhill/uMatrix/issues/37
  358. // Just rebind whenever possible: the URL changed, but the document
  359. // maybe is the same.
  360. // Example: Google Maps, Github
  361. // https://github.com/gorhill/uMatrix/issues/72
  362. // Need to double-check that the new scope is same as old scope
  363. if ( context === 'updateURL' && pageStore.pageHostname === tabContext.rootHostname ) {
  364. pageStore.rawURL = tabContext.rawURL;
  365. pageStore.normalURL = normalURL;
  366. this.updateTitle(tabId);
  367. this.pageStoresToken = Date.now();
  368. return pageStore;
  369. }
  370. // We won't be reusing this page store.
  371. this.unbindTabFromPageStats(tabId);
  372. }
  373. // Try to resurrect first.
  374. pageStore = this.resurrectPageStore(tabId, normalURL);
  375. if ( pageStore === null ) {
  376. pageStore = this.pageStoreFactory(tabContext);
  377. }
  378. this.pageStores[tabId] = pageStore;
  379. this.updateTitle(tabId);
  380. this.pageStoresToken = Date.now();
  381. // console.debug('tab.js > bindTabToPageStats(): dispatching traffic in tab id %d to page store "%s"', tabId, pageUrl);
  382. return pageStore;
  383. };
  384. /******************************************************************************/
  385. µm.unbindTabFromPageStats = function(tabId) {
  386. // Never unbind behind-the-scene page store.
  387. if ( vAPI.isBehindTheSceneTabId(tabId) ) {
  388. return;
  389. }
  390. var pageStore = this.pageStores[tabId] || null;
  391. if ( pageStore === null ) {
  392. return;
  393. }
  394. delete this.pageStores[tabId];
  395. this.pageStoresToken = Date.now();
  396. if ( pageStore.incinerationTimer ) {
  397. clearTimeout(pageStore.incinerationTimer);
  398. pageStore.incinerationTimer = null;
  399. }
  400. if ( this.pageStoreCemetery.hasOwnProperty(tabId) === false ) {
  401. this.pageStoreCemetery[tabId] = {};
  402. }
  403. var pageStoreCrypt = this.pageStoreCemetery[tabId];
  404. var pageURL = pageStore.pageUrl;
  405. pageStoreCrypt[pageURL] = pageStore;
  406. pageStore.incinerationTimer = vAPI.setTimeout(
  407. this.incineratePageStore.bind(this, tabId, pageURL),
  408. 4 * 60 * 1000
  409. );
  410. };
  411. /******************************************************************************/
  412. µm.resurrectPageStore = function(tabId, pageURL) {
  413. if ( this.pageStoreCemetery.hasOwnProperty(tabId) === false ) {
  414. return null;
  415. }
  416. var pageStoreCrypt = this.pageStoreCemetery[tabId];
  417. if ( pageStoreCrypt.hasOwnProperty(pageURL) === false ) {
  418. return null;
  419. }
  420. var pageStore = pageStoreCrypt[pageURL];
  421. if ( pageStore.incinerationTimer !== null ) {
  422. clearTimeout(pageStore.incinerationTimer);
  423. pageStore.incinerationTimer = null;
  424. }
  425. delete pageStoreCrypt[pageURL];
  426. if ( Object.keys(pageStoreCrypt).length === 0 ) {
  427. delete this.pageStoreCemetery[tabId];
  428. }
  429. return pageStore;
  430. };
  431. /******************************************************************************/
  432. µm.incineratePageStore = function(tabId, pageURL) {
  433. if ( this.pageStoreCemetery.hasOwnProperty(tabId) === false ) {
  434. return;
  435. }
  436. var pageStoreCrypt = this.pageStoreCemetery[tabId];
  437. if ( pageStoreCrypt.hasOwnProperty(pageURL) === false ) {
  438. return;
  439. }
  440. var pageStore = pageStoreCrypt[pageURL];
  441. if ( pageStore.incinerationTimer !== null ) {
  442. clearTimeout(pageStore.incinerationTimer);
  443. pageStore.incinerationTimer = null;
  444. }
  445. delete pageStoreCrypt[pageURL];
  446. if ( Object.keys(pageStoreCrypt).length === 0 ) {
  447. delete this.pageStoreCemetery[tabId];
  448. }
  449. pageStore.dispose();
  450. };
  451. /******************************************************************************/
  452. µm.pageStoreFromTabId = function(tabId) {
  453. return this.pageStores[tabId] || null;
  454. };
  455. // Never return null
  456. µm.mustPageStoreFromTabId = function(tabId) {
  457. return this.pageStores[tabId] || this.pageStores[vAPI.noTabId];
  458. };
  459. /******************************************************************************/
  460. µm.forceReload = function(tabId, bypassCache) {
  461. vAPI.tabs.reload(tabId, bypassCache);
  462. };
  463. /******************************************************************************/
  464. // Update badge
  465. // rhill 2013-11-09: well this sucks, I can't update icon/badge
  466. // incrementally, as chromium overwrite the icon at some point without
  467. // notifying me, and this causes internal cached state to be out of sync.
  468. µm.updateBadgeAsync = (function() {
  469. var tabIdToTimer = Object.create(null);
  470. var updateBadge = function(tabId) {
  471. delete tabIdToTimer[tabId];
  472. var iconId = null;
  473. var badgeStr = '';
  474. var pageStore = this.pageStoreFromTabId(tabId);
  475. if ( pageStore !== null ) {
  476. var total = pageStore.perLoadAllowedRequestCount +
  477. pageStore.perLoadBlockedRequestCount;
  478. if ( total ) {
  479. var squareSize = 19;
  480. var greenSize = squareSize * Math.sqrt(pageStore.perLoadAllowedRequestCount / total);
  481. iconId = greenSize < squareSize/2 ? Math.ceil(greenSize) : Math.floor(greenSize);
  482. }
  483. if ( this.userSettings.iconBadgeEnabled && pageStore.distinctRequestCount !== 0) {
  484. badgeStr = this.formatCount(pageStore.distinctRequestCount);
  485. }
  486. }
  487. vAPI.setIcon(tabId, iconId, badgeStr);
  488. };
  489. return function(tabId) {
  490. if ( tabIdToTimer[tabId] ) {
  491. return;
  492. }
  493. if ( vAPI.isBehindTheSceneTabId(tabId) ) {
  494. return;
  495. }
  496. tabIdToTimer[tabId] = vAPI.setTimeout(updateBadge.bind(this, tabId), 750);
  497. };
  498. })();
  499. /******************************************************************************/
  500. µm.updateTitle = (function() {
  501. var tabIdToTimer = Object.create(null);
  502. var tabIdToTryCount = Object.create(null);
  503. var delay = 499;
  504. var tryNoMore = function(tabId) {
  505. delete tabIdToTryCount[tabId];
  506. };
  507. var tryAgain = function(tabId) {
  508. var count = tabIdToTryCount[tabId];
  509. if ( count === undefined ) {
  510. return false;
  511. }
  512. if ( count === 1 ) {
  513. delete tabIdToTryCount[tabId];
  514. return false;
  515. }
  516. tabIdToTryCount[tabId] = count - 1;
  517. tabIdToTimer[tabId] = vAPI.setTimeout(updateTitle.bind(µm, tabId), delay);
  518. return true;
  519. };
  520. var onTabReady = function(tabId, tab) {
  521. if ( !tab ) {
  522. return tryNoMore(tabId);
  523. }
  524. var pageStore = this.pageStoreFromTabId(tabId);
  525. if ( pageStore === null ) {
  526. return tryNoMore(tabId);
  527. }
  528. if ( !tab.title && tryAgain(tabId) ) {
  529. return;
  530. }
  531. // https://github.com/gorhill/uMatrix/issues/225
  532. // Sometimes title changes while page is loading.
  533. var settled = tab.title && tab.title === pageStore.title;
  534. pageStore.title = tab.title || tab.url || '';
  535. this.pageStoresToken = Date.now();
  536. if ( settled || !tryAgain(tabId) ) {
  537. tryNoMore(tabId);
  538. }
  539. };
  540. var updateTitle = function(tabId) {
  541. delete tabIdToTimer[tabId];
  542. vAPI.tabs.get(tabId, onTabReady.bind(this, tabId));
  543. };
  544. return function(tabId) {
  545. if ( vAPI.isBehindTheSceneTabId(tabId) ) {
  546. return;
  547. }
  548. if ( tabIdToTimer[tabId] ) {
  549. clearTimeout(tabIdToTimer[tabId]);
  550. }
  551. tabIdToTimer[tabId] = vAPI.setTimeout(updateTitle.bind(this, tabId), delay);
  552. tabIdToTryCount[tabId] = 5;
  553. };
  554. })();
  555. /******************************************************************************/
  556. // Stale page store entries janitor
  557. // https://github.com/chrisaljoudi/uBlock/issues/455
  558. (function() {
  559. var cleanupPeriod = 7 * 60 * 1000;
  560. var cleanupSampleAt = 0;
  561. var cleanupSampleSize = 11;
  562. var cleanup = function() {
  563. var vapiTabs = vAPI.tabs;
  564. var tabIds = Object.keys(µm.pageStores).sort();
  565. var checkTab = function(tabId) {
  566. vapiTabs.get(tabId, function(tab) {
  567. if ( !tab ) {
  568. µm.unbindTabFromPageStats(tabId);
  569. }
  570. });
  571. };
  572. if ( cleanupSampleAt >= tabIds.length ) {
  573. cleanupSampleAt = 0;
  574. }
  575. var tabId;
  576. var n = Math.min(cleanupSampleAt + cleanupSampleSize, tabIds.length);
  577. for ( var i = cleanupSampleAt; i < n; i++ ) {
  578. tabId = tabIds[i];
  579. if ( vAPI.isBehindTheSceneTabId(tabId) ) {
  580. continue;
  581. }
  582. checkTab(tabId);
  583. }
  584. cleanupSampleAt = n;
  585. vAPI.setTimeout(cleanup, cleanupPeriod);
  586. };
  587. vAPI.setTimeout(cleanup, cleanupPeriod);
  588. })();
  589. /******************************************************************************/
  590. })();