PageRenderTime 67ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/blogs.dir/2/files/2011/02/20110220-dancemarathon/SatAM/com/greensock/loading/core/DisplayObjectLoader.as

https://github.com/digitalstrategyworks/Reese-WordPress
ActionScript | 232 lines | 156 code | 22 blank | 54 comment | 42 complexity | ec946aab4d578859147655f9c1924db2 MD5 | raw file
  1. /**
  2. * VERSION: 1.7
  3. * DATE: 2010-11-13
  4. * AS3
  5. * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/
  6. **/
  7. package com.greensock.loading.core {
  8. import com.greensock.events.LoaderEvent;
  9. import com.greensock.loading.LoaderMax;
  10. import com.greensock.loading.LoaderStatus;
  11. import com.greensock.loading.display.ContentDisplay;
  12. import flash.display.DisplayObject;
  13. import flash.display.Loader;
  14. import flash.display.Sprite;
  15. import flash.events.ErrorEvent;
  16. import flash.events.Event;
  17. import flash.events.ProgressEvent;
  18. import flash.net.LocalConnection;
  19. import flash.system.ApplicationDomain;
  20. import flash.system.Capabilities;
  21. import flash.system.LoaderContext;
  22. import flash.system.Security;
  23. import flash.system.SecurityDomain;
  24. /**
  25. * Serves as the base class for SWFLoader and ImageLoader. There is no reason to use this class on its own.
  26. * Please refer to the documentation for the other classes.
  27. * <br /><br />
  28. *
  29. * <b>Copyright 2010, GreenSock. All rights reserved.</b> This work is subject to the terms in <a href="http://www.greensock.com/terms_of_use.html">http://www.greensock.com/terms_of_use.html</a> or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership.
  30. *
  31. * @author Jack Doyle, jack@greensock.com
  32. */
  33. public class DisplayObjectLoader extends LoaderItem {
  34. /** @private the Sprite to which the EVENT_LISTENER was attached for forcing garbage collection after 1 frame (improves performance especially when multiple loaders are disposed at one time). **/
  35. protected static var _gcDispatcher:DisplayObject;
  36. /** @private **/
  37. protected static var _gcCycles:uint = 0;
  38. /** @private **/
  39. protected var _loader:Loader;
  40. /** @private **/
  41. protected var _sprite:Sprite;
  42. /** @private **/
  43. protected var _context:LoaderContext;
  44. /** @private **/
  45. protected var _initted:Boolean;
  46. /** @private used by SWFLoader when the loader is canceled before the SWF ever had a chance to init which causes garbage collection issues. We slip into stealthMode at that point, wait for it to init, and then cancel the _loader's loading.**/
  47. protected var _stealthMode:Boolean;
  48. /**
  49. * Constructor
  50. *
  51. * @param urlOrRequest The url (<code>String</code>) or <code>URLRequest</code> from which the loader should get its content
  52. * @param vars An object containing optional parameters like <code>estimatedBytes, name, autoDispose, onComplete, onProgress, onError</code>, etc. For example, <code>{estimatedBytes:2400, name:"myImage1", onComplete:completeHandler}</code>.
  53. */
  54. public function DisplayObjectLoader(urlOrRequest:*, vars:Object=null) {
  55. super(urlOrRequest, vars);
  56. _refreshLoader(false);
  57. if (LoaderMax.contentDisplayClass is Class) {
  58. _sprite = new LoaderMax.contentDisplayClass(this);
  59. if (!_sprite.hasOwnProperty("rawContent")) {
  60. throw new Error("LoaderMax.contentDisplayClass must be set to a class with a 'rawContent' property, like com.greensock.loading.display.ContentDisplay");
  61. }
  62. } else {
  63. _sprite = new ContentDisplay(this);
  64. }
  65. }
  66. /** @private Set inside ContentDisplay's or FlexContentDisplay's "loader" setter. **/
  67. public function setContentDisplay(contentDisplay:Sprite):void {
  68. _sprite = contentDisplay;
  69. }
  70. /** @private **/
  71. override protected function _load():void {
  72. _prepRequest();
  73. if (this.vars.context is LoaderContext) {
  74. _context = this.vars.context;
  75. } else if (_context == null && !_isLocal) {
  76. _context = (LoaderMax.defaultContext != null) ? LoaderMax.defaultContext : new LoaderContext(true, new ApplicationDomain(ApplicationDomain.currentDomain), SecurityDomain.currentDomain); //avoids some security sandbox headaches that plague many users.
  77. }
  78. if (Capabilities.playerType != "Desktop") { //AIR apps will choke on Security.allowDomain()
  79. Security.allowDomain(_url);
  80. }
  81. _loader.load(_request, _context);
  82. }
  83. /** @private **/
  84. protected function _refreshLoader(unloadContent:Boolean=true):void {
  85. if (_loader != null) {
  86. //to avoid gc issues and get around a bug in Flash that incorrectly reports progress values on Loaders that were closed before completing, we must force gc and recreate the Loader altogether...
  87. if (_status == LoaderStatus.LOADING) {
  88. try {
  89. _loader.close();
  90. } catch (error:Error) {
  91. }
  92. }
  93. _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _progressHandler);
  94. _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _completeHandler);
  95. _loader.contentLoaderInfo.removeEventListener("ioError", _failHandler);
  96. _loader.contentLoaderInfo.removeEventListener("securityError", _securityErrorHandler);
  97. _loader.contentLoaderInfo.removeEventListener("httpStatus", _httpStatusHandler);
  98. _loader.contentLoaderInfo.removeEventListener(Event.INIT, _initHandler);
  99. if (unloadContent) {
  100. try {
  101. if (_loader.hasOwnProperty("unloadAndStop")) { //Flash Player 10 and later only
  102. (_loader as Object).unloadAndStop();
  103. } else {
  104. _loader.unload();
  105. }
  106. } catch (error:Error) {
  107. }
  108. }
  109. forceGC(_sprite, (this.hasOwnProperty("getClass")) ? 3 : 1);
  110. }
  111. _initted = false;
  112. _loader = new Loader();
  113. _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, _progressHandler, false, 0, true);
  114. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _completeHandler, false, 0, true);
  115. _loader.contentLoaderInfo.addEventListener("ioError", _failHandler, false, 0, true);
  116. _loader.contentLoaderInfo.addEventListener("securityError", _securityErrorHandler, false, 0, true);
  117. _loader.contentLoaderInfo.addEventListener("httpStatus", _httpStatusHandler, false, 0, true);
  118. _loader.contentLoaderInfo.addEventListener(Event.INIT, _initHandler, false, 0, true);
  119. }
  120. /** @private works around bug in Flash Player that prevents SWFs from properly being garbage collected after being unloaded - for certain types of objects like swfs, this needs to be run more than once (spread out over several frames) to force Flash to properly garbage collect everything. **/
  121. public static function forceGC(dispatcher:DisplayObject, cycles:uint=1):void {
  122. if (_gcCycles < cycles) {
  123. _gcCycles = cycles;
  124. if (_gcDispatcher == null) {
  125. _gcDispatcher = dispatcher;
  126. _gcDispatcher.addEventListener(Event.ENTER_FRAME, _forceGCHandler, false, 0, true);
  127. }
  128. }
  129. }
  130. /** @private **/
  131. protected static function _forceGCHandler(event:Event):void {
  132. if (_gcCycles == 0) {
  133. _gcDispatcher.removeEventListener(Event.ENTER_FRAME, _forceGCHandler);
  134. _gcDispatcher = null;
  135. } else {
  136. _gcCycles--;
  137. }
  138. try {
  139. new LocalConnection().connect("FORCE_GC");
  140. new LocalConnection().connect("FORCE_GC");
  141. } catch (error:Error) {
  142. }
  143. }
  144. /** @private scrubLevel: 0 = cancel, 1 = unload, 2 = dispose, 3 = flush **/
  145. override protected function _dump(scrubLevel:int=0, newStatus:int=LoaderStatus.READY, suppressEvents:Boolean=false):void {
  146. if (scrubLevel == 1) { //unload
  147. (_sprite as Object).rawContent = null;
  148. } else if (scrubLevel == 2) { //dispose
  149. (_sprite as Object).loader = null;
  150. } else if (scrubLevel == 3) { //unload and dispose
  151. (_sprite as Object).dispose(false, false); //makes sure the ContentDisplay is removed from its parent as well.
  152. }
  153. if (!_stealthMode) {
  154. _refreshLoader(Boolean(scrubLevel != 2));
  155. }
  156. super._dump(scrubLevel, newStatus, suppressEvents);
  157. }
  158. /** @private **/
  159. protected function _determineScriptAccess():void {
  160. if (!_scriptAccessDenied) {
  161. if (!_loader.contentLoaderInfo.childAllowsParent) {
  162. _scriptAccessDenied = true;
  163. dispatchEvent(new LoaderEvent(LoaderEvent.SCRIPT_ACCESS_DENIED, this, "Error #2123: Security sandbox violation: " + this + ". No policy files granted access."));
  164. }
  165. }
  166. }
  167. //---- EVENT HANDLERS ------------------------------------------------------------------------------------
  168. /** @private **/
  169. protected function _securityErrorHandler(event:ErrorEvent):void {
  170. //If a security error is thrown because of a missing crossdomain.xml file for example and the user didn't define a specific LoaderContext, we'll try again without checking the policy file, accepting the restrictions that come along with it because typically people would rather have the content show up on the screen rather than just error out (and they can always check the scriptAccessDenied property if they need to figure out whether it's safe to do BitmapData stuff on it, etc.)
  171. if (_context != null && _context.checkPolicyFile && !(this.vars.context is LoaderContext)) {
  172. _context = new LoaderContext(false);
  173. _scriptAccessDenied = true;
  174. dispatchEvent(new LoaderEvent(LoaderEvent.SCRIPT_ACCESS_DENIED, this, event.text));
  175. _errorHandler(event);
  176. _load();
  177. } else {
  178. _failHandler(event);
  179. }
  180. }
  181. /** @private **/
  182. protected function _initHandler(event:Event):void {
  183. if (!_initted) {
  184. _initted = true;
  185. (_sprite as Object).rawContent = (_content as DisplayObject);
  186. dispatchEvent(new LoaderEvent(LoaderEvent.INIT, this));
  187. }
  188. }
  189. //---- GETTERS / SETTERS -------------------------------------------------------------------------
  190. /** A ContentDisplay object (a Sprite) that will contain the remote content as soon as the <code>INIT</code> event has been dispatched. This ContentDisplay can be accessed immediately; you do not need to wait for the content to load. **/
  191. override public function get content():* {
  192. return _sprite;
  193. }
  194. /**
  195. * The raw content that was successfully loaded <strong>into</strong> the <code>content</code> ContentDisplay
  196. * Sprite which varies depending on the type of loader and whether or not script access was denied while
  197. * attempting to load the file:
  198. *
  199. * <ul>
  200. * <li>ImageLoader with script access granted: <code>flash.display.Bitmap</code></li>
  201. * <li>ImageLoader with script access denied: <code>flash.display.Loader</code></li>
  202. * <li>SWFLoader with script access granted: <code>flash.display.DisplayObject</code> (the swf's <code>root</code>)</li>
  203. * <li>SWFLoader with script access denied: <code>flash.display.Loader</code> (the swf's <code>root</code> cannot be accessed because it would generate a security error)</li>
  204. * </ul>
  205. **/
  206. public function get rawContent():* {
  207. return _content;
  208. }
  209. }
  210. }