1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
9 // This is the parent process corresponding to nsDOMIdentity.
10 this.EXPORTED_SYMBOLS = ["DOMIdentity"];
12 Cu.import("resource://gre/modules/Services.jsm");
13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
15 XPCOMUtils.defineLazyModuleGetter(this, "objectCopy",
16 "resource://gre/modules/identity/IdentityUtils.jsm");
18 XPCOMUtils.defineLazyModuleGetter(this, "IdentityService",
19 #ifdef MOZ_B2G_VERSION
20 "resource://gre/modules/identity/MinimalIdentity.jsm");
22 "resource://gre/modules/identity/Identity.jsm");
25 XPCOMUtils.defineLazyModuleGetter(this,
27 "resource://gre/modules/identity/LogUtils.jsm");
29 XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
30 "@mozilla.org/parentprocessmessagemanager;1",
31 "nsIMessageListenerManager");
33 function log(...aMessageArgs) {
34 Logger.log.apply(Logger, ["DOMIdentity"].concat(aMessageArgs));
37 function IDDOMMessage(aOptions) {
38 objectCopy(aOptions, this);
41 function IDPProvisioningContext(aID, aOrigin, aTargetMM) {
43 this._origin = aOrigin;
47 IDPProvisioningContext.prototype = {
49 get origin() this._origin,
51 doBeginProvisioningCallback: function IDPPC_doBeginProvCB(aID, aCertDuration) {
52 let message = new IDDOMMessage({id: this.id});
53 message.identity = aID;
54 message.certDuration = aCertDuration;
55 this._mm.sendAsyncMessage("Identity:IDP:CallBeginProvisioningCallback",
59 doGenKeyPairCallback: function IDPPC_doGenKeyPairCallback(aPublicKey) {
60 log("doGenKeyPairCallback");
61 let message = new IDDOMMessage({id: this.id});
62 message.publicKey = aPublicKey;
63 this._mm.sendAsyncMessage("Identity:IDP:CallGenKeyPairCallback", message);
66 doError: function(msg) {
67 log("Provisioning ERROR: " + msg);
71 function IDPAuthenticationContext(aID, aOrigin, aTargetMM) {
73 this._origin = aOrigin;
77 IDPAuthenticationContext.prototype = {
79 get origin() this._origin,
81 doBeginAuthenticationCallback: function IDPAC_doBeginAuthCB(aIdentity) {
82 let message = new IDDOMMessage({id: this.id});
83 message.identity = aIdentity;
84 this._mm.sendAsyncMessage("Identity:IDP:CallBeginAuthenticationCallback",
88 doError: function IDPAC_doError(msg) {
89 log("Authentication ERROR: " + msg);
93 function RPWatchContext(aOptions, aTargetMM) {
94 objectCopy(aOptions, this);
96 // id and origin are required
97 if (! (this.id && this.origin)) {
98 throw new Error("id and origin are required for RP watch context");
101 // default for no loggedInUser is undefined, not null
102 this.loggedInUser = aOptions.loggedInUser;
105 this._internal = aOptions._internal;
107 this._mm = aTargetMM;
110 RPWatchContext.prototype = {
111 doLogin: function RPWatchContext_onlogin(aAssertion, aMaybeInternalParams) {
112 log("doLogin: " + this.id);
113 let message = new IDDOMMessage({id: this.id, assertion: aAssertion});
114 if (aMaybeInternalParams) {
115 message._internalParams = aMaybeInternalParams;
117 this._mm.sendAsyncMessage("Identity:RP:Watch:OnLogin", message);
120 doLogout: function RPWatchContext_onlogout() {
121 log("doLogout: " + this.id);
122 let message = new IDDOMMessage({id: this.id});
123 this._mm.sendAsyncMessage("Identity:RP:Watch:OnLogout", message);
126 doReady: function RPWatchContext_onready() {
127 log("doReady: " + this.id);
128 let message = new IDDOMMessage({id: this.id});
129 this._mm.sendAsyncMessage("Identity:RP:Watch:OnReady", message);
132 doCancel: function RPWatchContext_oncancel() {
133 log("doCancel: " + this.id);
134 let message = new IDDOMMessage({id: this.id});
135 this._mm.sendAsyncMessage("Identity:RP:Watch:OnCancel", message);
138 doError: function RPWatchContext_onerror(aMessage) {
139 log("doError: " + aMessage);
144 // nsIMessageListener
145 receiveMessage: function DOMIdentity_receiveMessage(aMessage) {
146 let msg = aMessage.json;
148 // Target is the frame message manager that called us and is
149 // used to send replies back to the proper window.
150 let targetMM = aMessage.target;
152 switch (aMessage.name) {
154 case "Identity:RP:Watch":
155 this._watch(msg, targetMM);
157 case "Identity:RP:Unwatch":
158 this._unwatch(msg, targetMM);
160 case "Identity:RP:Request":
161 this._request(msg, targetMM);
163 case "Identity:RP:Logout":
164 this._logout(msg, targetMM);
167 case "Identity:IDP:BeginProvisioning":
168 this._beginProvisioning(msg, targetMM);
170 case "Identity:IDP:GenKeyPair":
171 this._genKeyPair(msg);
173 case "Identity:IDP:RegisterCertificate":
174 this._registerCertificate(msg);
176 case "Identity:IDP:ProvisioningFailure":
177 this._provisioningFailure(msg);
179 case "Identity:IDP:BeginAuthentication":
180 this._beginAuthentication(msg, targetMM);
182 case "Identity:IDP:CompleteAuthentication":
183 this._completeAuthentication(msg);
185 case "Identity:IDP:AuthenticationFailure":
186 this._authenticationFailure(msg);
188 case "child-process-shutdown":
189 // we receive child-process-shutdown if the appliction crashes,
190 // including if it is crashed by the OS (killed for out-of-memory,
192 this._childProcessShutdown(targetMM);
198 observe: function DOMIdentity_observe(aSubject, aTopic, aData) {
200 case "xpcom-shutdown":
201 this._unsubscribeListeners();
202 Services.obs.removeObserver(this, "xpcom-shutdown");
203 Services.ww.unregisterNotification(this);
208 messages: ["Identity:RP:Watch", "Identity:RP:Request", "Identity:RP:Logout",
209 "Identity:IDP:BeginProvisioning", "Identity:IDP:ProvisioningFailure",
210 "Identity:IDP:RegisterCertificate", "Identity:IDP:GenKeyPair",
211 "Identity:IDP:BeginAuthentication",
212 "Identity:IDP:CompleteAuthentication",
213 "Identity:IDP:AuthenticationFailure",
214 "Identity:RP:Unwatch",
215 "child-process-shutdown"],
218 _init: function DOMIdentity__init() {
219 Services.ww.registerNotification(this);
220 Services.obs.addObserver(this, "xpcom-shutdown", false);
221 this._subscribeListeners();
224 _subscribeListeners: function DOMIdentity__subscribeListeners() {
226 for (let message of this.messages) {
227 ppmm.addMessageListener(message, this);
231 _unsubscribeListeners: function DOMIdentity__unsubscribeListeners() {
232 for (let message of this.messages) {
233 ppmm.removeMessageListener(message, this);
238 _resetFrameState: function(aContext) {
239 log("_resetFrameState: ", aContext.id);
241 throw new Error("ERROR: Trying to reset an invalid context");
243 let message = new IDDOMMessage({id: aContext.id});
244 aContext._mm.sendAsyncMessage("Identity:ResetState", message);
247 _watch: function DOMIdentity__watch(message, targetMM) {
248 log("DOMIdentity__watch: " + message.id);
249 // Pass an object with the watch members to Identity.jsm so it can call the
251 let context = new RPWatchContext(message, targetMM);
252 IdentityService.RP.watch(context);
255 _unwatch: function DOMIdentity_unwatch(message, targetMM) {
256 IdentityService.RP.unwatch(message.id, targetMM);
259 _request: function DOMIdentity__request(message) {
260 IdentityService.RP.request(message.id, message);
263 _logout: function DOMIdentity__logout(message) {
264 IdentityService.RP.logout(message.id, message.origin, message);
267 _childProcessShutdown: function DOMIdentity__childProcessShutdown(targetMM) {
268 IdentityService.RP.childProcessShutdown(targetMM);
271 _beginProvisioning: function DOMIdentity__beginProvisioning(message, targetMM) {
272 let context = new IDPProvisioningContext(message.id, message.origin,
274 IdentityService.IDP.beginProvisioning(context);
277 _genKeyPair: function DOMIdentity__genKeyPair(message) {
278 IdentityService.IDP.genKeyPair(message.id);
281 _registerCertificate: function DOMIdentity__registerCertificate(message) {
282 IdentityService.IDP.registerCertificate(message.id, message.cert);
285 _provisioningFailure: function DOMIdentity__provisioningFailure(message) {
286 IdentityService.IDP.raiseProvisioningFailure(message.id, message.reason);
289 _beginAuthentication: function DOMIdentity__beginAuthentication(message, targetMM) {
290 let context = new IDPAuthenticationContext(message.id, message.origin,
292 IdentityService.IDP.beginAuthentication(context);
295 _completeAuthentication: function DOMIdentity__completeAuthentication(message) {
296 IdentityService.IDP.completeAuthentication(message.id);
299 _authenticationFailure: function DOMIdentity__authenticationFailure(message) {
300 IdentityService.IDP.cancelAuthentication(message.id);
304 // Object is initialized by nsIDService.js