Neutralize localStorage access on mobile platform

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/899

window.localStorage is synchronous and thus should be
avoided at launch time. Currently the avoidance is only
for mobile platforms.
This commit is contained in:
Raymond Hill 2020-02-20 16:40:29 -05:00
parent c0947200e5
commit da0ef9454a
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
3 changed files with 20 additions and 3 deletions

View file

@ -1443,7 +1443,14 @@ vAPI.cloud = (( ) => {
const options = {
defaultDeviceName: window.navigator.platform,
deviceName: vAPI.localStorage.getItem('deviceName') || ''
deviceName: undefined,
};
const getDeviceName = function() {
if ( options.deviceName === undefined ) {
options.deviceName = vAPI.localStorage.getItem('deviceName') || '';
}
return options.deviceName;
};
// This is used to find out a rough count of how many chunks exists:
@ -1491,9 +1498,8 @@ vAPI.cloud = (( ) => {
};
const push = async function(dataKey, data) {
let bin = {
'source': options.deviceName || options.defaultDeviceName,
'source': getDeviceName() || options.defaultDeviceName,
'tstamp': Date.now(),
'data': data,
'size': 0
@ -1579,6 +1585,7 @@ vAPI.cloud = (( ) => {
const getOptions = function(callback) {
if ( typeof callback !== 'function' ) { return; }
getDeviceName();
callback(options);
};

View file

@ -226,6 +226,10 @@ vAPI.closePopup = function() {
// Always use a wrapper to seamlessly handle exceptions
vAPI.localStorage = {
started: vAPI.webextFlavor.soup.has('mobile') === false,
start: function() {
this.started = true;
},
clear: function() {
try {
window.localStorage.clear();
@ -233,6 +237,7 @@ vAPI.localStorage = {
}
},
getItem: function(key) {
if ( this.started === false ) { return null; }
try {
return window.localStorage.getItem(key);
} catch(ex) {
@ -246,6 +251,7 @@ vAPI.localStorage = {
}
},
setItem: function(key, value) {
if ( this.started === false ) { return; }
try {
window.localStorage.setItem(key, value);
} catch(ex) {

View file

@ -316,6 +316,10 @@ if ( selfieIsValid !== true ) {
// Start network observers.
µb.webRequest.start();
// https://github.com/uBlockOrigin/uBlock-issues/issues/899
// Signal that localStorage can be used now that uBO is ready.
vAPI.localStorage.start();
// Ensure that the resources allocated for decompression purpose (likely
// large buffers) are garbage-collectable immediately after launch.
// Otherwise I have observed that it may take quite a while before the