iframepollingtransport.js 29 KB

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