1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/identity/IdentityUtils.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ 1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// functions common to Identity.jsm and MinimalIdentity.jsm 1.11 + 1.12 +"use strict"; 1.13 + 1.14 +this.EXPORTED_SYMBOLS = [ 1.15 + "checkDeprecated", 1.16 + "checkRenamed", 1.17 + "getRandomId", 1.18 + "objectCopy", 1.19 + "makeMessageObject", 1.20 +]; 1.21 + 1.22 +const Cu = Components.utils; 1.23 + 1.24 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.25 + 1.26 +XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", 1.27 + "@mozilla.org/uuid-generator;1", 1.28 + "nsIUUIDGenerator"); 1.29 + 1.30 +XPCOMUtils.defineLazyModuleGetter(this, "Logger", 1.31 + "resource://gre/modules/identity/LogUtils.jsm"); 1.32 + 1.33 +function log(...aMessageArgs) { 1.34 + Logger.log.apply(Logger, ["Identity"].concat(aMessageArgs)); 1.35 +} 1.36 + 1.37 +function defined(item) { 1.38 + return typeof item !== 'undefined'; 1.39 +} 1.40 + 1.41 +var checkDeprecated = this.checkDeprecated = function checkDeprecated(aOptions, aField) { 1.42 + if (defined(aOptions[aField])) { 1.43 + log("WARNING: field is deprecated:", aField); 1.44 + return true; 1.45 + } 1.46 + return false; 1.47 +}; 1.48 + 1.49 +this.checkRenamed = function checkRenamed(aOptions, aOldName, aNewName) { 1.50 + if (defined(aOptions[aOldName]) && 1.51 + defined(aOptions[aNewName])) { 1.52 + let err = "You cannot provide both " + aOldName + " and " + aNewName; 1.53 + Logger.reportError(err); 1.54 + throw new Error(err); 1.55 + } 1.56 + 1.57 + if (checkDeprecated(aOptions, aOldName)) { 1.58 + aOptions[aNewName] = aOptions[aOldName]; 1.59 + delete(aOptions[aOldName]); 1.60 + } 1.61 +}; 1.62 + 1.63 +this.getRandomId = function getRandomId() { 1.64 + return uuidgen.generateUUID().toString(); 1.65 +}; 1.66 + 1.67 +/* 1.68 + * copy source object into target, excluding private properties 1.69 + * (those whose names begin with an underscore) 1.70 + */ 1.71 +this.objectCopy = function objectCopy(source, target){ 1.72 + let desc; 1.73 + Object.getOwnPropertyNames(source).forEach(function(name) { 1.74 + if (name[0] !== '_') { 1.75 + desc = Object.getOwnPropertyDescriptor(source, name); 1.76 + Object.defineProperty(target, name, desc); 1.77 + } 1.78 + }); 1.79 +}; 1.80 + 1.81 +this.makeMessageObject = function makeMessageObject(aRpCaller) { 1.82 + let options = {}; 1.83 + 1.84 + options.id = aRpCaller.id; 1.85 + options.origin = aRpCaller.origin; 1.86 + 1.87 + // Backwards compatibility with Persona beta: 1.88 + // loggedInUser can be undefined, null, or a string 1.89 + options.loggedInUser = aRpCaller.loggedInUser; 1.90 + 1.91 + // Special flag for internal calls for Persona in b2g 1.92 + options._internal = aRpCaller._internal; 1.93 + 1.94 + Object.keys(aRpCaller).forEach(function(option) { 1.95 + // Duplicate the callerobject, scrubbing out functions and other 1.96 + // internal variables (like _mm, the message manager object) 1.97 + if (!Object.hasOwnProperty(this, option) 1.98 + && option[0] !== '_' 1.99 + && typeof aRpCaller[option] !== 'function') { 1.100 + options[option] = aRpCaller[option]; 1.101 + } 1.102 + }); 1.103 + 1.104 + // check validity of message structure 1.105 + if ((typeof options.id === 'undefined') || 1.106 + (typeof options.origin === 'undefined')) { 1.107 + let err = "id and origin required in relying-party message: " + JSON.stringify(options); 1.108 + reportError(err); 1.109 + throw new Error(err); 1.110 + } 1.111 + 1.112 + return options; 1.113 +} 1.114 +