mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-04 08:37:11 +02:00
Imrpove saving request stats for non-persistent background page
Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/2969 Related previous commit: https://github.com/gorhill/uBlock/commit/5a338b7210 The save-to-storage period is back to being around ~4 minutes, but now browser.storage.session API is used to keep track of request stats should the extension be suspended before the period elapse.
This commit is contained in:
parent
09bba3199e
commit
059e4e5e28
@ -109,9 +109,9 @@ vAPI.generateSecret = (size = 1) => {
|
|||||||
*
|
*
|
||||||
* */
|
* */
|
||||||
|
|
||||||
vAPI.sessionStorage = webext.storage.session || {
|
vAPI.sessionStorage = browser.storage.session || {
|
||||||
get() {
|
get() {
|
||||||
return Promise.resolve({});
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
set() {
|
set() {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
@ -122,6 +122,7 @@ vAPI.sessionStorage = webext.storage.session || {
|
|||||||
clear() {
|
clear() {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
|
unavailable: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -315,10 +316,10 @@ vAPI.Tabs = class {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async executeScript() {
|
async executeScript(...args) {
|
||||||
let result;
|
let result;
|
||||||
try {
|
try {
|
||||||
result = await webext.tabs.executeScript(...arguments);
|
result = await webext.tabs.executeScript(...args);
|
||||||
}
|
}
|
||||||
catch(reason) {
|
catch(reason) {
|
||||||
}
|
}
|
||||||
@ -543,7 +544,7 @@ vAPI.Tabs = class {
|
|||||||
targetURL = vAPI.getURL(targetURL);
|
targetURL = vAPI.getURL(targetURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
vAPI.tabs.update(tabId, { url: targetURL });
|
return vAPI.tabs.update(tabId, { url: targetURL });
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(tabId) {
|
async remove(tabId) {
|
||||||
@ -1778,15 +1779,24 @@ vAPI.cloud = (( ) => {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
vAPI.alarms = browser.alarms || {
|
vAPI.alarms = {
|
||||||
create() {
|
create(...args) {
|
||||||
|
browser.alarms.create(...args);
|
||||||
},
|
},
|
||||||
clear() {
|
createIfNotPresent(name, ...args) {
|
||||||
|
browser.alarms.get(name).then(details => {
|
||||||
|
if ( details !== undefined ) { return; }
|
||||||
|
browser.alarms.create(name, ...args);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async clear(...args) {
|
||||||
|
return browser.alarms.clear(...args);
|
||||||
},
|
},
|
||||||
onAlarm: {
|
onAlarm: {
|
||||||
addListener() {
|
addListener(...args) {
|
||||||
}
|
browser.alarms.onAlarm.addListener(...args);
|
||||||
}
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -174,11 +174,10 @@ const µBlock = { // jshint ignore:line
|
|||||||
'moz-extension-scheme',
|
'moz-extension-scheme',
|
||||||
],
|
],
|
||||||
|
|
||||||
localSettings: {
|
requestStats: {
|
||||||
blockedRequestCount: 0,
|
blockedCount: 0,
|
||||||
allowedRequestCount: 0,
|
allowedCount: 0,
|
||||||
},
|
},
|
||||||
localSettingsLastModified: 0,
|
|
||||||
|
|
||||||
// Read-only
|
// Read-only
|
||||||
systemSettings: {
|
systemSettings: {
|
||||||
|
@ -365,8 +365,8 @@ const popupDataFromTabId = function(tabId, tabTitle) {
|
|||||||
colorBlindFriendly: µbus.colorBlindFriendly,
|
colorBlindFriendly: µbus.colorBlindFriendly,
|
||||||
cosmeticFilteringSwitch: false,
|
cosmeticFilteringSwitch: false,
|
||||||
firewallPaneMinimized: µbus.firewallPaneMinimized,
|
firewallPaneMinimized: µbus.firewallPaneMinimized,
|
||||||
globalAllowedRequestCount: µb.localSettings.allowedRequestCount,
|
globalAllowedRequestCount: µb.requestStats.allowedCount,
|
||||||
globalBlockedRequestCount: µb.localSettings.blockedRequestCount,
|
globalBlockedRequestCount: µb.requestStats.blockedCount,
|
||||||
fontSize: µbhs.popupFontSize,
|
fontSize: µbhs.popupFontSize,
|
||||||
godMode: µbhs.filterAuthorMode,
|
godMode: µbhs.filterAuthorMode,
|
||||||
netFilteringSwitch: false,
|
netFilteringSwitch: false,
|
||||||
|
@ -739,10 +739,8 @@ const PageStore = class {
|
|||||||
aggregateAllowed += 1;
|
aggregateAllowed += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( aggregateAllowed !== 0 || aggregateBlocked !== 0 ) {
|
if ( aggregateAllowed || aggregateBlocked ) {
|
||||||
µb.localSettings.blockedRequestCount += aggregateBlocked;
|
µb.incrementRequestStats(aggregateBlocked, aggregateAllowed);
|
||||||
µb.localSettings.allowedRequestCount += aggregateAllowed;
|
|
||||||
µb.localSettingsLastModified = now;
|
|
||||||
}
|
}
|
||||||
journal.length = 0;
|
journal.length = 0;
|
||||||
}
|
}
|
||||||
|
@ -327,7 +327,6 @@ const onFirstFetchReady = (fetched, adminExtra) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Order is important -- do not change:
|
// Order is important -- do not change:
|
||||||
fromFetch(µb.localSettings, fetched);
|
|
||||||
fromFetch(µb.restoreBackupSettings, fetched);
|
fromFetch(µb.restoreBackupSettings, fetched);
|
||||||
|
|
||||||
permanentFirewall.fromString(fetched.dynamicFilteringString);
|
permanentFirewall.fromString(fetched.dynamicFilteringString);
|
||||||
@ -362,14 +361,9 @@ const createDefaultProps = ( ) => {
|
|||||||
'dynamicFilteringString': µb.dynamicFilteringDefault.join('\n'),
|
'dynamicFilteringString': µb.dynamicFilteringDefault.join('\n'),
|
||||||
'urlFilteringString': '',
|
'urlFilteringString': '',
|
||||||
'hostnameSwitchesString': µb.hostnameSwitchesDefault.join('\n'),
|
'hostnameSwitchesString': µb.hostnameSwitchesDefault.join('\n'),
|
||||||
'lastRestoreFile': '',
|
|
||||||
'lastRestoreTime': 0,
|
|
||||||
'lastBackupFile': '',
|
|
||||||
'lastBackupTime': 0,
|
|
||||||
'netWhitelist': µb.netWhitelistDefault,
|
'netWhitelist': µb.netWhitelistDefault,
|
||||||
'version': '0.0.0.0'
|
'version': '0.0.0.0'
|
||||||
};
|
};
|
||||||
toFetch(µb.localSettings, fetchableProps);
|
|
||||||
toFetch(µb.restoreBackupSettings, fetchableProps);
|
toFetch(µb.restoreBackupSettings, fetchableProps);
|
||||||
return fetchableProps;
|
return fetchableProps;
|
||||||
};
|
};
|
||||||
@ -424,6 +418,7 @@ try {
|
|||||||
ubolog(`Cache magic numbers ready ${Date.now()-vAPI.T0} ms after launch`);
|
ubolog(`Cache magic numbers ready ${Date.now()-vAPI.T0} ms after launch`);
|
||||||
onCacheSettingsReady(bin);
|
onCacheSettingsReady(bin);
|
||||||
}),
|
}),
|
||||||
|
µb.loadLocalSettings(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/1547
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1547
|
||||||
@ -523,6 +518,9 @@ while ( µb.alarmQueue.length !== 0 ) {
|
|||||||
case 'createSelfie':
|
case 'createSelfie':
|
||||||
µb.selfieManager.create();
|
µb.selfieManager.create();
|
||||||
break;
|
break;
|
||||||
|
case 'saveLocalSettings':
|
||||||
|
µb.saveLocalSettings();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,23 +97,51 @@ import {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
{
|
{
|
||||||
let localSettingsLastSaved = Date.now();
|
µb.loadLocalSettings = ( ) => Promise.all([
|
||||||
|
vAPI.sessionStorage.get('requestStats'),
|
||||||
const shouldSave = ( ) => {
|
vAPI.storage.get('requestStats'),
|
||||||
if ( µb.localSettingsLastModified > localSettingsLastSaved ) {
|
vAPI.storage.get([ 'blockedRequestCount', 'allowedRequestCount' ]),
|
||||||
µb.saveLocalSettings();
|
]).then(([ a, b, c ]) => {
|
||||||
|
if ( a instanceof Object && a.requestStats ) { return a.requestStats; }
|
||||||
|
if ( b instanceof Object && b.requestStats ) { return b.requestStats; }
|
||||||
|
if ( c instanceof Object && Object.keys(c).length === 2 ) {
|
||||||
|
return {
|
||||||
|
blockedCount: c.blockedRequestCount,
|
||||||
|
allowedCount: c.allowedRequestCount,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
saveTimer.on(saveDelay);
|
return { blockedCount: 0, allowedCount: 0 };
|
||||||
|
}).then(({ blockedCount, allowedCount }) => {
|
||||||
|
µb.requestStats.blockedCount += blockedCount;
|
||||||
|
µb.requestStats.allowedCount += allowedCount;
|
||||||
|
});
|
||||||
|
|
||||||
|
const SAVE_DELAY_IN_MINUTES = 3.6;
|
||||||
|
const QUICK_SAVE_DELAY_IN_SECONDS = 23;
|
||||||
|
|
||||||
|
const saveTimer = vAPI.defer.create(( ) => {
|
||||||
|
µb.saveLocalSettings();
|
||||||
|
});
|
||||||
|
|
||||||
|
const quickSaveTimer = vAPI.defer.create(( ) => {
|
||||||
|
saveTimer.on({ min: SAVE_DELAY_IN_MINUTES });
|
||||||
|
if ( vAPI.sessionStorage.unavailable ) { return; }
|
||||||
|
vAPI.sessionStorage.set({ requestStats: µb.requestStats });
|
||||||
|
vAPI.alarms.createIfNotPresent('saveLocalSettings', {
|
||||||
|
delayInMinutes: SAVE_DELAY_IN_MINUTES + 0.5
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
µb.incrementRequestStats = (blocked, allowed) => {
|
||||||
|
µb.requestStats.blockedCount += blocked;
|
||||||
|
µb.requestStats.allowedCount += allowed;
|
||||||
|
quickSaveTimer.on({ sec: QUICK_SAVE_DELAY_IN_SECONDS });
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveTimer = vAPI.defer.create(shouldSave);
|
µb.saveLocalSettings = ( ) => {
|
||||||
const saveDelay = { sec: 23 };
|
vAPI.alarms.clear('saveLocalSettings');
|
||||||
|
quickSaveTimer.off(); saveTimer.off();
|
||||||
saveTimer.onidle(saveDelay);
|
return vAPI.storage.set({ requestStats: µb.requestStats });
|
||||||
|
|
||||||
µb.saveLocalSettings = function() {
|
|
||||||
localSettingsLastSaved = Date.now();
|
|
||||||
return vAPI.storage.set(this.localSettings);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user