function vLog(msg) { var args = Array.prototype.slice.call(arguments); args.unshift('[onenet]'); console.log.apply(console, args); } function onenet_guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); } class COneNetHelper{ constructor() { this.name = 'OneNet'; this.mCmdCbMap = new Map(); this.evtReqName = 'EventOneNetHost'; this.evtRespName = 'EventOneNetWeb'; this.type = 'OneNetReqBg'; this.msgGetUserInfo = 'GetUserInfo'; this.msgGetSignAndToken = 'GetSignAndToken'; this.msgGetUserCertInfo = 'GetUserCertInfo'; this.msgLogin ='Login'; this.msgLogout ='Logout'; this.msgQuerySecPortalStatus = 'QuerySecPortalStatus'; this.msgHeartBeat = 'HeartBeat'; this.msgGetGateWayAddrList = 'GetGateWayAddrList'; this.msgCheckExtensionInstalled = 'ExtensionInstalled'; this.msgGetUASToken = 'GetUASToken'; this.msgChangeUKeyPin = 'ChangeUKeyPin'; this.msgTestAction = 'testAction'; this.isInstalled = 0; this.timeout = 30000; this.AddonAvailableNotifyCB; } InsertCmdCBHandler(msg, webCB) { var that=this; this.mCmdCbMap.set(msg['logid'], webCB); setTimeout(function (){ var resp = msg; var cb = that.mCmdCbMap.get(resp.logid); if( cb !== undefined) { that.DeleteCmdCBHandler(resp); if(cb == that.AddonAvailableNotifyCB) { that.isInstalled = 0; resp['addon']=that.isInstalled; } else { resp['ret'] = 0; resp['errinfo'] = 'timeout'; } webCB(resp); } }, that.timeout); } DeleteCmdCBHandler(obj) { this.mCmdCbMap.delete(obj.logid); } InitCmdMsg(action, data) { var msg = {}; if(action == this.msgCheckExtensionInstalled) { msg['logid'] = action; } else { msg['logid'] = onenet_guid(); } msg['action'] = action; msg['data'] = data; return msg; } SendCustomCmd(msg,webCB) { if(msg['action'] === undefined) { vLog('CallEx() action error: ' + msg); return; } if(msg['data'] === undefined) { msg['data']=""; } var tmpmsg = this.InitCmdMsg(msg['action'], msg['data']); this.InsertCmdCBHandler(tmpmsg, webCB); this.BgRequest(tmpmsg); } GetUASToken(notify_web) { var msg = this.InitCmdMsg(this.msgGetUASToken, ""); this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } GetUserInfo(notify_web) { var msg = this.InitCmdMsg(this.msgGetUserInfo, ""); this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } GetSignAndToken(randomnum, notify_web) { var hostname = location.hostname; vLog('GetSignAndToken: ' + hostname); var msg = this.InitCmdMsg(this.msgGetSignAndToken, randomnum); msg['appServerAddr'] = hostname; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } GetUserCertInfo(notify_web) { var msg = this.InitCmdMsg(this.msgGetUserCertInfo, ""); this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } Login(appToken,username,userpwd,authtype,svctype, gw_id, notify_web) { var msg = this.InitCmdMsg(this.msgLogin, ""); msg['token'] = appToken; msg['username'] = username; msg['password'] = userpwd; msg['authtype']= authtype; msg['svctype']= svctype; msg['gw_id']= gw_id; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } ChangeUKeyPin(old_key_pin,new_key_pin,notify_web) { var msg = this.InitCmdMsg(this.msgChangeUKeyPin, ""); msg['oldpin'] = old_key_pin; msg['newpin'] = new_key_pin; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } SSLUsbKeyLogin(appToken,gw_id,key_pin,notify_web) { var msg = this.InitCmdMsg(this.msgLogin, ""); msg['token'] = appToken; msg['username'] =""; msg['password'] =key_pin; msg['authtype']= "1"; msg['svctype']= "1"; msg['gw_id']= gw_id; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } SSLUserPwdLogin(appToken,gw_id,username,userpwd,notify_web) { var msg = this.InitCmdMsg(this.msgLogin, ""); msg['token'] = appToken; msg['username'] =username; msg['password'] =userpwd; msg['authtype']= "2"; msg['svctype']= "1"; msg['gw_id']= gw_id; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } IPSecUsbKeyLogin(appToken,gw_id,key_pin,notify_web) { var msg = this.InitCmdMsg(this.msgLogin, ""); msg['token'] = appToken; msg['username'] =""; msg['password'] =key_pin; msg['authtype']= "1"; msg['svctype']= "2"; msg['gw_id']= gw_id; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } Logout(appToken,notify_web) { var msg = this.InitCmdMsg(this.msgLogout, ""); msg['token'] = appToken; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } QuerySecPortalStatus(appToken,notify_web) { var msg = this.InitCmdMsg(this.msgQuerySecPortalStatus, ""); msg['token'] = appToken; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } HeartBeat(appToken,notify_web) { var msg = this.InitCmdMsg(this.msgHeartBeat, ""); msg['token'] = appToken; this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } GetGateWayAddrList(notify_web) { var msg = this.InitCmdMsg(this.msgGetGateWayAddrList, ""); this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } JsonActionTestCmdDeal(notify_web) { var msg = this.InitCmdMsg(this.msgTestAction, ""); this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } IsAddonAvailable() { return this.isInstalled; } AddonAvailableNotify(notify_web) { if( notify_web === undefined) { alert('AddonAvailableNotify no set notify_web callback!'); return; } this.AddonAvailableNotifyCB=notify_web; var msg = this.InitCmdMsg(this.msgCheckExtensionInstalled, ""); this.InsertCmdCBHandler(msg, notify_web); this.BgRequest(msg); } BgRespone(msg) { vLog('BgRespone() rcv:' + msg); var obj = JSON.parse(msg); if(obj.action == this.msgCheckExtensionInstalled) { if(this.AddonAvailableNotifyCB !== undefined) { obj['logid'] = this.msgCheckExtensionInstalled; } else { this.isInstalled = 1; return; } } //var call = this.mCmdCbMap.get(obj.action); var cb = this.mCmdCbMap.get(obj.logid); if( cb !== undefined) { this.DeleteCmdCBHandler(obj); if(obj.action == this.msgCheckExtensionInstalled) { this.isInstalled = 1; obj['addon'] = this.isInstalled; this.AddonAvailableNotifyCB(obj); } else { cb(obj.info); } } else { vLog('OneNetHelper BgRespone() action error: ' + obj.action); } } BgRequest(msg) { var that=this; if(this.isInstalled == 1 || msg['action'] == this.msgCheckExtensionInstalled) { vLog('OneNetHelper BgRequest() send:' + JSON.stringify(msg)); var evt = window.top.document.createEvent("CustomEvent"); evt.initCustomEvent(this.evtReqName, true, false, msg); window.top.document.dispatchEvent(evt); } else { vLog('OneNetHelper BgRequest() err: oneNetHelper extension current is not available;' + JSON.stringify(msg)); this.DeleteCmdCBHandler(msg.logid); } } } var oneNetHelper_webext=new COneNetHelper(); //OneNetHandle: {"type":"ReqBg", "message":{"action":"GetUserInfo", "data":""}} window.top.document.addEventListener(oneNetHelper_webext.evtRespName, function(evt) { vLog('addEventListener::EventOneNetWeb():' + evt.detail); oneNetHelper_webext.BgRespone(evt.detail); }, false);