You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2011/04/01 20:40:38 UTC

svn commit: r1087845 - /shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js

Author: johnh
Date: Fri Apr  1 18:40:38 2011
New Revision: 1087845

URL: http://svn.apache.org/viewvc?rev=1087845&view=rev
Log:
...in favor of simply ensuring that postMessage always occurs asynchronously (in
window.setTimeout).

As well as being larger in size the previous code was, in race conditions,
causing the "postmessage.test" call to be retrieved by the *new*/real message
handler, leading to errors.


Modified:
    shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js

Modified: shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js?rev=1087845&r1=1087844&r2=1087845&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js (original)
+++ shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js Fri Apr  1 18:40:38 2011
@@ -45,9 +45,6 @@ if (!gadgets.rpctx.wpm) {  // make lib r
 
   gadgets.rpctx.wpm = function() {
     var process, ready;
-    var postMessage;
-    var pmSync = false;
-    var pmEventDomain = false;
     var isForceSecure = false;
 
     function attachBrowserEvent(eventName, callback, useCapture) {
@@ -66,39 +63,6 @@ if (!gadgets.rpctx.wpm) {  // make lib r
       }
     }
 
-    // Some browsers (IE, Opera) have an implementation of postMessage that is
-    // synchronous, although HTML5 specifies that it should be asynchronous.  In
-    // order to make all browsers behave consistently, we run a small test to detect
-    // if postMessage is asynchronous or not.  If not, we wrap calls to postMessage
-    // in a setTimeout with a timeout of 0.
-    // Also, Opera's "message" event does not have an "origin" property (at least,
-    // it doesn't in version 9.64;  presumably, it will in version 10).  If
-    // event.origin does not exist, use event.domain.  The other difference is that
-    // while event.origin looks like <scheme>://<hostname>:<port>, event.domain
-    // consists only of <hostname>.
-    function testPostMessage() {
-      var hit = false;
-
-      function receiveMsg(event) {
-        if (event.data == 'postmessage.test') {
-          hit = true;
-          if (typeof event.origin === 'undefined') {
-            pmEventDomain = true;
-          }
-        }
-      }
-
-      attachBrowserEvent('message', receiveMsg, false);
-      window.postMessage('postmessage.test', '*');
-
-      // if 'hit' is true here, then postMessage is synchronous
-      if (hit) {
-        pmSync = true;
-      }
-
-      removeBrowserEvent('message', receiveMsg, false);
-    }
-
     function onmessage(packet) {
       var rpc = gadgets.json.parse(packet.data);
       if (isForceSecure) {
@@ -110,7 +74,13 @@ if (!gadgets.rpctx.wpm) {  // make lib r
         var origRelay = gadgets.rpc.getRelayUrl(rpc['f']) ||
             gadgets.util.getUrlParameters()['parent'];
         var origin = gadgets.rpc.getOrigin(origRelay);
-        if (!pmEventDomain ? packet.origin !== origin :
+
+        // Opera's "message" event does not have an "origin" property (at least,
+        // it doesn't in version 9.64;  presumably, it will in version 10).  If
+        // event.origin does not exist, use event.domain.  The other difference is that
+        // while event.origin looks like <scheme>://<hostname>:<port>, event.domain
+        // consists only of <hostname>.
+        if (typeof packet.origin !== "undefined" ? packet.origin !== origin :
             packet.domain !== /^.+:\/\/([^:]+).*/.exec(origin)[1]) {
           return;
         }
@@ -131,19 +101,6 @@ if (!gadgets.rpctx.wpm) {  // make lib r
         process = processFn;
         ready = readyFn;
 
-        testPostMessage();
-        if (!pmSync) {
-          postMessage = function(win, msg, origin) {
-            win.postMessage(msg, origin);
-          };
-        } else {
-          postMessage = function(win, msg, origin) {
-            window.setTimeout(function() {
-              win.postMessage(msg, origin);
-            }, 0);
-          };
-        }
-
         // Set up native postMessage handler.
         attachBrowserEvent('message', onmessage, false);
 
@@ -170,7 +127,12 @@ if (!gadgets.rpctx.wpm) {  // make lib r
         var origin = gadgets.rpc.getTargetOrigin(targetId);
         var targetWin = gadgets.rpc._getTargetWin(targetId);
         if (origin) {
-          postMessage(targetWin, gadgets.json.stringify(rpc), origin);
+          // Some browsers (IE, Opera) have an implementation of postMessage that is
+          // synchronous, although HTML5 specifies that it should be asynchronous.  In
+          // order to make all browsers behave consistently, we wrap all postMessage
+          // calls in a setTimeout with a timeout of 0.
+          window.setTimeout(function() {
+              targetWin.postMessage(gadgets.json.stringify(rpc), origin); }, 0);
         } else {
           gadgets.error('No relay set (used as window.postMessage targetOrigin)' +
               ', cannot send cross-domain message');