PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/com/greensock/loading/core/DisplayObjectLoader.as

https://github.com/FrancisVarga/as3TweenLite
ActionScript | 233 lines | 155 code | 23 blank | 55 comment | 36 complexity | 29fbaf239ab1ff9ed7c3ae6d747c436d MD5 | raw file
  1. /**
  2. * VERSION: 1.14
  3. * DATE: 2010-06-22
  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.BitmapData;
  13. import flash.display.DisplayObject;
  14. import flash.display.Loader;
  15. import flash.display.Sprite;
  16. import flash.events.ErrorEvent;
  17. import flash.events.Event;
  18. import flash.events.ProgressEvent;
  19. import flash.net.LocalConnection;
  20. import flash.system.ApplicationDomain;
  21. import flash.system.LoaderContext;
  22. import flash.system.SecurityDomain;
  23. /**
  24. * Serves as the base class for SWFLoader and ImageLoader. There is no reason to use this class on its own.
  25. * Please refer to the documentation for the other classes.
  26. * <br /><br />
  27. *
  28. * <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.
  29. *
  30. * @author Jack Doyle, jack@greensock.com
  31. */
  32. public class DisplayObjectLoader extends LoaderItem {
  33. /** @private Just used to test for scriptAccessDenied. **/
  34. protected static var _bitmapData:BitmapData = new BitmapData(1, 1, false);
  35. /** @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). **/
  36. protected static var _gcDispatcher:DisplayObject;
  37. /** @private **/
  38. protected static var _gcCycles:uint = 0;
  39. /** @private **/
  40. protected var _loader:Loader;
  41. /** @private **/
  42. protected var _sprite:Sprite;
  43. /** @private **/
  44. protected var _context:LoaderContext;
  45. /** @private **/
  46. protected var _initted:Boolean;
  47. /** @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.**/
  48. protected var _stealthMode:Boolean;
  49. /**
  50. * Constructor
  51. *
  52. * @param urlOrRequest The url (<code>String</code>) or <code>URLRequest</code> from which the loader should get its content
  53. * @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>.
  54. */
  55. public function DisplayObjectLoader(urlOrRequest:*, vars:Object=null) {
  56. super(urlOrRequest, vars);
  57. _refreshLoader(false);
  58. if (LoaderMax.contentDisplayClass is Class) {
  59. _sprite = new LoaderMax.contentDisplayClass(this);
  60. if (!_sprite.hasOwnProperty("rawContent")) {
  61. throw new Error("LoaderMax.contentDisplayClass must be set to a class with a 'rawContent' property, like com.greensock.loading.display.ContentDisplay");
  62. }
  63. } else {
  64. _sprite = new ContentDisplay(this);
  65. }
  66. }
  67. /** @private Set inside ContentDisplay's or FlexContentDisplay's "loader" setter. **/
  68. public function setContentDisplay(contentDisplay:Sprite):void {
  69. _sprite = contentDisplay;
  70. }
  71. /** @private **/
  72. override protected function _load():void {
  73. _prepRequest();
  74. if (this.vars.context is LoaderContext) {
  75. _context = this.vars.context;
  76. } else if (_context == null && !_isLocal) {
  77. _context = new LoaderContext(true, ApplicationDomain.currentDomain, SecurityDomain.currentDomain); //avoids some security sandbox headaches that plague many users.
  78. }
  79. _loader.load(_request, _context);
  80. }
  81. /** @private **/
  82. protected function _refreshLoader(unloadContent:Boolean=true):void {
  83. if (_loader != null) {
  84. //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...
  85. if (_loader.contentLoaderInfo.bytesLoaded < _loader.contentLoaderInfo.bytesTotal) {
  86. try {
  87. _loader.close();
  88. } catch (error:Error) {
  89. }
  90. }
  91. _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, _progressHandler);
  92. _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _completeHandler);
  93. _loader.contentLoaderInfo.removeEventListener("ioError", _failHandler);
  94. _loader.contentLoaderInfo.removeEventListener("securityError", _securityErrorHandler);
  95. _loader.contentLoaderInfo.removeEventListener("httpStatus", _httpStatusHandler);
  96. _loader.contentLoaderInfo.removeEventListener(Event.INIT, _initHandler);
  97. if (unloadContent) {
  98. try {
  99. if (_loader.hasOwnProperty("unloadAndStop")) { //Flash Player 10 and later only
  100. (_loader as Object).unloadAndStop();
  101. } else {
  102. _loader.unload();
  103. }
  104. } catch (error:Error) {
  105. }
  106. }
  107. forceGC(_sprite, (this.hasOwnProperty("getClass")) ? 3 : 1);
  108. }
  109. _initted = false;
  110. _loader = new Loader();
  111. _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, _progressHandler, false, 0, true);
  112. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _completeHandler, false, 0, true);
  113. _loader.contentLoaderInfo.addEventListener("ioError", _failHandler, false, 0, true);
  114. _loader.contentLoaderInfo.addEventListener("securityError", _securityErrorHandler, false, 0, true);
  115. _loader.contentLoaderInfo.addEventListener("httpStatus", _httpStatusHandler, false, 0, true);
  116. _loader.contentLoaderInfo.addEventListener(Event.INIT, _initHandler, false, 0, true);
  117. }
  118. /** @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. **/
  119. public static function forceGC(dispatcher:DisplayObject, cycles:uint=1):void {
  120. if (_gcCycles < cycles) {
  121. _gcCycles = cycles;
  122. if (_gcDispatcher == null) {
  123. _gcDispatcher = dispatcher;
  124. _gcDispatcher.addEventListener(Event.ENTER_FRAME, _forceGCHandler, false, 0, true);
  125. }
  126. }
  127. }
  128. /** @private **/
  129. protected static function _forceGCHandler(event:Event):void {
  130. if (_gcCycles == 0) {
  131. _gcDispatcher.removeEventListener(Event.ENTER_FRAME, _forceGCHandler);
  132. _gcDispatcher = null;
  133. }
  134. _gcCycles--;
  135. try {
  136. new LocalConnection().connect("FORCE_GC");
  137. new LocalConnection().connect("FORCE_GC");
  138. } catch (error:Error) {
  139. }
  140. }
  141. /** @private scrubLevel: 0 = cancel, 1 = unload, 2 = dispose, 3 = flush **/
  142. override protected function _dump(scrubLevel:int=0, newStatus:int=LoaderStatus.READY, suppressEvents:Boolean=false):void {
  143. if (scrubLevel == 1) { //unload
  144. (_sprite as Object).rawContent = null;
  145. } else if (scrubLevel == 2) { //dispose
  146. (_sprite as Object).loader = null;
  147. } else if (scrubLevel == 3) { //unload and dispose
  148. (_sprite as Object).dispose(false, false); //makes sure the ContentDisplay is removed from its parent as well.
  149. }
  150. if (!_stealthMode) {
  151. _refreshLoader(Boolean(scrubLevel != 2));
  152. }
  153. super._dump(scrubLevel, newStatus, suppressEvents);
  154. }
  155. /** @private **/
  156. protected function _determineScriptAccess():void {
  157. if (!_scriptAccessDenied) {
  158. try {
  159. _bitmapData.draw(_loader.content);
  160. } catch (error:Error) {
  161. _scriptAccessDenied = true;
  162. dispatchEvent(new LoaderEvent(LoaderEvent.SCRIPT_ACCESS_DENIED, this, error.message));
  163. }
  164. }
  165. }
  166. //---- EVENT HANDLERS ------------------------------------------------------------------------------------
  167. /** @private **/
  168. protected function _securityErrorHandler(event:ErrorEvent):void {
  169. //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.)
  170. if (_context != null && _context.checkPolicyFile && !("context" in this.vars)) {
  171. _context.checkPolicyFile = false;
  172. _context.applicationDomain = null;
  173. _context.securityDomain = null;
  174. _scriptAccessDenied = true;
  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. }