Support managed storage

This commit is contained in:
Anton Nesterov 2020-06-15 13:22:19 +00:00
parent 8ef0a6f9f1
commit ccb5ff55c5

View file

@ -10,14 +10,15 @@ window.assignManager = {
exemptedTabs: {}, exemptedTabs: {},
getSiteStoreKey(pageUrlorUrlKey) { getSiteStoreKey(pageUrlorUrlKey) {
if (pageUrlorUrlKey.includes("siteContainerMap@@_")) return pageUrlorUrlKey;
const url = new window.URL(pageUrlorUrlKey);
const storagePrefix = "siteContainerMap@@_"; const storagePrefix = "siteContainerMap@@_";
if (pageUrlorUrlKey.startsWith(storagePrefix)) {
return pageUrlorUrlKey;
}
const url = new window.URL(pageUrlorUrlKey);
if (url.port === "80" || url.port === "443") { if (url.port === "80" || url.port === "443") {
return `${storagePrefix}${url.hostname}`; return `${storagePrefix}${url.hostname}`;
} else {
return `${storagePrefix}${url.hostname}${url.port}`;
} }
return `${storagePrefix}${url.hostname}${url.port}`;
}, },
setExempted(pageUrlorUrlKey, tabId) { setExempted(pageUrlorUrlKey, tabId) {
@ -51,17 +52,34 @@ window.assignManager = {
return !!syncEnabled; return !!syncEnabled;
}, },
getByUrlKey(siteStoreKey) { async getByUrlKey(siteStoreKey) {
return new Promise((resolve, reject) => { let res;
this.area.get([siteStoreKey]).then((storageResponse) => { if (
if (storageResponse && siteStoreKey in storageResponse) { siteStoreKey.startsWith("siteContainerMap@@_") &&
resolve(storageResponse[siteStoreKey]); browser.storage.managed instanceof Object === true
} ) {
resolve(null); try {
}).catch((e) => { res = await browser.storage.managed.get([siteStoreKey]);
reject(e); } catch(e) { }
}); }
});
if (!res || Object.keys(res).length === 0) {
res = await this.area.get([siteStoreKey]);
}
const storageResponse = (res && siteStoreKey in res) ?
res[siteStoreKey]: null;
if (
storageResponse &&
!storageResponse.identityMacAddonUUID &&
storageResponse.userContextId
) {
storageResponse.identityMacAddonUUID =
await identityState.lookupMACaddonUUID(storageResponse.userContextId);
}
return storageResponse;
}, },
async set(pageUrlorUrlKey, data, exemptedTabIds, backup = true) { async set(pageUrlorUrlKey, data, exemptedTabIds, backup = true) {
@ -101,9 +119,17 @@ window.assignManager = {
async getAssignedSites(userContextId = null) { async getAssignedSites(userContextId = null) {
const sites = {}; const sites = {};
const siteConfigs = await this.area.get(); const siteConfigs = await Promise.all([
this.area.get(),
(
browser.storage.managed instanceof Object &&
browser.storage.managed.get()
.catch(() => {})
) || {}
]).then(([a, b]) => ({...a, ...b}))
for(const urlKey of Object.keys(siteConfigs)) { for(const urlKey of Object.keys(siteConfigs)) {
if (urlKey.includes("siteContainerMap@@_")) { if (urlKey.startsWith("siteContainerMap@@_")) {
// For some reason this is stored as string... lets check // For some reason this is stored as string... lets check
// them both as that // them both as that
if (!!userContextId && if (!!userContextId &&
@ -130,7 +156,7 @@ window.assignManager = {
const identitiesList = await browser.contextualIdentities.query({}); const identitiesList = await browser.contextualIdentities.query({});
const macConfigs = await this.area.get(); const macConfigs = await this.area.get();
for(const configKey of Object.keys(macConfigs)) { for(const configKey of Object.keys(macConfigs)) {
if (configKey.includes("siteContainerMap@@_")) { if (configKey.startsWith("siteContainerMap@@_")) {
const cookieStoreId = const cookieStoreId =
"firefox-container-" + macConfigs[configKey].userContextId; "firefox-container-" + macConfigs[configKey].userContextId;
const match = identitiesList.find( const match = identitiesList.find(