You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
3.0 KiB

  1. if (IsUsingNaCl()) NaClLoadingInit();
  2. function InitNaClModuleVar() { g_embed_module = GetiFrameModuleVar(); }
  3. function RegisterListener()
  4. {
  5. //Register all the correct functions to the listener
  6. var div_listener = GetiFrameDivEmbed();
  7. if (div_listener)
  8. {
  9. div_listener.addEventListener('loadstart', ModuleStartedLoad, true);
  10. div_listener.addEventListener('load', ModuleDidLoad, true);
  11. div_listener.addEventListener('error', ModuleError, true);
  12. div_listener.addEventListener('progress', ModuleLoadUpdate, true);
  13. div_listener.addEventListener('message', ModuleSentMessage, true);
  14. div_listener.addEventListener('crash', ModuleCrash, true);
  15. window.setTimeout("UpdateTextStatus(0.1)", 100);
  16. }
  17. else if (IsUsingNaCl())
  18. window.setTimeout("RegisterListener()", 200);
  19. }
  20. //-------------------------------------------------------------------------
  21. // MODULE LOADING FUNCTIONS
  22. //-------------------------------------------------------------------------
  23. //Indicate page has been loaded.
  24. function NaClLoadingInit()
  25. {
  26. HideProgressStatus(false);
  27. //Page did load before NaCl module
  28. if (!g_embed_module)
  29. AddTextStatus('Please wait for module loading');
  30. RegisterListener();
  31. }
  32. //Module starts load
  33. function ModuleStartedLoad()
  34. {
  35. AddTextStatus('Module Started Loading');
  36. InitNaClModuleVar();
  37. }
  38. //Module progress event
  39. function ModuleLoadUpdate(event)
  40. {
  41. if (event.lengthComputable)
  42. UpdateProgressBarValue('Please wait, loading', event.loaded, event.total);
  43. else
  44. UpdateProgressBarValue('Please wait, calculating load balance ...');
  45. }
  46. //Indicate module load success.
  47. function ModuleDidLoad()
  48. {
  49. if (!g_embed_module)
  50. InitNaClModuleVar();
  51. //Hide the progress div
  52. AddTextStatus('Module is live, thank you for your patience.');
  53. window.setTimeout('HideProgressStatus(true)', GetMaxStatusTime(0.5) * 1000);
  54. }
  55. //Module did crash
  56. function ModuleCrash(event)
  57. {
  58. RestartModule();
  59. AddTextStatus("Module has crashed ! Restarting ...");
  60. }
  61. //Module had an error
  62. function ModuleError(event)
  63. {
  64. RestartModule();
  65. AddTextStatus("Module Load/start Error ! Restarting ...");
  66. }
  67. //Used to restart module on crash/error/load fail ....
  68. function RestartModule()
  69. {
  70. var id_frame_embed = GetFrameData();
  71. if (id_frame_embed)
  72. {
  73. HideProgressStatus(false);
  74. window.setTimeout('GetFrameData().contentDocument.location.reload(true)', 1000);
  75. }
  76. }
  77. //-------------------------------------------------------------------------
  78. // MODULE COMMUNICATION FUNCTIONS
  79. //-------------------------------------------------------------------------
  80. //Handle message from the NaCl module
  81. function ModuleSentMessage(message)
  82. {
  83. alert('Module sent message: ' + message.data);
  84. }
  85. //Called by the "Send Mesh Command !" button
  86. function SendMessageToModule()
  87. {
  88. if (g_embed_module)
  89. g_embed_module.postMessage(GetTextAreaCodeSrc().value);
  90. else
  91. alert("Module not loaded !");
  92. }