Clean up the CrossSiteIFrameLoadingStrategy class, removing some dead code,
adding comments to clarify what is going on, and making it throw an error
which is more explicit about what triggered the error

Review at http://gwt-code-reviews.appspot.com/1413801


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10002 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/core/client/CodeDownloadException.java b/user/src/com/google/gwt/core/client/CodeDownloadException.java
index 0607cac..ab65d64 100644
--- a/user/src/com/google/gwt/core/client/CodeDownloadException.java
+++ b/user/src/com/google/gwt/core/client/CodeDownloadException.java
@@ -30,16 +30,21 @@
     /**
      * Generic code for terminating the download.
      */
-    TERMINATED
+    TERMINATED,
   }
 
   private final Reason reason;
 
+  public CodeDownloadException(String message) {
+    super(message);
+    this.reason = Reason.TERMINATED;
+  }
+  
   public CodeDownloadException(String message, Reason reason) {
     super(message);
     this.reason = reason;
   }
-
+  
   public Reason getReason() {
     return reason;
   }
diff --git a/user/src/com/google/gwt/core/client/impl/CrossSiteIframeLoadingStrategy.java b/user/src/com/google/gwt/core/client/impl/CrossSiteIframeLoadingStrategy.java
index b056482..2cce53d 100644
--- a/user/src/com/google/gwt/core/client/impl/CrossSiteIframeLoadingStrategy.java
+++ b/user/src/com/google/gwt/core/client/impl/CrossSiteIframeLoadingStrategy.java
@@ -16,7 +16,6 @@
 
 package com.google.gwt.core.client.impl;
 
-import com.google.gwt.core.client.CodeDownloadException;
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.core.client.impl.AsyncFragmentLoader.LoadTerminatedHandler;
@@ -25,14 +24,6 @@
 /**
  * Load runAsync code using a script tag. Intended for use with the
  * {@link com.google.gwt.core.linker.CrossSiteIframeLinker}.
- *
- * <p>
- * The linker wraps its selection script code with a function refered to by
- * <code>__gwtModuleFunction</code>. On that function is a property
- * <code>installCode</code> that can be invoked to eval more code in a scope
- * nested somewhere within that function. The loaded script for fragment 123 is
- * expected to invoke <code>__gwtModuleFunction.runAsyncCallback123</code>
- * as the final thing it does.
  */
 public class CrossSiteIframeLoadingStrategy implements LoadingStrategy {
   /**
@@ -58,10 +49,6 @@
     }-*/;
   }
 
-  private static final RuntimeException LoadTerminated =
-      new CodeDownloadException("Code download terminated",
-                                CodeDownloadException.Reason.TERMINATED);
-
   /**
    * Clear callbacks on script objects. This is important on IE 6 and 7 to
    * prevent a memory leak. If the callbacks aren't cleared, there is a cyclical
@@ -73,13 +60,6 @@
     script.onerror = script.onload = script.onreadystatechange = nop;
   }-*/;
 
-  /**
-   * Clear the success callback for fragment <code>fragment</code>.
-   */
-  private static native void clearOnSuccess(int fragment) /*-{
-    delete __gwtModuleFunction['runAsyncCallback'+fragment];
-  }-*/;
-
   private static native JavaScriptObject createScriptTag(String url) /*-{
     var head = document.getElementsByTagName('head').item(0);
     var script = document.createElement('script');
@@ -97,33 +77,43 @@
       LoadTerminatedHandler loadFinishedHandler) /*-{
      return function(exception) {
        if (tag.parentNode == null) {
-         // onSuccess or onFailure must have already been called.
+         // This function must have already been called.
          return;
        }
        var head = document.getElementsByTagName('head').item(0);
-       @com.google.gwt.core.client.impl.CrossSiteIframeLoadingStrategy::clearOnSuccess(*)(fragment);
        @com.google.gwt.core.client.impl.CrossSiteIframeLoadingStrategy::clearCallbacks(*)(tag);
        head.removeChild(tag);
+       // It seems unintuitive to call the error function every time, but
+       // it appears that AsyncFragmentLoader::fragmentHasLoaded (which is
+       // called by each fragment) will set the fragmentLoading variable to
+       // -1 when the code in this fragment executes, so this
+       // loadTerminated call will fail the (fragmentLoading == fragment) check
+       // and will immediately exit, so no errors are actually fired.
        function callLoadTerminated() {
          loadFinishedHandler.@com.google.gwt.core.client.impl.AsyncFragmentLoader.LoadTerminatedHandler::loadTerminated(*)(exception);
        }
        $entry(callLoadTerminated)();
      }
    }-*/;
-
+  
   private static native void setOnTerminated(JavaScriptObject script,
       JavaScriptObject callback) /*-{
-    var exception = @com.google.gwt.core.client.impl.CrossSiteIframeLoadingStrategy::LoadTerminated;
     script.onerror = function() {
-      callback(exception);
+      var error = "Code download terminated, onerror called. Script src = " + script.src;
+      callback(@com.google.gwt.core.client.CodeDownloadException::new(Ljava/lang/String;)(error));
     }
     script.onload = function() {
-      callback(exception);
+      var error = "Code download terminated, onload called. Script src = " + script.src;
+      callback(@com.google.gwt.core.client.CodeDownloadException::new(Ljava/lang/String;)(error));
     }
     script.onreadystatechange = function () {
-      if (script.readyState == 'loaded' || script.readyState == 'complete') {
-        script.onreadystatechange = function () { }
-        callback(exception);
+      if (script.readyState == 'loaded') {
+        var error = "Code download terminated, readystate is loaded. Script src = " + script.src;
+        callback(@com.google.gwt.core.client.CodeDownloadException::new(Ljava/lang/String;)(error));
+      }
+      if (script.readyState == 'complete') {
+        var error = "Code download terminated, readystate is complete. Script src = " + script.src;
+        callback(@com.google.gwt.core.client.CodeDownloadException::new(Ljava/lang/String;)(error));
       }
     }
   }-*/;