diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 05:50:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 05:50:18 +0000 |
commit | 55a5d29a66503248916f249ad2a1d8b37cde5a03 (patch) | |
tree | a3beb6a90dd3bdaaf67ecb05d42152a494aff946 /platform/common | |
parent | Adding upstream version 1.55.0+dfsg. (diff) | |
download | ublock-origin-55a5d29a66503248916f249ad2a1d8b37cde5a03.tar.xz ublock-origin-55a5d29a66503248916f249ad2a1d8b37cde5a03.zip |
Adding upstream version 1.57.0+dfsg.upstream/1.57.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'platform/common')
-rw-r--r-- | platform/common/vapi-background.js | 133 | ||||
-rw-r--r-- | platform/common/vapi-client.js | 29 |
2 files changed, 77 insertions, 85 deletions
diff --git a/platform/common/vapi-background.js b/platform/common/vapi-background.js index 0d6fcdd..2ff99ac 100644 --- a/platform/common/vapi-background.js +++ b/platform/common/vapi-background.js @@ -87,6 +87,19 @@ vAPI.app = { }, }; +/******************************************************************************/ + +// Generate segments of random six alphanumeric characters, thus one segment +// is a value out of 36^6 = over 2x10^9 values. + +vAPI.generateSecret = (size = 1) => { + let secret = ''; + while ( size-- ) { + secret += (Math.floor(Math.random() * 2176782336) + 2176782336).toString(36).slice(1); + } + return secret; +}; + /******************************************************************************* * * https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/storage/session @@ -96,9 +109,9 @@ vAPI.app = { * * */ -vAPI.sessionStorage = { +vAPI.sessionStorage = browser.storage.session || { get() { - return Promise.resolve({}); + return Promise.resolve(); }, set() { return Promise.resolve(); @@ -109,7 +122,7 @@ vAPI.sessionStorage = { clear() { return Promise.resolve(); }, - implemented: false, + unavailable: true, }; /******************************************************************************* @@ -124,46 +137,21 @@ vAPI.sessionStorage = { vAPI.storage = { get(key, ...args) { - if ( vAPI.sessionStorage.implemented !== true ) { - return webext.storage.local.get(key, ...args).catch(reason => { - console.log(reason); - }); - } - return vAPI.sessionStorage.get(key, ...args).then(bin => { - const size = Object.keys(bin).length; - if ( size === 1 && typeof key === 'string' && bin[key] === null ) { - return {}; - } - if ( size !== 0 ) { return bin; } - return webext.storage.local.get(key, ...args).then(bin => { - if ( bin instanceof Object === false ) { return bin; } - // Mirror empty result as null value in order to prevent - // from falling back to storage.local when there is no need. - const tomirror = Object.assign({}, bin); - if ( typeof key === 'string' && Object.keys(bin).length === 0 ) { - Object.assign(tomirror, { [key]: null }); - } - vAPI.sessionStorage.set(tomirror); - return bin; - }).catch(reason => { - console.log(reason); - }); + return webext.storage.local.get(key, ...args).catch(reason => { + console.log(reason); }); }, set(...args) { - vAPI.sessionStorage.set(...args); return webext.storage.local.set(...args).catch(reason => { console.log(reason); }); }, remove(...args) { - vAPI.sessionStorage.remove(...args); return webext.storage.local.remove(...args).catch(reason => { console.log(reason); }); }, clear(...args) { - vAPI.sessionStorage.clear(...args); return webext.storage.local.clear(...args).catch(reason => { console.log(reason); }); @@ -328,10 +316,10 @@ vAPI.Tabs = class { }); } - async executeScript() { + async executeScript(...args) { let result; try { - result = await webext.tabs.executeScript(...arguments); + result = await webext.tabs.executeScript(...args); } catch(reason) { } @@ -445,22 +433,26 @@ vAPI.Tabs = class { // For some reasons, some platforms do not honor the left,top // position when specified. I found that further calling // windows.update again with the same position _may_ help. + // + // https://github.com/uBlockOrigin/uBlock-issues/issues/2249 + // Mind that the creation of the popup window might fail. if ( details.popup !== undefined && vAPI.windows instanceof Object ) { - const createDetails = { + const basicDetails = { url: details.url, type: details.popup, }; - if ( details.box instanceof Object ) { - Object.assign(createDetails, details.box); + const shouldRestorePosition = details.box instanceof Object; + const extraDetails = shouldRestorePosition + ? Object.assign({}, basicDetails, details.box) + : basicDetails; + const win = await vAPI.windows.create(extraDetails); + if ( win === null ) { + if ( shouldRestorePosition === false ) { return; } + return vAPI.windows.create(basicDetails); } - const win = await vAPI.windows.create(createDetails); - if ( win === null ) { return; } - if ( details.box instanceof Object === false ) { return; } - if ( - win.left === details.box.left && - win.top === details.box.top - ) { - return; + if ( shouldRestorePosition === false ) { return; } + if ( win.left === details.box.left ) { + if ( win.top === details.box.top ) { return; } } vAPI.windows.update(win.id, { left: details.box.left, @@ -556,7 +548,7 @@ vAPI.Tabs = class { targetURL = vAPI.getURL(targetURL); } - vAPI.tabs.update(tabId, { url: targetURL }); + return vAPI.tabs.update(tabId, { url: targetURL }); } async remove(tabId) { @@ -1165,11 +1157,6 @@ vAPI.messaging = { // Support using a new secret for every network request. { - // Generate a 6-character alphanumeric string, thus one random value out - // of 36^6 = over 2x10^9 values. - const generateSecret = ( ) => - (Math.floor(Math.random() * 2176782336) + 2176782336).toString(36).slice(1); - const root = vAPI.getURL('/'); const reSecret = /\?secret=(\w+)/; const shortSecrets = []; @@ -1207,7 +1194,7 @@ vAPI.messaging = { } } lastShortSecretTime = Date.now(); - const secret = generateSecret(); + const secret = vAPI.generateSecret(); shortSecrets.push(secret); return secret; }, @@ -1215,7 +1202,7 @@ vAPI.messaging = { if ( previous !== undefined ) { longSecrets.delete(previous); } - const secret = `${generateSecret()}${generateSecret()}${generateSecret()}`; + const secret = vAPI.generateSecret(3); longSecrets.add(secret); return secret; }, @@ -1663,10 +1650,7 @@ vAPI.cloud = (( ) => { const push = async function(details) { const { datakey, data, encode } = details; - if ( - data === undefined || - typeof data === 'string' && data === '' - ) { + if ( data === undefined || typeof data === 'string' && data === '' ) { return deleteChunks(datakey, 0); } const item = { @@ -1674,10 +1658,9 @@ vAPI.cloud = (( ) => { tstamp: Date.now(), data, }; - const json = JSON.stringify(item); const encoded = encode instanceof Function - ? await encode(json) - : json; + ? await encode(item) + : JSON.stringify(item); // Chunkify taking into account QUOTA_BYTES_PER_ITEM: // https://developer.chrome.com/extensions/storage#property-sync @@ -1742,13 +1725,16 @@ vAPI.cloud = (( ) => { i += 1; } encoded = encoded.join(''); - const json = decode instanceof Function - ? await decode(encoded) - : encoded; + let entry = null; try { - entry = JSON.parse(json); - } catch(ex) { + if ( decode instanceof Function ) { + entry = await decode(encoded) || null; + } + if ( typeof entry === 'string' ) { + entry = JSON.parse(entry); + } + } catch(_) { } return entry; }; @@ -1797,15 +1783,24 @@ vAPI.cloud = (( ) => { /******************************************************************************/ /******************************************************************************/ -vAPI.alarms = browser.alarms || { - create() { +vAPI.alarms = { + create(...args) { + webext.alarms.create(...args); }, - clear() { + createIfNotPresent(name, ...args) { + webext.alarms.get(name).then(details => { + if ( details !== undefined ) { return; } + webext.alarms.create(name, ...args); + }); + }, + async clear(...args) { + return webext.alarms.clear(...args); }, onAlarm: { - addListener() { - } - } + addListener(...args) { + webext.alarms.onAlarm.addListener(...args); + }, + }, }; /******************************************************************************/ diff --git a/platform/common/vapi-client.js b/platform/common/vapi-client.js index 9375e88..a6d46c6 100644 --- a/platform/common/vapi-client.js +++ b/platform/common/vapi-client.js @@ -22,10 +22,6 @@ // For non-background page -/* globals browser */ - -'use strict'; - /******************************************************************************/ // https://github.com/chrisaljoudi/uBlock/issues/456 @@ -85,6 +81,7 @@ vAPI.messaging = { portTimerDelay: 10000, msgIdGenerator: 1, pending: new Map(), + waitStartTime: 0, shuttingDown: false, shutdown: function() { @@ -115,16 +112,11 @@ vAPI.messaging = { // revisited to isolate the picker dialog DOM from the page DOM. messageListener: function(details) { if ( typeof details !== 'object' || details === null ) { return; } - - // Response to specific message previously sent - if ( details.msgId !== undefined ) { - const resolver = this.pending.get(details.msgId); - if ( resolver !== undefined ) { - this.pending.delete(details.msgId); - resolver(details.msg); - return; - } - } + if ( details.msgId === undefined ) { return; } + const resolver = this.pending.get(details.msgId); + if ( resolver === undefined ) { return; } + this.pending.delete(details.msgId); + resolver(details.msg); }, messageListenerBound: null, @@ -203,13 +195,18 @@ vAPI.messaging = { // the main process is no longer reachable: memory leaks and bad // performance become a risk -- especially for long-lived, dynamic // pages. Guard against this. - if ( this.pending.size > 50 ) { - vAPI.shutdown.exec(); + if ( this.pending.size > 64 ) { + if ( (Date.now() - this.waitStartTime) > 60000 ) { + vAPI.shutdown.exec(); + } } const port = this.getPort(); if ( port === null ) { return Promise.resolve(); } + if ( this.pending.size === 0 ) { + this.waitStartTime = Date.now(); + } const msgId = this.msgIdGenerator++; const promise = new Promise(resolve => { this.pending.set(msgId, resolve); |