onenethelper.webext.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. function vLog(msg)
  2. {
  3. var args = Array.prototype.slice.call(arguments);
  4. args.unshift('[onenet]');
  5. console.log.apply(console, args);
  6. }
  7. function onenet_guid()
  8. {
  9. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  10. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  11. return v.toString(16);
  12. });
  13. }
  14. class COneNetHelper{
  15. constructor()
  16. {
  17. this.name = 'OneNet';
  18. this.mCmdCbMap = new Map();
  19. this.evtReqName = 'EventOneNetHost';
  20. this.evtRespName = 'EventOneNetWeb';
  21. this.type = 'OneNetReqBg';
  22. this.msgGetUserInfo = 'GetUserInfo';
  23. this.msgGetSignAndToken = 'GetSignAndToken';
  24. this.msgGetUserCertInfo = 'GetUserCertInfo';
  25. this.msgLogin ='Login';
  26. this.msgLogout ='Logout';
  27. this.msgQuerySecPortalStatus = 'QuerySecPortalStatus';
  28. this.msgHeartBeat = 'HeartBeat';
  29. this.msgGetGateWayAddrList = 'GetGateWayAddrList';
  30. this.msgCheckExtensionInstalled = 'ExtensionInstalled';
  31. this.msgGetUASToken = 'GetUASToken';
  32. this.msgChangeUKeyPin = 'ChangeUKeyPin';
  33. this.msgTestAction = 'testAction';
  34. this.isInstalled = 0;
  35. this.timeout = 30000;
  36. this.AddonAvailableNotifyCB;
  37. }
  38. InsertCmdCBHandler(msg, webCB)
  39. {
  40. var that=this;
  41. this.mCmdCbMap.set(msg['logid'], webCB);
  42. setTimeout(function (){
  43. var resp = msg;
  44. var cb = that.mCmdCbMap.get(resp.logid);
  45. if( cb !== undefined)
  46. {
  47. that.DeleteCmdCBHandler(resp);
  48. if(cb == that.AddonAvailableNotifyCB)
  49. {
  50. that.isInstalled = 0;
  51. resp['addon']=that.isInstalled;
  52. }
  53. else
  54. {
  55. resp['ret'] = 0;
  56. resp['errinfo'] = 'timeout';
  57. }
  58. webCB(resp);
  59. }
  60. }, that.timeout);
  61. }
  62. DeleteCmdCBHandler(obj)
  63. {
  64. this.mCmdCbMap.delete(obj.logid);
  65. }
  66. InitCmdMsg(action, data)
  67. {
  68. var msg = {};
  69. if(action == this.msgCheckExtensionInstalled)
  70. {
  71. msg['logid'] = action;
  72. }
  73. else
  74. {
  75. msg['logid'] = onenet_guid();
  76. }
  77. msg['action'] = action;
  78. msg['data'] = data;
  79. return msg;
  80. }
  81. SendCustomCmd(msg,webCB)
  82. {
  83. if(msg['action'] === undefined)
  84. {
  85. vLog('CallEx() action error: ' + msg);
  86. return;
  87. }
  88. if(msg['data'] === undefined)
  89. {
  90. msg['data']="";
  91. }
  92. var tmpmsg = this.InitCmdMsg(msg['action'], msg['data']);
  93. this.InsertCmdCBHandler(tmpmsg, webCB);
  94. this.BgRequest(tmpmsg);
  95. }
  96. GetUASToken(notify_web)
  97. {
  98. var msg = this.InitCmdMsg(this.msgGetUASToken, "");
  99. this.InsertCmdCBHandler(msg, notify_web);
  100. this.BgRequest(msg);
  101. }
  102. GetUserInfo(notify_web)
  103. {
  104. var msg = this.InitCmdMsg(this.msgGetUserInfo, "");
  105. this.InsertCmdCBHandler(msg, notify_web);
  106. this.BgRequest(msg);
  107. }
  108. GetSignAndToken(randomnum, notify_web)
  109. {
  110. var hostname = location.hostname;
  111. vLog('GetSignAndToken: ' + hostname);
  112. var msg = this.InitCmdMsg(this.msgGetSignAndToken, randomnum);
  113. msg['appServerAddr'] = hostname;
  114. this.InsertCmdCBHandler(msg, notify_web);
  115. this.BgRequest(msg);
  116. }
  117. GetUserCertInfo(notify_web)
  118. {
  119. var msg = this.InitCmdMsg(this.msgGetUserCertInfo, "");
  120. this.InsertCmdCBHandler(msg, notify_web);
  121. this.BgRequest(msg);
  122. }
  123. Login(appToken,username,userpwd,authtype,svctype, gw_id, notify_web)
  124. {
  125. var msg = this.InitCmdMsg(this.msgLogin, "");
  126. msg['token'] = appToken;
  127. msg['username'] = username;
  128. msg['password'] = userpwd;
  129. msg['authtype']= authtype;
  130. msg['svctype']= svctype;
  131. msg['gw_id']= gw_id;
  132. this.InsertCmdCBHandler(msg, notify_web);
  133. this.BgRequest(msg);
  134. }
  135. ChangeUKeyPin(old_key_pin,new_key_pin,notify_web)
  136. {
  137. var msg = this.InitCmdMsg(this.msgChangeUKeyPin, "");
  138. msg['oldpin'] = old_key_pin;
  139. msg['newpin'] = new_key_pin;
  140. this.InsertCmdCBHandler(msg, notify_web);
  141. this.BgRequest(msg);
  142. }
  143. SSLUsbKeyLogin(appToken,gw_id,key_pin,notify_web)
  144. {
  145. var msg = this.InitCmdMsg(this.msgLogin, "");
  146. msg['token'] = appToken;
  147. msg['username'] ="";
  148. msg['password'] =key_pin;
  149. msg['authtype']= "1";
  150. msg['svctype']= "1";
  151. msg['gw_id']= gw_id;
  152. this.InsertCmdCBHandler(msg, notify_web);
  153. this.BgRequest(msg);
  154. }
  155. SSLUserPwdLogin(appToken,gw_id,username,userpwd,notify_web)
  156. {
  157. var msg = this.InitCmdMsg(this.msgLogin, "");
  158. msg['token'] = appToken;
  159. msg['username'] =username;
  160. msg['password'] =userpwd;
  161. msg['authtype']= "2";
  162. msg['svctype']= "1";
  163. msg['gw_id']= gw_id;
  164. this.InsertCmdCBHandler(msg, notify_web);
  165. this.BgRequest(msg);
  166. }
  167. IPSecUsbKeyLogin(appToken,gw_id,key_pin,notify_web)
  168. {
  169. var msg = this.InitCmdMsg(this.msgLogin, "");
  170. msg['token'] = appToken;
  171. msg['username'] ="";
  172. msg['password'] =key_pin;
  173. msg['authtype']= "1";
  174. msg['svctype']= "2";
  175. msg['gw_id']= gw_id;
  176. this.InsertCmdCBHandler(msg, notify_web);
  177. this.BgRequest(msg);
  178. }
  179. Logout(appToken,notify_web)
  180. {
  181. var msg = this.InitCmdMsg(this.msgLogout, "");
  182. msg['token'] = appToken;
  183. this.InsertCmdCBHandler(msg, notify_web);
  184. this.BgRequest(msg);
  185. }
  186. QuerySecPortalStatus(appToken,notify_web)
  187. {
  188. var msg = this.InitCmdMsg(this.msgQuerySecPortalStatus, "");
  189. msg['token'] = appToken;
  190. this.InsertCmdCBHandler(msg, notify_web);
  191. this.BgRequest(msg);
  192. }
  193. HeartBeat(appToken,notify_web)
  194. {
  195. var msg = this.InitCmdMsg(this.msgHeartBeat, "");
  196. msg['token'] = appToken;
  197. this.InsertCmdCBHandler(msg, notify_web);
  198. this.BgRequest(msg);
  199. }
  200. GetGateWayAddrList(notify_web)
  201. {
  202. var msg = this.InitCmdMsg(this.msgGetGateWayAddrList, "");
  203. this.InsertCmdCBHandler(msg, notify_web);
  204. this.BgRequest(msg);
  205. }
  206. JsonActionTestCmdDeal(notify_web)
  207. {
  208. var msg = this.InitCmdMsg(this.msgTestAction, "");
  209. this.InsertCmdCBHandler(msg, notify_web);
  210. this.BgRequest(msg);
  211. }
  212. IsAddonAvailable()
  213. {
  214. return this.isInstalled;
  215. }
  216. AddonAvailableNotify(notify_web)
  217. {
  218. if( notify_web === undefined)
  219. {
  220. alert('AddonAvailableNotify no set notify_web callback!');
  221. return;
  222. }
  223. this.AddonAvailableNotifyCB=notify_web;
  224. var msg = this.InitCmdMsg(this.msgCheckExtensionInstalled, "");
  225. this.InsertCmdCBHandler(msg, notify_web);
  226. this.BgRequest(msg);
  227. }
  228. BgRespone(msg)
  229. {
  230. vLog('BgRespone() rcv:' + msg);
  231. var obj = JSON.parse(msg);
  232. if(obj.action == this.msgCheckExtensionInstalled)
  233. {
  234. if(this.AddonAvailableNotifyCB !== undefined)
  235. {
  236. obj['logid'] = this.msgCheckExtensionInstalled;
  237. }
  238. else
  239. {
  240. this.isInstalled = 1;
  241. return;
  242. }
  243. }
  244. //var call = this.mCmdCbMap.get(obj.action);
  245. var cb = this.mCmdCbMap.get(obj.logid);
  246. if( cb !== undefined)
  247. {
  248. this.DeleteCmdCBHandler(obj);
  249. if(obj.action == this.msgCheckExtensionInstalled)
  250. {
  251. this.isInstalled = 1;
  252. obj['addon'] = this.isInstalled;
  253. this.AddonAvailableNotifyCB(obj);
  254. }
  255. else
  256. {
  257. cb(obj.info);
  258. }
  259. }
  260. else
  261. {
  262. vLog('OneNetHelper BgRespone() action error: ' + obj.action);
  263. }
  264. }
  265. BgRequest(msg)
  266. {
  267. var that=this;
  268. if(this.isInstalled == 1 || msg['action'] == this.msgCheckExtensionInstalled)
  269. {
  270. vLog('OneNetHelper BgRequest() send:' + JSON.stringify(msg));
  271. var evt = window.top.document.createEvent("CustomEvent");
  272. evt.initCustomEvent(this.evtReqName, true, false, msg);
  273. window.top.document.dispatchEvent(evt);
  274. }
  275. else
  276. {
  277. vLog('OneNetHelper BgRequest() err: oneNetHelper extension current is not available;' + JSON.stringify(msg));
  278. this.DeleteCmdCBHandler(msg.logid);
  279. }
  280. }
  281. }
  282. var oneNetHelper_webext=new COneNetHelper();
  283. //OneNetHandle: {"type":"ReqBg", "message":{"action":"GetUserInfo", "data":""}}
  284. window.top.document.addEventListener(oneNetHelper_webext.evtRespName, function(evt)
  285. {
  286. vLog('addEventListener::EventOneNetWeb():' + evt.detail);
  287. oneNetHelper_webext.BgRespone(evt.detail);
  288. }, false);