Bumping manifests a=b2g-bump
[gecko.git] / toolkit / identity / IdentityUtils.jsm
blob1a9ddefc6539e5dfc7d89c73059853e8382e61c5
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
9 "use strict";
11 this.EXPORTED_SYMBOLS = [
12   "checkDeprecated",
13   "checkRenamed",
14   "getRandomId",
15   "objectCopy",
16   "makeMessageObject",
19 const Cu = Components.utils;
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
23 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
24                                    "@mozilla.org/uuid-generator;1",
25                                    "nsIUUIDGenerator");
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);
41     return true;
42   }
43   return false;
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);
51     throw new Error(err);
52   }
54   if (checkDeprecated(aOptions, aOldName)) {
55     aOptions[aNewName] = aOptions[aOldName];
56     delete(aOptions[aOldName]);
57   }
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)
67  */
68 this.objectCopy = function objectCopy(source, target){
69   let desc;
70   Object.getOwnPropertyNames(source).forEach(function(name) {
71     if (name[0] !== '_') {
72       desc = Object.getOwnPropertyDescriptor(source, name);
73       Object.defineProperty(target, name, desc);
74     }
75   });
78 this.makeMessageObject = function makeMessageObject(aRpCaller) {
79   let options = {};
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)
95         && option[0] !== '_'
96         && typeof aRpCaller[option] !== 'function') {
97       options[option] = aRpCaller[option];
98     }
99   });
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);
105     reportError(err);
106     throw new Error(err);
107   }
109   return options;