Add chrome.proxy.settings shims for FireFox

This commit is contained in:
ilyaigpetrov 2020-12-05 15:03:42 +00:00
parent eb996931ae
commit 53571f6228
3 changed files with 100 additions and 39 deletions

View File

@ -0,0 +1,85 @@
'use strict';
if (window.apis.platform.ifFirefox) {
const prefix = 'firefox-only';
const ffxStorage = {
get(keys) {
return new Promise((resolve) => (
chrome.storage.local.get(
keys,
window.utils.getOrDie(resolve),
)
));
},
set(items) {
return new Promise((resolve) => (
chrome.storage.local.set(items, resolve)
));
},
remove(keys) {
return new Promise((resolve) => (
chrome.storage.local.remove(keys, resolve)
));
},
};
const originalSet = chrome.proxy.settings.set.bind( chrome.proxy.settings );
chrome.proxy.settings.set = function(details, cb) {
const pac = window.utils.getProp(details, 'value.pacScript') || {};
if (!(pac && pac.data)) {
return originalSet(details, cb);
}
const blob = new Blob([pac.data], { type : 'application/x-ns-proxy-autoconfig' });
const blobUrl = URL.createObjectURL(blob);
originalSet({
value: {
proxyType: 'autoConfig',
autoConfigUrl: blobUrl,
},
}, window.utils.chromified( async (err) => {
if (err) {
throw err;
}
await ffxStorage.set({ [`${prefix}-pacData`]: pac.data });
cb();
}));
};
const originalGet = chrome.proxy.settings.get.bind( chrome.proxy.settings );
chrome.proxy.settings.get = function(details, cb) {
originalGet(details, window.utils.chromified((err, originalDetails) => {
if (err) {
throw err;
}
const autoConfigUrl = window.utils.getProp(originalDetails, 'value.autoConfigUrl');
if (!autoConfigUrl) {
return cb(originalDetails);
}
window.apis.httpLib.get(autoConfigUrl, async (err, pacData) => {
if (err) {
pacData = await ffxStorage.get(`${prefix}-pacData`);
}
cb(Object.assign(
originalDetails,
{
value: {
mode: 'pac_script',
pacScript: {
data: pacData,
},
},
}
));
});
}));
};
const originalClear = chrome.proxy.settings.clear.bind( chrome.proxy.settings );
chrome.proxy.settings.clear = async function(details, cb) {
await ffxStorage.remove(`${prefix}-pacData`);
originalClear(details, cb);
};
}

View File

@ -544,7 +544,10 @@ ${
if ( if (
details && details.levelOfControl === 'controlled_by_this_extension' details && details.levelOfControl === 'controlled_by_this_extension'
) { ) {
return chrome.proxy.settings.set(details, chromified(cb)); const pac = window.utils.getProp(details, 'value.pacScript');
if (pac && pac.data) {
return chrome.proxy.settings.set(details, chromified(cb));
}
} }
kitchenState(ifIncontinence, true); kitchenState(ifIncontinence, true);
@ -621,50 +624,21 @@ ${
const originalSet = chrome.proxy.settings.set.bind( chrome.proxy.settings ); const originalSet = chrome.proxy.settings.set.bind( chrome.proxy.settings );
chrome.proxy.settings.set = function(details, cb) { chrome.proxy.settings.set = function(details, cb) {
const pac = window.utils.getProp(details, 'value.pacScript');
const pac = window.utils.getProp(details, 'value.pacScript') || {}; if (!(pac && pac.data)) {
const autoConfigUrl = window.utils.getProp(details, 'value.autoConfigUrl');
const ifNothingToCook = !(pac && pac.data || autoConfigUrl);
if (ifNothingToCook) {
return originalSet(details, cb); return originalSet(details, cb);
} }
const getPacData = (getPacCb) => const pacMods = getCurrentConfigs();
pac.data ? getPacCb(null, pac.data) : window.apis.httpLib.get(autoConfigUrl, getPacCb); pac.data = pacKitchen.cook( pac.data, pacMods );
originalSet({value: details.value}, window.utils.chromified((err) => {
getPacData((err, pacData) => {
if (err) { if (err) {
cb(err); throw err;
return;
} }
const pacMods = getCurrentConfigs(); kitchenState(ifIncontinence, null);
const cookedData = pacKitchen.cook( pacData, pacMods ); cb && cb();
const setCb = (/* No args. */) => {
kitchenState(ifIncontinence, null); }));
cb && cb();
};
if (window.apis.platform.ifFirefox) {
const autoConfigUrl = `data:application/x-ns-proxy-autoconfig,${escape(cookedData)}`;
return originalSet({
value: {
proxyType: 'autoConfig',
autoConfigUrl,
},
}, setCb);
}
details.value.pacScript.data = cookedData;
return originalSet({ value: details.value }, setCb);
});
}; };
pacKitchen.checkIncontinence();
chrome.proxy.settings.onChange.addListener(
timeouted(
pacKitchen.checkIncontinence.bind(pacKitchen)
)
);
} // Private namespace ends. } // Private namespace ends.

View File

@ -14,6 +14,7 @@
"proxy" "proxy"
, "alarms" , "alarms"
, "storage" , "storage"
, "unlimitedStorage"
, "<all_urls>" , "<all_urls>"
, "tabs" , "tabs"
, "contextMenus" , "contextMenus"
@ -30,6 +31,7 @@
, "11-error-handlers-api.js" , "11-error-handlers-api.js"
, "12-errors-lib.js" , "12-errors-lib.js"
, "13-http-lib.js" , "13-http-lib.js"
, "15-firefox-proxy-settings.js"
${scripts_2x} ${scripts_2x}
, "35-pac-kitchen-api.js" , "35-pac-kitchen-api.js"
, "37-sync-pac-script-with-pac-provider-api.js" , "37-sync-pac-script-with-pac-provider-api.js"