2012-11-01 16:23:14 -07:00
|
|
|
/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// functions common to Identity.jsm and MinimalIdentity.jsm
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-11-16 15:34:32 -08:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"checkDeprecated",
|
|
|
|
"checkRenamed",
|
|
|
|
"getRandomId",
|
|
|
|
"objectCopy"
|
|
|
|
];
|
|
|
|
|
2012-11-01 16:23:14 -07:00
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-11-16 15:34:32 -08:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator");
|
|
|
|
|
2013-03-27 10:01:46 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Logger",
|
|
|
|
"resource://gre/modules/identity/LogUtils.jsm");
|
|
|
|
|
|
|
|
function log(...aMessageArgs) {
|
|
|
|
Logger.log.apply(Logger, ["Identity"].concat(aMessageArgs));
|
|
|
|
}
|
2012-11-01 16:23:14 -07:00
|
|
|
|
|
|
|
function defined(item) {
|
|
|
|
return typeof item !== 'undefined';
|
|
|
|
}
|
|
|
|
|
2012-11-08 17:54:13 -08:00
|
|
|
var checkDeprecated = this.checkDeprecated = function checkDeprecated(aOptions, aField) {
|
2012-11-01 16:23:14 -07:00
|
|
|
if (defined(aOptions[aField])) {
|
2013-03-27 10:01:46 -07:00
|
|
|
log("WARNING: field is deprecated:", aField);
|
2012-11-01 16:23:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-11-08 17:54:13 -08:00
|
|
|
};
|
2012-11-01 16:23:14 -07:00
|
|
|
|
2012-11-08 17:54:13 -08:00
|
|
|
this.checkRenamed = function checkRenamed(aOptions, aOldName, aNewName) {
|
2012-11-01 16:23:14 -07:00
|
|
|
if (defined(aOptions[aOldName]) &&
|
|
|
|
defined(aOptions[aNewName])) {
|
|
|
|
let err = "You cannot provide both " + aOldName + " and " + aNewName;
|
2013-03-27 10:01:46 -07:00
|
|
|
Logger.reportError(err);
|
2012-11-01 16:23:14 -07:00
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkDeprecated(aOptions, aOldName)) {
|
|
|
|
aOptions[aNewName] = aOptions[aOldName];
|
|
|
|
delete(aOptions[aOldName]);
|
|
|
|
}
|
2012-11-08 17:54:13 -08:00
|
|
|
};
|
2012-11-16 15:34:32 -08:00
|
|
|
|
|
|
|
this.getRandomId = function getRandomId() {
|
|
|
|
return uuidgen.generateUUID().toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy source object into target, excluding private properties
|
|
|
|
* (those whose names begin with an underscore)
|
|
|
|
*/
|
2013-03-27 10:01:46 -07:00
|
|
|
this.objectCopy = function objectCopy(source, target){
|
2012-11-16 15:34:32 -08:00
|
|
|
let desc;
|
|
|
|
Object.getOwnPropertyNames(source).forEach(function(name) {
|
|
|
|
if (name[0] !== '_') {
|
|
|
|
desc = Object.getOwnPropertyDescriptor(source, name);
|
|
|
|
Object.defineProperty(target, name, desc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|