relay.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Standalone script to be included in the relay-document
  16. * used by goog.net.xpc.IframeRelayTransport. This script will decode the
  17. * fragment identifier, determine the target window object and deliver
  18. * the data to it.
  19. *
  20. */
  21. /** @suppress {extraProvide} */
  22. goog.provide('goog.net.xpc.relay');
  23. (function() {
  24. // Decode the fragement identifier.
  25. // location.href is expected to be structured as follows:
  26. // <url>#<channel_name>[,<iframe_id>]|<data>
  27. // Get the fragment identifier.
  28. var raw = window.location.hash;
  29. if (!raw) {
  30. return;
  31. }
  32. if (raw.charAt(0) == '#') {
  33. raw = raw.substring(1);
  34. }
  35. var pos = raw.indexOf('|');
  36. var head = raw.substring(0, pos).split(',');
  37. var channelName = head[0];
  38. var iframeId = head.length == 2 ? head[1] : null;
  39. var frame = raw.substring(pos + 1);
  40. // Find the window object of the peer.
  41. //
  42. // The general structure of the frames looks like this:
  43. // - peer1
  44. // - relay2
  45. // - peer2
  46. // - relay1
  47. //
  48. // We are either relay1 or relay2.
  49. var win;
  50. if (iframeId) {
  51. // We are relay2 and need to deliver the data to peer2.
  52. win = window.parent.frames[iframeId];
  53. } else {
  54. // We are relay1 and need to deliver the data to peer1.
  55. win = window.parent.parent;
  56. }
  57. // Deliver the data.
  58. try {
  59. win['xpcRelay'](channelName, frame);
  60. } catch (e) {
  61. // Nothing useful can be done here.
  62. // It would be great to inform the sender the delivery of this message
  63. // failed, but this is not possible because we are already in the receiver's
  64. // domain at this point.
  65. }
  66. })();