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 = { const options = {
defaultDeviceName: window.navigator.platform, 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: // 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) { const push = async function(dataKey, data) {
let bin = { let bin = {
'source': options.deviceName || options.defaultDeviceName, 'source': getDeviceName() || options.defaultDeviceName,
'tstamp': Date.now(), 'tstamp': Date.now(),
'data': data, 'data': data,
'size': 0 'size': 0
@ -1579,6 +1585,7 @@ vAPI.cloud = (( ) => {
const getOptions = function(callback) { const getOptions = function(callback) {
if ( typeof callback !== 'function' ) { return; } if ( typeof callback !== 'function' ) { return; }
getDeviceName();
callback(options); callback(options);
}; };

View File

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

View File

@ -316,6 +316,10 @@ if ( selfieIsValid !== true ) {
// Start network observers. // Start network observers.
µb.webRequest.start(); µ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 // Ensure that the resources allocated for decompression purpose (likely
// large buffers) are garbage-collectable immediately after launch. // large buffers) are garbage-collectable immediately after launch.
// Otherwise I have observed that it may take quite a while before the // Otherwise I have observed that it may take quite a while before the