Make SDM use callbacks correctly.

Bug: issue 8933
Change-Id: I79d6d3cf0ec25d8d74236e98f5204c3e4fd6f8ab
diff --git a/dev/codeserver/java/com/google/gwt/dev/codeserver/recompile_lib.js b/dev/codeserver/java/com/google/gwt/dev/codeserver/recompile_lib.js
index 8f9bc58..01011dd 100644
--- a/dev/codeserver/java/com/google/gwt/dev/codeserver/recompile_lib.js
+++ b/dev/codeserver/java/com/google/gwt/dev/codeserver/recompile_lib.js
@@ -209,13 +209,17 @@
  */
 Recompiler.prototype.__jsonp = function(url, callback) {
   var that = this;
-  var callback_id = 'c' + this.__globals.counter++;
+  var callback_id = 'c' + this.__globals.callbackCounter++;
   this.__globals.callbacks[callback_id] = function(json) {
     delete that.__globals.callbacks[callback_id];
     callback(json);
   };
 
   var url = url + '&_callback=__gwt_sdm_globals.callbacks.' + callback_id;
+  this.__injectScriptTag(url);
+};
+
+Recompiler.prototype.__injectScriptTag = function(url) {
   var script = $doc.createElement('script');
   script.src = url;
   var $head = $doc.head || $doc.getElementsByTagName('head')[0];
diff --git a/dev/codeserver/javatests/com/google/gwt/dev/codeserver/client/CodeServerGwtTest.java b/dev/codeserver/javatests/com/google/gwt/dev/codeserver/client/CodeServerGwtTest.java
index 30c95f2..db60ce7 100644
--- a/dev/codeserver/javatests/com/google/gwt/dev/codeserver/client/CodeServerGwtTest.java
+++ b/dev/codeserver/javatests/com/google/gwt/dev/codeserver/client/CodeServerGwtTest.java
@@ -82,6 +82,8 @@
 
   public native void testRecompiler() /*-{
     var Recompiler = $wnd.namespace.lib.Recompiler;
+
+    $wnd.__gwt_sdm_globals = { callbacks: {}, callbackCounter: 1234};
     var recompiler = new Recompiler('testModule', {prop1: 'val1', prop2 : 'val2'});
 
     var jsonpUrl = '';
@@ -95,9 +97,13 @@
       return "http://mytesthost:7812/";
     };
 
-    recompiler.__jsonp = function(url, callback) {
+    recompiler.__injectScriptTag = function(url) {
       jsonpUrl = url;
+      // call the callback that should have been stored in global context
+      var callback = $wnd.__gwt_sdm_globals.callbacks['c1234'];
+      assertTrue('No function found', typeof callback == 'function');
       callback({status : 'ok'});
+      assertTrue('Callback has not been deleted', $wnd.__gwt_sdm_globals.callbacks['c1234'] == null);
     };
 
     // do the test
@@ -105,7 +111,8 @@
       callbackCalled = true;
       //compile is done
       assertStringEquals('ok', result.status);
-      assertStringEquals('http://mytesthost:7812/recompile/testModule?prop1=val1&prop2=val2',
+      assertStringEquals('http://mytesthost:7812/recompile/testModule' +
+          '?prop1=val1&prop2=val2&_callback=__gwt_sdm_globals.callbacks.c1234',
           jsonpUrl);
     });
     assertTrue('callback for successful recompile was not executed', callbackCalled);
@@ -174,8 +181,7 @@
       return;
     }
     Resource res = GWT.create(Resource.class);
-    String before = "(function(){ \n" +
-    		"$wnd.namespace = {};$namespace = $wnd.namespace; $global = $wnd";
+    String before = "(function(){ \n $wnd.namespace = {};$namespace = $wnd.namespace;";
     String js = res.libJS().getText();
     ScriptInjector.fromString(before + js + "})()").inject();
   }