michael@0: /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // functions common to Identity.jsm and MinimalIdentity.jsm michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "checkDeprecated", michael@0: "checkRenamed", michael@0: "getRandomId", michael@0: "objectCopy", michael@0: "makeMessageObject", michael@0: ]; michael@0: michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", michael@0: "@mozilla.org/uuid-generator;1", michael@0: "nsIUUIDGenerator"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Logger", michael@0: "resource://gre/modules/identity/LogUtils.jsm"); michael@0: michael@0: function log(...aMessageArgs) { michael@0: Logger.log.apply(Logger, ["Identity"].concat(aMessageArgs)); michael@0: } michael@0: michael@0: function defined(item) { michael@0: return typeof item !== 'undefined'; michael@0: } michael@0: michael@0: var checkDeprecated = this.checkDeprecated = function checkDeprecated(aOptions, aField) { michael@0: if (defined(aOptions[aField])) { michael@0: log("WARNING: field is deprecated:", aField); michael@0: return true; michael@0: } michael@0: return false; michael@0: }; michael@0: michael@0: this.checkRenamed = function checkRenamed(aOptions, aOldName, aNewName) { michael@0: if (defined(aOptions[aOldName]) && michael@0: defined(aOptions[aNewName])) { michael@0: let err = "You cannot provide both " + aOldName + " and " + aNewName; michael@0: Logger.reportError(err); michael@0: throw new Error(err); michael@0: } michael@0: michael@0: if (checkDeprecated(aOptions, aOldName)) { michael@0: aOptions[aNewName] = aOptions[aOldName]; michael@0: delete(aOptions[aOldName]); michael@0: } michael@0: }; michael@0: michael@0: this.getRandomId = function getRandomId() { michael@0: return uuidgen.generateUUID().toString(); michael@0: }; michael@0: michael@0: /* michael@0: * copy source object into target, excluding private properties michael@0: * (those whose names begin with an underscore) michael@0: */ michael@0: this.objectCopy = function objectCopy(source, target){ michael@0: let desc; michael@0: Object.getOwnPropertyNames(source).forEach(function(name) { michael@0: if (name[0] !== '_') { michael@0: desc = Object.getOwnPropertyDescriptor(source, name); michael@0: Object.defineProperty(target, name, desc); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: this.makeMessageObject = function makeMessageObject(aRpCaller) { michael@0: let options = {}; michael@0: michael@0: options.id = aRpCaller.id; michael@0: options.origin = aRpCaller.origin; michael@0: michael@0: // Backwards compatibility with Persona beta: michael@0: // loggedInUser can be undefined, null, or a string michael@0: options.loggedInUser = aRpCaller.loggedInUser; michael@0: michael@0: // Special flag for internal calls for Persona in b2g michael@0: options._internal = aRpCaller._internal; michael@0: michael@0: Object.keys(aRpCaller).forEach(function(option) { michael@0: // Duplicate the callerobject, scrubbing out functions and other michael@0: // internal variables (like _mm, the message manager object) michael@0: if (!Object.hasOwnProperty(this, option) michael@0: && option[0] !== '_' michael@0: && typeof aRpCaller[option] !== 'function') { michael@0: options[option] = aRpCaller[option]; michael@0: } michael@0: }); michael@0: michael@0: // check validity of message structure michael@0: if ((typeof options.id === 'undefined') || michael@0: (typeof options.origin === 'undefined')) { michael@0: let err = "id and origin required in relying-party message: " + JSON.stringify(options); michael@0: reportError(err); michael@0: throw new Error(err); michael@0: } michael@0: michael@0: return options; michael@0: } michael@0: