Debian Codegraph @

closure-library/closure/goog/net/xpc/relay.js

raw
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 /**
16 * @fileoverview Standalone script to be included in the relay-document
17 * used by goog.net.xpc.IframeRelayTransport. This script will decode the
18 * fragment identifier, determine the target window object and deliver
19 * the data to it.
20 *
21 */
22
23 /** @suppress {extraProvide} */
24 goog.provide('goog.net.xpc.relay');
25
26 (function() {
27 // Decode the fragement identifier.
28 // location.href is expected to be structured as follows:
29 // <url>#<channel_name>[,<iframe_id>]|<data>
30
31 // Get the fragment identifier.
32 var raw = window.location.hash;
33 if (!raw) {
34 return;
35 }
36 if (raw.charAt(0) == '#') {
37 raw = raw.substring(1);
38 }
39 var pos = raw.indexOf('|');
40 var head = raw.substring(0, pos).split(',');
41 var channelName = head[0];
42 var iframeId = head.length == 2 ? head[1] : null;
43 var frame = raw.substring(pos + 1);
44
45 // Find the window object of the peer.
46 //
47 // The general structure of the frames looks like this:
48 // - peer1
49 // - relay2
50 // - peer2
51 // - relay1
52 //
53 // We are either relay1 or relay2.
54
55 var win;
56 if (iframeId) {
57 // We are relay2 and need to deliver the data to peer2.
58 win = window.parent.frames[iframeId];
59 } else {
60 // We are relay1 and need to deliver the data to peer1.
61 win = window.parent.parent;
62 }
63
64 // Deliver the data.
65 try {
66 win['xpcRelay'](channelName, frame);
67 } catch (e) {
68 // Nothing useful can be done here.
69 // It would be great to inform the sender the delivery of this message
70 // failed, but this is not possible because we are already in the receiver's
71 // domain at this point.
72 }
73 })();