1 /* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // functions common to Identity.jsm and MinimalIdentity.jsm
11 this.EXPORTED_SYMBOLS = [
19 const Cu = Components.utils;
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
23 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
24 "@mozilla.org/uuid-generator;1",
27 XPCOMUtils.defineLazyModuleGetter(this, "Logger",
28 "resource://gre/modules/identity/LogUtils.jsm");
30 function log(...aMessageArgs) {
31 Logger.log.apply(Logger, ["Identity"].concat(aMessageArgs));
34 function defined(item) {
35 return typeof item !== 'undefined';
38 var checkDeprecated = this.checkDeprecated = function checkDeprecated(aOptions, aField) {
39 if (defined(aOptions[aField])) {
40 log("WARNING: field is deprecated:", aField);
46 this.checkRenamed = function checkRenamed(aOptions, aOldName, aNewName) {
47 if (defined(aOptions[aOldName]) &&
48 defined(aOptions[aNewName])) {
49 let err = "You cannot provide both " + aOldName + " and " + aNewName;
50 Logger.reportError(err);
54 if (checkDeprecated(aOptions, aOldName)) {
55 aOptions[aNewName] = aOptions[aOldName];
56 delete(aOptions[aOldName]);
60 this.getRandomId = function getRandomId() {
61 return uuidgen.generateUUID().toString();
65 * copy source object into target, excluding private properties
66 * (those whose names begin with an underscore)
68 this.objectCopy = function objectCopy(source, target){
70 Object.getOwnPropertyNames(source).forEach(function(name) {
71 if (name[0] !== '_') {
72 desc = Object.getOwnPropertyDescriptor(source, name);
73 Object.defineProperty(target, name, desc);
78 this.makeMessageObject = function makeMessageObject(aRpCaller) {
81 options.id = aRpCaller.id;
82 options.origin = aRpCaller.origin;
84 // Backwards compatibility with Persona beta:
85 // loggedInUser can be undefined, null, or a string
86 options.loggedInUser = aRpCaller.loggedInUser;
88 // Special flag for internal calls for Persona in b2g
89 options._internal = aRpCaller._internal;
91 Object.keys(aRpCaller).forEach(function(option) {
92 // Duplicate the callerobject, scrubbing out functions and other
93 // internal variables (like _mm, the message manager object)
94 if (!Object.hasOwnProperty(this, option)
96 && typeof aRpCaller[option] !== 'function') {
97 options[option] = aRpCaller[option];
101 // check validity of message structure
102 if ((typeof options.id === 'undefined') ||
103 (typeof options.origin === 'undefined')) {
104 let err = "id and origin required in relying-party message: " + JSON.stringify(options);
106 throw new Error(err);