Change GWT History to work when multiple applications are loaded within the same page.

Patch by: jhollenbach
Review by: jat, jgw

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


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9637 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/impl/HistoryImpl.java b/user/src/com/google/gwt/user/client/impl/HistoryImpl.java
index 4d42db0..a711fb7 100644
--- a/user/src/com/google/gwt/user/client/impl/HistoryImpl.java
+++ b/user/src/com/google/gwt/user/client/impl/HistoryImpl.java
@@ -35,9 +35,11 @@
  */
 public class HistoryImpl implements HasValueChangeHandlers<String>, HasHandlers {
 
-  public static native String getToken() /*-{
-    return $wnd.__gwt_historyToken || "";
-  }-*/;
+  private static String token = "";
+
+  public static String getToken() {
+    return (token == null) ? "" : token;
+  }
 
   /**
    * Sets whether the IE6 history implementation will update the URL hash when
@@ -53,9 +55,9 @@
   public static void setUpdateHashOnIE6(boolean updateHash) {
   }
 
-  protected static native void setToken(String token) /*-{
-    $wnd.__gwt_historyToken = token;
-  }-*/;
+  protected static void setToken(String token) {
+    HistoryImpl.token = token;
+  }
 
   private HandlerManager handlers = new HandlerManager(null);
 
@@ -97,6 +99,9 @@
     @com.google.gwt.user.client.impl.HistoryImpl::setToken(Ljava/lang/String;)(token);
 
     var historyImpl = this;
+
+    var oldHandler = $wnd.onhashchange;
+
     $wnd.onhashchange = $entry(function() {
       var token = '', hash = $wnd.location.hash;
       if (hash.length > 0) {
@@ -104,6 +109,10 @@
       }
 
       historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::newItemOnEvent(Ljava/lang/String;)(token);
+
+      if (oldHandler) {
+        oldHandler();
+      }
     });
 
     return true;
diff --git a/user/src/com/google/gwt/user/client/impl/HistoryImplIE6.java b/user/src/com/google/gwt/user/client/impl/HistoryImplIE6.java
index e0b7f1e..83e64ef 100644
--- a/user/src/com/google/gwt/user/client/impl/HistoryImplIE6.java
+++ b/user/src/com/google/gwt/user/client/impl/HistoryImplIE6.java
@@ -179,9 +179,14 @@
 
   private native void injectGlobalHandler() /*-{
     var historyImplRef = this;
+    var oldOnLoad = $wnd.__gwt_onHistoryLoad;
 
     $wnd.__gwt_onHistoryLoad = $entry(function(token) {
       historyImplRef.@com.google.gwt.user.client.impl.HistoryImpl::newItemOnEvent(Ljava/lang/String;)(token);
+
+      if (oldOnLoad) {
+        oldOnLoad(token);
+      }
     });
   }-*/;
 
diff --git a/user/src/com/google/gwt/user/client/impl/HistoryImplTimer.java b/user/src/com/google/gwt/user/client/impl/HistoryImplTimer.java
index f5002b5..9a530fc 100644
--- a/user/src/com/google/gwt/user/client/impl/HistoryImplTimer.java
+++ b/user/src/com/google/gwt/user/client/impl/HistoryImplTimer.java
@@ -35,9 +35,8 @@
 
     // Create the timer that checks the browser's url hash every 1/4 s.
     var historyImpl = this;
-    $wnd.__checkHistory = $entry(function() {
-      $wnd.setTimeout($wnd.__checkHistory, 250);
 
+    var checkHistory = $entry(function() {
       var token = '', hash = $wnd.location.hash;
       if (hash.length > 0) {
         token = historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(hash.substring(1));
@@ -46,8 +45,13 @@
       historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::newItemOnEvent(Ljava/lang/String;)(token);
     });
 
+    var checkHistoryCycle = function() {
+      $wnd.setTimeout(checkHistoryCycle, 250);
+      checkHistory();
+    }
+
     // Kick off the timer.
-    $wnd.__checkHistory();
+    checkHistoryCycle();
     return true;
   }-*/;
 }