"use strict"
const{classes:Cc,interfaces:Ci,utils:Cu}=Components;Cu.import("resource://gre/modules/DOMRequestHelper.jsm");Cu.import("resource://gre/modules/MobileIdentityCommon.jsm");Cu.import("resource://gre/modules/XPCOMUtils.jsm");const MOBILEIDSERVICE_CID=Components.ID("{6ec1806c-d61f-4724-9495-68c26d46dc53}");const IPC_MSG_NAMES=["MobileId:GetAssertion:Return:OK","MobileId:GetAssertion:Return:KO"];XPCOMUtils.defineLazyServiceGetter(this,"cpmm","@mozilla.org/childprocessmessagemanager;1","nsIMessageSender");function MobileIdentityService(){}
MobileIdentityService.prototype={__proto__:DOMRequestIpcHelper.prototype, _windows:{},getMobileIdAssertion:function(aWindow,aOptions){log.debug("getMobileIdAssertion");if(!this.init){this.initDOMRequestHelper(aWindow,IPC_MSG_NAMES);this.init=true;}
return new aWindow.Promise((resolve,reject)=>{let promiseId=this.getPromiseResolverId({resolve:resolve,reject:reject});this._windows[promiseId]=aWindow;cpmm.sendAsyncMessage("MobileId:GetAssertion",{promiseId:promiseId,options:aOptions},null,aWindow.document.nodePrincipal);});},receiveMessage:function(aMessage){let name=aMessage.name;let msg=aMessage.json;log.debug("Received message "+name+": "+JSON.stringify(msg));let promiseId=msg.promiseId||msg.requestID;let promise=this.takePromiseResolver(promiseId);if(!promise){log.error("Promise not found");return;}
let _window=this._windows[promiseId];delete this._windows[promiseId];if(!_window){log.error("No window object found");return;}
switch(name){case"MobileId:GetAssertion:Return:OK":if(!msg.result){promise.reject(new _window.DOMError(ERROR_INTERNAL_UNEXPECTED));} 
promise.resolve(msg.result);break;case"MobileId:GetAssertion:Return:KO":promise.reject(new _window.DOMError(msg.error||ERROR_UNKNOWN));break;}},classID:MOBILEIDSERVICE_CID,QueryInterface:XPCOMUtils.generateQI([Ci.nsIMobileIdentityService,Ci.nsISupportsWeakReference,Ci.nsIObserver]),classInfo:XPCOMUtils.generateCI({classID:MOBILEIDSERVICE_CID,contractID:"@mozilla.org/mobileidentity-service;1",interfaces:[Ci.nsIMobileIdentityService],flags:Ci.nsIClassInfo.SINGLETON})};this.NSGetFactory=XPCOMUtils.generateNSGetFactory([MobileIdentityService]);