| 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 Contains the iframe polling transport. |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
goog.provide('goog.net.xpc.IframePollingTransport'); |
| 21 |
goog.provide('goog.net.xpc.IframePollingTransport.Receiver'); |
| 22 |
goog.provide('goog.net.xpc.IframePollingTransport.Sender'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.dom'); |
| 26 |
goog.require('goog.dom.TagName'); |
| 27 |
goog.require('goog.dom.safe'); |
| 28 |
goog.require('goog.log'); |
| 29 |
goog.require('goog.log.Level'); |
| 30 |
goog.require('goog.net.xpc'); |
| 31 |
goog.require('goog.net.xpc.CfgFields'); |
| 32 |
goog.require('goog.net.xpc.CrossPageChannelRole'); |
| 33 |
goog.require('goog.net.xpc.Transport'); |
| 34 |
goog.require('goog.net.xpc.TransportTypes'); |
| 35 |
goog.require('goog.userAgent'); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Iframe polling transport. Uses hidden iframes to transfer data |
| 41 |
* in the fragment identifier of the URL. The peer polls the iframe's location |
| 42 |
* for changes. |
| 43 |
* Unfortunately, in Safari this screws up the history, because Safari doesn't |
| 44 |
* allow to call location.replace() on a window containing a document from a |
| 45 |
* different domain (last version tested: 2.0.4). |
| 46 |
* |
| 47 |
* @param {goog.net.xpc.CrossPageChannel} channel The channel this |
| 48 |
* transport belongs to. |
| 49 |
* @param {goog.dom.DomHelper=} opt_domHelper The dom helper to use for finding |
| 50 |
* the correct window. |
| 51 |
* @constructor |
| 52 |
* @extends {goog.net.xpc.Transport} |
| 53 |
* @final |
| 54 |
*/ |
| 55 |
goog.net.xpc.IframePollingTransport = function(channel, opt_domHelper) { |
| 56 |
goog.net.xpc.IframePollingTransport.base(this, 'constructor', opt_domHelper); |
| 57 |
|
| 58 |
/** |
| 59 |
* The channel this transport belongs to. |
| 60 |
* @type {goog.net.xpc.CrossPageChannel} |
| 61 |
* @private |
| 62 |
*/ |
| 63 |
this.channel_ = channel; |
| 64 |
|
| 65 |
/** |
| 66 |
* The URI used to send messages. |
| 67 |
* @type {string} |
| 68 |
* @private |
| 69 |
*/ |
| 70 |
this.sendUri_ = |
| 71 |
this.channel_.getConfig()[goog.net.xpc.CfgFields.PEER_POLL_URI]; |
| 72 |
|
| 73 |
/** |
| 74 |
* The URI which is polled for incoming messages. |
| 75 |
* @type {string} |
| 76 |
* @private |
| 77 |
*/ |
| 78 |
this.rcvUri_ = |
| 79 |
this.channel_.getConfig()[goog.net.xpc.CfgFields.LOCAL_POLL_URI]; |
| 80 |
|
| 81 |
/** |
| 82 |
* The queue to hold messages which can't be sent immediately. |
| 83 |
* @type {Array<string>} |
| 84 |
* @private |
| 85 |
*/ |
| 86 |
this.sendQueue_ = []; |
| 87 |
}; |
| 88 |
goog.inherits(goog.net.xpc.IframePollingTransport, goog.net.xpc.Transport); |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* The number of times the inner frame will check for evidence of the outer |
| 93 |
* frame before it tries its reconnection sequence. These occur at 100ms |
| 94 |
* intervals, making this an effective max waiting period of 500ms. |
| 95 |
* @type {number} |
| 96 |
* @private |
| 97 |
*/ |
| 98 |
goog.net.xpc.IframePollingTransport.prototype.pollsBeforeReconnect_ = 5; |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* The transport type. |
| 103 |
* @type {number} |
| 104 |
* @protected |
| 105 |
* @override |
| 106 |
*/ |
| 107 |
goog.net.xpc.IframePollingTransport.prototype.transportType = |
| 108 |
goog.net.xpc.TransportTypes.IFRAME_POLLING; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Sequence counter. |
| 113 |
* @type {number} |
| 114 |
* @private |
| 115 |
*/ |
| 116 |
goog.net.xpc.IframePollingTransport.prototype.sequence_ = 0; |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Flag indicating whether we are waiting for an acknoledgement. |
| 121 |
* @type {boolean} |
| 122 |
* @private |
| 123 |
*/ |
| 124 |
goog.net.xpc.IframePollingTransport.prototype.waitForAck_ = false; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Flag indicating if channel has been initialized. |
| 129 |
* @type {boolean} |
| 130 |
* @private |
| 131 |
*/ |
| 132 |
goog.net.xpc.IframePollingTransport.prototype.initialized_ = false; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Reconnection iframe created by inner peer. |
| 137 |
* @type {Element} |
| 138 |
* @private |
| 139 |
*/ |
| 140 |
goog.net.xpc.IframePollingTransport.prototype.reconnectFrame_ = null; |
| 141 |
|
| 142 |
|
| 143 |
/** @private {goog.net.xpc.IframePollingTransport.Receiver} */ |
| 144 |
goog.net.xpc.IframePollingTransport.prototype.ackReceiver_; |
| 145 |
|
| 146 |
|
| 147 |
/** @private {goog.net.xpc.IframePollingTransport.Sender} */ |
| 148 |
goog.net.xpc.IframePollingTransport.prototype.ackSender_; |
| 149 |
|
| 150 |
|
| 151 |
/** @private */ |
| 152 |
goog.net.xpc.IframePollingTransport.prototype.ackIframeElm_; |
| 153 |
|
| 154 |
|
| 155 |
/** @private */ |
| 156 |
goog.net.xpc.IframePollingTransport.prototype.ackWinObj_; |
| 157 |
|
| 158 |
|
| 159 |
/** @private {!Function|undefined} */ |
| 160 |
goog.net.xpc.IframePollingTransport.prototype.checkLocalFramesPresentCb_; |
| 161 |
|
| 162 |
|
| 163 |
/** @private */ |
| 164 |
goog.net.xpc.IframePollingTransport.prototype.deliveryQueue_; |
| 165 |
|
| 166 |
|
| 167 |
/** @private */ |
| 168 |
goog.net.xpc.IframePollingTransport.prototype.msgIframeElm_; |
| 169 |
|
| 170 |
|
| 171 |
/** @private */ |
| 172 |
goog.net.xpc.IframePollingTransport.prototype.msgReceiver_; |
| 173 |
|
| 174 |
|
| 175 |
/** @private */ |
| 176 |
goog.net.xpc.IframePollingTransport.prototype.msgSender_; |
| 177 |
|
| 178 |
|
| 179 |
/** @private */ |
| 180 |
goog.net.xpc.IframePollingTransport.prototype.msgWinObj_; |
| 181 |
|
| 182 |
|
| 183 |
/** @private */ |
| 184 |
goog.net.xpc.IframePollingTransport.prototype.rcvdConnectionSetupAck_; |
| 185 |
|
| 186 |
|
| 187 |
/** @private */ |
| 188 |
goog.net.xpc.IframePollingTransport.prototype.sentConnectionSetupAck_; |
| 189 |
|
| 190 |
|
| 191 |
/** @private */ |
| 192 |
goog.net.xpc.IframePollingTransport.prototype.parts_; |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* The string used to prefix all iframe names and IDs. |
| 197 |
* @type {string} |
| 198 |
*/ |
| 199 |
goog.net.xpc.IframePollingTransport.IFRAME_PREFIX = 'googlexpc'; |
| 200 |
|
| 201 |
|
| 202 |
/** |
| 203 |
* Returns the name/ID of the message frame. |
| 204 |
* @return {string} Name of message frame. |
| 205 |
* @private |
| 206 |
*/ |
| 207 |
goog.net.xpc.IframePollingTransport.prototype.getMsgFrameName_ = function() { |
| 208 |
return goog.net.xpc.IframePollingTransport.IFRAME_PREFIX + '_' + |
| 209 |
this.channel_.name + '_msg'; |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Returns the name/ID of the ack frame. |
| 215 |
* @return {string} Name of ack frame. |
| 216 |
* @private |
| 217 |
*/ |
| 218 |
goog.net.xpc.IframePollingTransport.prototype.getAckFrameName_ = function() { |
| 219 |
return goog.net.xpc.IframePollingTransport.IFRAME_PREFIX + '_' + |
| 220 |
this.channel_.name + '_ack'; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* Determines whether the channel is still available. The channel is |
| 226 |
* unavailable if the transport was disposed or the peer is no longer |
| 227 |
* available. |
| 228 |
* @return {boolean} Whether the channel is available. |
| 229 |
*/ |
| 230 |
goog.net.xpc.IframePollingTransport.prototype.isChannelAvailable = function() { |
| 231 |
return !this.isDisposed() && this.channel_.isPeerAvailable(); |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Safely retrieves the frames from the peer window. If an error is thrown |
| 237 |
* (e.g. the window is closing) an empty frame object is returned. |
| 238 |
* @return {!Object<string|number, !Window>} The frames from the peer window. |
| 239 |
* @private |
| 240 |
*/ |
| 241 |
goog.net.xpc.IframePollingTransport.prototype.getPeerFrames_ = function() { |
| 242 |
try { |
| 243 |
if (this.isChannelAvailable()) { |
| 244 |
return this.channel_.getPeerWindowObject().frames || {}; |
| 245 |
} |
| 246 |
} catch (e) { |
| 247 |
// An error may be thrown if the window is closing. |
| 248 |
goog.log.fine(goog.net.xpc.logger, 'error retrieving peer frames'); |
| 249 |
} |
| 250 |
return {}; |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Safely retrieves the peer frame with the specified name. |
| 256 |
* @param {string} frameName The name of the peer frame to retrieve. |
| 257 |
* @return {!Window} The peer frame with the specified name. |
| 258 |
* @private |
| 259 |
*/ |
| 260 |
goog.net.xpc.IframePollingTransport.prototype.getPeerFrame_ = function( |
| 261 |
frameName) { |
| 262 |
return this.getPeerFrames_()[frameName]; |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* Connects this transport. |
| 268 |
* @override |
| 269 |
*/ |
| 270 |
goog.net.xpc.IframePollingTransport.prototype.connect = function() { |
| 271 |
if (!this.isChannelAvailable()) { |
| 272 |
// When the channel is unavailable there is no peer to poll so stop trying |
| 273 |
// to connect. |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
goog.log.fine(goog.net.xpc.logger, 'transport connect called'); |
| 278 |
if (!this.initialized_) { |
| 279 |
goog.log.fine(goog.net.xpc.logger, 'initializing...'); |
| 280 |
this.constructSenderFrames_(); |
| 281 |
this.initialized_ = true; |
| 282 |
} |
| 283 |
this.checkForeignFramesReady_(); |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
/** |
| 288 |
* Creates the iframes which are used to send messages (and acknowledgements) |
| 289 |
* to the peer. Sender iframes contain a document from a different origin and |
| 290 |
* therefore their content can't be accessed. |
| 291 |
* @private |
| 292 |
*/ |
| 293 |
goog.net.xpc.IframePollingTransport.prototype.constructSenderFrames_ = |
| 294 |
function() { |
| 295 |
var name = this.getMsgFrameName_(); |
| 296 |
this.msgIframeElm_ = this.constructSenderFrame_(name); |
| 297 |
this.msgWinObj_ = this.getWindow().frames[name]; |
| 298 |
|
| 299 |
name = this.getAckFrameName_(); |
| 300 |
this.ackIframeElm_ = this.constructSenderFrame_(name); |
| 301 |
this.ackWinObj_ = this.getWindow().frames[name]; |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Constructs a sending frame the the given id. |
| 307 |
* @param {string} id The id. |
| 308 |
* @return {!Element} The constructed frame. |
| 309 |
* @private |
| 310 |
*/ |
| 311 |
goog.net.xpc.IframePollingTransport.prototype.constructSenderFrame_ = function( |
| 312 |
id) { |
| 313 |
goog.log.log( |
| 314 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 315 |
'constructing sender frame: ' + id); |
| 316 |
var ifr = goog.dom.createElement(goog.dom.TagName.IFRAME); |
| 317 |
var s = ifr.style; |
| 318 |
s.position = 'absolute'; |
| 319 |
s.top = '-10px'; |
| 320 |
s.left = '10px'; |
| 321 |
s.width = '1px'; |
| 322 |
s.height = '1px'; |
| 323 |
ifr.id = ifr.name = id; |
| 324 |
ifr.src = this.sendUri_ + '#INITIAL'; |
| 325 |
this.getWindow().document.body.appendChild(ifr); |
| 326 |
return ifr; |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
/** |
| 331 |
* The protocol for reconnecting is for the inner frame to change channel |
| 332 |
* names, and then communicate the new channel name to the outer peer. |
| 333 |
* The outer peer looks in a predefined location for the channel name |
| 334 |
* upate. It is important to use a completely new channel name, as this |
| 335 |
* will ensure that all messaging iframes are not in the bfcache. |
| 336 |
* Otherwise, Safari may pollute the history when modifying the location |
| 337 |
* of bfcached iframes. |
| 338 |
* @private |
| 339 |
*/ |
| 340 |
goog.net.xpc.IframePollingTransport.prototype.maybeInnerPeerReconnect_ = |
| 341 |
function() { |
| 342 |
// Reconnection has been found to not function on some browsers (eg IE7), so |
| 343 |
// it's important that the mechanism only be triggered as a last resort. As |
| 344 |
// such, we poll a number of times to find the outer iframe before triggering |
| 345 |
// it. |
| 346 |
if (this.reconnectFrame_ || this.pollsBeforeReconnect_-- > 0) { |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
goog.log.log( |
| 351 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 352 |
'Inner peer reconnect triggered.'); |
| 353 |
this.channel_.updateChannelNameAndCatalog(goog.net.xpc.getRandomString(10)); |
| 354 |
goog.log.log( |
| 355 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 356 |
'switching channels: ' + this.channel_.name); |
| 357 |
this.deconstructSenderFrames_(); |
| 358 |
this.initialized_ = false; |
| 359 |
// Communicate new channel name to outer peer. |
| 360 |
this.reconnectFrame_ = this.constructSenderFrame_( |
| 361 |
goog.net.xpc.IframePollingTransport.IFRAME_PREFIX + '_reconnect_' + |
| 362 |
this.channel_.name); |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* Scans inner peer for a reconnect message, which will be used to update |
| 368 |
* the outer peer's channel name. If a reconnect message is found, the |
| 369 |
* sender frames will be cleaned up to make way for the new sender frames. |
| 370 |
* Only called by the outer peer. |
| 371 |
* @private |
| 372 |
*/ |
| 373 |
goog.net.xpc.IframePollingTransport.prototype.outerPeerReconnect_ = function() { |
| 374 |
goog.log.log( |
| 375 |
goog.net.xpc.logger, goog.log.Level.FINEST, 'outerPeerReconnect called'); |
| 376 |
var frames = this.getPeerFrames_(); |
| 377 |
var length = frames.length; |
| 378 |
for (var i = 0; i < length; i++) { |
| 379 |
var frameName; |
| 380 |
try { |
| 381 |
if (frames[i] && frames[i].name) { |
| 382 |
frameName = frames[i].name; |
| 383 |
} |
| 384 |
} catch (e) { |
| 385 |
// Do nothing. |
| 386 |
} |
| 387 |
if (!frameName) { |
| 388 |
continue; |
| 389 |
} |
| 390 |
var message = frameName.split('_'); |
| 391 |
if (message.length == 3 && |
| 392 |
message[0] == goog.net.xpc.IframePollingTransport.IFRAME_PREFIX && |
| 393 |
message[1] == 'reconnect') { |
| 394 |
// This is a legitimate reconnect message from the peer. Start using |
| 395 |
// the peer provided channel name, and start a connection over from |
| 396 |
// scratch. |
| 397 |
this.channel_.name = message[2]; |
| 398 |
this.deconstructSenderFrames_(); |
| 399 |
this.initialized_ = false; |
| 400 |
break; |
| 401 |
} |
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Cleans up the existing sender frames owned by this peer. Only called by |
| 408 |
* the outer peer. |
| 409 |
* @private |
| 410 |
*/ |
| 411 |
goog.net.xpc.IframePollingTransport.prototype.deconstructSenderFrames_ = |
| 412 |
function() { |
| 413 |
goog.log.log( |
| 414 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 415 |
'deconstructSenderFrames called'); |
| 416 |
if (this.msgIframeElm_) { |
| 417 |
this.msgIframeElm_.parentNode.removeChild(this.msgIframeElm_); |
| 418 |
this.msgIframeElm_ = null; |
| 419 |
this.msgWinObj_ = null; |
| 420 |
} |
| 421 |
if (this.ackIframeElm_) { |
| 422 |
this.ackIframeElm_.parentNode.removeChild(this.ackIframeElm_); |
| 423 |
this.ackIframeElm_ = null; |
| 424 |
this.ackWinObj_ = null; |
| 425 |
} |
| 426 |
}; |
| 427 |
|
| 428 |
|
| 429 |
/** |
| 430 |
* Checks if the frames in the peer's page are ready. These contain a |
| 431 |
* document from the own domain and are the ones messages are received through. |
| 432 |
* @private |
| 433 |
*/ |
| 434 |
goog.net.xpc.IframePollingTransport.prototype.checkForeignFramesReady_ = |
| 435 |
function() { |
| 436 |
// check if the connected iframe ready |
| 437 |
if (!(this.isRcvFrameReady_(this.getMsgFrameName_()) && |
| 438 |
this.isRcvFrameReady_(this.getAckFrameName_()))) { |
| 439 |
goog.log.log( |
| 440 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 441 |
'foreign frames not (yet) present'); |
| 442 |
|
| 443 |
if (this.channel_.getRole() == goog.net.xpc.CrossPageChannelRole.INNER) { |
| 444 |
// The outer peer might need a short time to get its frames ready, as |
| 445 |
// CrossPageChannel prevents them from getting created until the inner |
| 446 |
// peer's frame has thrown its loaded event. This method is a noop for |
| 447 |
// the first few times it's called, and then allows the reconnection |
| 448 |
// sequence to begin. |
| 449 |
this.maybeInnerPeerReconnect_(); |
| 450 |
} else if ( |
| 451 |
this.channel_.getRole() == goog.net.xpc.CrossPageChannelRole.OUTER) { |
| 452 |
// The inner peer is either not loaded yet, or the receiving |
| 453 |
// frames are simply missing. Since we cannot discern the two cases, we |
| 454 |
// should scan for a reconnect message from the inner peer. |
| 455 |
this.outerPeerReconnect_(); |
| 456 |
} |
| 457 |
|
| 458 |
// start a timer to check again |
| 459 |
this.getWindow().setTimeout(goog.bind(this.connect, this), 100); |
| 460 |
} else { |
| 461 |
goog.log.fine(goog.net.xpc.logger, 'foreign frames present'); |
| 462 |
|
| 463 |
// Create receivers. |
| 464 |
this.msgReceiver_ = new goog.net.xpc.IframePollingTransport.Receiver( |
| 465 |
this, this.getPeerFrame_(this.getMsgFrameName_()), |
| 466 |
goog.bind(this.processIncomingMsg, this)); |
| 467 |
this.ackReceiver_ = new goog.net.xpc.IframePollingTransport.Receiver( |
| 468 |
this, this.getPeerFrame_(this.getAckFrameName_()), |
| 469 |
goog.bind(this.processIncomingAck, this)); |
| 470 |
|
| 471 |
this.checkLocalFramesPresent_(); |
| 472 |
} |
| 473 |
}; |
| 474 |
|
| 475 |
|
| 476 |
/** |
| 477 |
* Checks if the receiving frame is ready. |
| 478 |
* @param {string} frameName Which receiving frame to check. |
| 479 |
* @return {boolean} Whether the receiving frame is ready. |
| 480 |
* @private |
| 481 |
*/ |
| 482 |
goog.net.xpc.IframePollingTransport.prototype.isRcvFrameReady_ = function( |
| 483 |
frameName) { |
| 484 |
goog.log.log( |
| 485 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 486 |
'checking for receive frame: ' + frameName); |
| 487 |
|
| 488 |
try { |
| 489 |
var winObj = this.getPeerFrame_(frameName); |
| 490 |
if (!winObj || winObj.location.href.indexOf(this.rcvUri_) != 0) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
} catch (e) { |
| 494 |
return false; |
| 495 |
} |
| 496 |
return true; |
| 497 |
}; |
| 498 |
|
| 499 |
|
| 500 |
/** |
| 501 |
* Checks if the iframes created in the own document are ready. |
| 502 |
* @private |
| 503 |
*/ |
| 504 |
goog.net.xpc.IframePollingTransport.prototype.checkLocalFramesPresent_ = |
| 505 |
function() { |
| 506 |
|
| 507 |
// Are the sender frames ready? |
| 508 |
// These contain a document from the peer's domain, therefore we can only |
| 509 |
// check if the frame itself is present. |
| 510 |
var frames = this.getPeerFrames_(); |
| 511 |
if (!(frames[this.getAckFrameName_()] && frames[this.getMsgFrameName_()])) { |
| 512 |
// start a timer to check again |
| 513 |
if (!this.checkLocalFramesPresentCb_) { |
| 514 |
this.checkLocalFramesPresentCb_ = |
| 515 |
goog.bind(this.checkLocalFramesPresent_, this); |
| 516 |
} |
| 517 |
this.getWindow().setTimeout(this.checkLocalFramesPresentCb_, 100); |
| 518 |
goog.log.fine(goog.net.xpc.logger, 'local frames not (yet) present'); |
| 519 |
} else { |
| 520 |
// Create senders. |
| 521 |
this.msgSender_ = new goog.net.xpc.IframePollingTransport.Sender( |
| 522 |
this.sendUri_, this.msgWinObj_); |
| 523 |
this.ackSender_ = new goog.net.xpc.IframePollingTransport.Sender( |
| 524 |
this.sendUri_, this.ackWinObj_); |
| 525 |
|
| 526 |
goog.log.fine(goog.net.xpc.logger, 'local frames ready'); |
| 527 |
|
| 528 |
this.getWindow().setTimeout(goog.bind(function() { |
| 529 |
this.msgSender_.send(goog.net.xpc.SETUP); |
| 530 |
this.waitForAck_ = true; |
| 531 |
goog.log.fine(goog.net.xpc.logger, 'SETUP sent'); |
| 532 |
}, this), 100); |
| 533 |
} |
| 534 |
}; |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* Check if connection is ready. |
| 539 |
* @private |
| 540 |
*/ |
| 541 |
goog.net.xpc.IframePollingTransport.prototype.checkIfConnected_ = function() { |
| 542 |
if (this.sentConnectionSetupAck_ && this.rcvdConnectionSetupAck_) { |
| 543 |
this.channel_.notifyConnected(); |
| 544 |
|
| 545 |
if (this.deliveryQueue_) { |
| 546 |
goog.log.fine( |
| 547 |
goog.net.xpc.logger, 'delivering queued messages ' + |
| 548 |
'(' + this.deliveryQueue_.length + ')'); |
| 549 |
|
| 550 |
for (var i = 0, m; i < this.deliveryQueue_.length; i++) { |
| 551 |
m = this.deliveryQueue_[i]; |
| 552 |
this.channel_.xpcDeliver(m.service, m.payload); |
| 553 |
} |
| 554 |
delete this.deliveryQueue_; |
| 555 |
} |
| 556 |
} else { |
| 557 |
goog.log.log( |
| 558 |
goog.net.xpc.logger, goog.log.Level.FINEST, 'checking if connected: ' + |
| 559 |
'ack sent:' + this.sentConnectionSetupAck_ + ', ack rcvd: ' + |
| 560 |
this.rcvdConnectionSetupAck_); |
| 561 |
} |
| 562 |
}; |
| 563 |
|
| 564 |
|
| 565 |
/** |
| 566 |
* Processes an incoming message. |
| 567 |
* @param {string} raw The complete received string. |
| 568 |
*/ |
| 569 |
goog.net.xpc.IframePollingTransport.prototype.processIncomingMsg = function( |
| 570 |
raw) { |
| 571 |
goog.log.log( |
| 572 |
goog.net.xpc.logger, goog.log.Level.FINEST, 'msg received: ' + raw); |
| 573 |
|
| 574 |
if (raw == goog.net.xpc.SETUP) { |
| 575 |
if (!this.ackSender_) { |
| 576 |
// Got SETUP msg, but we can't send an ack. |
| 577 |
return; |
| 578 |
} |
| 579 |
|
| 580 |
this.ackSender_.send(goog.net.xpc.SETUP_ACK_); |
| 581 |
goog.log.log(goog.net.xpc.logger, goog.log.Level.FINEST, 'SETUP_ACK sent'); |
| 582 |
|
| 583 |
this.sentConnectionSetupAck_ = true; |
| 584 |
this.checkIfConnected_(); |
| 585 |
|
| 586 |
} else if (this.channel_.isConnected() || this.sentConnectionSetupAck_) { |
| 587 |
var pos = raw.indexOf('|'); |
| 588 |
var head = raw.substring(0, pos); |
| 589 |
var frame = raw.substring(pos + 1); |
| 590 |
|
| 591 |
// check if it is a framed message |
| 592 |
pos = head.indexOf(','); |
| 593 |
if (pos == -1) { |
| 594 |
var seq = head; |
| 595 |
// send acknowledgement |
| 596 |
this.ackSender_.send('ACK:' + seq); |
| 597 |
this.deliverPayload_(frame); |
| 598 |
} else { |
| 599 |
var seq = head.substring(0, pos); |
| 600 |
// send acknowledgement |
| 601 |
this.ackSender_.send('ACK:' + seq); |
| 602 |
|
| 603 |
var partInfo = head.substring(pos + 1).split('/'); |
| 604 |
var part0 = parseInt(partInfo[0], 10); |
| 605 |
var part1 = parseInt(partInfo[1], 10); |
| 606 |
// create an array to accumulate the parts if this is the |
| 607 |
// first frame of a message |
| 608 |
if (part0 == 1) { |
| 609 |
this.parts_ = []; |
| 610 |
} |
| 611 |
this.parts_.push(frame); |
| 612 |
// deliver the message if this was the last frame of a message |
| 613 |
if (part0 == part1) { |
| 614 |
this.deliverPayload_(this.parts_.join('')); |
| 615 |
delete this.parts_; |
| 616 |
} |
| 617 |
} |
| 618 |
} else { |
| 619 |
goog.log.warning( |
| 620 |
goog.net.xpc.logger, 'received msg, but channel is not connected'); |
| 621 |
} |
| 622 |
}; |
| 623 |
|
| 624 |
|
| 625 |
/** |
| 626 |
* Process an incoming acknowdedgement. |
| 627 |
* @param {string} msgStr The incoming ack string to process. |
| 628 |
*/ |
| 629 |
goog.net.xpc.IframePollingTransport.prototype.processIncomingAck = function( |
| 630 |
msgStr) { |
| 631 |
goog.log.log( |
| 632 |
goog.net.xpc.logger, goog.log.Level.FINEST, 'ack received: ' + msgStr); |
| 633 |
|
| 634 |
if (msgStr == goog.net.xpc.SETUP_ACK_) { |
| 635 |
this.waitForAck_ = false; |
| 636 |
this.rcvdConnectionSetupAck_ = true; |
| 637 |
// send the next frame |
| 638 |
this.checkIfConnected_(); |
| 639 |
|
| 640 |
} else if (this.channel_.isConnected()) { |
| 641 |
if (!this.waitForAck_) { |
| 642 |
goog.log.warning(goog.net.xpc.logger, 'got unexpected ack'); |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
var seq = parseInt(msgStr.split(':')[1], 10); |
| 647 |
if (seq == this.sequence_) { |
| 648 |
this.waitForAck_ = false; |
| 649 |
this.sendNextFrame_(); |
| 650 |
} else { |
| 651 |
goog.log.warning(goog.net.xpc.logger, 'got ack with wrong sequence'); |
| 652 |
} |
| 653 |
} else { |
| 654 |
goog.log.warning( |
| 655 |
goog.net.xpc.logger, 'received ack, but channel not connected'); |
| 656 |
} |
| 657 |
}; |
| 658 |
|
| 659 |
|
| 660 |
/** |
| 661 |
* Sends a frame (message part). |
| 662 |
* @private |
| 663 |
*/ |
| 664 |
goog.net.xpc.IframePollingTransport.prototype.sendNextFrame_ = function() { |
| 665 |
// do nothing if we are waiting for an acknowledgement or the |
| 666 |
// queue is emtpy |
| 667 |
if (this.waitForAck_ || !this.sendQueue_.length) { |
| 668 |
return; |
| 669 |
} |
| 670 |
|
| 671 |
var s = this.sendQueue_.shift(); |
| 672 |
++this.sequence_; |
| 673 |
this.msgSender_.send(this.sequence_ + s); |
| 674 |
goog.log.log( |
| 675 |
goog.net.xpc.logger, goog.log.Level.FINEST, |
| 676 |
'msg sent: ' + this.sequence_ + s); |
| 677 |
|
| 678 |
|
| 679 |
this.waitForAck_ = true; |
| 680 |
}; |
| 681 |
|
| 682 |
|
| 683 |
/** |
| 684 |
* Delivers a message. |
| 685 |
* @param {string} s The complete message string ("<service_name>:<payload>"). |
| 686 |
* @private |
| 687 |
*/ |
| 688 |
goog.net.xpc.IframePollingTransport.prototype.deliverPayload_ = function(s) { |
| 689 |
// determine the service name and the payload |
| 690 |
var pos = s.indexOf(':'); |
| 691 |
var service = s.substr(0, pos); |
| 692 |
var payload = s.substring(pos + 1); |
| 693 |
|
| 694 |
// deliver the message |
| 695 |
if (!this.channel_.isConnected()) { |
| 696 |
// as valid messages can come in before a SETUP_ACK has |
| 697 |
// been received (because subchannels for msgs and acks are independent), |
| 698 |
// delay delivery of early messages until after 'connect'-event |
| 699 |
(this.deliveryQueue_ || (this.deliveryQueue_ = [ |
| 700 |
])).push({service: service, payload: payload}); |
| 701 |
goog.log.log(goog.net.xpc.logger, goog.log.Level.FINEST, 'queued delivery'); |
| 702 |
} else { |
| 703 |
this.channel_.xpcDeliver(service, payload); |
| 704 |
} |
| 705 |
}; |
| 706 |
|
| 707 |
|
| 708 |
// ---- send message ---- |
| 709 |
|
| 710 |
|
| 711 |
/** |
| 712 |
* Maximal frame length. |
| 713 |
* @type {number} |
| 714 |
* @private |
| 715 |
*/ |
| 716 |
goog.net.xpc.IframePollingTransport.prototype.MAX_FRAME_LENGTH_ = 3800; |
| 717 |
|
| 718 |
|
| 719 |
/** |
| 720 |
* Sends a message. Splits it in multiple frames if too long (exceeds IE's |
| 721 |
* URL-length maximum. |
| 722 |
* Wireformat: `<seq>[,<frame_no>/<#frames>]|<frame_content>` |
| 723 |
* |
| 724 |
* @param {string} service Name of service this the message has to be delivered. |
| 725 |
* @param {string} payload The message content. |
| 726 |
* @override |
| 727 |
*/ |
| 728 |
goog.net.xpc.IframePollingTransport.prototype.send = function( |
| 729 |
service, payload) { |
| 730 |
var frame = service + ':' + payload; |
| 731 |
// put in queue |
| 732 |
if (!goog.userAgent.IE || payload.length <= this.MAX_FRAME_LENGTH_) { |
| 733 |
this.sendQueue_.push('|' + frame); |
| 734 |
} else { |
| 735 |
var l = payload.length; |
| 736 |
var num = Math.ceil(l / this.MAX_FRAME_LENGTH_); // number of frames |
| 737 |
var pos = 0; |
| 738 |
var i = 1; |
| 739 |
while (pos < l) { |
| 740 |
this.sendQueue_.push( |
| 741 |
',' + i + '/' + num + '|' + |
| 742 |
frame.substr(pos, this.MAX_FRAME_LENGTH_)); |
| 743 |
i++; |
| 744 |
pos += this.MAX_FRAME_LENGTH_; |
| 745 |
} |
| 746 |
} |
| 747 |
this.sendNextFrame_(); |
| 748 |
}; |
| 749 |
|
| 750 |
|
| 751 |
/** @override */ |
| 752 |
goog.net.xpc.IframePollingTransport.prototype.disposeInternal = function() { |
| 753 |
goog.net.xpc.IframePollingTransport.base(this, 'disposeInternal'); |
| 754 |
|
| 755 |
var receivers = goog.net.xpc.IframePollingTransport.receivers_; |
| 756 |
goog.array.remove(receivers, this.msgReceiver_); |
| 757 |
goog.array.remove(receivers, this.ackReceiver_); |
| 758 |
this.msgReceiver_ = this.ackReceiver_ = null; |
| 759 |
|
| 760 |
goog.dom.removeNode(this.msgIframeElm_); |
| 761 |
goog.dom.removeNode(this.ackIframeElm_); |
| 762 |
this.msgIframeElm_ = this.ackIframeElm_ = null; |
| 763 |
this.msgWinObj_ = this.ackWinObj_ = null; |
| 764 |
}; |
| 765 |
|
| 766 |
|
| 767 |
/** |
| 768 |
* Array holding all Receiver-instances. |
| 769 |
* @type {Array<goog.net.xpc.IframePollingTransport.Receiver>} |
| 770 |
* @private |
| 771 |
*/ |
| 772 |
goog.net.xpc.IframePollingTransport.receivers_ = []; |
| 773 |
|
| 774 |
|
| 775 |
/** |
| 776 |
* Short polling interval. |
| 777 |
* @type {number} |
| 778 |
* @private |
| 779 |
*/ |
| 780 |
goog.net.xpc.IframePollingTransport.TIME_POLL_SHORT_ = 10; |
| 781 |
|
| 782 |
|
| 783 |
/** |
| 784 |
* Long polling interval. |
| 785 |
* @type {number} |
| 786 |
* @private |
| 787 |
*/ |
| 788 |
goog.net.xpc.IframePollingTransport.TIME_POLL_LONG_ = 100; |
| 789 |
|
| 790 |
|
| 791 |
/** |
| 792 |
* Period how long to use TIME_POLL_SHORT_ before raising polling-interval |
| 793 |
* to TIME_POLL_LONG_ after an activity. |
| 794 |
* @type {number} |
| 795 |
* @private |
| 796 |
*/ |
| 797 |
goog.net.xpc.IframePollingTransport.TIME_SHORT_POLL_AFTER_ACTIVITY_ = 1000; |
| 798 |
|
| 799 |
|
| 800 |
/** |
| 801 |
* Polls all receivers. |
| 802 |
* @private |
| 803 |
*/ |
| 804 |
goog.net.xpc.IframePollingTransport.receive_ = function() { |
| 805 |
var receivers = goog.net.xpc.IframePollingTransport.receivers_; |
| 806 |
var receiver; |
| 807 |
var rcvd = false; |
| 808 |
|
| 809 |
|
| 810 |
try { |
| 811 |
for (var i = 0; receiver = receivers[i]; i++) { |
| 812 |
rcvd = rcvd || receiver.receive(); |
| 813 |
} |
| 814 |
} catch (e) { |
| 815 |
goog.log.info(goog.net.xpc.logger, 'receive_() failed: ' + e); |
| 816 |
|
| 817 |
// Notify the channel that the transport had an error. |
| 818 |
receiver.transport_.channel_.notifyTransportError(); |
| 819 |
|
| 820 |
// notifyTransportError() closes the channel and disposes the transport. |
| 821 |
// If there are no other channels present, this.receivers_ will now be empty |
| 822 |
// and there is no need to keep polling. |
| 823 |
if (!receivers.length) { |
| 824 |
return; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
var now = goog.now(); |
| 829 |
if (rcvd) { |
| 830 |
goog.net.xpc.IframePollingTransport.lastActivity_ = now; |
| 831 |
} |
| 832 |
|
| 833 |
// Schedule next check. |
| 834 |
var t = now - goog.net.xpc.IframePollingTransport.lastActivity_ < |
| 835 |
goog.net.xpc.IframePollingTransport.TIME_SHORT_POLL_AFTER_ACTIVITY_ ? |
| 836 |
goog.net.xpc.IframePollingTransport.TIME_POLL_SHORT_ : |
| 837 |
goog.net.xpc.IframePollingTransport.TIME_POLL_LONG_; |
| 838 |
goog.net.xpc.IframePollingTransport.rcvTimer_ = |
| 839 |
window.setTimeout(goog.net.xpc.IframePollingTransport.receiveCb_, t); |
| 840 |
}; |
| 841 |
|
| 842 |
|
| 843 |
/** |
| 844 |
* Callback that wraps receive_ to be used in timers. |
| 845 |
* @type {Function} |
| 846 |
* @private |
| 847 |
*/ |
| 848 |
goog.net.xpc.IframePollingTransport.receiveCb_ = goog.bind( |
| 849 |
goog.net.xpc.IframePollingTransport.receive_, |
| 850 |
goog.net.xpc.IframePollingTransport); |
| 851 |
|
| 852 |
|
| 853 |
/** |
| 854 |
* Starts the polling loop. |
| 855 |
* @private |
| 856 |
*/ |
| 857 |
goog.net.xpc.IframePollingTransport.startRcvTimer_ = function() { |
| 858 |
goog.log.fine(goog.net.xpc.logger, 'starting receive-timer'); |
| 859 |
goog.net.xpc.IframePollingTransport.lastActivity_ = goog.now(); |
| 860 |
if (goog.net.xpc.IframePollingTransport.rcvTimer_) { |
| 861 |
window.clearTimeout(goog.net.xpc.IframePollingTransport.rcvTimer_); |
| 862 |
} |
| 863 |
goog.net.xpc.IframePollingTransport.rcvTimer_ = window.setTimeout( |
| 864 |
goog.net.xpc.IframePollingTransport.receiveCb_, |
| 865 |
goog.net.xpc.IframePollingTransport.TIME_POLL_SHORT_); |
| 866 |
}; |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
/** |
| 871 |
* goog.net.xpc.IframePollingTransport.Sender |
| 872 |
* |
| 873 |
* Utility class to send message-parts to a document from a different origin. |
| 874 |
* |
| 875 |
* @constructor |
| 876 |
* @param {string} url The url the other document will use for polling. Must |
| 877 |
* be an http:// or https:// URL. |
| 878 |
* @param {Object} windowObj The frame used for sending information to. |
| 879 |
* @final |
| 880 |
*/ |
| 881 |
goog.net.xpc.IframePollingTransport.Sender = function(url, windowObj) { |
| 882 |
// This class is instantiated from goog.net.xpc.IframePollingTransport, which |
| 883 |
// takes its URLs from a goog.net.xpc.CrossPageChannel, which in turns |
| 884 |
// sanitizes them. However, since this class can be instantiated from |
| 885 |
// elsewhere than IframePollingTransport the url needs to be sanitized |
| 886 |
// here too. |
| 887 |
if (!/^https?:\/\//.test(url)) { |
| 888 |
throw new Error('URL ' + url + ' is invalid'); |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
* The URI used to sending messages. |
| 893 |
* @type {string} |
| 894 |
* @private |
| 895 |
*/ |
| 896 |
this.sanitizedSendUri_ = url; |
| 897 |
|
| 898 |
/** |
| 899 |
* The window object of the iframe used to send messages. |
| 900 |
* The script instantiating the Sender won't have access to |
| 901 |
* the content of sendFrame_. |
| 902 |
* @type {Window} |
| 903 |
* @private |
| 904 |
*/ |
| 905 |
this.sendFrame_ = /** @type {Window} */ (windowObj); |
| 906 |
|
| 907 |
/** |
| 908 |
* Cycle counter (used to make sure that sending two identical messages sent |
| 909 |
* in direct succession can be recognized as such by the receiver). |
| 910 |
* @type {number} |
| 911 |
* @private |
| 912 |
*/ |
| 913 |
this.cycle_ = 0; |
| 914 |
}; |
| 915 |
|
| 916 |
|
| 917 |
/** |
| 918 |
* Sends a message-part (frame) to the peer. |
| 919 |
* The message-part is encoded and put in the fragment identifier |
| 920 |
* of the URL used for sending (and belongs to the origin/domain of the peer). |
| 921 |
* @param {string} payload The message to send. |
| 922 |
*/ |
| 923 |
goog.net.xpc.IframePollingTransport.Sender.prototype.send = function(payload) { |
| 924 |
this.cycle_ = ++this.cycle_ % 2; |
| 925 |
|
| 926 |
var url = |
| 927 |
this.sanitizedSendUri_ + '#' + this.cycle_ + encodeURIComponent(payload); |
| 928 |
|
| 929 |
// TODO(user) Find out if try/catch is still needed |
| 930 |
|
| 931 |
try { |
| 932 |
// safari doesn't allow to call location.replace() |
| 933 |
if (goog.userAgent.WEBKIT) { |
| 934 |
goog.dom.safe.setLocationHref(this.sendFrame_.location, url); |
| 935 |
} else { |
| 936 |
this.sendFrame_.location.replace(url); |
| 937 |
} |
| 938 |
} catch (e) { |
| 939 |
goog.log.error(goog.net.xpc.logger, 'sending failed', e); |
| 940 |
} |
| 941 |
|
| 942 |
// Restart receiver timer on short polling interval, to support use-cases |
| 943 |
// where we need to capture responses quickly. |
| 944 |
goog.net.xpc.IframePollingTransport.startRcvTimer_(); |
| 945 |
}; |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
/** |
| 950 |
* goog.net.xpc.IframePollingTransport.Receiver |
| 951 |
* |
| 952 |
* @constructor |
| 953 |
* @param {goog.net.xpc.IframePollingTransport} transport The transport to |
| 954 |
* receive from. |
| 955 |
* @param {Object} windowObj The window-object to poll for location-changes. |
| 956 |
* @param {Function} callback The callback-function to be called when |
| 957 |
* location has changed. |
| 958 |
* @final |
| 959 |
*/ |
| 960 |
goog.net.xpc.IframePollingTransport.Receiver = function( |
| 961 |
transport, windowObj, callback) { |
| 962 |
/** |
| 963 |
* The transport to receive from. |
| 964 |
* @type {goog.net.xpc.IframePollingTransport} |
| 965 |
* @private |
| 966 |
*/ |
| 967 |
this.transport_ = transport; |
| 968 |
this.rcvFrame_ = windowObj; |
| 969 |
|
| 970 |
this.cb_ = callback; |
| 971 |
this.currentLoc_ = this.rcvFrame_.location.href.split('#')[0] + '#INITIAL'; |
| 972 |
|
| 973 |
goog.net.xpc.IframePollingTransport.receivers_.push(this); |
| 974 |
goog.net.xpc.IframePollingTransport.startRcvTimer_(); |
| 975 |
}; |
| 976 |
|
| 977 |
|
| 978 |
/** |
| 979 |
* Polls the location of the receiver-frame for changes. |
| 980 |
* @return {boolean} Whether a change has been detected. |
| 981 |
*/ |
| 982 |
goog.net.xpc.IframePollingTransport.Receiver.prototype.receive = function() { |
| 983 |
var loc = this.rcvFrame_.location.href; |
| 984 |
|
| 985 |
if (loc != this.currentLoc_) { |
| 986 |
this.currentLoc_ = loc; |
| 987 |
var payload = loc.split('#')[1]; |
| 988 |
if (payload) { |
| 989 |
payload = payload.substr(1); // discard first character (cycle) |
| 990 |
this.cb_(decodeURIComponent(payload)); |
| 991 |
} |
| 992 |
return true; |
| 993 |
} else { |
| 994 |
return false; |
| 995 |
} |
| 996 |
}; |