gecko-dev/toolkit/components/extensions/parent/ext-contextualIdentities.js
Luke Crouch 241edb5fa9 contextualIdentity: add gray color and fence icon (Bug 1532746); r=jkt,flod
Adds a gray color and fence icon for Containers. This is originally from
Facebook Container: https://addons.mozilla.org/firefox/addon/facebook-container/

User Research showed a strong affinity for the fence icon as an indicator of
the kind of "boundary" protection that Containers provide.
https://docs.google.com/spreadsheets/d/1bnqjcWTV893RESMPJzAXOrDN0juN_Z0-EE93Mvn3Z5g/edit#gid=0

Differential Revision: https://phabricator.services.mozilla.com/D26988

--HG--
extra : moz-landing-system : lando
2019-04-17 16:43:30 +00:00

290 lines
8.7 KiB
JavaScript

"use strict";
ChromeUtils.defineModuleGetter(this, "ContextualIdentityService",
"resource://gre/modules/ContextualIdentityService.jsm");
XPCOMUtils.defineLazyPreferenceGetter(this, "containersEnabled",
"privacy.userContext.enabled");
var {ExtensionPreferencesManager} = ChromeUtils.import("resource://gre/modules/ExtensionPreferencesManager.jsm");
var {
ExtensionError,
} = ExtensionUtils;
const CONTAINER_PREF_INSTALL_DEFAULTS = {
"privacy.userContext.enabled": true,
"privacy.userContext.longPressBehavior": 2,
"privacy.userContext.ui.enabled": true,
"privacy.usercontext.about_newtab_segregation.enabled": true,
"privacy.userContext.extension": undefined,
};
const CONTAINERS_ENABLED_SETTING_NAME = "privacy.containers";
const CONTAINER_COLORS = new Map([
["blue", "#37adff"],
["turquoise", "#00c79a"],
["green", "#51cd00"],
["yellow", "#ffcb00"],
["orange", "#ff9f00"],
["red", "#ff613d"],
["pink", "#ff4bda"],
["purple", "#af51f5"],
["toolbar", "#7c7c7d"],
]);
const CONTAINER_ICONS = new Set([
"briefcase",
"cart",
"circle",
"dollar",
"fence",
"fingerprint",
"gift",
"vacation",
"food",
"fruit",
"pet",
"tree",
"chill",
]);
function getContainerIcon(iconName) {
if (!CONTAINER_ICONS.has(iconName)) {
throw new ExtensionError(`Invalid icon ${iconName} for container`);
}
return `resource://usercontext-content/${iconName}.svg`;
}
function getContainerColor(colorName) {
if (!CONTAINER_COLORS.has(colorName)) {
throw new ExtensionError(`Invalid color name ${colorName} for container`);
}
return CONTAINER_COLORS.get(colorName);
}
const convertIdentity = identity => {
let result = {
name: ContextualIdentityService.getUserContextLabel(identity.userContextId),
icon: identity.icon,
iconUrl: getContainerIcon(identity.icon),
color: identity.color,
colorCode: getContainerColor(identity.color),
cookieStoreId: getCookieStoreIdForContainer(identity.userContextId),
};
return result;
};
const checkAPIEnabled = () => {
if (!containersEnabled) {
throw new ExtensionError("Contextual identities are currently disabled");
}
};
const convertIdentityFromObserver = wrappedIdentity => {
let identity = wrappedIdentity.wrappedJSObject;
let iconUrl, colorCode;
try {
iconUrl = getContainerIcon(identity.icon);
colorCode = getContainerColor(identity.color);
} catch (e) {
return null;
}
let result = {
name: identity.name,
icon: identity.icon,
iconUrl,
color: identity.color,
colorCode,
cookieStoreId: getCookieStoreIdForContainer(identity.userContextId),
};
return result;
};
ExtensionPreferencesManager.addSetting(CONTAINERS_ENABLED_SETTING_NAME, {
prefNames: Object.keys(CONTAINER_PREF_INSTALL_DEFAULTS),
setCallback(value) {
if (value !== true) {
return Object.assign(CONTAINER_PREF_INSTALL_DEFAULTS, {
"privacy.userContext.extension": value,
});
}
let prefs = {};
for (let pref of this.prefNames) {
prefs[pref] = undefined;
}
return prefs;
},
});
this.contextualIdentities = class extends ExtensionAPI {
onStartup() {
let {extension} = this;
if (extension.hasPermission("contextualIdentities")) {
ExtensionPreferencesManager.setSetting(extension.id, CONTAINERS_ENABLED_SETTING_NAME, extension.id);
}
}
getAPI(context) {
let self = {
contextualIdentities: {
async get(cookieStoreId) {
checkAPIEnabled();
let containerId = getContainerForCookieStoreId(cookieStoreId);
if (!containerId) {
throw new ExtensionError(`Invalid contextual identity: ${cookieStoreId}`);
}
let identity = ContextualIdentityService.getPublicIdentityFromId(containerId);
return convertIdentity(identity);
},
async query(details) {
checkAPIEnabled();
let identities = [];
ContextualIdentityService.getPublicIdentities().forEach(identity => {
if (details.name &&
ContextualIdentityService.getUserContextLabel(identity.userContextId) != details.name) {
return;
}
identities.push(convertIdentity(identity));
});
return identities;
},
async create(details) {
// Lets prevent making containers that are not valid
getContainerIcon(details.icon);
getContainerColor(details.color);
let identity = ContextualIdentityService.create(details.name,
details.icon,
details.color);
return convertIdentity(identity);
},
async update(cookieStoreId, details) {
checkAPIEnabled();
let containerId = getContainerForCookieStoreId(cookieStoreId);
if (!containerId) {
throw new ExtensionError(`Invalid contextual identity: ${cookieStoreId}`);
}
let identity = ContextualIdentityService.getPublicIdentityFromId(containerId);
if (!identity) {
throw new ExtensionError(`Invalid contextual identity: ${cookieStoreId}`);
}
if (details.name !== null) {
identity.name = details.name;
}
if (details.color !== null) {
getContainerColor(details.color);
identity.color = details.color;
}
if (details.icon !== null) {
getContainerIcon(details.icon);
identity.icon = details.icon;
}
if (!ContextualIdentityService.update(identity.userContextId,
identity.name, identity.icon,
identity.color)) {
throw new ExtensionError(`Contextual identity failed to update: ${cookieStoreId}`);
}
return convertIdentity(identity);
},
async remove(cookieStoreId) {
checkAPIEnabled();
let containerId = getContainerForCookieStoreId(cookieStoreId);
if (!containerId) {
throw new ExtensionError(`Invalid contextual identity: ${cookieStoreId}`);
}
let identity = ContextualIdentityService.getPublicIdentityFromId(containerId);
if (!identity) {
throw new ExtensionError(`Invalid contextual identity: ${cookieStoreId}`);
}
// We have to create the identity object before removing it.
let convertedIdentity = convertIdentity(identity);
if (!ContextualIdentityService.remove(identity.userContextId)) {
throw new ExtensionError(`Contextual identity failed to remove: ${cookieStoreId}`);
}
return convertedIdentity;
},
onCreated: new EventManager({
context,
name: "contextualIdentities.onCreated",
register: fire => {
let observer = (subject, topic) => {
let convertedIdentity = convertIdentityFromObserver(subject);
if (convertedIdentity) {
fire.async({contextualIdentity: convertedIdentity});
}
};
Services.obs.addObserver(observer, "contextual-identity-created");
return () => {
Services.obs.removeObserver(observer, "contextual-identity-created");
};
},
}).api(),
onUpdated: new EventManager({
context,
name: "contextualIdentities.onUpdated",
register: fire => {
let observer = (subject, topic) => {
let convertedIdentity = convertIdentityFromObserver(subject);
if (convertedIdentity) {
fire.async({contextualIdentity: convertedIdentity});
}
};
Services.obs.addObserver(observer, "contextual-identity-updated");
return () => {
Services.obs.removeObserver(observer, "contextual-identity-updated");
};
},
}).api(),
onRemoved: new EventManager({
context,
name: "contextualIdentities.onRemoved",
register: fire => {
let observer = (subject, topic) => {
let convertedIdentity = convertIdentityFromObserver(subject);
if (convertedIdentity) {
fire.async({contextualIdentity: convertedIdentity});
}
};
Services.obs.addObserver(observer, "contextual-identity-deleted");
return () => {
Services.obs.removeObserver(observer, "contextual-identity-deleted");
};
},
}).api(),
},
};
return self;
}
};