Debian Codegraph @

dojox/flash/DojoExternalInterface.as

raw
1 /**
2 A wrapper around Flash 8's ExternalInterface; this is needed
3 because ExternalInterface has a number of serialization bugs that we
4 need to correct for.
5
6 @author Brad Neuberg
7 */
8
9 import flash.external.ExternalInterface;
10
11 class DojoExternalInterface{
12 public static var available:Boolean;
13 public static var dojoPath = "";
14
15 public static function initialize(){
16 //trace("DojoExternalInterface.initialize");
17
18 // extract the dojo base path
19 DojoExternalInterface.dojoPath = DojoExternalInterface.getDojoPath();
20
21 // see if we need to do an express install
22 var install:ExpressInstall = new ExpressInstall();
23 if(install.needsUpdate){
24 install.init();
25 }
26
27 // set whether communication is available
28 DojoExternalInterface.available = ExternalInterface.available;
29
30 // make sure we can play nice in XD settings
31 // Note, this has been commented out as it may expose a cross domain sec issue
32 //System.security.allowDomain(unescape(_root.xdomain));
33 }
34
35 /** Called when we are finished adding methods through addCallback. */
36 public static function done(){
37 //trace("done");
38 DojoExternalInterface.call("dojox.flash.loaded");
39 }
40
41 public static function addCallback(methodName:String, instance:Object,
42 method:Function):Boolean{
43 //trace("addCallback");
44 ExternalInterface.addCallback(methodName, instance, function(){
45 instance = (instance) ? instance : null;
46 var params = [];
47 if(arguments && arguments.length){
48 for(var i = 0; i < arguments.length; i++){
49 params[i] = DojoExternalInterface.decodeData(arguments[i]);
50 }
51 }
52
53 var results = method.apply(instance, params);
54 results = DojoExternalInterface.encodeData(results);
55
56 return results;
57 });
58
59 // tell JavaScript about DojoExternalInterface new method so we can create a proxy
60 ExternalInterface.call("dojox.flash.comm._addExternalInterfaceCallback",
61 methodName);
62
63 return true;
64 }
65
66 public static function call(methodName:String):Void{
67 // we might have any number of optional arguments, so we have to
68 // pass them in dynamically; strip out the results callback
69 var parameters = new Array();
70 for(var i = 0; i < arguments.length; i++){
71 parameters.push(arguments[i]);
72 }
73
74 // FIXME: Should we be encoding or decoding the data to get
75 // around Flash's serialization bugs?
76
77 var results = ExternalInterface.call.apply(ExternalInterface, parameters);
78
79 return results;
80 }
81
82 /**
83 Called by Flash to indicate to JavaScript that we are ready to have
84 our Flash functions called. Calling loaded()
85 will fire the dojox.flash.loaded() event, so that JavaScript can know that
86 Flash has finished loading and adding its callbacks, and can begin to
87 interact with the Flash file.
88 */
89 public static function loaded(){
90 DojoExternalInterface.call("dojox.flash.loaded");
91 }
92
93 /**
94 Utility trace implementation that prints out to console.debug.
95 */
96 public static function trace(msg){
97 DojoExternalInterface.call("console.debug", "FLASH: " + msg);
98 }
99
100 private static function decodeData(data):String{
101 if(!data || typeof data != "string"){
102 return data;
103 }
104
105 // JAC: Using unicode character 0001 to store instead of Unicode null
106 // which causes trouble
107 data = replaceStr(data, "&custom_null;", "\u0001");
108
109 // we have to use custom encodings for certain characters when passing
110 // them over; for example, passing a backslash over as //// from JavaScript
111 // to Flash doesn't work
112 data = replaceStr(data, "&custom_backslash;", "\\");
113
114 return data;
115 }
116
117 private static function encodeData(data):String{
118 if(!data || typeof data != "string"){
119 return data;
120 }
121
122 // double encode all entity values, or they will be mis-decoded
123 // by Flash when returned
124 data = replaceStr(data, "&", "&amp;");
125
126 // certain XMLish characters break Flash's wire serialization for
127 // ExternalInterface; encode these into a custom encoding, rather than
128 // the standard entity encoding, because otherwise we won't be able to
129 // differentiate between our own encoding and any entity characters
130 // that are being used in the string itself
131 data = replaceStr(data, '<', '&custom_lt;');
132 data = replaceStr(data, '>', '&custom_gt;');
133
134 // needed for IE
135 data = replaceStr(data, '\\', '&custom_backslash;');
136 data = replaceStr(data, "\u0001", "&custom_null;");
137
138 // encode control characters and JavaScript delimiters
139 data = replaceStr(data, "\n", "\\n");
140 data = replaceStr(data, "\r", "\\r");
141 data = replaceStr(data, "\f", "\\f");
142 data = replaceStr(data, "'", "\\'");
143 data = replaceStr(data, '"', '\"');
144
145 return data;
146 }
147
148 /**
149 Flash ActionScript has no String.replace method or support for
150 Regular Expressions! We roll our own very simple one.
151 */
152 public static function replaceStr(inputStr:String, replaceThis:String,
153 withThis:String):String{
154 var splitStr = inputStr.split(replaceThis);
155 if(!splitStr){
156 return inputStr;
157 }
158
159 inputStr = splitStr.join(withThis);
160 return inputStr;
161 }
162
163 private static function getDojoPath(){
164 var url = _root._url;
165 var start = url.indexOf("baseUrl=") + "baseUrl=".length;
166 var path = url.substring(start);
167 var end = path.indexOf("&");
168 if(end != -1){
169 path = path.substring(0, end);
170 }
171
172 // some browsers append a junk string at the end: '%20'%20quality=
173 if(path.indexOf("'%20'%20quality=") != -1){
174 path = path.substring(0, path.indexOf("'%20'%20quality="));
175 }
176 return unescape(path);
177 }
178 }