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 2009/05/22 21:55:03 UTC

svn commit: r777683 [1/2] - in /incubator/shindig/trunk/features/src/main/javascript/features/rpc: dpm.transport.js fe.transport.js feature.xml ifpc.transport.js nix.transport.js rmr.transport.js rpc.js wpm.transport.js

Author: johnh
Date: Fri May 22 19:55:02 2009
New Revision: 777683

URL: http://svn.apache.org/viewvc?rev=777683&view=rev
Log:
Breaking out rpc transport code into separate files. All are served as before, prepended to core gadgets.rpc code.


Added:
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/dpm.transport.js
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/fe.transport.js
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/ifpc.transport.js
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/nix.transport.js
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/rmr.transport.js
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/wpm.transport.js
Modified:
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/feature.xml
    incubator/shindig/trunk/features/src/main/javascript/features/rpc/rpc.js

Added: incubator/shindig/trunk/features/src/main/javascript/features/rpc/dpm.transport.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/dpm.transport.js?rev=777683&view=auto
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/dpm.transport.js (added)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/dpm.transport.js Fri May 22 19:55:02 2009
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+var gadgets = gadgets || {};
+gadgets.rpctx = gadgets.rpctx || {};
+
+/**
+ * Similar transport to WPM, but uses document element instead.
+ * This was implemented by Opera 8 before the WPM standard was complete.
+ *
+ *     dpm: postMessage on the document object.
+ *        - Opera 8+
+ */
+gadgets.rpctx.Dpm = function() {
+  var ready;
+  return {
+    getCode: function() {
+      return 'dpm';
+    },
+
+    isParentVerifiable: function() {
+      return false;
+    },
+
+    init: function(processFn, readyFn) {
+      ready = readyFn;
+      // Set up native postMessage handler.
+      window.addEventListener('message', function(packet) {
+        // TODO validate packet.domain for security reasons
+        processFn(gadgets.json.parse(packet.data));
+      }, false);
+      ready('..', true);  // Ready immediately.
+      return true;
+    },
+
+    setup: function(receiverId, token) {
+      // See WPM setup comment.
+      if (receiverId === '..') {
+        gadgets.rpc.call(receiverId, gadgets.rpc.ACK);
+      }
+      return true;
+    },
+
+    call: function(targetId, from, rpc) {
+      var targetDoc = targetId === '..' ? parent.document :
+                                          frames[targetId].document;
+      targetDoc.postMessage(gadgets.json.stringify(rpc));
+      return true;
+    }
+  };
+}();

Added: incubator/shindig/trunk/features/src/main/javascript/features/rpc/fe.transport.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/fe.transport.js?rev=777683&view=auto
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/fe.transport.js (added)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/fe.transport.js Fri May 22 19:55:02 2009
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+var gadgets = gadgets || {};
+gadgets.rpctx = gadgets.rpctx || {};
+
+/*
+ * For Gecko-based browsers, the security model allows a child to call a
+ * function on the frameElement of the iframe, even if the child is in
+ * a different domain. This method is dubbed "frameElement" (fe).
+ *
+ * The ability to add and call such functions on the frameElement allows
+ * a bidirectional channel to be setup via the adding of simple function
+ * references on the frameElement object itself. In this implementation,
+ * when the container sets up the authentication information for that gadget
+ * (by calling setAuth(...)) it as well adds a special function on the
+ * gadget's iframe. This function can then be used by the gadget to send
+ * messages to the container. In turn, when the gadget tries to send a
+ * message, it checks to see if this function has its own function stored
+ * that can be used by the container to call the gadget. If not, the
+ * function is created and subsequently used by the container.
+ * Note that as a result, FE can only be used by a container to call a
+ * particular gadget *after* that gadget has called the container at
+ * least once via FE.
+ *
+ *   fe: Gecko-specific frameElement trick.
+ *      - Firefox 1+
+ */
+gadgets.rpctx.FrameElement = function() {
+  // Consts for FrameElement.
+  var FE_G2C_CHANNEL = '__g2c_rpc';
+  var FE_C2G_CHANNEL = '__c2g_rpc';
+  var process;
+  var ready;
+
+  function callFrameElement(targetId, from, rpc) {
+    try {
+      if (from !== '..') {
+        // Call from gadget to the container.
+        var fe = window.frameElement;
+
+        if (typeof fe[FE_G2C_CHANNEL] === 'function') {
+          // Complete the setup of the FE channel if need be.
+          if (typeof fe[FE_G2C_CHANNEL][FE_C2G_CHANNEL] !== 'function') {
+            fe[FE_G2C_CHANNEL][FE_C2G_CHANNEL] = function(args) {
+              process(gadgets.json.parse(args));
+            };
+          }
+
+          // Conduct the RPC call.
+          fe[FE_G2C_CHANNEL](gadgets.json.stringify(rpc));
+          return;
+        }
+      } else {
+        // Call from container to gadget[targetId].
+        var frame = document.getElementById(targetId);
+
+        if (typeof frame[FE_G2C_CHANNEL] === 'function' &&
+            typeof frame[FE_G2C_CHANNEL][FE_C2G_CHANNEL] === 'function') {
+
+          // Conduct the RPC call.
+          frame[FE_G2C_CHANNEL][FE_C2G_CHANNEL](gadgets.json.stringify(rpc));
+          return;
+        }
+      }
+    } catch (e) {
+      return false;
+    }
+    return true;
+  }
+
+  return {
+    getCode: function() {
+      return 'fe';
+    },
+
+    isParentVerifiable: function() {
+      return false;
+    },
+  
+    init: function(processFn, readyFn) {
+      // No global setup.
+      process = processFn;
+      ready = readyFn;
+      return true;
+    },
+
+    setup: function(receiverId, token) {
+      // Indicate OK to call to container. This will be true
+      // by the end of this method.
+      if (receiverId !== '..') {
+        try {
+          var frame = document.getElementById(receiverId);
+          frame[FE_G2C_CHANNEL] = function(args) {
+            process(gadgets.json.parse(args));
+          };
+        } catch (e) {
+          return false;
+        }
+      }
+      if (receiverId === '..') {
+        ready('..', true);
+        var ackFn = function() {
+          window.setTimeout(function() {
+            gadgets.rpc.call(receiverId, gadgets.rpc.ACK)
+          }, 0);
+        }
+        if (document.body) {
+          // TODO Consider moving document.body check to registerOnLoadHandler
+          ackFn();
+        } else {
+          gadgets.util.registerOnLoadHandler(ackFn);
+        }
+      }
+      return true;
+    },
+
+    call: function(targetId, from, rpc) {
+      callFrameElement(targetId, from, rpc);
+    } 
+
+  };
+}();

