зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1287091 - part 4 - ContextualIdentityService create/remove/update, r=Gijs
This commit is contained in:
Родитель
0d52ef50ca
Коммит
2809a4efe7
|
@ -445,8 +445,12 @@ function createUserContextMenu(event, addCommandAttribute = true, excludeUserCon
|
|||
|
||||
let menuitem = document.createElement("menuitem");
|
||||
menuitem.setAttribute("usercontextid", identity.userContextId);
|
||||
menuitem.setAttribute("label", bundle.getString(identity.label));
|
||||
menuitem.setAttribute("accesskey", bundle.getString(identity.accessKey));
|
||||
menuitem.setAttribute("label", ContextualIdentityService.getUserContextLabel(identity.userContextId));
|
||||
|
||||
if (identity.accessKey) {
|
||||
menuitem.setAttribute("accesskey", bundle.getString(identity.accessKey));
|
||||
}
|
||||
|
||||
menuitem.classList.add("menuitem-iconic");
|
||||
|
||||
if (addCommandAttribute) {
|
||||
|
|
|
@ -1134,7 +1134,7 @@ const CustomizableWidgets = [
|
|||
|
||||
ContextualIdentityService.getIdentities().forEach(identity => {
|
||||
let bundle = doc.getElementById("bundle_browser");
|
||||
let label = bundle.getString(identity.label);
|
||||
let label = ContextualIdentityService.getUserContextLabel(identity.userContextId);
|
||||
|
||||
let item = doc.createElementNS(kNSXUL, "toolbarbutton");
|
||||
item.setAttribute("label", label);
|
||||
|
|
|
@ -33,13 +33,17 @@ XPCOMUtils.defineLazyModuleGetter(this, "DeferredTask",
|
|||
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
||||
"resource://gre/modules/FileUtils.jsm");
|
||||
|
||||
this.ContextualIdentityService = {
|
||||
function _ContextualIdentityService(path) {
|
||||
this.init(path);
|
||||
}
|
||||
|
||||
_ContextualIdentityService.prototype = {
|
||||
_defaultIdentities: [
|
||||
{ userContextId: 1,
|
||||
public: true,
|
||||
icon: "chrome://browser/skin/usercontext/personal.svg",
|
||||
color: "#00a7e0",
|
||||
label: "userContextPersonal.label",
|
||||
l10nID: "userContextPersonal.label",
|
||||
accessKey: "userContextPersonal.accesskey",
|
||||
telemetryId: 1,
|
||||
},
|
||||
|
@ -47,7 +51,7 @@ this.ContextualIdentityService = {
|
|||
public: true,
|
||||
icon: "chrome://browser/skin/usercontext/work.svg",
|
||||
color: "#f89c24",
|
||||
label: "userContextWork.label",
|
||||
l10nID: "userContextWork.label",
|
||||
accessKey: "userContextWork.accesskey",
|
||||
telemetryId: 2,
|
||||
},
|
||||
|
@ -55,7 +59,7 @@ this.ContextualIdentityService = {
|
|||
public: true,
|
||||
icon: "chrome://browser/skin/usercontext/banking.svg",
|
||||
color: "#7dc14c",
|
||||
label: "userContextBanking.label",
|
||||
l10nID: "userContextBanking.label",
|
||||
accessKey: "userContextBanking.accesskey",
|
||||
telemetryId: 3,
|
||||
},
|
||||
|
@ -63,7 +67,7 @@ this.ContextualIdentityService = {
|
|||
public: true,
|
||||
icon: "chrome://browser/skin/usercontext/shopping.svg",
|
||||
color: "#ee5195",
|
||||
label: "userContextShopping.label",
|
||||
l10nID: "userContextShopping.label",
|
||||
accessKey: "userContextShopping.accesskey",
|
||||
telemetryId: 4,
|
||||
},
|
||||
|
@ -71,7 +75,7 @@ this.ContextualIdentityService = {
|
|||
public: false,
|
||||
icon: "",
|
||||
color: "",
|
||||
label: "userContextIdInternal.thumbnail",
|
||||
name: "userContextIdInternal.thumbnail",
|
||||
accessKey: "" },
|
||||
],
|
||||
|
||||
|
@ -84,9 +88,8 @@ this.ContextualIdentityService = {
|
|||
|
||||
_saver: null,
|
||||
|
||||
init() {
|
||||
this._path = OS.Path.join(OS.Constants.Path.profileDir, "containers.json");
|
||||
|
||||
init(path) {
|
||||
this._path = path;
|
||||
this._saver = new DeferredTask(() => this.save(), SAVE_DELAY_MS);
|
||||
AsyncShutdown.profileBeforeChange.addBlocker("ContextualIdentityService: writing data",
|
||||
() => this._saver.finalize());
|
||||
|
@ -159,6 +162,49 @@ this.ContextualIdentityService = {
|
|||
{ tmpPath: this._path + ".tmp" });
|
||||
},
|
||||
|
||||
create(name, icon, color) {
|
||||
let identity = {
|
||||
userContextId: ++this._lastUserContextId,
|
||||
public: true,
|
||||
icon,
|
||||
color,
|
||||
name
|
||||
};
|
||||
|
||||
this._identities.push(identity);
|
||||
this.saveSoon();
|
||||
|
||||
return Cu.cloneInto(identity, {});
|
||||
},
|
||||
|
||||
update(userContextId, name, icon, color) {
|
||||
let identity = this._identities.find(identity => identity.userContextId == userContextId &&
|
||||
identity.public);
|
||||
if (identity) {
|
||||
identity.name = name;
|
||||
identity.color = color;
|
||||
identity.icon = icon;
|
||||
delete identity.l10nID;
|
||||
delete identity.accessKey;
|
||||
this.saveSoon();
|
||||
}
|
||||
|
||||
return !!identity;
|
||||
},
|
||||
|
||||
remove(userContextId) {
|
||||
let index = this._identities.findIndex(i => i.userContextId == userContextId && i.public);
|
||||
if (index == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._identities.splice(index, 1);
|
||||
this._openedIdentities.delete(userContextId);
|
||||
this.saveSoon();
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
ensureDataReady() {
|
||||
if (this._dataReady) {
|
||||
return;
|
||||
|
@ -186,17 +232,18 @@ this.ContextualIdentityService = {
|
|||
|
||||
getIdentities() {
|
||||
this.ensureDataReady();
|
||||
return this._identities.filter(info => info.public);
|
||||
return Cu.cloneInto(this._identities.filter(info => info.public), {});
|
||||
},
|
||||
|
||||
getPrivateIdentity(label) {
|
||||
getPrivateIdentity(name) {
|
||||
this.ensureDataReady();
|
||||
return this._identities.find(info => !info.public && info.label == label);
|
||||
return Cu.cloneInto(this._identities.find(info => !info.public && info.name == name), {});
|
||||
},
|
||||
|
||||
getIdentityFromId(userContextId) {
|
||||
this.ensureDataReady();
|
||||
return this._identities.find(info => info.userContextId == userContextId);
|
||||
return Cu.cloneInto(this._identities.find(info => info.userContextId == userContextId &&
|
||||
info.public), {});
|
||||
},
|
||||
|
||||
getUserContextLabel(userContextId) {
|
||||
|
@ -204,7 +251,13 @@ this.ContextualIdentityService = {
|
|||
if (!identity.public) {
|
||||
return "";
|
||||
}
|
||||
return gBrowserBundle.GetStringFromName(identity.label);
|
||||
|
||||
// We cannot localize the user-created identity names.
|
||||
if (identity.name) {
|
||||
return identity.name;
|
||||
}
|
||||
|
||||
return gBrowserBundle.GetStringFromName(identity.l10nID);
|
||||
},
|
||||
|
||||
setTabStyle(tab) {
|
||||
|
@ -246,6 +299,11 @@ this.ContextualIdentityService = {
|
|||
.add(identity.telemetryId);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
ContextualIdentityService.init();
|
||||
createNewInstanceForTesting(path) {
|
||||
return new _ContextualIdentityService(path);
|
||||
},
|
||||
};
|
||||
|
||||
let path = OS.Path.join(OS.Constants.Path.profileDir, "containers.json");
|
||||
this.ContextualIdentityService = new _ContextualIdentityService(path);
|
||||
|
|
|
@ -7,3 +7,5 @@
|
|||
EXTRA_JS_MODULES += [
|
||||
'ContextualIdentityService.jsm',
|
||||
]
|
||||
|
||||
XPCSHELL_TESTS_MANIFESTS += ['tests/unit/xpcshell.ini']
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
"use strict";
|
||||
|
||||
do_get_profile();
|
||||
|
||||
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/ContextualIdentityService.jsm");
|
||||
|
||||
const TEST_STORE_FILE_NAME = "test-containers.json";
|
||||
|
||||
let cis;
|
||||
|
||||
// Basic tests
|
||||
add_task(function() {
|
||||
ok(!!ContextualIdentityService, "ContextualIdentityService exists");
|
||||
|
||||
cis = ContextualIdentityService.createNewInstanceForTesting(TEST_STORE_FILE_NAME);
|
||||
ok(!!cis, "We have our instance of ContextualIdentityService");
|
||||
|
||||
equal(cis.getIdentities().length, 4, "By default, 4 containers.");
|
||||
equal(cis.getIdentityFromId(0), null, "No identity with id 0");
|
||||
|
||||
ok(!!cis.getIdentityFromId(1), "Identity 1 exists");
|
||||
ok(!!cis.getIdentityFromId(2), "Identity 2 exists");
|
||||
ok(!!cis.getIdentityFromId(3), "Identity 3 exists");
|
||||
ok(!!cis.getIdentityFromId(4), "Identity 4 exists");
|
||||
});
|
||||
|
||||
// Create a new identity
|
||||
add_task(function() {
|
||||
equal(cis.getIdentities().length, 4, "By default, 4 containers.");
|
||||
|
||||
let identity = cis.create("New Container", "Icon", "Color");
|
||||
ok(!!identity, "New container created");
|
||||
equal(identity.name, "New Container", "Name matches");
|
||||
equal(identity.icon, "Icon", "Icon matches");
|
||||
equal(identity.color, "Color", "Color matches");
|
||||
|
||||
equal(cis.getIdentities().length, 5, "Expected 5 containers.");
|
||||
|
||||
ok(!!cis.getIdentityFromId(identity.userContextId), "Identity exists");
|
||||
equal(cis.getIdentityFromId(identity.userContextId).name, "New Container", "Identity name is OK");
|
||||
equal(cis.getIdentityFromId(identity.userContextId).icon, "Icon", "Identity icon is OK");
|
||||
equal(cis.getIdentityFromId(identity.userContextId).color, "Color", "Identity color is OK");
|
||||
equal(cis.getUserContextLabel(identity.userContextId), "New Container", "Identity label is OK");
|
||||
|
||||
// Remove an identity
|
||||
equal(cis.remove(-1), false, "cis.remove() returns false if identity doesn't exist.");
|
||||
equal(cis.remove(1), true, "cis.remove() returns true if identity exists.");
|
||||
|
||||
equal(cis.getIdentities().length, 4, "Expected 4 containers.");
|
||||
});
|
||||
|
||||
// Update an identity
|
||||
add_task(function() {
|
||||
ok(!!cis.getIdentityFromId(2), "Identity 2 exists");
|
||||
|
||||
equal(cis.update(-1, "Container", "Icon", "Color"), false, "Update returns false if the identity doesn't exist");
|
||||
|
||||
equal(cis.update(2, "Container", "Icon", "Color"), true, "Update returns true if everything is OK");
|
||||
|
||||
ok(!!cis.getIdentityFromId(2), "Identity exists");
|
||||
equal(cis.getIdentityFromId(2).name, "Container", "Identity name is OK");
|
||||
equal(cis.getIdentityFromId(2).icon, "Icon", "Identity icon is OK");
|
||||
equal(cis.getIdentityFromId(2).color, "Color", "Identity color is OK");
|
||||
equal(cis.getUserContextLabel(2), "Container", "Identity label is OK");
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
[DEFAULT]
|
||||
|
||||
[test_basic.js]
|
Загрузка…
Ссылка в новой задаче