DojoExternalInterface.as 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. A wrapper around Flash 8's ExternalInterface; DojoExternalInterface is needed so that we
  3. can do a Flash 6 implementation of ExternalInterface, and be able
  4. to support having a single codebase that uses DojoExternalInterface
  5. across Flash versions rather than having two seperate source bases,
  6. where one uses ExternalInterface and the other uses DojoExternalInterface.
  7. DojoExternalInterface class does a variety of optimizations to bypass ExternalInterface's
  8. unbelievably bad performance so that we can have good performance
  9. on Safari; see the blog post
  10. http://codinginparadise.org/weblog/2006/02/how-to-speed-up-flash-8s.html
  11. for details.
  12. @author Brad Neuberg, bkn3@columbia.edu
  13. */
  14. import flash.external.ExternalInterface;
  15. class DojoExternalInterface{
  16. public static var available:Boolean;
  17. public static var dojoPath = "";
  18. private static var flashMethods:Array = new Array();
  19. private static var numArgs:Number;
  20. private static var argData:Array;
  21. private static var resultData = null;
  22. public static function initialize(){
  23. // extract the dojo base path
  24. DojoExternalInterface.dojoPath = DojoExternalInterface.getDojoPath();
  25. // see if we need to do an express install
  26. var install:ExpressInstall = new ExpressInstall();
  27. if(install.needsUpdate){
  28. install.init();
  29. }
  30. // register our callback functions
  31. ExternalInterface.addCallback("startExec", DojoExternalInterface, startExec);
  32. ExternalInterface.addCallback("setNumberArguments", DojoExternalInterface,
  33. setNumberArguments);
  34. ExternalInterface.addCallback("chunkArgumentData", DojoExternalInterface,
  35. chunkArgumentData);
  36. ExternalInterface.addCallback("exec", DojoExternalInterface, exec);
  37. ExternalInterface.addCallback("getReturnLength", DojoExternalInterface,
  38. getReturnLength);
  39. ExternalInterface.addCallback("chunkReturnData", DojoExternalInterface,
  40. chunkReturnData);
  41. ExternalInterface.addCallback("endExec", DojoExternalInterface, endExec);
  42. // set whether communication is available
  43. DojoExternalInterface.available = ExternalInterface.available;
  44. DojoExternalInterface.call("loaded");
  45. }
  46. public static function addCallback(methodName:String, instance:Object,
  47. method:Function) : Boolean{
  48. // register DojoExternalInterface methodName with it's instance
  49. DojoExternalInterface.flashMethods[methodName] = instance;
  50. // tell JavaScript about DojoExternalInterface new method so we can create a proxy
  51. ExternalInterface.call("dojox.flash.comm._addExternalInterfaceCallback",
  52. methodName);
  53. return true;
  54. }
  55. public static function call(methodName:String,
  56. resultsCallback:Function) : Void{
  57. // we might have any number of optional arguments, so we have to
  58. // pass them in dynamically; strip out the results callback
  59. var parameters = new Array();
  60. for(var i = 0; i < arguments.length; i++){
  61. if(i != 1){ // skip the callback
  62. parameters.push(arguments[i]);
  63. }
  64. }
  65. var results = ExternalInterface.call.apply(ExternalInterface, parameters);
  66. // immediately give the results back, since ExternalInterface is
  67. // synchronous
  68. if(resultsCallback != null && typeof resultsCallback != "undefined"){
  69. resultsCallback.call(null, results);
  70. }
  71. }
  72. /**
  73. Called by Flash to indicate to JavaScript that we are ready to have
  74. our Flash functions called. Calling loaded()
  75. will fire the dojox.flash.loaded() event, so that JavaScript can know that
  76. Flash has finished loading and adding its callbacks, and can begin to
  77. interact with the Flash file.
  78. */
  79. public static function loaded(){
  80. DojoExternalInterface.call("dojox.flash.loaded");
  81. }
  82. public static function startExec():Void{
  83. DojoExternalInterface.numArgs = null;
  84. DojoExternalInterface.argData = null;
  85. DojoExternalInterface.resultData = null;
  86. }
  87. public static function setNumberArguments(numArgs):Void{
  88. DojoExternalInterface.numArgs = numArgs;
  89. DojoExternalInterface.argData = new Array(DojoExternalInterface.numArgs);
  90. }
  91. public static function chunkArgumentData(value, argIndex:Number):Void{
  92. //getURL("javascript:console.debug('FLASH: chunkArgumentData, value="+value+", argIndex="+argIndex+"')");
  93. var currentValue = DojoExternalInterface.argData[argIndex];
  94. if(currentValue == null || typeof currentValue == "undefined"){
  95. DojoExternalInterface.argData[argIndex] = value;
  96. }else{
  97. DojoExternalInterface.argData[argIndex] += value;
  98. }
  99. }
  100. public static function exec(methodName):Void{
  101. // decode all of the arguments that were passed in
  102. for(var i = 0; i < DojoExternalInterface.argData.length; i++){
  103. DojoExternalInterface.argData[i] =
  104. DojoExternalInterface.decodeData(DojoExternalInterface.argData[i]);
  105. }
  106. var instance = DojoExternalInterface.flashMethods[methodName];
  107. DojoExternalInterface.resultData = instance[methodName].apply(
  108. instance, DojoExternalInterface.argData);
  109. // encode the result data
  110. DojoExternalInterface.resultData =
  111. DojoExternalInterface.encodeData(DojoExternalInterface.resultData);
  112. //getURL("javascript:console.debug('FLASH: encoded result data="+DojoExternalInterface.resultData+"')");
  113. }
  114. public static function getReturnLength():Number{
  115. if(DojoExternalInterface.resultData == null ||
  116. typeof DojoExternalInterface.resultData == "undefined"){
  117. return 0;
  118. }
  119. var segments = Math.ceil(DojoExternalInterface.resultData.length / 1024);
  120. return segments;
  121. }
  122. public static function chunkReturnData(segment:Number):String{
  123. var numSegments = DojoExternalInterface.getReturnLength();
  124. var startCut = segment * 1024;
  125. var endCut = segment * 1024 + 1024;
  126. if(segment == (numSegments - 1)){
  127. endCut = segment * 1024 + DojoExternalInterface.resultData.length;
  128. }
  129. var piece = DojoExternalInterface.resultData.substring(startCut, endCut);
  130. //getURL("javascript:console.debug('FLASH: chunking return piece="+piece+"')");
  131. return piece;
  132. }
  133. public static function endExec():Void{
  134. }
  135. private static function decodeData(data):String{
  136. // we have to use custom encodings for certain characters when passing
  137. // them over; for example, passing a backslash over as //// from JavaScript
  138. // to Flash doesn't work
  139. data = DojoExternalInterface.replaceStr(data, "&custom_backslash;", "\\");
  140. data = DojoExternalInterface.replaceStr(data, "\\\'", "\'");
  141. data = DojoExternalInterface.replaceStr(data, "\\\"", "\"");
  142. return data;
  143. }
  144. private static function encodeData(data){
  145. //getURL("javascript:console.debug('inside flash, data before="+data+"')");
  146. // double encode all entity values, or they will be mis-decoded
  147. // by Flash when returned
  148. data = DojoExternalInterface.replaceStr(data, "&", "&amp;");
  149. // certain XMLish characters break Flash's wire serialization for
  150. // ExternalInterface; encode these into a custom encoding, rather than
  151. // the standard entity encoding, because otherwise we won't be able to
  152. // differentiate between our own encoding and any entity characters
  153. // that are being used in the string itself
  154. data = DojoExternalInterface.replaceStr(data, '<', '&custom_lt;');
  155. data = DojoExternalInterface.replaceStr(data, '>', '&custom_gt;');
  156. // encode control characters and JavaScript delimiters
  157. data = DojoExternalInterface.replaceStr(data, "\n", "\\n");
  158. data = DojoExternalInterface.replaceStr(data, "\r", "\\r");
  159. data = DojoExternalInterface.replaceStr(data, "\f", "\\f");
  160. data = DojoExternalInterface.replaceStr(data, "'", "\\'");
  161. data = DojoExternalInterface.replaceStr(data, '"', '\"');
  162. //getURL("javascript:console.debug('inside flash, data after="+data+"')");
  163. return data;
  164. }
  165. /**
  166. Flash ActionScript has no String.replace method or support for
  167. Regular Expressions! We roll our own very simple one.
  168. */
  169. private static function replaceStr(inputStr:String, replaceThis:String,
  170. withThis:String):String {
  171. var splitStr = inputStr.split(replaceThis)
  172. inputStr = splitStr.join(withThis)
  173. return inputStr;
  174. }
  175. private static function getDojoPath(){
  176. var url = _root._url;
  177. var start = url.indexOf("baseRelativePath=") + "baseRelativePath=".length;
  178. var path = url.substring(start);
  179. var end = path.indexOf("&");
  180. if(end != -1){
  181. path = path.substring(0, end);
  182. }
  183. return path;
  184. }
  185. }
  186. // vim:ts=4:noet:tw=0: