Skip to content

Commit ec7db30

Browse files
committed
Simplify fetching title of tabs
The title of tabs in uBO is solely to have a better presentation in the logger -- no other purpose. This commit simplify keeping track of the titles, from an active approach by directly querying it from tabs whenever a change occurs, to a passive approach by storing it when the title string become available in some tab event handlers.
1 parent 01cdf50 commit ec7db30

File tree

3 files changed

+27
-69
lines changed

3 files changed

+27
-69
lines changed

src/js/pagestore.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ const HostnameDetailsMap = class extends Map {
328328
/******************************************************************************/
329329

330330
const PageStore = class {
331-
constructor(tabId, context) {
331+
constructor(tabId, details) {
332332
this.extraData = new Map();
333333
this.journal = [];
334334
this.journalTimer = undefined;
@@ -337,15 +337,15 @@ const PageStore = class {
337337
this.netFilteringCache = NetFilteringResultCache.factory();
338338
this.hostnameDetailsMap = new HostnameDetailsMap();
339339
this.counts = new CountDetails();
340-
this.init(tabId, context);
340+
this.init(tabId, details);
341341
}
342342

343-
static factory(tabId, context) {
343+
static factory(tabId, details) {
344344
let entry = PageStore.junkyard.pop();
345345
if ( entry === undefined ) {
346-
entry = new PageStore(tabId, context);
346+
entry = new PageStore(tabId, details);
347347
} else {
348-
entry.init(tabId, context);
348+
entry.init(tabId, details);
349349
}
350350
return entry;
351351
}
@@ -354,7 +354,7 @@ const PageStore = class {
354354
// The context is used to determine whether we report behavior change
355355
// to the logger.
356356

357-
init(tabId) {
357+
init(tabId, details) {
358358
const tabContext = µb.tabContextManager.mustLookup(tabId);
359359
this.tabId = tabId;
360360

@@ -368,7 +368,6 @@ const PageStore = class {
368368
}
369369

370370
this.tabHostname = tabContext.rootHostname;
371-
this.title = tabContext.rawURL;
372371
this.rawURL = tabContext.rawURL;
373372
this.hostnameDetailsMap.reset();
374373
this.contentLastModified = 0;
@@ -385,13 +384,17 @@ const PageStore = class {
385384
this.frames = new Map();
386385
this.setFrameURL({ url: tabContext.rawURL });
387386

387+
if ( this.titleFromDetails(details) === false ) {
388+
this.title = tabContext.rawURL;
389+
}
390+
388391
// Evaluated on-demand
389392
this._noCosmeticFiltering = undefined;
390393

391394
return this;
392395
}
393396

394-
reuse(context) {
397+
reuse(context, details) {
395398
// When force refreshing a page, the page store data needs to be reset.
396399

397400
// If the hostname changes, we can't merely just update the context.
@@ -411,6 +414,7 @@ const PageStore = class {
411414
// URL changed, force a re-evaluation of filtering switch
412415
this.rawURL = tabContext.rawURL;
413416
this.setFrameURL({ url: this.rawURL });
417+
this.titleFromDetails(details);
414418
return this;
415419
}
416420

@@ -420,7 +424,7 @@ const PageStore = class {
420424
this.largeMediaTimer = null;
421425
}
422426
this.disposeFrameStores();
423-
this.init(this.tabId, context);
427+
this.init(this.tabId, details);
424428
return this;
425429
}
426430

@@ -450,6 +454,14 @@ const PageStore = class {
450454
return null;
451455
}
452456

457+
titleFromDetails(details) {
458+
if ( details instanceof Object && details.title !== undefined ) {
459+
this.title = details.title;
460+
return true;
461+
}
462+
return false;
463+
}
464+
453465
disposeFrameStores() {
454466
for ( const frameStore of this.frames.values() ) {
455467
frameStore.dispose();

src/js/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const initializeTabs = async function() {
6969
if ( tab.discarded === true ) { continue; }
7070
const { id, url } = tab;
7171
µb.tabContextManager.commit(id, url);
72-
µb.bindTabToPageStore(id);
72+
µb.bindTabToPageStore(id, 'tabCommitted', tab);
7373
// https://github.com/chrisaljoudi/uBlock/issues/129
7474
// Find out whether content scripts need to be injected
7575
// programmatically. This may be necessary for web pages which

src/js/tab.js

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ vAPI.Tabs = class extends vAPI.Tabs {
884884
const { frameId, tabId, url } = details;
885885
if ( frameId === 0 ) {
886886
µb.tabContextManager.commit(tabId, url);
887-
const pageStore = µb.bindTabToPageStore(tabId, 'tabCommitted');
887+
const pageStore = µb.bindTabToPageStore(tabId, 'tabCommitted', details);
888888
if ( pageStore !== null ) {
889889
pageStore.journalAddRootFrame('committed', url);
890890
}
@@ -910,7 +910,7 @@ vAPI.Tabs = class extends vAPI.Tabs {
910910
if ( !tab.url || tab.url === '' ) { return; }
911911
if ( !changeInfo.url ) { return; }
912912
µBlock.tabContextManager.commit(tabId, changeInfo.url);
913-
µBlock.bindTabToPageStore(tabId, 'tabUpdated');
913+
µBlock.bindTabToPageStore(tabId, 'tabUpdated', tab);
914914
}
915915
};
916916

@@ -921,7 +921,7 @@ vAPI.tabs = new vAPI.Tabs();
921921

922922
// Create an entry for the tab if it doesn't exist.
923923

924-
µBlock.bindTabToPageStore = function(tabId, context) {
924+
µBlock.bindTabToPageStore = function(tabId, context, details = undefined) {
925925
this.updateToolbarIcon(tabId, 0b111);
926926

927927
// Do not create a page store for URLs which are of no interests
@@ -935,8 +935,7 @@ vAPI.tabs = new vAPI.Tabs();
935935

936936
// Tab is not bound
937937
if ( pageStore === undefined ) {
938-
this.updateTitle(tabId);
939-
pageStore = this.PageStore.factory(tabId, context);
938+
pageStore = this.PageStore.factory(tabId, details);
940939
this.pageStores.set(tabId, pageStore);
941940
this.pageStoresToken = Date.now();
942941
return pageStore;
@@ -958,9 +957,8 @@ vAPI.tabs = new vAPI.Tabs();
958957
// Rebind according to context. We rebind even if the URL did not change,
959958
// as maybe the tab was force-reloaded, in which case the page stats must
960959
// be all reset.
961-
pageStore.reuse(context);
960+
pageStore.reuse(context, details);
962961

963-
this.updateTitle(tabId);
964962
this.pageStoresToken = Date.now();
965963

966964
return pageStore;
@@ -1094,58 +1092,6 @@ vAPI.tabs = new vAPI.Tabs();
10941092

10951093
/******************************************************************************/
10961094

1097-
µBlock.updateTitle = (( ) => {
1098-
const tabIdToCount = new Map();
1099-
const delay = 499;
1100-
1101-
const updateTitle = async function(tabId) {
1102-
let count = tabIdToCount.get(tabId);
1103-
if ( count === undefined ) { return; }
1104-
tabIdToCount.delete(tabId);
1105-
const tab = await vAPI.tabs.get(tabId);
1106-
if ( tab instanceof Object === false || tab.discarded === true ) {
1107-
return;
1108-
}
1109-
const µb = µBlock;
1110-
const pageStore = µb.pageStoreFromTabId(tabId);
1111-
if ( pageStore === null ) { return; }
1112-
// Firefox needs this: if you detach a tab, the new tab won't have
1113-
// its rawURL set. Concretely, this causes the logger to report an
1114-
// entry to itself in the logger's tab selector.
1115-
// TODO: Investigate for a fix vAPI-side.
1116-
pageStore.rawURL = tab.url;
1117-
µb.pageStoresToken = Date.now();
1118-
// https://github.com/gorhill/uMatrix/issues/225
1119-
// Sometimes title changes while page is loading.
1120-
const settled =
1121-
typeof tab.title === 'string' &&
1122-
tab.title !== '' &&
1123-
tab.title === pageStore.title;
1124-
pageStore.title = tab.title || tab.url || '';
1125-
if ( settled ) { return; }
1126-
if ( tabIdToCount.has(tabId) ) { return; }
1127-
count -= 1;
1128-
if ( count === 0 ) { return; }
1129-
tabIdToCount.set(tabId, count);
1130-
updateTitleAsync(tabId);
1131-
};
1132-
1133-
const updateTitleAsync = function(tabId) {
1134-
vAPI.setTimeout(( ) => { updateTitle(tabId); }, delay);
1135-
};
1136-
1137-
return function(tabId) {
1138-
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
1139-
const count = tabIdToCount.get(tabId);
1140-
tabIdToCount.set(tabId, 5);
1141-
if ( count === undefined ) {
1142-
updateTitleAsync(tabId);
1143-
}
1144-
};
1145-
})();
1146-
1147-
/******************************************************************************/
1148-
11491095
// https://github.com/chrisaljoudi/uBlock/issues/455
11501096
// Stale page store entries janitor
11511097

0 commit comments

Comments
 (0)