FABridge.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. /*
  3. Copyright 2006 Adobe Systems Incorporated
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  10. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  11. OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12. */
  13. /*
  14. * The Bridge class, responsible for navigating AS instances
  15. */
  16. function FABridge(target,bridgeName)
  17. {
  18. this.target = target;
  19. this.remoteTypeCache = {};
  20. this.remoteInstanceCache = {};
  21. this.remoteFunctionCache = {};
  22. this.localFunctionCache = {};
  23. this.bridgeID = FABridge.nextBridgeID++;
  24. this.name = bridgeName;
  25. this.nextLocalFuncID = 0;
  26. FABridge.instances[this.name] = this;
  27. FABridge.idMap[this.bridgeID] = this;
  28. return this;
  29. }
  30. // type codes for packed values
  31. FABridge.TYPE_ASINSTANCE = 1;
  32. FABridge.TYPE_ASFUNCTION = 2;
  33. FABridge.TYPE_JSFUNCTION = 3;
  34. FABridge.TYPE_ANONYMOUS = 4;
  35. FABridge.initCallbacks = {};
  36. FABridge.userTypes = {};
  37. FABridge.addToUserTypes = function()
  38. {
  39. for (var i = 0; i < arguments.length; i++)
  40. {
  41. FABridge.userTypes[arguments[i]] = {
  42. 'typeName': arguments[i],
  43. 'enriched': false
  44. };
  45. }
  46. }
  47. FABridge.argsToArray = function(args)
  48. {
  49. var result = [];
  50. for (var i = 0; i < args.length; i++)
  51. {
  52. result[i] = args[i];
  53. }
  54. return result;
  55. }
  56. function instanceFactory(objID)
  57. {
  58. this.fb_instance_id = objID;
  59. return this;
  60. }
  61. function FABridge__invokeJSFunction(args)
  62. {
  63. var funcID = args[0];
  64. var throughArgs = args.concat();//FABridge.argsToArray(arguments);
  65. throughArgs.shift();
  66. var bridge = FABridge.extractBridgeFromID(funcID);
  67. return bridge.invokeLocalFunction(funcID, throughArgs);
  68. }
  69. FABridge.addInitializationCallback = function(bridgeName, callback)
  70. {
  71. var inst = FABridge.instances[bridgeName];
  72. if (inst != undefined)
  73. {
  74. callback.call(inst);
  75. return;
  76. }
  77. var callbackList = FABridge.initCallbacks[bridgeName];
  78. if(callbackList == null)
  79. {
  80. FABridge.initCallbacks[bridgeName] = callbackList = [];
  81. }
  82. callbackList.push(callback);
  83. }
  84. // updated for changes to SWFObject2
  85. function FABridge__bridgeInitialized(bridgeName) {
  86. var objects = document.getElementsByTagName("object");
  87. var ol = objects.length;
  88. var activeObjects = [];
  89. if (ol > 0) {
  90. for (var i = 0; i < ol; i++) {
  91. if (typeof objects[i].SetVariable != "undefined") {
  92. activeObjects[activeObjects.length] = objects[i];
  93. }
  94. }
  95. }
  96. var embeds = document.getElementsByTagName("embed");
  97. var el = embeds.length;
  98. var activeEmbeds = [];
  99. if (el > 0) {
  100. for (var j = 0; j < el; j++) {
  101. if (typeof embeds[j].SetVariable != "undefined") {
  102. activeEmbeds[activeEmbeds.length] = embeds[j];
  103. }
  104. }
  105. }
  106. var aol = activeObjects.length;
  107. var ael = activeEmbeds.length;
  108. var searchStr = "bridgeName="+ bridgeName;
  109. if ((aol == 1 && !ael) || (aol == 1 && ael == 1)) {
  110. FABridge.attachBridge(activeObjects[0], bridgeName);
  111. }
  112. else if (ael == 1 && !aol) {
  113. FABridge.attachBridge(activeEmbeds[0], bridgeName);
  114. }
  115. else {
  116. var flash_found = false;
  117. if (aol > 1) {
  118. for (var k = 0; k < aol; k++) {
  119. var params = activeObjects[k].childNodes;
  120. for (var l = 0; l < params.length; l++) {
  121. var param = params[l];
  122. if (param.nodeType == 1 && param.tagName.toLowerCase() == "param" && param["name"].toLowerCase() == "flashvars" && param["value"].indexOf(searchStr) >= 0) {
  123. FABridge.attachBridge(activeObjects[k], bridgeName);
  124. flash_found = true;
  125. break;
  126. }
  127. }
  128. if (flash_found) {
  129. break;
  130. }
  131. }
  132. }
  133. if (!flash_found && ael > 1) {
  134. for (var m = 0; m < ael; m++) {
  135. var flashVars = activeEmbeds[m].attributes.getNamedItem("flashVars").nodeValue;
  136. if (flashVars.indexOf(searchStr) >= 0) {
  137. FABridge.attachBridge(activeEmbeds[m], bridgeName);
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. // used to track multiple bridge instances, since callbacks from AS are global across the page.
  146. FABridge.nextBridgeID = 0;
  147. FABridge.instances = {};
  148. FABridge.idMap = {};
  149. FABridge.refCount = 0;
  150. FABridge.extractBridgeFromID = function(id)
  151. {
  152. var bridgeID = (id >> 16);
  153. return FABridge.idMap[bridgeID];
  154. }
  155. FABridge.attachBridge = function(instance, bridgeName)
  156. {
  157. var newBridgeInstance = new FABridge(instance, bridgeName);
  158. FABridge[bridgeName] = newBridgeInstance;
  159. /* FABridge[bridgeName] = function() {
  160. return newBridgeInstance.root();
  161. }
  162. */
  163. var callbacks = FABridge.initCallbacks[bridgeName];
  164. if (callbacks == null)
  165. {
  166. return;
  167. }
  168. for (var i = 0; i < callbacks.length; i++)
  169. {
  170. callbacks[i].call(newBridgeInstance);
  171. }
  172. delete FABridge.initCallbacks[bridgeName]
  173. }
  174. // some methods can't be proxied. You can use the explicit get,set, and call methods if necessary.
  175. FABridge.blockedMethods =
  176. {
  177. toString: true,
  178. get: true,
  179. set: true,
  180. call: true
  181. };
  182. FABridge.prototype =
  183. {
  184. // bootstrapping
  185. root: function()
  186. {
  187. return this.deserialize(this.target.getRoot());
  188. },
  189. //clears all of the AS objects in the cache maps
  190. releaseASObjects: function()
  191. {
  192. return this.target.releaseASObjects();
  193. },
  194. //clears a specific object in AS from the type maps
  195. releaseNamedASObject: function(value)
  196. {
  197. if(typeof(value) != "object")
  198. {
  199. return false;
  200. }
  201. else
  202. {
  203. var ret = this.target.releaseNamedASObject(value.fb_instance_id);
  204. return ret;
  205. }
  206. },
  207. //create a new AS Object
  208. create: function(className)
  209. {
  210. return this.deserialize(this.target.create(className));
  211. },
  212. // utilities
  213. makeID: function(token)
  214. {
  215. return (this.bridgeID << 16) + token;
  216. },
  217. // low level access to the flash object
  218. //get a named property from an AS object
  219. getPropertyFromAS: function(objRef, propName)
  220. {
  221. if (FABridge.refCount > 0)
  222. {
  223. throw new Error("You are trying to call recursively into the Flash Player which is not allowed. In most cases the JavaScript setTimeout function, can be used as a workaround.");
  224. }
  225. else
  226. {
  227. FABridge.refCount++;
  228. retVal = this.target.getPropFromAS(objRef, propName);
  229. retVal = this.handleError(retVal);
  230. FABridge.refCount--;
  231. return retVal;
  232. }
  233. },
  234. //set a named property on an AS object
  235. setPropertyInAS: function(objRef,propName, value)
  236. {
  237. if (FABridge.refCount > 0)
  238. {
  239. throw new Error("You are trying to call recursively into the Flash Player which is not allowed. In most cases the JavaScript setTimeout function, can be used as a workaround.");
  240. }
  241. else
  242. {
  243. FABridge.refCount++;
  244. retVal = this.target.setPropInAS(objRef,propName, this.serialize(value));
  245. retVal = this.handleError(retVal);
  246. FABridge.refCount--;
  247. return retVal;
  248. }
  249. },
  250. //call an AS function
  251. callASFunction: function(funcID, args)
  252. {
  253. if (FABridge.refCount > 0)
  254. {
  255. throw new Error("You are trying to call recursively into the Flash Player which is not allowed. In most cases the JavaScript setTimeout function, can be used as a workaround.");
  256. }
  257. else
  258. {
  259. FABridge.refCount++;
  260. retVal = this.target.invokeASFunction(funcID, this.serialize(args));
  261. retVal = this.handleError(retVal);
  262. FABridge.refCount--;
  263. return retVal;
  264. }
  265. },
  266. //call a method on an AS object
  267. callASMethod: function(objID, funcName, args)
  268. {
  269. if (FABridge.refCount > 0)
  270. {
  271. throw new Error("You are trying to call recursively into the Flash Player which is not allowed. In most cases the JavaScript setTimeout function, can be used as a workaround.");
  272. }
  273. else
  274. {
  275. FABridge.refCount++;
  276. args = this.serialize(args);
  277. retVal = this.target.invokeASMethod(objID, funcName, args);
  278. retVal = this.handleError(retVal);
  279. FABridge.refCount--;
  280. return retVal;
  281. }
  282. },
  283. // responders to remote calls from flash
  284. //callback from flash that executes a local JS function
  285. //used mostly when setting js functions as callbacks on events
  286. invokeLocalFunction: function(funcID, args)
  287. {
  288. var result;
  289. var func = this.localFunctionCache[funcID];
  290. if(func != undefined)
  291. {
  292. result = this.serialize(func.apply(null, this.deserialize(args)));
  293. }
  294. return result;
  295. },
  296. // Object Types and Proxies
  297. // accepts an object reference, returns a type object matching the obj reference.
  298. getTypeFromName: function(objTypeName)
  299. {
  300. return this.remoteTypeCache[objTypeName];
  301. },
  302. //create an AS proxy for the given object ID and type
  303. createProxy: function(objID, typeName)
  304. {
  305. var objType = this.getTypeFromName(typeName);
  306. instanceFactory.prototype = objType;
  307. var instance = new instanceFactory(objID);
  308. this.remoteInstanceCache[objID] = instance;
  309. return instance;
  310. },
  311. //return the proxy associated with the given object ID
  312. getProxy: function(objID)
  313. {
  314. return this.remoteInstanceCache[objID];
  315. },
  316. // accepts a type structure, returns a constructed type
  317. addTypeDataToCache: function(typeData)
  318. {
  319. var newType = new ASProxy(this, typeData.name);
  320. var accessors = typeData.accessors;
  321. for (var i = 0; i < accessors.length; i++)
  322. {
  323. this.addPropertyToType(newType, accessors[i]);
  324. }
  325. var methods = typeData.methods;
  326. for (var i = 0; i < methods.length; i++)
  327. {
  328. if (FABridge.blockedMethods[methods[i]] == undefined)
  329. {
  330. this.addMethodToType(newType, methods[i]);
  331. }
  332. }
  333. this.remoteTypeCache[newType.typeName] = newType;
  334. return newType;
  335. },
  336. //add a property to a typename; used to define the properties that can be called on an AS proxied object
  337. addPropertyToType: function(ty, propName)
  338. {
  339. var c = propName.charAt(0);
  340. var setterName;
  341. var getterName;
  342. if(c >= "a" && c <= "z")
  343. {
  344. getterName = "get" + c.toUpperCase() + propName.substr(1);
  345. setterName = "set" + c.toUpperCase() + propName.substr(1);
  346. }
  347. else
  348. {
  349. getterName = "get" + propName;
  350. setterName = "set" + propName;
  351. }
  352. ty[setterName] = function(val)
  353. {
  354. this.bridge.setPropertyInAS(this.fb_instance_id, propName, val);
  355. }
  356. ty[getterName] = function()
  357. {
  358. return this.bridge.deserialize(this.bridge.getPropertyFromAS(this.fb_instance_id, propName));
  359. }
  360. },
  361. //add a method to a typename; used to define the methods that can be callefd on an AS proxied object
  362. addMethodToType: function(ty, methodName)
  363. {
  364. ty[methodName] = function()
  365. {
  366. return this.bridge.deserialize(this.bridge.callASMethod(this.fb_instance_id, methodName, FABridge.argsToArray(arguments)));
  367. }
  368. },
  369. // Function Proxies
  370. //returns the AS proxy for the specified function ID
  371. getFunctionProxy: function(funcID)
  372. {
  373. var bridge = this;
  374. if (this.remoteFunctionCache[funcID] == null)
  375. {
  376. this.remoteFunctionCache[funcID] = function()
  377. {
  378. bridge.callASFunction(funcID, FABridge.argsToArray(arguments));
  379. }
  380. }
  381. return this.remoteFunctionCache[funcID];
  382. },
  383. //reutrns the ID of the given function; if it doesnt exist it is created and added to the local cache
  384. getFunctionID: function(func)
  385. {
  386. if (func.__bridge_id__ == undefined)
  387. {
  388. func.__bridge_id__ = this.makeID(this.nextLocalFuncID++);
  389. this.localFunctionCache[func.__bridge_id__] = func;
  390. }
  391. return func.__bridge_id__;
  392. },
  393. // serialization / deserialization
  394. serialize: function(value)
  395. {
  396. var result = {};
  397. var t = typeof(value);
  398. //primitives are kept as such
  399. if (t == "number" || t == "string" || t == "boolean" || t == null || t == undefined)
  400. {
  401. result = value;
  402. }
  403. else if (value instanceof Array)
  404. {
  405. //arrays are serializesd recursively
  406. result = [];
  407. for (var i = 0; i < value.length; i++)
  408. {
  409. result[i] = this.serialize(value[i]);
  410. }
  411. }
  412. else if (t == "function")
  413. {
  414. //js functions are assigned an ID and stored in the local cache
  415. result.type = FABridge.TYPE_JSFUNCTION;
  416. result.value = this.getFunctionID(value);
  417. }
  418. else if (value instanceof ASProxy)
  419. {
  420. result.type = FABridge.TYPE_ASINSTANCE;
  421. result.value = value.fb_instance_id;
  422. }
  423. else
  424. {
  425. result.type = FABridge.TYPE_ANONYMOUS;
  426. result.value = value;
  427. }
  428. return result;
  429. },
  430. //on deserialization we always check the return for the specific error code that is used to marshall NPE's into JS errors
  431. // the unpacking is done by returning the value on each pachet for objects/arrays
  432. deserialize: function(packedValue)
  433. {
  434. var result;
  435. var t = typeof(packedValue);
  436. if (t == "number" || t == "string" || t == "boolean" || packedValue == null || packedValue == undefined)
  437. {
  438. result = this.handleError(packedValue);
  439. }
  440. else if (packedValue instanceof Array)
  441. {
  442. result = [];
  443. for (var i = 0; i < packedValue.length; i++)
  444. {
  445. result[i] = this.deserialize(packedValue[i]);
  446. }
  447. }
  448. else if (t == "object")
  449. {
  450. for(var i = 0; i < packedValue.newTypes.length; i++)
  451. {
  452. this.addTypeDataToCache(packedValue.newTypes[i]);
  453. }
  454. for (var aRefID in packedValue.newRefs)
  455. {
  456. this.createProxy(aRefID, packedValue.newRefs[aRefID]);
  457. }
  458. if (packedValue.type == FABridge.TYPE_PRIMITIVE)
  459. {
  460. result = packedValue.value;
  461. }
  462. else if (packedValue.type == FABridge.TYPE_ASFUNCTION)
  463. {
  464. result = this.getFunctionProxy(packedValue.value);
  465. }
  466. else if (packedValue.type == FABridge.TYPE_ASINSTANCE)
  467. {
  468. result = this.getProxy(packedValue.value);
  469. }
  470. else if (packedValue.type == FABridge.TYPE_ANONYMOUS)
  471. {
  472. result = packedValue.value;
  473. }
  474. }
  475. return result;
  476. },
  477. //increases the reference count for the given object
  478. addRef: function(obj)
  479. {
  480. this.target.incRef(obj.fb_instance_id);
  481. },
  482. //decrease the reference count for the given object and release it if needed
  483. release:function(obj)
  484. {
  485. this.target.releaseRef(obj.fb_instance_id);
  486. },
  487. // check the given value for the components of the hard-coded error code : __FLASHERROR
  488. // used to marshall NPE's into flash
  489. handleError: function(value)
  490. {
  491. if (typeof(value)=="string" && value.indexOf("__FLASHERROR")==0)
  492. {
  493. var myErrorMessage = value.split("||");
  494. if(FABridge.refCount > 0 )
  495. {
  496. FABridge.refCount--;
  497. }
  498. throw new Error(myErrorMessage[1]);
  499. return value;
  500. }
  501. else
  502. {
  503. return value;
  504. }
  505. }
  506. };
  507. // The root ASProxy class that facades a flash object
  508. ASProxy = function(bridge, typeName)
  509. {
  510. this.bridge = bridge;
  511. this.typeName = typeName;
  512. return this;
  513. };
  514. //methods available on each ASProxy object
  515. ASProxy.prototype =
  516. {
  517. get: function(propName)
  518. {
  519. return this.bridge.deserialize(this.bridge.getPropertyFromAS(this.fb_instance_id, propName));
  520. },
  521. set: function(propName, value)
  522. {
  523. this.bridge.setPropertyInAS(this.fb_instance_id, propName, value);
  524. },
  525. call: function(funcName, args)
  526. {
  527. this.bridge.callASMethod(this.fb_instance_id, funcName, args);
  528. },
  529. addRef: function() {
  530. this.bridge.addRef(this);
  531. },
  532. release: function() {
  533. this.bridge.release(this);
  534. }
  535. };