Modified: incubator/shindig/trunk/features/src/main/javascript/features/rpc/feature.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/feature.xml?rev=777683&r1=777682&r2=777683&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/feature.xml (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/feature.xml Fri May 22 19:55:02 2009
@@ -29,9 +29,21 @@
 -->
   <name>rpc</name>
   <gadget>
+    <script src="wpm.transport.js"/>
+    <script src="dpm.transport.js"/>
+    <script src="fe.transport.js"/>
+    <script src="nix.transport.js"/>
+    <script src="rmr.transport.js"/>
+    <script src="ifpc.transport.js"/>
     <script src="rpc.js"/>
   </gadget>
   <container>
+    <script src="wpm.transport.js"/>
+    <script src="dpm.transport.js"/>
+    <script src="fe.transport.js"/>
+    <script src="nix.transport.js"/>
+    <script src="rmr.transport.js"/>
+    <script src="ifpc.transport.js"/>
     <script src="rpc.js"/>
   </container>
 </feature>

Added: incubator/shindig/trunk/features/src/main/javascript/features/rpc/ifpc.transport.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/ifpc.transport.js?rev=777683&view=auto
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/ifpc.transport.js (added)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/ifpc.transport.js Fri May 22 19:55:02 2009
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+var gadgets = gadgets || {};
+gadgets.rpctx = gadgets.rpctx || {};
+
+/*
+ * For all others, we have a fallback mechanism known as "ifpc". IFPC
+ * exploits the fact that while same-origin policy prohibits a frame from
+ * accessing members on a window not in the same domain, that frame can,
+ * however, navigate the window heirarchy (via parent). This is exploited by
+ * having a page on domain A that wants to talk to domain B create an iframe
+ * on domain B pointing to a special relay file and with a message encoded
+ * after the hash (#). This relay, in turn, finds the page on domain B, and
+ * can call a receipt function with the message given to it. The relay URL
+ * used by each caller is set via the gadgets.rpc.setRelayUrl(..) and
+ * *must* be called before the call method is used.
+ *
+ *   ifpc: Iframe-based method, utilizing a relay page, to send a message.
+ *      - No known major browsers still use this method, but it remains
+ *        useful as a catch-all fallback for the time being.
+ */
+gadgets.rpctx.Ifpc = function() {
+  var iframePool = [];
+  var callId = 0;
+  var ready;
+
+  /**
+   * Encodes arguments for the legacy IFPC wire format.
+   *
+   * @param {Object} args
+   * @return {String} the encoded args
+   */
+  function encodeLegacyData(args) {
+    var argsEscaped = [];
+    for(var i = 0, j = args.length; i < j; ++i) {
+      argsEscaped.push(encodeURIComponent(gadgets.json.stringify(args[i])));
+    }
+    return argsEscaped.join('&');
+  }
+
+  /**
+   * Helper function to emit an invisible IFrame.
+   * @param {String} src SRC attribute of the IFrame to emit.
+   * @private
+   */
+  function emitInvisibleIframe(src) {
+    var iframe;
+    // Recycle IFrames
+    for (var i = iframePool.length - 1; i >=0; --i) {
+      var ifr = iframePool[i];
+      try {
+        if (ifr && (ifr.recyclable || ifr.readyState === 'complete')) {
+          ifr.parentNode.removeChild(ifr);
+          if (window.ActiveXObject) {
+            // For MSIE, delete any iframes that are no longer being used. MSIE
+            // cannot reuse the IFRAME because a navigational click sound will
+            // be triggered when we set the SRC attribute.
+            // Other browsers scan the pool for a free iframe to reuse.
+            iframePool[i] = ifr = null;
+            iframePool.splice(i, 1);
+          } else {
+            ifr.recyclable = false;
+            iframe = ifr;
+            break;
+          }
+        }
+      } catch (e) {
+        // Ignore; IE7 throws an exception when trying to read readyState and
+        // readyState isn't set.
+      }
+    }
+    // Create IFrame if necessary
+    if (!iframe) {
+      iframe = document.createElement('iframe');
+      iframe.style.border = iframe.style.width = iframe.style.height = '0px';
+      iframe.style.visibility = 'hidden';
+      iframe.style.position = 'absolute';
+      iframe.onload = function() { this.recyclable = true; };
+      iframePool.push(iframe);
+    }
+    iframe.src = src;
+    setTimeout(function() { document.body.appendChild(iframe); }, 0);
+  }
+
+  return {
+    getCode: function() {
+      return 'ifpc';
+    },
+
+    isParentVerifiable: function() {
+      return true;
+    },
+
+    init: function(processFn, readyFn) {
+      // No global setup.
+      ready = readyFn;
+      ready('..', true);  // Ready immediately.
+      return true;
+    },
+
+    setup: function(receiverId, token) {
+      // Indicate readiness to send to receiver.
+      ready(receiverId, true);
+      return true;
+    },
+
+    call: function(targetId, from, rpc) {
+      // Retrieve the relay file used by IFPC. Note that
+      // this must be set before the call, and so we conduct
+      // an extra check to ensure it is not blank.
+      var relay = gadgets.rpc.getRelayUrl(targetId);
+      ++callId;
+
+      if (!relay) {
+        gadgets.warn('No relay file assigned for IFPC');
+        return;
+      }
+
+      // The RPC mechanism supports two formats for IFPC (legacy and current).
+      var src = null;
+      if (rpc.l) {
+        // Use legacy protocol.
+        // Format: #iframe_id&callId&num_packets&packet_num&block_of_data
+        var callArgs = rpc.a;
+        src = [relay, '#', encodeLegacyData([from, callId, 1, 0,
+               encodeLegacyData([from, serviceName, '', '', from].concat(
+                 callArgs))])].join('');
+      } else {
+        // Format: #targetId & sourceId@callId & packetNum & packetId & packetData
+        src = [relay, '#', targetId, '&', from, '@', callId,
+               '&1&0&', encodeURIComponent(gadgets.json.stringify(rpc))].join('');
+      }
+
+      // Conduct the IFPC call by creating the Iframe with
+      // the relay URL and appended message.
+      emitInvisibleIframe(src);
+      return true;
+    }
+  };
+}();

Added: incubator/shindig/trunk/features/src/main/javascript/features/rpc/nix.transport.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/nix.transport.js?rev=777683&view=auto
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/nix.transport.js (added)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/nix.transport.js Fri May 22 19:55:02 2009
@@ -0,0 +1,278 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+var gadgets = gadgets || {};
+gadgets.rpctx = gadgets.rpctx || {};
+
+/**
+ * For Internet Explorer before version 8, the security model allows anyone
+ * parent to set the value of the "opener" property on another window,
+ * with only the receiving window able to read it.
+ * This method is dubbed "Native IE XDC" (NIX).
+ *
+ * This method works by placing a handler object in the "opener" property
+ * of a gadget when the container sets up the authentication information
+ * for that gadget (by calling setAuthToken(...)). At that point, a NIX
+ * wrapper is created and placed into the gadget by calling
+ * theframe.contentWindow.opener = wrapper. Note that as a result, NIX can
+ * only be used by a container to call a particular gadget *after* that
+ * gadget has called the container at least once via NIX.
+ *
+ * The NIX wrappers in this RPC implementation are instances of a VBScript
+ * class that is created when this implementation loads. The reason for
+ * using a VBScript class stems from the fact that any object can be passed
+ * into the opener property.
+ * While this is a good thing, as it lets us pass functions and setup a true
+ * bidirectional channel via callbacks, it opens a potential security hole
+ * by which the other page can get ahold of the "window" or "document"
+ * objects in the parent page and in turn wreak havok. This is due to the
+ * fact that any JS object useful for establishing such a bidirectional
+ * channel (such as a function) can be used to access a function
+ * (eg. obj.toString, or a function itself) created in a specific context,
+ * in particular the global context of the sender. Suppose container
+ * domain C passes object obj to gadget on domain G. Then the gadget can
+ * access C's global context using:
+ * var parentWindow = (new obj.toString.constructor("return window;"))();
+ * Nulling out all of obj's properties doesn't fix this, since IE helpfully
+ * restores them to their original values if you do something like:
+ * delete obj.toString; delete obj.toString;
+ * Thus, we wrap the necessary functions and information inside a VBScript
+ * object. VBScript objects in IE, like DOM objects, are in fact COM
+ * wrappers when used in JavaScript, so we can safely pass them around
+ * without worrying about a breach of context while at the same time
+ * allowing them to act as a pass-through mechanism for information
+ * and function calls. The implementation details of this VBScript wrapper
+ * can be found in the setupChannel() method below.
+ *
+ *   nix: Internet Explorer-specific window.opener trick.
+ *     - Internet Explorer 6
+ *     - Internet Explorer 7
+ */
+gadgets.rpctx.Nix = function() {
+  // Consts for NIX. VBScript doesn't
+  // allow items to start with _ for some reason,
+  // so we need to make these names quite unique, as
+  // they will go into the global namespace.
+  var NIX_WRAPPER = 'GRPC____NIXVBS_wrapper';
+  var NIX_GET_WRAPPER = 'GRPC____NIXVBS_get_wrapper';
+  var NIX_HANDLE_MESSAGE = 'GRPC____NIXVBS_handle_message';
+  var NIX_CREATE_CHANNEL = 'GRPC____NIXVBS_create_channel';
+  var MAX_NIX_SEARCHES = 10;
+  var NIX_SEARCH_PERIOD = 500;
+
+  // JavaScript reference to the NIX VBScript wrappers.
+  // Gadgets will have but a single channel under
+  // nix_channels['..'] while containers will have a channel
+  // per gadget stored under the gadget's ID.
+  var nix_channels = {};
+
+  // Store the ready signal method for use on handshake complete.
+  var ready;
+  var numHandlerSearches = 0;
+
+  // Search for NIX handler to parent. Tries MAX_NIX_SEARCHES times every
+  // NIX_SEARCH_PERIOD milliseconds.
+  function conductHandlerSearch() {
+    // Call from gadget to the container.
+    var handler = nix_channels['..'];
+    if (handler) {
+      return;
+    }
+
+    if (++numHandlerSearches > MAX_NIX_SEARCHES) {
+      // Handshake failed. Will fall back.
+      gadgets.warn('Nix transport setup failed, falling back...');
+      ready('..', false);
+      return;
+    }
+
+    // If the gadget has yet to retrieve a reference to
+    // the NIX handler, try to do so now. We don't do a
+    // typeof(window.opener.GetAuthToken) check here
+    // because it means accessing that field on the COM object, which,
+    // being an internal function reference, is not allowed.
+    // "in" works because it merely checks for the prescence of
+    // the key, rather than actually accessing the object's property.
+    // This is just a sanity check, not a validity check.
+    if (!handler && window.opener && "GetAuthToken" in window.opener) {
+      handler = window.opener;
+
+      // Create the channel to the parent/container.
+      // First verify that it knows our auth token to ensure it's not
+      // an impostor.
+      if (handler.GetAuthToken() == gadgets.rpc.getAuthToken('..')) {
+        // Auth match - pass it back along with our wrapper to finish.
+        // own wrapper and our authentication token for co-verification.
+        var token = gadgets.rpc.getAuthToken('..');
+        handler.CreateChannel(window[NIX_GET_WRAPPER]('..', token),
+                              token);
+        // Set channel handler
+        nix_channels['..'] = handler;
+        window.opener = null;
+
+        // Signal success and readiness to send to parent.
+        // Container-to-gadget bit flipped in CreateChannel.
+        ready('..', true);
+        return;
+      }
+    }
+
+    // Try again.
+    window.setTimeout(function() { conductHandlerSearch(); },
+                      NIX_SEARCH_PERIOD);
+  }
+
+  return {
+    getCode: function() {
+      return 'nix';
+    },
+
+    isParentVerifiable: function() {
+      return false;
+    },
+
+    init: function(processFn, readyFn) {
+      ready = readyFn;
+
+      // Ensure VBScript wrapper code is in the page and that the
+      // global Javascript handlers have been set.
+      // VBScript methods return a type of 'unknown' when
+      // checked via the typeof operator in IE. Fortunately
+      // for us, this only applies to COM objects, so we
+      // won't see this for a real Javascript object.
+      if (typeof window[NIX_GET_WRAPPER] !== 'unknown') {
+        window[NIX_HANDLE_MESSAGE] = function(data) {
+          window.setTimeout(
+              function() { processFn(gadgets.json.parse(data)) }, 0);
+        };
+
+        window[NIX_CREATE_CHANNEL] = function(name, channel, token) {
+          // Verify the authentication token of the gadget trying
+          // to create a channel for us.
+          if (gadgets.rpc.getAuthToken(name) === token) {
+            nix_channels[name] = channel;
+            ready(name, true);
+          }
+        };
+
+        // Inject the VBScript code needed.
+        var vbscript =
+          // We create a class to act as a wrapper for
+          // a Javascript call, to prevent a break in of
+          // the context.
+          'Class ' + NIX_WRAPPER + '\n '
+
+          // An internal member for keeping track of the
+          // name of the document (container or gadget)
+          // for which this wrapper is intended. For
+          // those wrappers created by gadgets, this is not
+          // used (although it is set to "..")
+          + 'Private m_Intended\n'
+
+          // Stores the auth token used to communicate with
+          // the gadget. The GetChannelCreator method returns
+          // an object that returns this auth token. Upon matching
+          // that with its own, the gadget uses the object
+          // to actually establish the communication channel.
+          + 'Private m_Auth\n'
+
+          // Method for internally setting the value
+          // of the m_Intended property.
+          + 'Public Sub SetIntendedName(name)\n '
+          + 'If isEmpty(m_Intended) Then\n'
+          + 'm_Intended = name\n'
+          + 'End If\n'
+          + 'End Sub\n'
+
+          // Method for internally setting the value of the m_Auth property.
+          + 'Public Sub SetAuth(auth)\n '
+          + 'If isEmpty(m_Auth) Then\n'
+          + 'm_Auth = auth\n'
+          + 'End If\n'
+          + 'End Sub\n'
+
+          // A wrapper method which actually causes a
+          // message to be sent to the other context.
+          + 'Public Sub SendMessage(data)\n '
+          + NIX_HANDLE_MESSAGE + '(data)\n'
+          + 'End Sub\n'
+
+          // Returns the auth token to the gadget, so it can
+          // confirm a match before initiating the connection
+          + 'Public Function GetAuthToken()\n '
+          + 'GetAuthToken = m_Auth\n'
+          + 'End Function\n'
+
+          // Method for setting up the container->gadget
+          // channel. Not strictly needed in the gadget's
+          // wrapper, but no reason to get rid of it. Note here
+          // that we pass the intended name to the NIX_CREATE_CHANNEL
+          // method so that it can save the channel in the proper place
+          // *and* verify the channel via the authentication token passed
+          // here.
+          + 'Public Sub CreateChannel(channel, auth)\n '
+          + 'Call ' + NIX_CREATE_CHANNEL + '(m_Intended, channel, auth)\n'
+          + 'End Sub\n'
+          + 'End Class\n'
+
+          // Function to get a reference to the wrapper.
+          + 'Function ' + NIX_GET_WRAPPER + '(name, auth)\n'
+          + 'Dim wrap\n'
+          + 'Set wrap = New ' + NIX_WRAPPER + '\n'
+          + 'wrap.SetIntendedName name\n'
+          + 'wrap.SetAuth auth\n'
+          + 'Set ' + NIX_GET_WRAPPER + ' = wrap\n'
+          + 'End Function';
+
+        try {
+          window.execScript(vbscript, 'vbscript');
+        } catch (e) {
+          return false;
+        }
+      }
+      return true;
+    },
+
+    setup: function(receiverId, token) {
+      if (receiverId === '..') {
+        conductHandlerSearch();
+        return true;
+      }
+      try {
+        var frame = document.getElementById(receiverId);
+        var wrapper = window[NIX_GET_WRAPPER](receiverId, token);
+        frame.contentWindow.opener = wrapper;
+      } catch (e) {
+        return false;
+      }
+      return true;
+    },
+
+    call: function(targetId, from, rpc) {
+      try {
+        // If we have a handler, call it.
+        if (nix_channels[targetId]) {
+          nix_channels[targetId].SendMessage(gadgets.json.stringify(rpc));
+        }
+      } catch (e) {
+        return false;
+      }
+      return true;
+    }
+  };
+}();

Added: incubator/shindig/trunk/features/src/main/javascript/features/rpc/rmr.transport.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/rmr.transport.js?rev=777683&view=auto
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/rmr.transport.js (added)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/rmr.transport.js Fri May 22 19:55:02 2009
@@ -0,0 +1,484 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+var gadgets = gadgets || {};
+gadgets.rpctx = gadgets.rpctx || {};
+
+/*
+ * For older WebKit-based browsers, the security model does not allow for any
+ * known "native" hacks for conducting cross browser communication. However,
+ * a variation of the IFPC (see below) can be used, entitled "RMR". RMR is
+ * a technique that uses the resize event of the iframe to indicate that a
+ * message was sent (instead of the much slower/performance heavy polling
+ * technique used when a defined relay page is not avaliable). Simply put,
+ * RMR uses the same "pass the message by the URL hash" trick that IFPC
+ * uses to send a message, but instead of having an active relay page that
+ * runs a piece of code when it is loaded, RMR merely changes the URL
+ * of the relay page (which does not even have to exist on the domain)
+ * and then notifies the other party by resizing the relay iframe. RMR
+ * exploits the fact that iframes in the dom of page A can be resized
+ * by page A while the onresize event will be fired in the DOM of page B,
+ * thus providing a single bit channel indicating "message sent to you".
+ * This method has the added benefit that the relay need not be active,
+ * nor even exist: a 404 suffices just as well.
+ *
+ *   rmr: WebKit-specific resizing trick.
+ *      - Safari 2+
+ *      - Chrome 1
+ */
+gadgets.rpctx.Rmr = function() {
+  // Consts for RMR, including time in ms RMR uses to poll for
+  // its relay frame to be created, and the max # of polls it does.
+  var RMR_SEARCH_TIMEOUT = 500;
+  var RMR_MAX_POLLS = 10;
+
+  // JavaScript references to the channel objects used by RMR.
+  // Gadgets will have but a single channel under
+  // rmr_channels['..'] while containers will have a channel
+  // per gadget stored under the gadget's ID.
+  var rmr_channels = {};
+  
+  var process;
+  var ready;
+
+  /**
+   * Append an RMR relay frame to the document. This allows the receiver
+   * to start receiving messages.
+   *
+   * @param {object} channelFrame Relay frame to add to the DOM body.
+   * @param {string} relayUri Base URI for the frame.
+   * @param {string} Data to pass along to the frame.
+   */
+  function appendRmrFrame(channelFrame, relayUri, data) {
+    var appendFn = function() {
+      // Append the iframe.
+      document.body.appendChild(channelFrame);
+
+      // Set the src of the iframe to 'about:blank' first and then set it
+      // to the relay URI. This prevents the iframe from maintaining a src
+      // to the 'old' relay URI if the page is returned to from another.
+      // In other words, this fixes the bfcache issue that causes the iframe's
+      // src property to not be updated despite us assigning it a new value here.
+      channelFrame.src = 'about:blank';
+      channelFrame.src = relayUri + '#' + data;
+    }
+
+    if (document.body) {
+      appendFn();
+    } else {
+      // Common gadget case: attaching header during in-gadget handshake,
+      // when we may still be in script in head. Attach onload.
+      gadgets.util.registerOnLoadHandler(function() { appendFn(); });
+    }
+  }
+
+  /**
+   * Sets up the RMR transport frame for the given frameId. For gadgets
+   * calling containers, the frameId should be '..'.
+   *
+   * @param {string} frameId The ID of the frame.
+   */
+  function setupRmr(frameId) {
+    if (typeof rmr_channels[frameId] === "object") {
+      // Sanity check. Already done.
+      return;
+    }
+
+    var channelFrame = document.createElement('iframe');
+    var frameStyle = channelFrame.style;
+    frameStyle.position = 'absolute';
+    frameStyle.top = '0px';
+    frameStyle.border = '0';
+    frameStyle.opacity = '0';
+
+    // The width here is important as RMR
+    // makes use of the resize handler for the frame.
+    // Do not modify unless you test thoroughly!
+    frameStyle.width = '10px'
+    frameStyle.height = '1px';
+    channelFrame.id = 'rmrtransport-' + frameId;
+    channelFrame.name = channelFrame.id;
+
+    // Determine the relay uri by taking the existing one,
+    // removing the path and appending robots.txt. It is
+    // not important if robots.txt actually exists, since RMR
+    // browsers treat 404s as legitimate for the purposes of
+    // this communication.
+    var relayUri =
+        gadgets.rpc.getDomainRoot(gadgets.rpc.getRelayUrl(frameId)) + '/robots.txt';
+
+    rmr_channels[frameId] = {
+      frame: channelFrame,
+      receiveWindow: null,
+      relayUri: relayUri,
+      searchCounter : 0,
+      width: 10,
+
+      // Waiting means "waiting for acknowledgement to be received."
+      // Acknowledgement always comes as a special ACK
+      // message having been received. This message is received
+      // during handshake in different ways by the container and
+      // gadget, and by normal RMR message passing once the handshake
+      // is complete.
+      waiting: true,
+      queue: [],
+
+      // Number of non-ACK messages that have been sent to the recipient
+      // and have been acknowledged.
+      sendId: 0,
+
+      // Number of messages received and processed from the sender.
+      // This is the number that accompanies every ACK to tell the
+      // sender to clear its queue.
+      recvId: 0
+    };
+
+    if (frameId !== '..') {
+      // Container always appends a relay to the gadget, before
+      // the gadget appends its own relay back to container. The
+      // gadget, in the meantime, refuses to attach the container
+      // relay until it finds this one. Thus, the container knows
+      // for certain that gadget to container communication is set
+      // up by the time it finds its own relay. In addition to
+      // establishing a reliable handshake protocol, this also
+      // makes it possible for the gadget to send an initial batch
+      // of messages to the container ASAP.
+      appendRmrFrame(channelFrame, relayUri, getRmrData(frameId));
+    }
+     
+    // Start searching for our own frame on the other page.
+    conductRmrSearch(frameId);
+  }
+
+  /**
+   * Searches for a relay frame, created by the sender referenced by
+   * frameId, with which this context receives messages. Once
+   * found with proper permissions, attaches a resize handler which
+   * signals messages to be sent.
+   *
+   * @param {string} frameId Frame ID of the prospective sender.
+   */
+  function conductRmrSearch(frameId) {
+    var channelWindow = null;
+
+    // Increment the search counter.
+    rmr_channels[frameId].searchCounter++;
+
+    if (frameId === '..') {
+      // We are a gadget.
+      channelWindow = window.parent.frames['rmrtransport-' + window.name];
+    } else {
+      // We are a container.
+      channelWindow = window.frames[frameId].frames['rmrtransport-..'];
+    }
+
+    var status = false;
+
+    if (channelWindow) {
+      // We have a valid reference to "our" RMR transport frame.
+      // Register the proper event handlers.
+      status = registerRmrChannel(frameId, channelWindow);
+    }
+
+    if (!status) {
+      // Not found yet. Continue searching, but only if the counter
+      // has not reached the threshold.
+      if (rmr_channels[frameId].searchCounter > RMR_MAX_POLLS) {
+        // If we reach this point, then RMR has failed and we
+        // fall back to IFPC.
+        return;
+      }
+
+      setTimeout(function() {
+        conductRmrSearch(frameId);
+      }, RMR_SEARCH_TIMEOUT);
+    }
+  }
+
+  /**
+   * Attempts to conduct an RPC call to the specified
+   * target with the specified data via the RMR
+   * method. If this method fails, the system attempts again
+   * using the known default of IFPC.
+   *
+   * @param {String} targetId Module Id of the RPC service provider.
+   * @param {String} serviceName Name of the service to call.
+   * @param {String} from Module Id of the calling provider.
+   * @param {Object} rpc The RPC data for this call.
+   */
+  function callRmr(targetId, serviceName, from, rpc) {
+    var handler = null;
+
+    if (from !== '..') {
+      // Call from gadget to the container.
+      handler = rmr_channels['..'];
+    } else {
+      // Call from container to the gadget.
+      handler = rmr_channels[targetId];
+    }
+
+    if (handler) {
+      // Queue the current message if not ACK.
+      // ACK is always sent through getRmrData(...).
+      if (serviceName !== gadgets.rpc.ACK) {
+        handler.queue.push(rpc);
+      }
+
+      if (handler.waiting ||
+          (handler.queue.length === 0 &&
+           !(serviceName === gadgets.rpc.ACK && rpc && rpc.ackAlone === true))) {
+        // If we are awaiting a response from any previously-sent messages,
+        // or if we don't have anything new to send, just return.
+        // Note that we don't short-return if we're ACKing just-received
+        // messages.
+        return true;
+      }
+
+      if (handler.queue.length > 0) {
+        handler.waiting = true;
+      }
+
+      var url = handler.relayUri + "#" + getRmrData(targetId);
+
+      try {
+        // Update the URL with the message.
+        handler.frame.contentWindow.location = url;
+
+        // Resize the frame.
+        var newWidth = handler.width == 10 ? 20 : 10;
+        handler.frame.style.width = newWidth + 'px';
+        handler.width = newWidth;
+
+        // Done!
+      } catch (e) {
+        // Something about location-setting or resizing failed.
+        // This should never happen, but if it does, fall back to
+        // the default transport.
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  /**
+   * Returns as a string the data to be appended to an RMR relay frame,
+   * constructed from the current request queue plus an ACK message indicating
+   * the currently latest-processed message ID.
+   *
+   * @param {string} toFrameId Frame whose sendable queued data to retrieve.
+   */
+  function getRmrData(toFrameId) {
+    var channel = rmr_channels[toFrameId];
+    var rmrData = {id: channel.sendId};
+    if (channel) {
+      rmrData.d = Array.prototype.slice.call(channel.queue, 0);
+      rmrData.d.push({s:gadgets.rpc.ACK, id:channel.recvId});
+    }
+    return gadgets.json.stringify(rmrData);
+  }
+
+  /**
+   * Retrieve data from the channel keyed by the given frameId,
+   * processing it as a batch. All processed data is assumed to have been
+   * generated by getRmrData(...), pairing that method with this.
+   *
+   * @param {string} fromFrameId Frame from which data is being retrieved.
+   */
+  function processRmrData(fromFrameId) {
+    var channel = rmr_channels[fromFrameId];
+    var data = channel.receiveWindow.location.hash.substring(1);
+
+    // Decode the RPC object array.
+    var rpcObj = gadgets.json.parse(decodeURIComponent(data)) || {};
+    var rpcArray = rpcObj.d || [];
+
+    var nonAckReceived = false;
+    var noLongerWaiting = false;
+
+    var numBypassed = 0;
+    var numToBypass = (channel.recvId - rpcObj.id);
+    for (var i = 0; i < rpcArray.length; ++i) {
+      var rpc = rpcArray[i];
+
+      // If we receive an ACK message, then mark the current
+      // handler as no longer waiting and send out the next
+      // queued message.
+      if (rpc.s === gadgets.rpc.ACK) {
+        // ACK received - whether this came from a handshake or
+        // an active call, in either case it indicates readiness to
+        // send messages to the from frame.
+        ready(fromFrameId, true);
+
+        if (channel.waiting) {
+          noLongerWaiting = true;
+        }
+
+        channel.waiting = false;
+        var newlyAcked = Math.max(0, rpc.id - channel.sendId);
+        channel.queue.splice(0, newlyAcked);
+        channel.sendId = Math.max(channel.sendId, rpc.id || 0);
+        continue;
+      }
+
+      // If we get here, we've received > 0 non-ACK messages to
+      // process. Indicate this bit for later.
+      nonAckReceived = true;
+
+      // Bypass any messages already received.
+      if (++numBypassed <= numToBypass) {
+        continue;
+      }
+
+      ++channel.recvId;
+      process(rpc);  // actually dispatch the message
+    }
+
+    // Send an ACK indicating that we got/processed the message(s).
+    // Do so if we've received a message to process or if we were waiting
+    // before but a received ACK has cleared our waiting bit, and we have
+    // more messages to send. Performing this operation causes additional
+    // messages to be sent.
+    if (nonAckReceived ||
+        (noLongerWaiting && channel.queue.length > 0)) {
+      var from = (fromFrameId === '..') ? window.name : '..';
+      callRmr(fromFrameId, gadgets.rpc.ACK, from, {ackAlone: nonAckReceived});
+    }
+  }
+
+  /**
+   * Registers the RMR channel handler for the given frameId and associated
+   * channel window.
+   *
+   * @param {string} frameId The ID of the frame for which this channel is being
+   *   registered.
+   * @param {Object} channelWindow The window of the receive frame for this
+   *   channel, if any.
+   *
+   * @return {boolean} True if the frame was setup successfully, false
+   *   otherwise.
+   */
+  function registerRmrChannel(frameId, channelWindow) {
+    var channel = rmr_channels[frameId];
+
+    // Verify that the channel is ready for receiving.
+    try {
+      var canAccess = false;
+
+      // Check to see if the document is in the window. For Chrome, this
+      // will return 'false' if the channelWindow is inaccessible by this
+      // piece of JavaScript code, meaning that the URL of the channelWindow's
+      // parent iframe has not yet changed from 'about:blank'. We do this
+      // check this way because any true *access* on the channelWindow object
+      // will raise a security exception, which, despite the try-catch, still
+      // gets reported to the debugger (it does not break execution, the try
+      // handles that problem, but it is still reported, which is bad form).
+      // This check always succeeds in Safari 3.1 regardless of the state of
+      // the window.
+      canAccess = 'document' in channelWindow;
+
+      if (!canAccess) {
+        return false;
+      }
+
+      // Check to see if the document is an object. For Safari 3.1, this will
+      // return undefined if the page is still inaccessible. Unfortunately, this
+      // *will* raise a security issue in the debugger.
+      // TODO Find a way around this problem.
+      canAccess = typeof channelWindow['document'] == 'object';
+
+      if (!canAccess) {
+        return false;
+      }
+
+      // Once we get here, we know we can access the document (and anything else)
+      // on the window object. Therefore, we check to see if the location is
+      // still about:blank (this takes care of the Safari 3.2 case).
+      var loc = channelWindow.location.href;
+
+      // Check if this is about:blank for Safari.
+      if (loc === 'about:blank') {
+        return false;
+      }
+    } catch (ex) {
+      // For some reason, the iframe still points to about:blank. We try
+      // again in a bit.
+      return false;
+    }
+
+    // Save a reference to the receive window.
+    channel.receiveWindow = channelWindow;
+
+    // Register the onresize handler.
+    channelWindow.onresize = function() {
+      processRmrData(frameId);
+    };
+
+    if (frameId === '..') {
+      // Gadget to container. Signal to the container that the gadget
+      // is ready to receive messages by attaching the g -> c relay.
+      // As a nice optimization, pass along any gadget to container
+      // queued messages that have backed up since then. ACK is enqueued in
+      // getRmrData to ensure that the container's waiting flag is set to false
+      // (this happens in the below code run on the container side).
+      appendRmrFrame(channel.frame, channel.relayUri, getRmrData(frameId));
+    }
+
+    // Attempt to process the messages that the other party may have set in
+    // the resize frame's hash. For gadget to container, this is the gadget
+    // finding the container-set IFRAME, which has an ACK attached to it.
+    // For gadget to container, the gadget may have enqueued messages already,
+    // and sent them in the above code. This too will have an ACK message.
+    // The net effect is that both sides of the connection will have their
+    // "waiting" bit set to false, so they're ready to send messages.
+    processRmrData(frameId);
+
+    return true;
+  }
+
+  return {
+    getCode: function() {
+      return 'rmr';
+    },
+
+    isParentVerifiable: function() {
+      return true;
+    },
+
+    init: function(processFn, readyFn) {
+      // No global setup.
+      process = processFn;
+      ready = readyFn;
+      return true;
+    },
+
+    setup: function(receiverId, token) {
+      try {
+        setupRmr(receiverId);
+      } catch (e) {
+        gadgets.warn('Caught exception setting up RMR: ' + e);
+        return false;
+      }
+      return true;
+    },
+
+    call: function(targetId, from, rpc) {
+      return callRmr(targetId, rpc.s, from, rpc);
+    }
+  };
+}();

Modified: incubator/shindig/trunk/features/src/main/javascript/features/rpc/rpc.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/rpc/rpc.js?rev=777683&r1=777682&r2=777683&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/rpc/rpc.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/rpc/rpc.js Fri May 22 19:55:02 2009
@@ -23,6 +23,11 @@
 var gadgets = gadgets || {};
 
 /**
+ * gadgets.rpc Transports
+ *
+ * All transports are stored in object gadgets.rpctx, and are provided
+ * to the core gadgets.rpc library by various build rules.
+ * 
  * Transports used by core gadgets.rpc code to actually pass messages.
  * each transport implements the same "interface" exposing hooks that
  * the core library calls at strategic points to set up and use
@@ -49,1093 +54,6 @@
  *     the sender identified by 'from'. Payload is an rpc packet. Returns
  *     true if successful, false otherwise.
  */
-gadgets.transports = {};
-
-/**
- * Transport for browsers that support native messaging (various implementations
- * of the HTML5 postMessage method). Officially defined at
- * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html.
- *
- * postMessage is a native implementation of XDC. A page registers that
- * it would like to receive messages by listening the the "message" event
- * on the window (document in DPM) object. In turn, another page can
- * raise that event by calling window.postMessage (document.postMessage
- * in DPM) with a string representing the message and a string
- * indicating on which domain the receiving page must be to receive
- * the message. The target page will then have its "message" event raised
- * if the domain matches and can, in turn, check the origin of the message
- * and process the data contained within.
- *
- *   wpm: postMessage on the window object.
- *      - Internet Explorer 8+
- *      - Safari (latest nightlies as of 26/6/2008)
- *      - Firefox 3+
- *      - Opera 9+
- */
-gadgets.transports.Wpm = function() {
-  var ready;
-
-  return {
-    getCode: function() {
-      return 'wpm';
-    },
-
-    isParentVerifiable: function() {
-      return true;
-    },
-
-    init: function(processFn, readyFn) {
-      ready = readyFn;
-      // Set up native postMessage handler.
-      window.addEventListener('message', function(packet) {
-        // TODO validate packet.domain for security reasons
-        processFn(gadgets.json.parse(packet.data));
-      }, false);
-      ready('..', true);  // Immediately ready to send to parent.
-      return true;
-    },
-
-    setup: function(receiverId, token) {
-      // If we're a gadget, send an ACK message to indicate to container
-      // that we're ready to receive messages.
-      if (receiverId === '..') {
-        gadgets.rpc.call(receiverId, gadgets.rpc.ACK);
-      }
-      return true;
-    },
-
-    call: function(targetId, from, rpc) {
-      var targetWin = targetId === '..' ? parent : frames[targetId];
-      var relay = gadgets.rpc.getRelayUrl(targetId);
-      if (relay) {
-        targetWin.postMessage(gadgets.json.stringify(rpc), relay);
-      }
-      return true;
-    }
-  };
-}();
-
-/**
- * Similar transport to WPM, but uses document element instead.
- * This was implemented by Opera 8 before the WPM standard was complete.
- *
- *     dpm: postMessage on the document object.
- *        - Opera 8+
- */
-gadgets.transports.Dpm = function() {
-  var ready;
-  return {
-    getCode: function() {
-      return 'dpm';
-    },
-
-    isParentVerifiable: function() {
-      return false;
-    },
-
-    init: function(processFn, readyFn) {
-      ready = readyFn;
-      // Set up native postMessage handler.
-      window.addEventListener('message', function(packet) {
-        // TODO validate packet.domain for security reasons
-        processFn(gadgets.json.parse(packet.data));
-      }, false);
-      ready('..', true);  // Ready immediately.
-      return true;
-    },
-
-    setup: function(receiverId, token) {
-      // See WPM setup comment.
-      if (receiverId === '..') {
-        gadgets.rpc.call(receiverId, gadgets.rpc.ACK);
-      }
-      return true;
-    },
-
-    call: function(targetId, from, rpc) {
-      var targetDoc = targetId === '..' ? parent.document :
-                                          frames[targetId].document;
-      targetDoc.postMessage(gadgets.json.stringify(rpc));
-      return true;
-    }
-  };
-}();
-
-/**
- * For Internet Explorer before version 8, the security model allows anyone
- * parent to set the value of the "opener" property on another window,
- * with only the receiving window able to read it.
- * This method is dubbed "Native IE XDC" (NIX).
- *
- * This method works by placing a handler object in the "opener" property
- * of a gadget when the container sets up the authentication information
- * for that gadget (by calling setAuthToken(...)). At that point, a NIX
- * wrapper is created and placed into the gadget by calling
- * theframe.contentWindow.opener = wrapper. Note that as a result, NIX can
- * only be used by a container to call a particular gadget *after* that
- * gadget has called the container at least once via NIX.
- *
- * The NIX wrappers in this RPC implementation are instances of a VBScript
- * class that is created when this implementation loads. The reason for
- * using a VBScript class stems from the fact that any object can be passed
- * into the opener property.
- * While this is a good thing, as it lets us pass functions and setup a true
- * bidirectional channel via callbacks, it opens a potential security hole
- * by which the other page can get ahold of the "window" or "document"
- * objects in the parent page and in turn wreak havok. This is due to the
- * fact that any JS object useful for establishing such a bidirectional
- * channel (such as a function) can be used to access a function
- * (eg. obj.toString, or a function itself) created in a specific context,
- * in particular the global context of the sender. Suppose container
- * domain C passes object obj to gadget on domain G. Then the gadget can
- * access C's global context using:
- * var parentWindow = (new obj.toString.constructor("return window;"))();
- * Nulling out all of obj's properties doesn't fix this, since IE helpfully
- * restores them to their original values if you do something like:
- * delete obj.toString; delete obj.toString;
- * Thus, we wrap the necessary functions and information inside a VBScript
- * object. VBScript objects in IE, like DOM objects, are in fact COM
- * wrappers when used in JavaScript, so we can safely pass them around
- * without worrying about a breach of context while at the same time
- * allowing them to act as a pass-through mechanism for information
- * and function calls. The implementation details of this VBScript wrapper
- * can be found in the setupChannel() method below.
- *
- *   nix: Internet Explorer-specific window.opener trick.
- *     - Internet Explorer 6
- *     - Internet Explorer 7
- */
-gadgets.transports.Nix = function() {
-  // Consts for NIX. VBScript doesn't
-  // allow items to start with _ for some reason,
-  // so we need to make these names quite unique, as
-  // they will go into the global namespace.
-  var NIX_WRAPPER = 'GRPC____NIXVBS_wrapper';
-  var NIX_GET_WRAPPER = 'GRPC____NIXVBS_get_wrapper';
-  var NIX_HANDLE_MESSAGE = 'GRPC____NIXVBS_handle_message';
-  var NIX_CREATE_CHANNEL = 'GRPC____NIXVBS_create_channel';
-  var MAX_NIX_SEARCHES = 10;
-  var NIX_SEARCH_PERIOD = 500;
-
-  // JavaScript reference to the NIX VBScript wrappers.
-  // Gadgets will have but a single channel under
-  // nix_channels['..'] while containers will have a channel
-  // per gadget stored under the gadget's ID.
-  var nix_channels = {};
-
-  // Store the ready signal method for use on handshake complete.
-  var ready;
-  var numHandlerSearches = 0;
-
-  // Search for NIX handler to parent. Tries MAX_NIX_SEARCHES times every
-  // NIX_SEARCH_PERIOD milliseconds.
-  function conductHandlerSearch() {
-    // Call from gadget to the container.
-    var handler = nix_channels['..'];
-    if (handler) {
-      return;
-    }
-
-    if (++numHandlerSearches > MAX_NIX_SEARCHES) {
-      // Handshake failed. Will fall back.
-      gadgets.warn('Nix transport setup failed, falling back...');
-      ready('..', false);
-      return;
-    }
-
-    // If the gadget has yet to retrieve a reference to
-    // the NIX handler, try to do so now. We don't do a
-    // typeof(window.opener.GetAuthToken) check here
-    // because it means accessing that field on the COM object, which,
-    // being an internal function reference, is not allowed.
-    // "in" works because it merely checks for the prescence of
-    // the key, rather than actually accessing the object's property.
-    // This is just a sanity check, not a validity check.
-    if (!handler && window.opener && "GetAuthToken" in window.opener) {
-      handler = window.opener;
-
-      // Create the channel to the parent/container.
-      // First verify that it knows our auth token to ensure it's not
-      // an impostor.
-      if (handler.GetAuthToken() == gadgets.rpc.getAuthToken('..')) {
-        // Auth match - pass it back along with our wrapper to finish.
-        // own wrapper and our authentication token for co-verification.
-        var token = gadgets.rpc.getAuthToken('..');
-        handler.CreateChannel(window[NIX_GET_WRAPPER]('..', token),
-                              token);
-        // Set channel handler
-        nix_channels['..'] = handler;
-        window.opener = null;
-
-        // Signal success and readiness to send to parent.
-        // Container-to-gadget bit flipped in CreateChannel.
-        ready('..', true);
-        return;
-      }
-    }
-
-    // Try again.
-    window.setTimeout(function() { conductHandlerSearch(); },
-                      NIX_SEARCH_PERIOD);
-  }
-
-  return {
-    getCode: function() {
-      return 'nix';
-    },
-
-    isParentVerifiable: function() {
-      return false;
-    },
-
-    init: function(processFn, readyFn) {
-      ready = readyFn;
-
-      // Ensure VBScript wrapper code is in the page and that the
-      // global Javascript handlers have been set.
-      // VBScript methods return a type of 'unknown' when
-      // checked via the typeof operator in IE. Fortunately
-      // for us, this only applies to COM objects, so we
-      // won't see this for a real Javascript object.
-      if (typeof window[NIX_GET_WRAPPER] !== 'unknown') {
-        window[NIX_HANDLE_MESSAGE] = function(data) {
-          window.setTimeout(
-              function() { processFn(gadgets.json.parse(data)) }, 0);
-        };
-
-        window[NIX_CREATE_CHANNEL] = function(name, channel, token) {
-          // Verify the authentication token of the gadget trying
-          // to create a channel for us.
-          if (gadgets.rpc.getAuthToken(name) === token) {
-            nix_channels[name] = channel;
-            ready(name, true);
-          }
-        };
-
-        // Inject the VBScript code needed.
-        var vbscript =
-          // We create a class to act as a wrapper for
-          // a Javascript call, to prevent a break in of
-          // the context.
-          'Class ' + NIX_WRAPPER + '\n '
-
-          // An internal member for keeping track of the
-          // name of the document (container or gadget)
-          // for which this wrapper is intended. For
-          // those wrappers created by gadgets, this is not
-          // used (although it is set to "..")
-          + 'Private m_Intended\n'
-
-          // Stores the auth token used to communicate with
-          // the gadget. The GetChannelCreator method returns
-          // an object that returns this auth token. Upon matching
-          // that with its own, the gadget uses the object
-          // to actually establish the communication channel.
-          + 'Private m_Auth\n'
-
-          // Method for internally setting the value
-          // of the m_Intended property.
-          + 'Public Sub SetIntendedName(name)\n '
-          + 'If isEmpty(m_Intended) Then\n'
-          + 'm_Intended = name\n'
-          + 'End If\n'
-          + 'End Sub\n'
-
-          // Method for internally setting the value of the m_Auth property.
-          + 'Public Sub SetAuth(auth)\n '
-          + 'If isEmpty(m_Auth) Then\n'
-          + 'm_Auth = auth\n'
-          + 'End If\n'
-          + 'End Sub\n'
-
-          // A wrapper method which actually causes a
-          // message to be sent to the other context.
-          + 'Public Sub SendMessage(data)\n '
-          + NIX_HANDLE_MESSAGE + '(data)\n'
-          + 'End Sub\n'
-
-          // Returns the auth token to the gadget, so it can
-          // confirm a match before initiating the connection
-          + 'Public Function GetAuthToken()\n '
-          + 'GetAuthToken = m_Auth\n'
-          + 'End Function\n'
-
-          // Method for setting up the container->gadget
-          // channel. Not strictly needed in the gadget's
-          // wrapper, but no reason to get rid of it. Note here
-          // that we pass the intended name to the NIX_CREATE_CHANNEL
-          // method so that it can save the channel in the proper place
-          // *and* verify the channel via the authentication token passed
-          // here.
-          + 'Public Sub CreateChannel(channel, auth)\n '
-          + 'Call ' + NIX_CREATE_CHANNEL + '(m_Intended, channel, auth)\n'
-          + 'End Sub\n'
-          + 'End Class\n'
-
-          // Function to get a reference to the wrapper.
-          + 'Function ' + NIX_GET_WRAPPER + '(name, auth)\n'
-          + 'Dim wrap\n'
-          + 'Set wrap = New ' + NIX_WRAPPER + '\n'
-          + 'wrap.SetIntendedName name\n'
-          + 'wrap.SetAuth auth\n'
-          + 'Set ' + NIX_GET_WRAPPER + ' = wrap\n'
-          + 'End Function';
-
-        try {
-          window.execScript(vbscript, 'vbscript');
-        } catch (e) {
-          return false;
-        }
-      }
-      return true;
-    },
-
-    setup: function(receiverId, token) {
-      if (receiverId === '..') {
-        conductHandlerSearch();
-        return true;
-      }
-      try {
-        var frame = document.getElementById(receiverId);
-        var wrapper = window[NIX_GET_WRAPPER](receiverId, token);
-        frame.contentWindow.opener = wrapper;
-      } catch (e) {
-        return false;
-      }
-      return true;
-    },
-
-    call: function(targetId, from, rpc) {
-      try {
-        // If we have a handler, call it.
-        if (nix_channels[targetId]) {
-          nix_channels[targetId].SendMessage(gadgets.json.stringify(rpc));
-        }
-      } catch (e) {
-        return false;
-      }
-      return true;
-    }
-  };
-}();
-
-/*
- * For Gecko-based browsers, the security model allows a child to call a
- * function on the frameElement of the iframe, even if the child is in
- * a different domain. This method is dubbed "frameElement" (fe).
- *
- * The ability to add and call such functions on the frameElement allows
- * a bidirectional channel to be setup via the adding of simple function
- * references on the frameElement object itself. In this implementation,
- * when the container sets up the authentication information for that gadget
- * (by calling setAuth(...)) it as well adds a special function on the
- * gadget's iframe. This function can then be used by the gadget to send
- * messages to the container. In turn, when the gadget tries to send a
- * message, it checks to see if this function has its own function stored
- * that can be used by the container to call the gadget. If not, the
- * function is created and subsequently used by the container.
- * Note that as a result, FE can only be used by a container to call a
- * particular gadget *after* that gadget has called the container at
- * least once via FE.
- *
- *   fe: Gecko-specific frameElement trick.
- *      - Firefox 1+
- */
-gadgets.transports.FrameElement = function() {
-  // Consts for FrameElement.
-  var FE_G2C_CHANNEL = '__g2c_rpc';
-  var FE_C2G_CHANNEL = '__c2g_rpc';
-  var process;
-  var ready;
-
-  function callFrameElement(targetId, from, rpc) {
-    try {
-      if (from !== '..') {
-        // Call from gadget to the container.
-        var fe = window.frameElement;
-
-        if (typeof fe[FE_G2C_CHANNEL] === 'function') {
-          // Complete the setup of the FE channel if need be.
-          if (typeof fe[FE_G2C_CHANNEL][FE_C2G_CHANNEL] !== 'function') {
-            fe[FE_G2C_CHANNEL][FE_C2G_CHANNEL] = function(args) {
-              process(gadgets.json.parse(args));
-            };
-          }
-
-          // Conduct the RPC call.
-          fe[FE_G2C_CHANNEL](gadgets.json.stringify(rpc));
-          return;
-        }
-      } else {
-        // Call from container to gadget[targetId].
-        var frame = document.getElementById(targetId);
-
-        if (typeof frame[FE_G2C_CHANNEL] === 'function' &&
-            typeof frame[FE_G2C_CHANNEL][FE_C2G_CHANNEL] === 'function') {
-
-          // Conduct the RPC call.
-          frame[FE_G2C_CHANNEL][FE_C2G_CHANNEL](gadgets.json.stringify(rpc));
-          return;
-        }
-      }
-    } catch (e) {
-      return false;
-    }
-    return true;
-  }
-
-  return {
-    getCode: function() {
-      return 'fe';
-    },
-
-    isParentVerifiable: function() {
-      return false;
-    },
-  
-    init: function(processFn, readyFn) {
-      // No global setup.
-      process = processFn;
-      ready = readyFn;
-      return true;
-    },
-
-    setup: function(receiverId, token) {
-      // Indicate OK to call to container. This will be true
-      // by the end of this method.
-      if (receiverId !== '..') {
-        try {
-          var frame = document.getElementById(receiverId);
-          frame[FE_G2C_CHANNEL] = function(args) {
-            process(gadgets.json.parse(args));
-          };
-        } catch (e) {
-          return false;
-        }
-      }
-      if (receiverId === '..') {
-        ready('..', true);
-        var ackFn = function() {
-          window.setTimeout(function() {
-            gadgets.rpc.call(receiverId, gadgets.rpc.ACK)
-          }, 0);
-        }
-        if (document.body) {
-          // TODO Consider moving document.body check to registerOnLoadHandler
-          ackFn();
-        } else {
-          gadgets.util.registerOnLoadHandler(ackFn);
-        }
-      }
-      return true;
-    },
-
-    call: function(targetId, from, rpc) {
-      callFrameElement(targetId, from, rpc);
-    } 
-
-  };
-}();
-
-/*
- * For older WebKit-based browsers, the security model does not allow for any
- * known "native" hacks for conducting cross browser communication. However,
- * a variation of the IFPC (see below) can be used, entitled "RMR". RMR is
- * a technique that uses the resize event of the iframe to indicate that a
- * message was sent (instead of the much slower/performance heavy polling
- * technique used when a defined relay page is not avaliable). Simply put,
- * RMR uses the same "pass the message by the URL hash" trick that IFPC
- * uses to send a message, but instead of having an active relay page that
- * runs a piece of code when it is loaded, RMR merely changes the URL
- * of the relay page (which does not even have to exist on the domain)
- * and then notifies the other party by resizing the relay iframe. RMR
- * exploits the fact that iframes in the dom of page A can be resized
- * by page A while the onresize event will be fired in the DOM of page B,
- * thus providing a single bit channel indicating "message sent to you".
- * This method has the added benefit that the relay need not be active,
- * nor even exist: a 404 suffices just as well.
- *
- *   rmr: WebKit-specific resizing trick.
- *      - Safari 2+
- *      - Chrome 1
- */
-gadgets.transports.Rmr = function() {
-  // Consts for RMR, including time in ms RMR uses to poll for
-  // its relay frame to be created, and the max # of polls it does.
-  var RMR_SEARCH_TIMEOUT = 500;
-  var RMR_MAX_POLLS = 10;
-
-  // JavaScript references to the channel objects used by RMR.
-  // Gadgets will have but a single channel under
-  // rmr_channels['..'] while containers will have a channel
-  // per gadget stored under the gadget's ID.
-  var rmr_channels = {};
-  
-  var process;
-  var ready;
-
-  /**
-   * Append an RMR relay frame to the document. This allows the receiver
-   * to start receiving messages.
-   *
-   * @param {object} channelFrame Relay frame to add to the DOM body.
-   * @param {string} relayUri Base URI for the frame.
-   * @param {string} Data to pass along to the frame.
-   */
-  function appendRmrFrame(channelFrame, relayUri, data) {
-    var appendFn = function() {
-      // Append the iframe.
-      document.body.appendChild(channelFrame);
-
-      // Set the src of the iframe to 'about:blank' first and then set it
-      // to the relay URI. This prevents the iframe from maintaining a src
-      // to the 'old' relay URI if the page is returned to from another.
-      // In other words, this fixes the bfcache issue that causes the iframe's
-      // src property to not be updated despite us assigning it a new value here.
-      channelFrame.src = 'about:blank';
-      channelFrame.src = relayUri + '#' + data;
-    }
-
-    if (document.body) {
-      appendFn();
-    } else {
-      // Common gadget case: attaching header during in-gadget handshake,
-      // when we may still be in script in head. Attach onload.
-      gadgets.util.registerOnLoadHandler(function() { appendFn(); });
-    }
-  }
-
-  /**
-   * Sets up the RMR transport frame for the given frameId. For gadgets
-   * calling containers, the frameId should be '..'.
-   *
-   * @param {string} frameId The ID of the frame.
-   */
-  function setupRmr(frameId) {
-    if (typeof rmr_channels[frameId] === "object") {
-      // Sanity check. Already done.
-      return;
-    }
-
-    var channelFrame = document.createElement('iframe');
-    var frameStyle = channelFrame.style;
-    frameStyle.position = 'absolute';
-    frameStyle.top = '0px';
-    frameStyle.border = '0';
-    frameStyle.opacity = '0';
-
-    // The width here is important as RMR
-    // makes use of the resize handler for the frame.
-    // Do not modify unless you test thoroughly!
-    frameStyle.width = '10px'
-    frameStyle.height = '1px';
-    channelFrame.id = 'rmrtransport-' + frameId;
-    channelFrame.name = channelFrame.id;
-
-    // Determine the relay uri by taking the existing one,
-    // removing the path and appending robots.txt. It is
-    // not important if robots.txt actually exists, since RMR
-    // browsers treat 404s as legitimate for the purposes of
-    // this communication.
-    var relayUri =
-        gadgets.rpc.getDomainRoot(gadgets.rpc.getRelayUrl(frameId)) + '/robots.txt';
-
-    rmr_channels[frameId] = {
-      frame: channelFrame,
-      receiveWindow: null,
-      relayUri: relayUri,
-      searchCounter : 0,
-      width: 10,
-
-      // Waiting means "waiting for acknowledgement to be received."
-      // Acknowledgement always comes as a special ACK
-      // message having been received. This message is received
-      // during handshake in different ways by the container and
-      // gadget, and by normal RMR message passing once the handshake
-      // is complete.
-      waiting: true,
-      queue: [],
-
-      // Number of non-ACK messages that have been sent to the recipient
-      // and have been acknowledged.
-      sendId: 0,
-
-      // Number of messages received and processed from the sender.
-      // This is the number that accompanies every ACK to tell the
-      // sender to clear its queue.
-      recvId: 0
-    };
-
-    if (frameId !== '..') {
-      // Container always appends a relay to the gadget, before
-      // the gadget appends its own relay back to container. The
-      // gadget, in the meantime, refuses to attach the container
-      // relay until it finds this one. Thus, the container knows
-      // for certain that gadget to container communication is set
-      // up by the time it finds its own relay. In addition to
-      // establishing a reliable handshake protocol, this also
-      // makes it possible for the gadget to send an initial batch
-      // of messages to the container ASAP.
-      appendRmrFrame(channelFrame, relayUri, getRmrData(frameId));
-    }
-     
-    // Start searching for our own frame on the other page.
-    conductRmrSearch(frameId);
-  }
-
-  /**
-   * Searches for a relay frame, created by the sender referenced by
-   * frameId, with which this context receives messages. Once
-   * found with proper permissions, attaches a resize handler which
-   * signals messages to be sent.
-   *
-   * @param {string} frameId Frame ID of the prospective sender.
-   */
-  function conductRmrSearch(frameId) {
-    var channelWindow = null;
-
-    // Increment the search counter.
-    rmr_channels[frameId].searchCounter++;
-
-    if (frameId === '..') {
-      // We are a gadget.
-      channelWindow = window.parent.frames['rmrtransport-' + window.name];
-    } else {
-      // We are a container.
-      channelWindow = window.frames[frameId].frames['rmrtransport-..'];
-    }
-
-    var status = false;
-
-    if (channelWindow) {
-      // We have a valid reference to "our" RMR transport frame.
-      // Register the proper event handlers.
-      status = registerRmrChannel(frameId, channelWindow);
-    }
-
-    if (!status) {
-      // Not found yet. Continue searching, but only if the counter
-      // has not reached the threshold.
-      if (rmr_channels[frameId].searchCounter > RMR_MAX_POLLS) {
-        // If we reach this point, then RMR has failed and we
-        // fall back to IFPC.
-        return;
-      }
-
-      setTimeout(function() {
-        conductRmrSearch(frameId);
-      }, RMR_SEARCH_TIMEOUT);
-    }
-  }
-
-  /**
-   * Attempts to conduct an RPC call to the specified
-   * target with the specified data via the RMR
-   * method. If this method fails, the system attempts again
-   * using the known default of IFPC.
-   *
-   * @param {String} targetId Module Id of the RPC service provider.
-   * @param {String} serviceName Name of the service to call.
-   * @param {String} from Module Id of the calling provider.
-   * @param {Object} rpc The RPC data for this call.
-   */
-  function callRmr(targetId, serviceName, from, rpc) {
-    var handler = null;
-
-    if (from !== '..') {
-      // Call from gadget to the container.
-      handler = rmr_channels['..'];
-    } else {
-      // Call from container to the gadget.
-      handler = rmr_channels[targetId];
-    }
-
-    if (handler) {
-      // Queue the current message if not ACK.
-      // ACK is always sent through getRmrData(...).
-      if (serviceName !== gadgets.rpc.ACK) {
-        handler.queue.push(rpc);
-      }
-
-      if (handler.waiting ||
-          (handler.queue.length === 0 &&
-           !(serviceName === gadgets.rpc.ACK && rpc && rpc.ackAlone === true))) {
-        // If we are awaiting a response from any previously-sent messages,
-        // or if we don't have anything new to send, just return.
-        // Note that we don't short-return if we're ACKing just-received
-        // messages.
-        return true;
-      }
-
-      if (handler.queue.length > 0) {
-        handler.waiting = true;
-      }
-
-      var url = handler.relayUri + "#" + getRmrData(targetId);
-
-      try {
-        // Update the URL with the message.
-        handler.frame.contentWindow.location = url;
-
-        // Resize the frame.
-        var newWidth = handler.width == 10 ? 20 : 10;
-        handler.frame.style.width = newWidth + 'px';
-        handler.width = newWidth;
-
-        // Done!
-      } catch (e) {
-        // Something about location-setting or resizing failed.
-        // This should never happen, but if it does, fall back to
-        // the default transport.
-        return false;
-      }
-    }
-
-    return true;
-  }
-
-  /**
-   * Returns as a string the data to be appended to an RMR relay frame,
-   * constructed from the current request queue plus an ACK message indicating
-   * the currently latest-processed message ID.
-   *
-   * @param {string} toFrameId Frame whose sendable queued data to retrieve.
-   */
-  function getRmrData(toFrameId) {
-    var channel = rmr_channels[toFrameId];
-    var rmrData = {id: channel.sendId};
-    if (channel) {
-      rmrData.d = Array.prototype.slice.call(channel.queue, 0);
-      rmrData.d.push({s:gadgets.rpc.ACK, id:channel.recvId});
-    }
-    return gadgets.json.stringify(rmrData);
-  }
-
-  /**
-   * Retrieve data from the channel keyed by the given frameId,
-   * processing it as a batch. All processed data is assumed to have been
-   * generated by getRmrData(...), pairing that method with this.
-   *
-   * @param {string} fromFrameId Frame from which data is being retrieved.
-   */
-  function processRmrData(fromFrameId) {
-    var channel = rmr_channels[fromFrameId];
-    var data = channel.receiveWindow.location.hash.substring(1);
-
-    // Decode the RPC object array.
-    var rpcObj = gadgets.json.parse(decodeURIComponent(data)) || {};
-    var rpcArray = rpcObj.d || [];
-
-    var nonAckReceived = false;
-    var noLongerWaiting = false;
-
-    var numBypassed = 0;
-    var numToBypass = (channel.recvId - rpcObj.id);
-    for (var i = 0; i < rpcArray.length; ++i) {
-      var rpc = rpcArray[i];
-
-      // If we receive an ACK message, then mark the current
-      // handler as no longer waiting and send out the next
-      // queued message.
-      if (rpc.s === gadgets.rpc.ACK) {
-        // ACK received - whether this came from a handshake or
-        // an active call, in either case it indicates readiness to
-        // send messages to the from frame.
-        ready(fromFrameId, true);
-
-        if (channel.waiting) {
-          noLongerWaiting = true;
-        }
-
-        channel.waiting = false;
-        var newlyAcked = Math.max(0, rpc.id - channel.sendId);
-        channel.queue.splice(0, newlyAcked);
-        channel.sendId = Math.max(channel.sendId, rpc.id || 0);
-        continue;
-      }
-
-      // If we get here, we've received > 0 non-ACK messages to
-      // process. Indicate this bit for later.
-      nonAckReceived = true;
-
-      // Bypass any messages already received.
-      if (++numBypassed <= numToBypass) {
-        continue;
-      }
-
-      ++channel.recvId;
-      process(rpc);  // actually dispatch the message
-    }
-
-    // Send an ACK indicating that we got/processed the message(s).
-    // Do so if we've received a message to process or if we were waiting
-    // before but a received ACK has cleared our waiting bit, and we have
-    // more messages to send. Performing this operation causes additional
-    // messages to be sent.
-    if (nonAckReceived ||
-        (noLongerWaiting && channel.queue.length > 0)) {
-      var from = (fromFrameId === '..') ? window.name : '..';
-      callRmr(fromFrameId, gadgets.rpc.ACK, from, {ackAlone: nonAckReceived});
-    }
-  }
-
-  /**
-   * Registers the RMR channel handler for the given frameId and associated
-   * channel window.
-   *
-   * @param {string} frameId The ID of the frame for which this channel is being
-   *   registered.
-   * @param {Object} channelWindow The window of the receive frame for this
-   *   channel, if any.
-   *
-   * @return {boolean} True if the frame was setup successfully, false
-   *   otherwise.
-   */
-  function registerRmrChannel(frameId, channelWindow) {
-    var channel = rmr_channels[frameId];
-
-    // Verify that the channel is ready for receiving.
-    try {
-      var canAccess = false;
-
-      // Check to see if the document is in the window. For Chrome, this
-      // will return 'false' if the channelWindow is inaccessible by this
-      // piece of JavaScript code, meaning that the URL of the channelWindow's
-      // parent iframe has not yet changed from 'about:blank'. We do this
-      // check this way because any true *access* on the channelWindow object
-      // will raise a security exception, which, despite the try-catch, still
-      // gets reported to the debugger (it does not break execution, the try
-      // handles that problem, but it is still reported, which is bad form).
-      // This check always succeeds in Safari 3.1 regardless of the state of
-      // the window.
-      canAccess = 'document' in channelWindow;
-
-      if (!canAccess) {
-        return false;
-      }
-
-      // Check to see if the document is an object. For Safari 3.1, this will
-      // return undefined if the page is still inaccessible. Unfortunately, this
-      // *will* raise a security issue in the debugger.
-      // TODO Find a way around this problem.
-      canAccess = typeof channelWindow['document'] == 'object';
-
-      if (!canAccess) {
-        return false;
-      }
-
-      // Once we get here, we know we can access the document (and anything else)
-      // on the window object. Therefore, we check to see if the location is
-      // still about:blank (this takes care of the Safari 3.2 case).
-      var loc = channelWindow.location.href;
-
-      // Check if this is about:blank for Safari.
-      if (loc === 'about:blank') {
-        return false;
-      }
-    } catch (ex) {
-      // For some reason, the iframe still points to about:blank. We try
-      // again in a bit.
-      return false;
-    }
-
-    // Save a reference to the receive window.
-    channel.receiveWindow = channelWindow;
-
-    // Register the onresize handler.
-    channelWindow.onresize = function() {
-      processRmrData(frameId);
-    };
-
-    if (frameId === '..') {
-      // Gadget to container. Signal to the container that the gadget
-      // is ready to receive messages by attaching the g -> c relay.
-      // As a nice optimization, pass along any gadget to container
-      // queued messages that have backed up since then. ACK is enqueued in
-      // getRmrData to ensure that the container's waiting flag is set to false
-      // (this happens in the below code run on the container side).
-      appendRmrFrame(channel.frame, channel.relayUri, getRmrData(frameId));
-    }
-
-    // Attempt to process the messages that the other party may have set in
-    // the resize frame's hash. For gadget to container, this is the gadget
-    // finding the container-set IFRAME, which has an ACK attached to it.
-    // For gadget to container, the gadget may have enqueued messages already,
-    // and sent them in the above code. This too will have an ACK message.
-    // The net effect is that both sides of the connection will have their
-    // "waiting" bit set to false, so they're ready to send messages.
-    processRmrData(frameId);
-
-    return true;
-  }
-
-  return {
-    getCode: function() {
-      return 'rmr';
-    },
-
-    isParentVerifiable: function() {
-      return true;
-    },
-
-    init: function(processFn, readyFn) {
-      // No global setup.
-      process = processFn;
-      ready = readyFn;
-      return true;
-    },
-
-    setup: function(receiverId, token) {
-      try {
-        setupRmr(receiverId);
-      } catch (e) {
-        gadgets.warn('Caught exception setting up RMR: ' + e);
-        return false;
-      }
-      return true;
-    },
-
-    call: function(targetId, from, rpc) {
-      return callRmr(targetId, rpc.s, from, rpc);
-    }
-  };
-}();
-
-/*
- * For all others, we have a fallback mechanism known as "ifpc". IFPC
- * exploits the fact that while same-origin policy prohibits a frame from
- * accessing members on a window not in the same domain, that frame can,
- * however, navigate the window heirarchy (via parent). This is exploited by
- * having a page on domain A that wants to talk to domain B create an iframe
- * on domain B pointing to a special relay file and with a message encoded
- * after the hash (#). This relay, in turn, finds the page on domain B, and
- * can call a receipt function with the message given to it. The relay URL
- * used by each caller is set via the gadgets.rpc.setRelayUrl(..) and
- * *must* be called before the call method is used.
- *
- *   ifpc: Iframe-based method, utilizing a relay page, to send a message.
- *      - No known major browsers still use this method, but it remains
- *        useful as a catch-all fallback for the time being.
- */
-gadgets.transports.Ifpc = function() {
-  var iframePool = [];
-  var callId = 0;
-  var ready;
-
-  /**
-   * Encodes arguments for the legacy IFPC wire format.
-   *
-   * @param {Object} args
-   * @return {String} the encoded args
-   */
-  function encodeLegacyData(args) {
-    var argsEscaped = [];
-    for(var i = 0, j = args.length; i < j; ++i) {
-      argsEscaped.push(encodeURIComponent(gadgets.json.stringify(args[i])));
-    }
-    return argsEscaped.join('&');
-  }
-
-  /**
-   * Helper function to emit an invisible IFrame.
-   * @param {String} src SRC attribute of the IFrame to emit.
-   * @private
-   */
-  function emitInvisibleIframe(src) {
-    var iframe;
-    // Recycle IFrames
-    for (var i = iframePool.length - 1; i >=0; --i) {
-      var ifr = iframePool[i];
-      try {
-        if (ifr && (ifr.recyclable || ifr.readyState === 'complete')) {
-          ifr.parentNode.removeChild(ifr);
-          if (window.ActiveXObject) {
-            // For MSIE, delete any iframes that are no longer being used. MSIE
-            // cannot reuse the IFRAME because a navigational click sound will
-            // be triggered when we set the SRC attribute.
-            // Other browsers scan the pool for a free iframe to reuse.
-            iframePool[i] = ifr = null;
-            iframePool.splice(i, 1);
-          } else {
-            ifr.recyclable = false;
-            iframe = ifr;
-            break;
-          }
-        }
-      } catch (e) {
-        // Ignore; IE7 throws an exception when trying to read readyState and
-        // readyState isn't set.
-      }
-    }
-    // Create IFrame if necessary
-    if (!iframe) {
-      iframe = document.createElement('iframe');
-      iframe.style.border = iframe.style.width = iframe.style.height = '0px';
-      iframe.style.visibility = 'hidden';
-      iframe.style.position = 'absolute';
-      iframe.onload = function() { this.recyclable = true; };
-      iframePool.push(iframe);
-    }
-    iframe.src = src;
-    setTimeout(function() { document.body.appendChild(iframe); }, 0);
-  }
-
-  return {
-    getCode: function() {
-      return 'ifpc';
-    },
-
-    isParentVerifiable: function() {
-      return true;
-    },
-
-    init: function(processFn, readyFn) {
-      // No global setup.
-      ready = readyFn;
-      ready('..', true);  // Ready immediately.
-      return true;
-    },
-
-    setup: function(receiverId, token) {
-      // Indicate readiness to send to receiver.
-      ready(receiverId, true);
-      return true;
-    },
-
-    call: function(targetId, from, rpc) {
-      // Retrieve the relay file used by IFPC. Note that
-      // this must be set before the call, and so we conduct
-      // an extra check to ensure it is not blank.
-      var relay = gadgets.rpc.getRelayUrl(targetId);
-      ++callId;
-
-      if (!relay) {
-        gadgets.warn('No relay file assigned for IFPC');
-        return;
-      }
-
-      // The RPC mechanism supports two formats for IFPC (legacy and current).
-      var src = null;
-      if (rpc.l) {
-        // Use legacy protocol.
-        // Format: #iframe_id&callId&num_packets&packet_num&block_of_data
-        var callArgs = rpc.a;
-        src = [relay, '#', encodeLegacyData([from, callId, 1, 0,
-               encodeLegacyData([from, serviceName, '', '', from].concat(
-                 callArgs))])].join('');
-      } else {
-        // Format: #targetId & sourceId@callId & packetNum & packetId & packetData
-        src = [relay, '#', targetId, '&', from, '@', callId,
-               '&1&0&', encodeURIComponent(gadgets.json.stringify(rpc))].join('');
-      }
-
-      // Conduct the IFPC call by creating the Iframe with
-      // the relay URL and appended message.
-      emitInvisibleIframe(src);
-      return true;
-    }
-  };
-}();
 
 /**
  * @static
@@ -1162,7 +80,7 @@
   var receiverTx = {};
   var earlyRpcQueue = {};
   var isGadget = false;
-  var fallbackTransport = gadgets.transports.Ifpc;
+  var fallbackTransport = gadgets.rpctx.Ifpc;
 
   // Load the authentication token for speaking to the container
   // from the gadget's parameters, or default to '0' if not found.
@@ -1188,12 +106,12 @@
    * See each transport's commentary/documentation for details.
    */
   function getTransport() {
-    return typeof window.postMessage === 'function' ? gadgets.transports.Wpm :
-           typeof document.postMessage === 'function' ? gadgets.transports.Dpm :
-           window.ActiveXObject ? gadgets.transports.Nix :
-           navigator.userAgent.indexOf('WebKit') > 0 ? gadgets.transports.Rmr :
-           navigator.product === 'Gecko' ? gadgets.transports.FrameElement :
-           gadgets.transports.Ifpc;
+    return typeof window.postMessage === 'function' ? gadgets.rpctx.Wpm :
+           typeof document.postMessage === 'function' ? gadgets.rpctx.Dpm :
+           window.ActiveXObject ? gadgets.rpctx.Nix :
+           navigator.userAgent.indexOf('WebKit') > 0 ? gadgets.rpctx.Rmr :
+           navigator.product === 'Gecko' ? gadgets.rpctx.FrameElement :
+           gadgets.rpctx.Ifpc;
   }
 
   /**
@@ -1537,7 +455,7 @@
      */
     forceParentVerifiable: function() {
       if (!transport.isParentVerifiable()) {
-        transport = gadgets.transports.Ifpc;
+        transport = gadgets.rpctx.Ifpc;
       }
     },