Backed out changeset 9157ea42ff41 (bug 914925) for Android/B2G test bustage.
[gecko.git] / dom / identity / DOMIdentity.jsm
blob1b713c56d3a203d541edb9d59264d7fe5e084e07
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/. */
5 "use strict";
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");
21 #else
22                                   "resource://gre/modules/identity/Identity.jsm");
23 #endif
25 XPCOMUtils.defineLazyModuleGetter(this,
26                                   "Logger",
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) {
42   this._id = aID;
43   this._origin = aOrigin;
44   this._mm = aTargetMM;
47 IDPProvisioningContext.prototype = {
48   get id() this._id,
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",
56                               message);
57   },
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);
64   },
66   doError: function(msg) {
67     log("Provisioning ERROR: " + msg);
68   }
71 function IDPAuthenticationContext(aID, aOrigin, aTargetMM) {
72   this._id = aID;
73   this._origin = aOrigin;
74   this._mm = aTargetMM;
77 IDPAuthenticationContext.prototype = {
78   get id() this._id,
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",
85                               message);
86   },
88   doError: function IDPAC_doError(msg) {
89     log("Authentication ERROR: " + msg);
90   }
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");
99   }
101   // default for no loggedInUser is undefined, not null
102   this.loggedInUser = aOptions.loggedInUser;
104   // Maybe internal
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;
116     }
117     this._mm.sendAsyncMessage("Identity:RP:Watch:OnLogin", message);
118   },
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);
124   },
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);
130   },
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);
136   },
138   doError: function RPWatchContext_onerror(aMessage) {
139     log("doError: " + aMessage);
140   }
143 this.DOMIdentity = {
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) {
153       // RP
154       case "Identity:RP:Watch":
155         this._watch(msg, targetMM);
156         break;
157       case "Identity:RP:Unwatch":
158         this._unwatch(msg, targetMM);
159         break;
160       case "Identity:RP:Request":
161         this._request(msg, targetMM);
162         break;
163       case "Identity:RP:Logout":
164         this._logout(msg, targetMM);
165         break;
166       // IDP
167       case "Identity:IDP:BeginProvisioning":
168         this._beginProvisioning(msg, targetMM);
169         break;
170       case "Identity:IDP:GenKeyPair":
171         this._genKeyPair(msg);
172         break;
173       case "Identity:IDP:RegisterCertificate":
174         this._registerCertificate(msg);
175         break;
176       case "Identity:IDP:ProvisioningFailure":
177         this._provisioningFailure(msg);
178         break;
179       case "Identity:IDP:BeginAuthentication":
180         this._beginAuthentication(msg, targetMM);
181         break;
182       case "Identity:IDP:CompleteAuthentication":
183         this._completeAuthentication(msg);
184         break;
185       case "Identity:IDP:AuthenticationFailure":
186         this._authenticationFailure(msg);
187         break;
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,
191         // for example)
192         this._childProcessShutdown(targetMM);
193         break;
194     }
195   },
197   // nsIObserver
198   observe: function DOMIdentity_observe(aSubject, aTopic, aData) {
199     switch (aTopic) {
200       case "xpcom-shutdown":
201         this._unsubscribeListeners();
202         Services.obs.removeObserver(this, "xpcom-shutdown");
203         Services.ww.unregisterNotification(this);
204         break;
205     }
206   },
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"],
217   // Private.
218   _init: function DOMIdentity__init() {
219     Services.ww.registerNotification(this);
220     Services.obs.addObserver(this, "xpcom-shutdown", false);
221     this._subscribeListeners();
222   },
224   _subscribeListeners: function DOMIdentity__subscribeListeners() {
225     if (!ppmm) return;
226     for (let message of this.messages) {
227       ppmm.addMessageListener(message, this);
228     }
229   },
231   _unsubscribeListeners: function DOMIdentity__unsubscribeListeners() {
232     for (let message of this.messages) {
233       ppmm.removeMessageListener(message, this);
234     }
235     ppmm = null;
236   },
238   _resetFrameState: function(aContext) {
239     log("_resetFrameState: ", aContext.id);
240     if (!aContext._mm) {
241       throw new Error("ERROR: Trying to reset an invalid context");
242     }
243     let message = new IDDOMMessage({id: aContext.id});
244     aContext._mm.sendAsyncMessage("Identity:ResetState", message);
245   },
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
250     // callbacks.
251     let context = new RPWatchContext(message, targetMM);
252     IdentityService.RP.watch(context);
253   },
255   _unwatch: function DOMIdentity_unwatch(message, targetMM) {
256     IdentityService.RP.unwatch(message.id, targetMM);
257   },
259   _request: function DOMIdentity__request(message) {
260     IdentityService.RP.request(message.id, message);
261   },
263   _logout: function DOMIdentity__logout(message) {
264     IdentityService.RP.logout(message.id, message.origin, message);
265   },
267   _childProcessShutdown: function DOMIdentity__childProcessShutdown(targetMM) {
268     IdentityService.RP.childProcessShutdown(targetMM);
269   },
271   _beginProvisioning: function DOMIdentity__beginProvisioning(message, targetMM) {
272     let context = new IDPProvisioningContext(message.id, message.origin,
273                                              targetMM);
274     IdentityService.IDP.beginProvisioning(context);
275   },
277   _genKeyPair: function DOMIdentity__genKeyPair(message) {
278     IdentityService.IDP.genKeyPair(message.id);
279   },
281   _registerCertificate: function DOMIdentity__registerCertificate(message) {
282     IdentityService.IDP.registerCertificate(message.id, message.cert);
283   },
285   _provisioningFailure: function DOMIdentity__provisioningFailure(message) {
286     IdentityService.IDP.raiseProvisioningFailure(message.id, message.reason);
287   },
289   _beginAuthentication: function DOMIdentity__beginAuthentication(message, targetMM) {
290     let context = new IDPAuthenticationContext(message.id, message.origin,
291                                                targetMM);
292     IdentityService.IDP.beginAuthentication(context);
293   },
295   _completeAuthentication: function DOMIdentity__completeAuthentication(message) {
296     IdentityService.IDP.completeAuthentication(message.id);
297   },
299   _authenticationFailure: function DOMIdentity__authenticationFailure(message) {
300     IdentityService.IDP.cancelAuthentication(message.id);
301   }
304 // Object is initialized by nsIDService.js