|
1 /* -*- Mode: js2; js2-basic-offset: 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/. */ |
|
6 |
|
7 // functions common to Identity.jsm and MinimalIdentity.jsm |
|
8 |
|
9 "use strict"; |
|
10 |
|
11 this.EXPORTED_SYMBOLS = [ |
|
12 "checkDeprecated", |
|
13 "checkRenamed", |
|
14 "getRandomId", |
|
15 "objectCopy", |
|
16 "makeMessageObject", |
|
17 ]; |
|
18 |
|
19 const Cu = Components.utils; |
|
20 |
|
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
22 |
|
23 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", |
|
24 "@mozilla.org/uuid-generator;1", |
|
25 "nsIUUIDGenerator"); |
|
26 |
|
27 XPCOMUtils.defineLazyModuleGetter(this, "Logger", |
|
28 "resource://gre/modules/identity/LogUtils.jsm"); |
|
29 |
|
30 function log(...aMessageArgs) { |
|
31 Logger.log.apply(Logger, ["Identity"].concat(aMessageArgs)); |
|
32 } |
|
33 |
|
34 function defined(item) { |
|
35 return typeof item !== 'undefined'; |
|
36 } |
|
37 |
|
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; |
|
44 }; |
|
45 |
|
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 } |
|
53 |
|
54 if (checkDeprecated(aOptions, aOldName)) { |
|
55 aOptions[aNewName] = aOptions[aOldName]; |
|
56 delete(aOptions[aOldName]); |
|
57 } |
|
58 }; |
|
59 |
|
60 this.getRandomId = function getRandomId() { |
|
61 return uuidgen.generateUUID().toString(); |
|
62 }; |
|
63 |
|
64 /* |
|
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 }); |
|
76 }; |
|
77 |
|
78 this.makeMessageObject = function makeMessageObject(aRpCaller) { |
|
79 let options = {}; |
|
80 |
|
81 options.id = aRpCaller.id; |
|
82 options.origin = aRpCaller.origin; |
|
83 |
|
84 // Backwards compatibility with Persona beta: |
|
85 // loggedInUser can be undefined, null, or a string |
|
86 options.loggedInUser = aRpCaller.loggedInUser; |
|
87 |
|
88 // Special flag for internal calls for Persona in b2g |
|
89 options._internal = aRpCaller._internal; |
|
90 |
|
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 }); |
|
100 |
|
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 } |
|
108 |
|
109 return options; |
|
110 } |
|
111 |