Migrate storage for <=0.0.1.48. Fix ffx bugs

This commit is contained in:
ilyaigpetrov 2020-12-07 16:52:09 +00:00
parent aa727d9329
commit c566fcecb5
5 changed files with 68 additions and 61 deletions

View File

@ -158,6 +158,32 @@ console.log('Extension started.');
}, },
promisedLocalStorage: {
get(key) {
return new Promise((resolve) => (
chrome.storage.local.get(
key,
window.utils.getOrDie((storage) => resolve(key ? storage[key] : storage)),
)
));
},
set(items) {
return new Promise((resolve) => (
chrome.storage.local.set(items, resolve)
));
},
remove(keys) {
return new Promise((resolve) => (
chrome.storage.local.remove(keys, resolve)
));
},
clear() {
return new Promise((resolve) => (
chrome.storage.local.clear(resolve)
));
},
},
/* /*
* Possible values for levelOfControl: * Possible values for levelOfControl:
* *

View File

@ -4,27 +4,6 @@ if (window.apis.platform.ifFirefox) {
const prefix = 'firefox-only'; const prefix = 'firefox-only';
const ffxStorage = {
get(key) {
return new Promise((resolve) => (
chrome.storage.local.get(
key,
window.utils.getOrDie((obj) => resolve(obj[key])),
)
));
},
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 ); 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') || {};
@ -45,52 +24,41 @@ if (window.apis.platform.ifFirefox) {
cb(); cb();
return; return;
} }
window.pacData = pac.data; await window.utils.promisedLocalStorage.set({ [`${prefix}-pac-data`]: pac.data });
window.ffxStorage = ffxStorage;
await ffxStorage.set({ [`${prefix}-pac-data`]: pac.data });
cb(); cb();
})); }));
}; };
const originalGet = chrome.proxy.settings.get.bind( chrome.proxy.settings ); const originalGet = chrome.proxy.settings.get.bind( chrome.proxy.settings );
chrome.proxy.settings.get = function(details, cb) { chrome.proxy.settings.get = function(details, cb) {
originalGet(details, window.utils.chromified((err, originalDetails) => { originalGet(details, window.utils.chromified(async (err, originalDetails) => {
if (err) { if (err) {
window.utils.lastError = err; window.utils.lastError = err;
cb(originalDetails); cb(originalDetails);
return; return;
} }
const autoConfigUrl = window.utils.getProp(originalDetails, 'value.autoConfigUrl'); let pacData = await window.utils.promisedLocalStorage.get(`${prefix}-pac-data`);
if (!autoConfigUrl) { if (!pacData || !Object.keys(pacData).length) {
return cb(originalDetails); cb(originalDetails);
return;
} }
window.apis.httpLib.get(autoConfigUrl, async (err, pacData) => { cb(Object.assign(
if (err) { originalDetails,
pacData = await ffxStorage.get(`${prefix}-pac-data`); {
if (!pacData || !Object.keys(pacData).length) { value: {
window.utils.lastError = err; mode: 'pac_script',
cb(originalDetails); pacScript: {
return; data: pacData,
}
}
cb(Object.assign(
originalDetails,
{
value: {
mode: 'pac_script',
pacScript: {
data: pacData,
},
}, },
} },
)); }
}); ));
})); }));
}; };
const originalClear = chrome.proxy.settings.clear.bind( chrome.proxy.settings ); const originalClear = chrome.proxy.settings.clear.bind( chrome.proxy.settings );
chrome.proxy.settings.clear = async function(details, cb) { chrome.proxy.settings.clear = async function(details, cb) {
await ffxStorage.remove(`${prefix}-pac-data`); await window.utils.promisedLocalStorage.remove(`${prefix}-pac-data`);
originalClear(details, cb); originalClear(details, cb);
}; };
} }

View File

@ -380,10 +380,13 @@
} }
} }
chrome.storage.local.set( chrome.storage.local.remove(
onlySettable, 'antiCensorRu',
chromified(cb), () => chrome.storage.local.set(
) { antiCensorRu: onlySettable },
chromified(cb),
),
);
}, },
@ -521,10 +524,18 @@
}; };
// ON EACH LAUNCH, STARTUP, RELOAD, UPDATE, ENABLE // ON EACH LAUNCH, STARTUP, RELOAD, UPDATE, ENABLE
chrome.storage.local.get(null, chromified( async (err, oldStorage) => { (async () => {
let oldStorage = await window.utils.promisedLocalStorage.get('antiCensorRu') || {};
if (err) { if (!Object.keys(oldStorage).length) {
throw err; const storage = await window.utils.promisedLocalStorage.get(null);
if (storage.version && window.apis.version.isLeq(storage.version, '0.0.1.48')) {
const ffxPacData = storage['firefox-only-pac-data'];
delete storage['firefox-only-pac-data'];
await window.utils.promisedLocalStorage.clear();
await window.utils.promisedLocalStorage.set({ antiCensorRu: storage });
oldStorage = storage;
}
} }
/* /*
@ -560,7 +571,7 @@
console.log('Keep cooked...'); console.log('Keep cooked...');
await new Promise((resolve) => window.apis.pacKitchen.keepCookedNowAsync(resolve)); await new Promise((resolve) => window.apis.pacKitchen.keepCookedNowAsync(resolve));
console.log('Storage on init:', oldStorage); //console.log('Storage on init:', oldStorage);
antiCensorRu.ifFirstInstall = Object.keys(oldStorage).length === 0; antiCensorRu.ifFirstInstall = Object.keys(oldStorage).length === 0;
if (antiCensorRu.ifFirstInstall) { if (antiCensorRu.ifFirstInstall) {
@ -695,6 +706,6 @@
* Add storage.lastPacUpdateStamp. * Add storage.lastPacUpdateStamp.
**/ **/
})); })();
} }

View File

@ -7,8 +7,10 @@ export default function getLastUpdateDate(theState) {
componentWillMount() { componentWillMount() {
this.onStorageChangedHandler = (changes) => this.onStorageChangedHandler = (changes) => {
changes.lastPacUpdateStamp.newValue && this.forceUpdate(); const ac = changes.antiCensorRu;
return ac && ac.newValue && ac.newValue.lastPacUpdateStamp && this.forceUpdate();
};
chrome.storage.onChanged.addListener( this.onStorageChangedHandler ); chrome.storage.onChanged.addListener( this.onStorageChangedHandler );

View File

@ -43,7 +43,7 @@ chrome.runtime.getBackgroundPage( (bgWindow) =>
// winChrome.runtime.sendMessage({ currentTab, eventName: 'POPUP_OPENED' }); // winChrome.runtime.sendMessage({ currentTab, eventName: 'POPUP_OPENED' });
theState.flags.ifInsideOptionsPage = !currentTab || /.*:\/\/extensions\/\?options=/g.test(currentTab.url) || currentTab.url.startsWith('about:addons'); theState.flags.ifInsideOptionsPage = !currentTab || /.*:\/\/extensions\/\?options=/g.test(currentTab.url) || currentTab.url.startsWith('about:addons');
theState.flags.ifInsideEdgeOptionsPage = theState.flags.ifInsideOptionsPage && currentTab.url.startsWith('edge://'); theState.flags.ifInsideEdgeOptionsPage = theState.flags.ifInsideOptionsPage && currentTab && currentTab.url.startsWith('edge://');
theState.currentTab = currentTab; theState.currentTab = currentTab;