webchannelbase.js 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  1. // Copyright 2006 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 Base WebChannel implementation.
  16. *
  17. */
  18. goog.provide('goog.labs.net.webChannel.WebChannelBase');
  19. goog.require('goog.Uri');
  20. goog.require('goog.array');
  21. goog.require('goog.asserts');
  22. goog.require('goog.debug.TextFormatter');
  23. goog.require('goog.json');
  24. goog.require('goog.labs.net.webChannel.BaseTestChannel');
  25. goog.require('goog.labs.net.webChannel.Channel');
  26. goog.require('goog.labs.net.webChannel.ChannelRequest');
  27. goog.require('goog.labs.net.webChannel.ConnectionState');
  28. goog.require('goog.labs.net.webChannel.ForwardChannelRequestPool');
  29. goog.require('goog.labs.net.webChannel.WebChannelDebug');
  30. goog.require('goog.labs.net.webChannel.Wire');
  31. goog.require('goog.labs.net.webChannel.WireV8');
  32. goog.require('goog.labs.net.webChannel.netUtils');
  33. goog.require('goog.labs.net.webChannel.requestStats');
  34. goog.require('goog.log');
  35. goog.require('goog.net.WebChannel');
  36. goog.require('goog.net.XhrIo');
  37. goog.require('goog.net.rpc.HttpCors');
  38. goog.require('goog.object');
  39. goog.require('goog.string');
  40. goog.require('goog.structs');
  41. goog.require('goog.structs.CircularBuffer');
  42. goog.scope(function() {
  43. var WebChannel = goog.net.WebChannel;
  44. var BaseTestChannel = goog.labs.net.webChannel.BaseTestChannel;
  45. var ChannelRequest = goog.labs.net.webChannel.ChannelRequest;
  46. var ConnectionState = goog.labs.net.webChannel.ConnectionState;
  47. var ForwardChannelRequestPool =
  48. goog.labs.net.webChannel.ForwardChannelRequestPool;
  49. var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug;
  50. var Wire = goog.labs.net.webChannel.Wire;
  51. var WireV8 = goog.labs.net.webChannel.WireV8;
  52. var netUtils = goog.labs.net.webChannel.netUtils;
  53. var requestStats = goog.labs.net.webChannel.requestStats;
  54. var httpCors = goog.module.get('goog.net.rpc.HttpCors');
  55. /**
  56. * This WebChannel implementation is branched off goog.net.BrowserChannel
  57. * for now. Ongoing changes to goog.net.BrowserChannel will be back
  58. * ported to this implementation as needed.
  59. *
  60. * @param {!goog.net.WebChannel.Options=} opt_options Configuration for the
  61. * WebChannel instance.
  62. * @param {number=} opt_clientVersion An application-specific version number
  63. * that is sent to the server when connected.
  64. * @param {!ConnectionState=} opt_conn Previously determined connection
  65. * conditions.
  66. * @constructor
  67. * @struct
  68. * @implements {goog.labs.net.webChannel.Channel}
  69. */
  70. goog.labs.net.webChannel.WebChannelBase = function(
  71. opt_options, opt_clientVersion, opt_conn) {
  72. /**
  73. * The client library version (capabilities).
  74. * @private {number}
  75. */
  76. this.clientVersion_ = opt_clientVersion || 0;
  77. /**
  78. * The server library version (capabilities).
  79. * @private {number}
  80. */
  81. this.serverVersion_ = 0;
  82. /**
  83. * An array of queued maps that need to be sent to the server.
  84. * @private {!Array<Wire.QueuedMap>}
  85. */
  86. this.outgoingMaps_ = [];
  87. /**
  88. * An array of dequeued maps that we have either received a non-successful
  89. * response for, or no response at all, and which therefore may or may not
  90. * have been received by the server.
  91. * @private {!Array<Wire.QueuedMap>}
  92. */
  93. this.pendingMaps_ = [];
  94. /**
  95. * The channel debug used for logging
  96. * @private {!WebChannelDebug}
  97. */
  98. this.channelDebug_ = new WebChannelDebug();
  99. /**
  100. * Previous connectivity test results.
  101. * @private {!ConnectionState}
  102. */
  103. this.connState_ = opt_conn || new ConnectionState();
  104. /**
  105. * Extra HTTP headers to add to all the requests sent to the server.
  106. * @private {Object}
  107. */
  108. this.extraHeaders_ = null;
  109. /**
  110. * Extra HTTP headers to add to the init request(s) sent to the server.
  111. * @private {Object}
  112. */
  113. this.initHeaders_ = null;
  114. /**
  115. * @private {?string} The URL param name to overwrite custom HTTP headers
  116. * to bypass CORS preflight.
  117. */
  118. this.httpHeadersOverwriteParam_ = null;
  119. /**
  120. * Extra parameters to add to all the requests sent to the server.
  121. * @private {Object}
  122. */
  123. this.extraParams_ = null;
  124. /**
  125. * Parameter name for the http session id.
  126. * @private {?string}
  127. */
  128. this.httpSessionIdParam_ = null;
  129. /**
  130. * The http session id, to be sent with httpSessionIdParam_ with each
  131. * request after the initial handshake.
  132. * @private {?string}
  133. */
  134. this.httpSessionId_ = null;
  135. /**
  136. * The ChannelRequest object for the backchannel.
  137. * @private {ChannelRequest}
  138. */
  139. this.backChannelRequest_ = null;
  140. /**
  141. * The relative path (in the context of the the page hosting the browser
  142. * channel) for making requests to the server.
  143. * @private {?string}
  144. */
  145. this.path_ = null;
  146. /**
  147. * The absolute URI for the forwardchannel request.
  148. * @private {goog.Uri}
  149. */
  150. this.forwardChannelUri_ = null;
  151. /**
  152. * The absolute URI for the backchannel request.
  153. * @private {goog.Uri}
  154. */
  155. this.backChannelUri_ = null;
  156. /**
  157. * A subdomain prefix for using a subdomain in IE for the backchannel
  158. * requests.
  159. * @private {?string}
  160. */
  161. this.hostPrefix_ = null;
  162. /**
  163. * Whether we allow the use of a subdomain in IE for the backchannel requests.
  164. * @private {boolean}
  165. */
  166. this.allowHostPrefix_ = true;
  167. /**
  168. * The next id to use for the RID (request identifier) parameter. This
  169. * identifier uniquely identifies the forward channel request.
  170. * @private {number}
  171. */
  172. this.nextRid_ = 0;
  173. /**
  174. * The id to use for the next outgoing map. This identifier uniquely
  175. * identifies a sent map.
  176. * @private {number}
  177. */
  178. this.nextMapId_ = 0;
  179. /**
  180. * Whether to fail forward-channel requests after one try or a few tries.
  181. * @private {boolean}
  182. */
  183. this.failFast_ = false;
  184. /**
  185. * The handler that receive callbacks for state changes and data.
  186. * @private {goog.labs.net.webChannel.WebChannelBase.Handler}
  187. */
  188. this.handler_ = null;
  189. /**
  190. * Timer identifier for asynchronously making a forward channel request.
  191. * @private {?number}
  192. */
  193. this.forwardChannelTimerId_ = null;
  194. /**
  195. * Timer identifier for asynchronously making a back channel request.
  196. * @private {?number}
  197. */
  198. this.backChannelTimerId_ = null;
  199. /**
  200. * Timer identifier for the timer that waits for us to retry the backchannel
  201. * in the case where it is dead and no longer receiving data.
  202. * @private {?number}
  203. */
  204. this.deadBackChannelTimerId_ = null;
  205. /**
  206. * The TestChannel object which encapsulates the logic for determining
  207. * interesting network conditions about the client.
  208. * @private {BaseTestChannel}
  209. */
  210. this.connectionTest_ = null;
  211. /**
  212. * Whether the client's network conditions can support chunked responses.
  213. * @private {?boolean}
  214. */
  215. this.useChunked_ = null;
  216. /**
  217. * Whether chunked mode is allowed. In certain debugging situations, it's
  218. * useful to disable this.
  219. * @private {boolean}
  220. */
  221. this.allowChunkedMode_ = true;
  222. /**
  223. * The array identifier of the last array received from the server for the
  224. * backchannel request.
  225. * @private {number}
  226. */
  227. this.lastArrayId_ = -1;
  228. /**
  229. * The array id of the last array sent by the server that we know about.
  230. * @private {number}
  231. */
  232. this.lastPostResponseArrayId_ = -1;
  233. /**
  234. * The last status code received.
  235. * @private {number}
  236. */
  237. this.lastStatusCode_ = -1;
  238. /**
  239. * Number of times we have retried the current forward channel request.
  240. * @private {number}
  241. */
  242. this.forwardChannelRetryCount_ = 0;
  243. /**
  244. * Number of times in a row that we have retried the current back channel
  245. * request and received no data.
  246. * @private {number}
  247. */
  248. this.backChannelRetryCount_ = 0;
  249. /**
  250. * The attempt id for the current back channel request. Starts at 1 and
  251. * increments for each reconnect. The server uses this to log if our
  252. * connection is flaky or not.
  253. * @private {number}
  254. */
  255. this.backChannelAttemptId_ = 0;
  256. /**
  257. * The base part of the time before firing next retry request. Default is 5
  258. * seconds. Note that a random delay is added (see {@link retryDelaySeedMs_})
  259. * for all retries, and linear backoff is applied to the sum for subsequent
  260. * retries.
  261. * @private {number}
  262. */
  263. this.baseRetryDelayMs_ = 5 * 1000;
  264. /**
  265. * A random time between 0 and this number of MS is added to the
  266. * {@link baseRetryDelayMs_}. Default is 10 seconds.
  267. * @private {number}
  268. */
  269. this.retryDelaySeedMs_ = 10 * 1000;
  270. /**
  271. * Maximum number of attempts to connect to the server for forward channel
  272. * requests. Defaults to 2.
  273. * @private {number}
  274. */
  275. this.forwardChannelMaxRetries_ = 2;
  276. /**
  277. * The timeout in milliseconds for a forward channel request. Defaults to 20
  278. * seconds. Note that part of this timeout can be randomized.
  279. * @private {number}
  280. */
  281. this.forwardChannelRequestTimeoutMs_ = 20 * 1000;
  282. /**
  283. * A throttle time in ms for readystatechange events for the backchannel.
  284. * Useful for throttling when ready state is INTERACTIVE (partial data).
  285. *
  286. * This throttle is useful if the server sends large data chunks down the
  287. * backchannel. It prevents examining XHR partial data on every readystate
  288. * change event. This is useful because large chunks can trigger hundreds
  289. * of readystatechange events, each of which takes ~5ms or so to handle,
  290. * in turn making the UI unresponsive for a significant period.
  291. *
  292. * If set to zero no throttle is used.
  293. * @private {number}
  294. */
  295. this.readyStateChangeThrottleMs_ = 0;
  296. /**
  297. * Whether cross origin requests are supported for the channel.
  298. *
  299. * See {@link goog.net.XhrIo#setWithCredentials}.
  300. * @private {boolean}
  301. */
  302. this.supportsCrossDomainXhrs_ =
  303. (opt_options && opt_options.supportsCrossDomainXhr) || false;
  304. /**
  305. * The current session id.
  306. * @private {string}
  307. */
  308. this.sid_ = '';
  309. /**
  310. * The current ChannelRequest pool for the forward channel.
  311. * @private {!ForwardChannelRequestPool}
  312. */
  313. this.forwardChannelRequestPool_ = new ForwardChannelRequestPool(
  314. opt_options && opt_options.concurrentRequestLimit);
  315. /**
  316. * The V8 codec.
  317. * @private {!WireV8}
  318. */
  319. this.wireCodec_ = new WireV8();
  320. /**
  321. * Whether to run the channel test as a background process to not block
  322. * the OPEN event.
  323. *
  324. * @private {boolean}
  325. */
  326. this.backgroundChannelTest_ =
  327. opt_options && goog.isDef(opt_options.backgroundChannelTest) ?
  328. opt_options.backgroundChannelTest :
  329. true;
  330. /**
  331. * Whether to turn on the fast handshake behavior.
  332. *
  333. * @private {boolean}
  334. */
  335. this.fastHandshake_ = (opt_options && opt_options.fastHandshake) || false;
  336. };
  337. var WebChannelBase = goog.labs.net.webChannel.WebChannelBase;
  338. /**
  339. * The channel version that we negotiated with the server for this session.
  340. * Starts out as the version we request, and then is changed to the negotiated
  341. * version after the initial open.
  342. * @private {number}
  343. */
  344. WebChannelBase.prototype.channelVersion_ = Wire.LATEST_CHANNEL_VERSION;
  345. /**
  346. * Enum type for the channel state machine.
  347. * @enum {number}
  348. */
  349. WebChannelBase.State = {
  350. /** The channel is closed. */
  351. CLOSED: 0,
  352. /** The channel has been initialized but hasn't yet initiated a connection. */
  353. INIT: 1,
  354. /** The channel is in the process of opening a connection to the server. */
  355. OPENING: 2,
  356. /** The channel is open. */
  357. OPENED: 3
  358. };
  359. /**
  360. * The current state of the WebChannel.
  361. * @private {!WebChannelBase.State}
  362. */
  363. WebChannelBase.prototype.state_ = WebChannelBase.State.INIT;
  364. /**
  365. * The timeout in milliseconds for a forward channel request.
  366. * @type {number}
  367. */
  368. WebChannelBase.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;
  369. /**
  370. * Maximum number of attempts to connect to the server for back channel
  371. * requests.
  372. * @type {number}
  373. */
  374. WebChannelBase.BACK_CHANNEL_MAX_RETRIES = 3;
  375. /**
  376. * A number in MS of how long we guess the maxmium amount of time a round trip
  377. * to the server should take. In the future this could be substituted with a
  378. * real measurement of the RTT.
  379. * @type {number}
  380. */
  381. WebChannelBase.RTT_ESTIMATE = 3 * 1000;
  382. /**
  383. * When retrying for an inactive channel, we will multiply the total delay by
  384. * this number.
  385. * @type {number}
  386. */
  387. WebChannelBase.INACTIVE_CHANNEL_RETRY_FACTOR = 2;
  388. /**
  389. * Enum type for identifying an error.
  390. * @enum {number}
  391. */
  392. WebChannelBase.Error = {
  393. /** Value that indicates no error has occurred. */
  394. OK: 0,
  395. /** An error due to a request failing. */
  396. REQUEST_FAILED: 2,
  397. /** An error due to the user being logged out. */
  398. LOGGED_OUT: 4,
  399. /** An error due to server response which contains no data. */
  400. NO_DATA: 5,
  401. /** An error due to a server response indicating an unknown session id */
  402. UNKNOWN_SESSION_ID: 6,
  403. /** An error due to a server response requesting to stop the channel. */
  404. STOP: 7,
  405. /** A general network error. */
  406. NETWORK: 8,
  407. /** An error due to bad data being returned from the server. */
  408. BAD_DATA: 10,
  409. /** An error due to a response that is not parsable. */
  410. BAD_RESPONSE: 11
  411. };
  412. /**
  413. * Internal enum type for the two channel types.
  414. * @enum {number}
  415. * @private
  416. */
  417. WebChannelBase.ChannelType_ = {
  418. FORWARD_CHANNEL: 1,
  419. BACK_CHANNEL: 2
  420. };
  421. /**
  422. * The maximum number of maps that can be sent in one POST. Should match
  423. * MAX_MAPS_PER_REQUEST on the server code.
  424. * @type {number}
  425. * @private
  426. */
  427. WebChannelBase.MAX_MAPS_PER_REQUEST_ = 1000;
  428. /**
  429. * A guess at a cutoff at which to no longer assume the backchannel is dead
  430. * when we are slow to receive data. Number in bytes.
  431. *
  432. * Assumption: The worst bandwidth we work on is 50 kilobits/sec
  433. * 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout
  434. * @type {number}
  435. */
  436. WebChannelBase.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500;
  437. /**
  438. * @return {!ForwardChannelRequestPool} The forward channel request pool.
  439. */
  440. WebChannelBase.prototype.getForwardChannelRequestPool = function() {
  441. return this.forwardChannelRequestPool_;
  442. };
  443. /**
  444. * @return {!Object} The codec object, to be used for the test channel.
  445. */
  446. WebChannelBase.prototype.getWireCodec = function() {
  447. return this.wireCodec_;
  448. };
  449. /**
  450. * Returns the logger.
  451. *
  452. * @return {!WebChannelDebug} The channel debug object.
  453. */
  454. WebChannelBase.prototype.getChannelDebug = function() {
  455. return this.channelDebug_;
  456. };
  457. /**
  458. * Sets the logger.
  459. *
  460. * @param {!WebChannelDebug} channelDebug The channel debug object.
  461. */
  462. WebChannelBase.prototype.setChannelDebug = function(channelDebug) {
  463. this.channelDebug_ = channelDebug;
  464. };
  465. /**
  466. * Starts the channel. This initiates connections to the server.
  467. *
  468. * @param {string} testPath The path for the test connection.
  469. * @param {string} channelPath The path for the channel connection.
  470. * @param {!Object=} opt_extraParams Extra parameter keys and values to add to
  471. * the requests.
  472. * @param {string=} opt_oldSessionId Session ID from a previous session.
  473. * @param {number=} opt_oldArrayId The last array ID from a previous session.
  474. */
  475. WebChannelBase.prototype.connect = function(
  476. testPath, channelPath, opt_extraParams, opt_oldSessionId, opt_oldArrayId) {
  477. this.channelDebug_.debug('connect()');
  478. requestStats.notifyStatEvent(requestStats.Stat.CONNECT_ATTEMPT);
  479. this.path_ = channelPath;
  480. this.extraParams_ = opt_extraParams || {};
  481. // Attach parameters about the previous session if reconnecting.
  482. if (opt_oldSessionId && goog.isDef(opt_oldArrayId)) {
  483. this.extraParams_['OSID'] = opt_oldSessionId;
  484. this.extraParams_['OAID'] = opt_oldArrayId;
  485. }
  486. if (this.backgroundChannelTest_) {
  487. this.channelDebug_.debug('connect() bypassed channel-test.');
  488. this.connState_.handshakeResult = [];
  489. this.connState_.bufferingProxyResult = false;
  490. // TODO(user): merge states with background channel test
  491. // requestStats.setTimeout(goog.bind(this.connectTest_, this, testPath), 0);
  492. // this.connectChannel_();
  493. }
  494. this.connectTest_(testPath);
  495. };
  496. /**
  497. * Disconnects and closes the channel.
  498. */
  499. WebChannelBase.prototype.disconnect = function() {
  500. this.channelDebug_.debug('disconnect()');
  501. this.cancelRequests_();
  502. if (this.state_ == WebChannelBase.State.OPENED) {
  503. var rid = this.nextRid_++;
  504. var uri = this.forwardChannelUri_.clone();
  505. uri.setParameterValue('SID', this.sid_);
  506. uri.setParameterValue('RID', rid);
  507. uri.setParameterValue('TYPE', 'terminate');
  508. // Add the reconnect parameters.
  509. this.addAdditionalParams_(uri);
  510. var request = ChannelRequest.createChannelRequest(
  511. this, this.channelDebug_, this.sid_, rid);
  512. request.sendCloseRequest(uri);
  513. }
  514. this.onClose_();
  515. };
  516. /**
  517. * Returns the session id of the channel. Only available after the
  518. * channel has been opened.
  519. * @return {string} Session ID.
  520. */
  521. WebChannelBase.prototype.getSessionId = function() {
  522. return this.sid_;
  523. };
  524. /**
  525. * Starts the test channel to determine network conditions.
  526. *
  527. * @param {string} testPath The relative PATH for the test connection.
  528. * @private
  529. */
  530. WebChannelBase.prototype.connectTest_ = function(testPath) {
  531. this.channelDebug_.debug('connectTest_()');
  532. if (!this.okToMakeRequest_()) {
  533. return; // channel is cancelled
  534. }
  535. this.connectionTest_ = new BaseTestChannel(this, this.channelDebug_);
  536. if (this.httpHeadersOverwriteParam_ === null) {
  537. this.connectionTest_.setExtraHeaders(this.extraHeaders_);
  538. }
  539. var urlPath = testPath;
  540. if (this.httpHeadersOverwriteParam_ && this.extraHeaders_) {
  541. urlPath = httpCors.setHttpHeadersWithOverwriteParam(
  542. testPath, this.httpHeadersOverwriteParam_, this.extraHeaders_);
  543. }
  544. this.connectionTest_.connect(/** @type {string} */ (urlPath));
  545. };
  546. /**
  547. * Starts the regular channel which is run after the test channel is complete.
  548. * @private
  549. */
  550. WebChannelBase.prototype.connectChannel_ = function() {
  551. this.channelDebug_.debug('connectChannel_()');
  552. this.ensureInState_(WebChannelBase.State.INIT, WebChannelBase.State.CLOSED);
  553. this.forwardChannelUri_ =
  554. this.getForwardChannelUri(/** @type {string} */ (this.path_));
  555. this.ensureForwardChannel_();
  556. };
  557. /**
  558. * Cancels all outstanding requests.
  559. * @private
  560. */
  561. WebChannelBase.prototype.cancelRequests_ = function() {
  562. if (this.connectionTest_) {
  563. this.connectionTest_.abort();
  564. this.connectionTest_ = null;
  565. }
  566. if (this.backChannelRequest_) {
  567. this.backChannelRequest_.cancel();
  568. this.backChannelRequest_ = null;
  569. }
  570. if (this.backChannelTimerId_) {
  571. goog.global.clearTimeout(this.backChannelTimerId_);
  572. this.backChannelTimerId_ = null;
  573. }
  574. this.clearDeadBackchannelTimer_();
  575. this.forwardChannelRequestPool_.cancel();
  576. if (this.forwardChannelTimerId_) {
  577. goog.global.clearTimeout(this.forwardChannelTimerId_);
  578. this.forwardChannelTimerId_ = null;
  579. }
  580. };
  581. /**
  582. * Returns the extra HTTP headers to add to all the requests sent to the server.
  583. *
  584. * @return {Object} The HTTP headers, or null.
  585. */
  586. WebChannelBase.prototype.getExtraHeaders = function() {
  587. return this.extraHeaders_;
  588. };
  589. /**
  590. * Sets extra HTTP headers to add to all the requests sent to the server.
  591. *
  592. * @param {Object} extraHeaders The HTTP headers, or null.
  593. */
  594. WebChannelBase.prototype.setExtraHeaders = function(extraHeaders) {
  595. this.extraHeaders_ = extraHeaders;
  596. };
  597. /**
  598. * Returns the extra HTTP headers to add to the init requests
  599. * sent to the server.
  600. *
  601. * @return {Object} The HTTP headers, or null.
  602. */
  603. WebChannelBase.prototype.getInitHeaders = function() {
  604. return this.initHeaders_;
  605. };
  606. /**
  607. * Sets extra HTTP headers to add to the init requests sent to the server.
  608. *
  609. * @param {Object} initHeaders The HTTP headers, or null.
  610. */
  611. WebChannelBase.prototype.setInitHeaders = function(initHeaders) {
  612. this.initHeaders_ = initHeaders;
  613. };
  614. /**
  615. * Sets the URL param name to overwrite custom HTTP headers.
  616. *
  617. * @param {string} httpHeadersOverwriteParam The URL param name.
  618. */
  619. WebChannelBase.prototype.setHttpHeadersOverwriteParam = function(
  620. httpHeadersOverwriteParam) {
  621. this.httpHeadersOverwriteParam_ = httpHeadersOverwriteParam;
  622. };
  623. /**
  624. * @override
  625. */
  626. WebChannelBase.prototype.setHttpSessionIdParam = function(httpSessionIdParam) {
  627. this.httpSessionIdParam_ = httpSessionIdParam;
  628. };
  629. /**
  630. * @override
  631. */
  632. WebChannelBase.prototype.getHttpSessionIdParam = function() {
  633. return this.httpSessionIdParam_;
  634. };
  635. /**
  636. * @override
  637. */
  638. WebChannelBase.prototype.setHttpSessionId = function(httpSessionId) {
  639. this.httpSessionId_ = httpSessionId;
  640. };
  641. /**
  642. * @override
  643. */
  644. WebChannelBase.prototype.getHttpSessionId = function() {
  645. return this.httpSessionId_;
  646. };
  647. /**
  648. * @override
  649. */
  650. WebChannelBase.prototype.getBackgroundChannelTest = function() {
  651. return this.backgroundChannelTest_;
  652. };
  653. /**
  654. * Sets the throttle for handling onreadystatechange events for the request.
  655. *
  656. * @param {number} throttle The throttle in ms. A value of zero indicates
  657. * no throttle.
  658. */
  659. WebChannelBase.prototype.setReadyStateChangeThrottle = function(throttle) {
  660. this.readyStateChangeThrottleMs_ = throttle;
  661. };
  662. /**
  663. * Sets whether cross origin requests are supported for the channel.
  664. *
  665. * Setting this allows the creation of requests to secondary domains and
  666. * sends XHRs with the CORS withCredentials bit set to true.
  667. *
  668. * In order for cross-origin requests to work, the server will also need to set
  669. * CORS response headers as per:
  670. * https://developer.mozilla.org/en-US/docs/HTTP_access_control
  671. *
  672. * See {@link goog.net.XhrIo#setWithCredentials}.
  673. * @param {boolean} supportCrossDomain Whether cross domain XHRs are supported.
  674. */
  675. WebChannelBase.prototype.setSupportsCrossDomainXhrs = function(
  676. supportCrossDomain) {
  677. this.supportsCrossDomainXhrs_ = supportCrossDomain;
  678. };
  679. /**
  680. * Returns the handler used for channel callback events.
  681. *
  682. * @return {WebChannelBase.Handler} The handler.
  683. */
  684. WebChannelBase.prototype.getHandler = function() {
  685. return this.handler_;
  686. };
  687. /**
  688. * Sets the handler used for channel callback events.
  689. * @param {WebChannelBase.Handler} handler The handler to set.
  690. */
  691. WebChannelBase.prototype.setHandler = function(handler) {
  692. this.handler_ = handler;
  693. };
  694. /**
  695. * Returns whether the channel allows the use of a subdomain. There may be
  696. * cases where this isn't allowed.
  697. * @return {boolean} Whether a host prefix is allowed.
  698. */
  699. WebChannelBase.prototype.getAllowHostPrefix = function() {
  700. return this.allowHostPrefix_;
  701. };
  702. /**
  703. * Sets whether the channel allows the use of a subdomain. There may be cases
  704. * where this isn't allowed, for example, logging in with troutboard where
  705. * using a subdomain causes Apache to force the user to authenticate twice.
  706. * @param {boolean} allowHostPrefix Whether a host prefix is allowed.
  707. */
  708. WebChannelBase.prototype.setAllowHostPrefix = function(allowHostPrefix) {
  709. this.allowHostPrefix_ = allowHostPrefix;
  710. };
  711. /**
  712. * Returns whether the channel is buffered or not. This state is valid for
  713. * querying only after the test connection has completed. This may be
  714. * queried in the WebChannelBase.okToMakeRequest() callback.
  715. * A channel may be buffered if the test connection determines that
  716. * a chunked response could not be sent down within a suitable time.
  717. * @return {boolean} Whether the channel is buffered.
  718. */
  719. WebChannelBase.prototype.isBuffered = function() {
  720. return !this.useChunked_;
  721. };
  722. /**
  723. * Returns whether chunked mode is allowed. In certain debugging situations,
  724. * it's useful for the application to have a way to disable chunked mode for a
  725. * user.
  726. * @return {boolean} Whether chunked mode is allowed.
  727. */
  728. WebChannelBase.prototype.getAllowChunkedMode = function() {
  729. return this.allowChunkedMode_;
  730. };
  731. /**
  732. * Sets whether chunked mode is allowed. In certain debugging situations, it's
  733. * useful for the application to have a way to disable chunked mode for a user.
  734. * @param {boolean} allowChunkedMode Whether chunked mode is allowed.
  735. */
  736. WebChannelBase.prototype.setAllowChunkedMode = function(allowChunkedMode) {
  737. this.allowChunkedMode_ = allowChunkedMode;
  738. };
  739. /**
  740. * Sends a request to the server. The format of the request is a Map data
  741. * structure of key/value pairs. These maps are then encoded in a format
  742. * suitable for the wire and then reconstituted as a Map data structure that
  743. * the server can process.
  744. * @param {!Object|!goog.structs.Map} map The map to send.
  745. * @param {!Object=} opt_context The context associated with the map.
  746. */
  747. WebChannelBase.prototype.sendMap = function(map, opt_context) {
  748. goog.asserts.assert(
  749. this.state_ != WebChannelBase.State.CLOSED,
  750. 'Invalid operation: sending map when state is closed');
  751. // We can only send 1000 maps per POST, but typically we should never have
  752. // that much to send, so warn if we exceed that (we still send all the maps).
  753. if (this.outgoingMaps_.length == WebChannelBase.MAX_MAPS_PER_REQUEST_) {
  754. // severe() is temporary so that we get these uploaded and can figure out
  755. // what's causing them. Afterwards can change to warning().
  756. this.channelDebug_.severe(
  757. 'Already have ' + WebChannelBase.MAX_MAPS_PER_REQUEST_ +
  758. ' queued maps upon queueing ' + goog.json.serialize(map));
  759. }
  760. this.outgoingMaps_.push(
  761. new Wire.QueuedMap(this.nextMapId_++, map, opt_context));
  762. if (this.state_ == WebChannelBase.State.OPENING ||
  763. this.state_ == WebChannelBase.State.OPENED) {
  764. this.ensureForwardChannel_();
  765. }
  766. };
  767. /**
  768. * When set to true, this changes the behavior of the forward channel so it
  769. * will not retry requests; it will fail after one network failure, and if
  770. * there was already one network failure, the request will fail immediately.
  771. * @param {boolean} failFast Whether or not to fail fast.
  772. */
  773. WebChannelBase.prototype.setFailFast = function(failFast) {
  774. this.failFast_ = failFast;
  775. this.channelDebug_.info('setFailFast: ' + failFast);
  776. if ((this.forwardChannelRequestPool_.hasPendingRequest() ||
  777. this.forwardChannelTimerId_) &&
  778. this.forwardChannelRetryCount_ > this.getForwardChannelMaxRetries()) {
  779. this.channelDebug_.info(
  780. 'Retry count ' + this.forwardChannelRetryCount_ + ' > new maxRetries ' +
  781. this.getForwardChannelMaxRetries() + '. Fail immediately!');
  782. if (!this.forwardChannelRequestPool_.forceComplete(
  783. goog.bind(this.onRequestComplete, this))) {
  784. // i.e., this.forwardChannelTimerId_
  785. goog.global.clearTimeout(this.forwardChannelTimerId_);
  786. this.forwardChannelTimerId_ = null;
  787. // The error code from the last failed request is gone, so just use a
  788. // generic one.
  789. this.signalError_(WebChannelBase.Error.REQUEST_FAILED);
  790. }
  791. }
  792. };
  793. /**
  794. * @return {number} The max number of forward-channel retries, which will be 0
  795. * in fail-fast mode.
  796. */
  797. WebChannelBase.prototype.getForwardChannelMaxRetries = function() {
  798. return this.failFast_ ? 0 : this.forwardChannelMaxRetries_;
  799. };
  800. /**
  801. * Sets the maximum number of attempts to connect to the server for forward
  802. * channel requests.
  803. * @param {number} retries The maximum number of attempts.
  804. */
  805. WebChannelBase.prototype.setForwardChannelMaxRetries = function(retries) {
  806. this.forwardChannelMaxRetries_ = retries;
  807. };
  808. /**
  809. * Sets the timeout for a forward channel request.
  810. * @param {number} timeoutMs The timeout in milliseconds.
  811. */
  812. WebChannelBase.prototype.setForwardChannelRequestTimeout = function(timeoutMs) {
  813. this.forwardChannelRequestTimeoutMs_ = timeoutMs;
  814. };
  815. /**
  816. * @return {number} The max number of back-channel retries, which is a constant.
  817. */
  818. WebChannelBase.prototype.getBackChannelMaxRetries = function() {
  819. // Back-channel retries is a constant.
  820. return WebChannelBase.BACK_CHANNEL_MAX_RETRIES;
  821. };
  822. /**
  823. * @override
  824. */
  825. WebChannelBase.prototype.isClosed = function() {
  826. return this.state_ == WebChannelBase.State.CLOSED;
  827. };
  828. /**
  829. * Returns the channel state.
  830. * @return {WebChannelBase.State} The current state of the channel.
  831. */
  832. WebChannelBase.prototype.getState = function() {
  833. return this.state_;
  834. };
  835. /**
  836. * Return the last status code received for a request.
  837. * @return {number} The last status code received for a request.
  838. */
  839. WebChannelBase.prototype.getLastStatusCode = function() {
  840. return this.lastStatusCode_;
  841. };
  842. /**
  843. * @return {number} The last array id received.
  844. */
  845. WebChannelBase.prototype.getLastArrayId = function() {
  846. return this.lastArrayId_;
  847. };
  848. /**
  849. * Returns whether there are outstanding requests servicing the channel.
  850. * @return {boolean} true if there are outstanding requests.
  851. */
  852. WebChannelBase.prototype.hasOutstandingRequests = function() {
  853. return this.getOutstandingRequests_() != 0;
  854. };
  855. /**
  856. * Returns the number of outstanding requests.
  857. * @return {number} The number of outstanding requests to the server.
  858. * @private
  859. */
  860. WebChannelBase.prototype.getOutstandingRequests_ = function() {
  861. var count = 0;
  862. if (this.backChannelRequest_) {
  863. count++;
  864. }
  865. count += this.forwardChannelRequestPool_.getRequestCount();
  866. return count;
  867. };
  868. /**
  869. * Ensures that a forward channel request is scheduled.
  870. * @private
  871. */
  872. WebChannelBase.prototype.ensureForwardChannel_ = function() {
  873. if (this.forwardChannelRequestPool_.isFull()) {
  874. // enough connection in process - no need to start a new request
  875. return;
  876. }
  877. if (this.forwardChannelTimerId_) {
  878. // no need to start a new request - one is already scheduled
  879. return;
  880. }
  881. this.forwardChannelTimerId_ = requestStats.setTimeout(
  882. goog.bind(this.onStartForwardChannelTimer_, this), 0);
  883. this.forwardChannelRetryCount_ = 0;
  884. };
  885. /**
  886. * Schedules a forward-channel retry for the specified request, unless the max
  887. * retries has been reached.
  888. * @param {ChannelRequest} request The failed request to retry.
  889. * @return {boolean} true iff a retry was scheduled.
  890. * @private
  891. */
  892. WebChannelBase.prototype.maybeRetryForwardChannel_ = function(request) {
  893. if (this.forwardChannelRequestPool_.isFull() || this.forwardChannelTimerId_) {
  894. // Should be impossible to be called in this state.
  895. this.channelDebug_.severe('Request already in progress');
  896. return false;
  897. }
  898. // No retry for open_() and fail-fast
  899. if (this.state_ == WebChannelBase.State.INIT ||
  900. this.state_ == WebChannelBase.State.OPENING ||
  901. (this.forwardChannelRetryCount_ >= this.getForwardChannelMaxRetries())) {
  902. return false;
  903. }
  904. this.channelDebug_.debug('Going to retry POST');
  905. this.forwardChannelTimerId_ = requestStats.setTimeout(
  906. goog.bind(this.onStartForwardChannelTimer_, this, request),
  907. this.getRetryTime_(this.forwardChannelRetryCount_));
  908. this.forwardChannelRetryCount_++;
  909. return true;
  910. };
  911. /**
  912. * Timer callback for ensureForwardChannel
  913. * @param {ChannelRequest=} opt_retryRequest A failed request
  914. * to retry.
  915. * @private
  916. */
  917. WebChannelBase.prototype.onStartForwardChannelTimer_ = function(
  918. opt_retryRequest) {
  919. this.forwardChannelTimerId_ = null;
  920. this.startForwardChannel_(opt_retryRequest);
  921. };
  922. /**
  923. * Begins a new forward channel operation to the server.
  924. * @param {ChannelRequest=} opt_retryRequest A failed request to retry.
  925. * @private
  926. */
  927. WebChannelBase.prototype.startForwardChannel_ = function(opt_retryRequest) {
  928. this.channelDebug_.debug('startForwardChannel_');
  929. if (!this.okToMakeRequest_()) {
  930. return; // channel is cancelled
  931. } else if (this.state_ == WebChannelBase.State.INIT) {
  932. if (opt_retryRequest) {
  933. this.channelDebug_.severe('Not supposed to retry the open');
  934. return;
  935. }
  936. this.open_();
  937. this.state_ = WebChannelBase.State.OPENING;
  938. } else if (this.state_ == WebChannelBase.State.OPENED) {
  939. if (opt_retryRequest) {
  940. this.makeForwardChannelRequest_(opt_retryRequest);
  941. return;
  942. }
  943. if (this.outgoingMaps_.length == 0) {
  944. this.channelDebug_.debug(
  945. 'startForwardChannel_ returned: ' +
  946. 'nothing to send');
  947. // no need to start a new forward channel request
  948. return;
  949. }
  950. if (this.forwardChannelRequestPool_.isFull()) {
  951. // Should be impossible to be called in this state.
  952. this.channelDebug_.severe(
  953. 'startForwardChannel_ returned: ' +
  954. 'connection already in progress');
  955. return;
  956. }
  957. this.makeForwardChannelRequest_();
  958. this.channelDebug_.debug('startForwardChannel_ finished, sent request');
  959. }
  960. };
  961. /**
  962. * Establishes a new channel session with the the server.
  963. * @private
  964. */
  965. WebChannelBase.prototype.open_ = function() {
  966. this.channelDebug_.debug('open_()');
  967. this.nextRid_ = Math.floor(Math.random() * 100000);
  968. var rid = this.nextRid_++;
  969. var request =
  970. ChannelRequest.createChannelRequest(this, this.channelDebug_, '', rid);
  971. // mix the init headers
  972. var extraHeaders = this.extraHeaders_;
  973. if (this.initHeaders_) {
  974. if (extraHeaders) {
  975. extraHeaders = goog.object.clone(extraHeaders);
  976. goog.object.extend(extraHeaders, this.initHeaders_);
  977. } else {
  978. extraHeaders = this.initHeaders_;
  979. }
  980. }
  981. if (this.httpHeadersOverwriteParam_ === null) {
  982. request.setExtraHeaders(extraHeaders);
  983. }
  984. var requestText = this.dequeueOutgoingMaps_();
  985. var uri = this.forwardChannelUri_.clone();
  986. uri.setParameterValue('RID', rid);
  987. if (this.clientVersion_ > 0) {
  988. uri.setParameterValue('CVER', this.clientVersion_);
  989. }
  990. // http-session-id to be generated as the response
  991. if (this.getBackgroundChannelTest() && this.getHttpSessionIdParam()) {
  992. uri.setParameterValues(
  993. WebChannel.X_HTTP_SESSION_ID, this.getHttpSessionIdParam());
  994. }
  995. // Add the reconnect parameters.
  996. this.addAdditionalParams_(uri);
  997. if (this.httpHeadersOverwriteParam_ && extraHeaders) {
  998. httpCors.setHttpHeadersWithOverwriteParam(
  999. uri, this.httpHeadersOverwriteParam_, extraHeaders);
  1000. }
  1001. this.forwardChannelRequestPool_.addRequest(request);
  1002. request.xmlHttpPost(uri, requestText, true);
  1003. };
  1004. /**
  1005. * Makes a forward channel request using XMLHTTP.
  1006. * @param {!ChannelRequest=} opt_retryRequest A failed request to retry.
  1007. * @private
  1008. */
  1009. WebChannelBase.prototype.makeForwardChannelRequest_ = function(
  1010. opt_retryRequest) {
  1011. var rid;
  1012. var requestText;
  1013. if (opt_retryRequest) {
  1014. this.requeuePendingMaps_();
  1015. rid = this.nextRid_ - 1; // Must use last RID
  1016. requestText = this.dequeueOutgoingMaps_();
  1017. } else {
  1018. rid = this.nextRid_++;
  1019. requestText = this.dequeueOutgoingMaps_();
  1020. }
  1021. var uri = this.forwardChannelUri_.clone();
  1022. uri.setParameterValue('SID', this.sid_);
  1023. uri.setParameterValue('RID', rid);
  1024. uri.setParameterValue('AID', this.lastArrayId_);
  1025. // Add the additional reconnect parameters.
  1026. this.addAdditionalParams_(uri);
  1027. if (this.httpHeadersOverwriteParam_ && this.extraHeaders_) {
  1028. httpCors.setHttpHeadersWithOverwriteParam(
  1029. uri, this.httpHeadersOverwriteParam_, this.extraHeaders_);
  1030. }
  1031. var request = ChannelRequest.createChannelRequest(
  1032. this, this.channelDebug_, this.sid_, rid,
  1033. this.forwardChannelRetryCount_ + 1);
  1034. if (this.httpHeadersOverwriteParam_ === null) {
  1035. request.setExtraHeaders(this.extraHeaders_);
  1036. }
  1037. // Randomize from 50%-100% of the forward channel timeout to avoid
  1038. // a big hit if servers happen to die at once.
  1039. request.setTimeout(
  1040. Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50) +
  1041. Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50 * Math.random()));
  1042. this.forwardChannelRequestPool_.addRequest(request);
  1043. request.xmlHttpPost(uri, requestText, true);
  1044. };
  1045. /**
  1046. * Adds the additional parameters from the handler to the given URI.
  1047. * @param {!goog.Uri} uri The URI to add the parameters to.
  1048. * @private
  1049. */
  1050. WebChannelBase.prototype.addAdditionalParams_ = function(uri) {
  1051. // Add the additional reconnect parameters as needed.
  1052. if (this.handler_) {
  1053. var params = this.handler_.getAdditionalParams(this);
  1054. if (params) {
  1055. goog.structs.forEach(params, function(value, key, coll) {
  1056. uri.setParameterValue(key, value);
  1057. });
  1058. }
  1059. }
  1060. };
  1061. /**
  1062. * Returns the request text from the outgoing maps and resets it.
  1063. * @return {string} The encoded request text created from all the currently
  1064. * queued outgoing maps.
  1065. * @private
  1066. */
  1067. WebChannelBase.prototype.dequeueOutgoingMaps_ = function() {
  1068. var count =
  1069. Math.min(this.outgoingMaps_.length, WebChannelBase.MAX_MAPS_PER_REQUEST_);
  1070. var badMapHandler = this.handler_ ?
  1071. goog.bind(this.handler_.badMapError, this.handler_, this) :
  1072. null;
  1073. var result = this.wireCodec_.encodeMessageQueue(
  1074. this.outgoingMaps_, count, badMapHandler);
  1075. this.pendingMaps_ =
  1076. this.pendingMaps_.concat(this.outgoingMaps_.splice(0, count));
  1077. return result;
  1078. };
  1079. /**
  1080. * Requeues unacknowledged sent arrays for retransmission in the next forward
  1081. * channel request.
  1082. * @private
  1083. */
  1084. WebChannelBase.prototype.requeuePendingMaps_ = function() {
  1085. this.outgoingMaps_ = this.pendingMaps_.concat(this.outgoingMaps_);
  1086. this.pendingMaps_.length = 0;
  1087. };
  1088. /**
  1089. * Ensures there is a backchannel request for receiving data from the server.
  1090. * @private
  1091. */
  1092. WebChannelBase.prototype.ensureBackChannel_ = function() {
  1093. if (this.backChannelRequest_) {
  1094. // already have one
  1095. return;
  1096. }
  1097. if (this.backChannelTimerId_) {
  1098. // no need to start a new request - one is already scheduled
  1099. return;
  1100. }
  1101. this.backChannelAttemptId_ = 1;
  1102. this.backChannelTimerId_ = requestStats.setTimeout(
  1103. goog.bind(this.onStartBackChannelTimer_, this), 0);
  1104. this.backChannelRetryCount_ = 0;
  1105. };
  1106. /**
  1107. * Schedules a back-channel retry, unless the max retries has been reached.
  1108. * @return {boolean} true iff a retry was scheduled.
  1109. * @private
  1110. */
  1111. WebChannelBase.prototype.maybeRetryBackChannel_ = function() {
  1112. if (this.backChannelRequest_ || this.backChannelTimerId_) {
  1113. // Should be impossible to be called in this state.
  1114. this.channelDebug_.severe('Request already in progress');
  1115. return false;
  1116. }
  1117. if (this.backChannelRetryCount_ >= this.getBackChannelMaxRetries()) {
  1118. return false;
  1119. }
  1120. this.channelDebug_.debug('Going to retry GET');
  1121. this.backChannelAttemptId_++;
  1122. this.backChannelTimerId_ = requestStats.setTimeout(
  1123. goog.bind(this.onStartBackChannelTimer_, this),
  1124. this.getRetryTime_(this.backChannelRetryCount_));
  1125. this.backChannelRetryCount_++;
  1126. return true;
  1127. };
  1128. /**
  1129. * Timer callback for ensureBackChannel_.
  1130. * @private
  1131. */
  1132. WebChannelBase.prototype.onStartBackChannelTimer_ = function() {
  1133. this.backChannelTimerId_ = null;
  1134. this.startBackChannel_();
  1135. };
  1136. /**
  1137. * Begins a new back channel operation to the server.
  1138. * @private
  1139. */
  1140. WebChannelBase.prototype.startBackChannel_ = function() {
  1141. if (!this.okToMakeRequest_()) {
  1142. // channel is cancelled
  1143. return;
  1144. }
  1145. this.channelDebug_.debug('Creating new HttpRequest');
  1146. this.backChannelRequest_ = ChannelRequest.createChannelRequest(
  1147. this, this.channelDebug_, this.sid_, 'rpc', this.backChannelAttemptId_);
  1148. if (this.httpHeadersOverwriteParam_ === null) {
  1149. this.backChannelRequest_.setExtraHeaders(this.extraHeaders_);
  1150. }
  1151. this.backChannelRequest_.setReadyStateChangeThrottle(
  1152. this.readyStateChangeThrottleMs_);
  1153. var uri = this.backChannelUri_.clone();
  1154. uri.setParameterValue('RID', 'rpc');
  1155. uri.setParameterValue('SID', this.sid_);
  1156. uri.setParameterValue('CI', this.useChunked_ ? '0' : '1');
  1157. uri.setParameterValue('AID', this.lastArrayId_);
  1158. // Add the reconnect parameters.
  1159. this.addAdditionalParams_(uri);
  1160. uri.setParameterValue('TYPE', 'xmlhttp');
  1161. if (this.httpHeadersOverwriteParam_ && this.extraHeaders_) {
  1162. httpCors.setHttpHeadersWithOverwriteParam(
  1163. uri, this.httpHeadersOverwriteParam_, this.extraHeaders_);
  1164. }
  1165. this.backChannelRequest_.xmlHttpGet(
  1166. uri, true /* decodeChunks */, this.hostPrefix_, false /* opt_noClose */);
  1167. this.channelDebug_.debug('New Request created');
  1168. };
  1169. /**
  1170. * Gives the handler a chance to return an error code and stop channel
  1171. * execution. A handler might want to do this to check that the user is still
  1172. * logged in, for example.
  1173. * @private
  1174. * @return {boolean} If it's OK to make a request.
  1175. */
  1176. WebChannelBase.prototype.okToMakeRequest_ = function() {
  1177. if (this.handler_) {
  1178. var result = this.handler_.okToMakeRequest(this);
  1179. if (result != WebChannelBase.Error.OK) {
  1180. this.channelDebug_.debug(
  1181. 'Handler returned error code from okToMakeRequest');
  1182. this.signalError_(result);
  1183. return false;
  1184. }
  1185. }
  1186. return true;
  1187. };
  1188. /**
  1189. * @override
  1190. */
  1191. WebChannelBase.prototype.testConnectionFinished = function(
  1192. testChannel, useChunked) {
  1193. this.channelDebug_.debug('Test Connection Finished');
  1194. // Forward channel will not be used prior to this method is called
  1195. var clientProtocol = testChannel.getClientProtocol();
  1196. if (clientProtocol) {
  1197. this.forwardChannelRequestPool_.applyClientProtocol(clientProtocol);
  1198. }
  1199. this.useChunked_ = this.allowChunkedMode_ && useChunked;
  1200. this.lastStatusCode_ = testChannel.getLastStatusCode();
  1201. this.connectChannel_();
  1202. };
  1203. /**
  1204. * @override
  1205. */
  1206. WebChannelBase.prototype.testConnectionFailure = function(
  1207. testChannel, errorCode) {
  1208. this.channelDebug_.debug('Test Connection Failed');
  1209. this.lastStatusCode_ = testChannel.getLastStatusCode();
  1210. this.signalError_(WebChannelBase.Error.REQUEST_FAILED);
  1211. };
  1212. /**
  1213. * @override
  1214. */
  1215. WebChannelBase.prototype.onRequestData = function(request, responseText) {
  1216. if (this.state_ == WebChannelBase.State.CLOSED ||
  1217. (this.backChannelRequest_ != request &&
  1218. !this.forwardChannelRequestPool_.hasRequest(request))) {
  1219. // either CLOSED or a request we don't know about (perhaps an old request)
  1220. return;
  1221. }
  1222. this.lastStatusCode_ = request.getLastStatusCode();
  1223. if (this.forwardChannelRequestPool_.hasRequest(request) &&
  1224. this.state_ == WebChannelBase.State.OPENED) {
  1225. var response;
  1226. try {
  1227. response = this.wireCodec_.decodeMessage(responseText);
  1228. } catch (ex) {
  1229. response = null;
  1230. }
  1231. if (goog.isArray(response) && response.length == 3) {
  1232. this.handlePostResponse_(/** @type {!Array<?>} */ (response), request);
  1233. } else {
  1234. this.channelDebug_.debug('Bad POST response data returned');
  1235. this.signalError_(WebChannelBase.Error.BAD_RESPONSE);
  1236. }
  1237. } else {
  1238. if (this.backChannelRequest_ == request) {
  1239. this.clearDeadBackchannelTimer_();
  1240. }
  1241. if (!goog.string.isEmptyOrWhitespace(responseText)) {
  1242. var response = this.wireCodec_.decodeMessage(responseText);
  1243. this.onInput_(/** @type {!Array<?>} */ (response), request);
  1244. }
  1245. }
  1246. };
  1247. /**
  1248. * Handles a POST response from the server.
  1249. * @param {Array<number>} responseValues The key value pairs in
  1250. * the POST response.
  1251. * @param {!ChannelRequest} forwardReq The forward channel request that
  1252. * triggers this function call.
  1253. * @private
  1254. */
  1255. WebChannelBase.prototype.handlePostResponse_ = function(
  1256. responseValues, forwardReq) {
  1257. // The first response value is set to 0 if server is missing backchannel.
  1258. if (responseValues[0] == 0) {
  1259. this.handleBackchannelMissing_(forwardReq);
  1260. return;
  1261. }
  1262. this.lastPostResponseArrayId_ = responseValues[1];
  1263. var outstandingArrays = this.lastPostResponseArrayId_ - this.lastArrayId_;
  1264. if (0 < outstandingArrays) {
  1265. var numOutstandingBackchannelBytes = responseValues[2];
  1266. this.channelDebug_.debug(
  1267. numOutstandingBackchannelBytes + ' bytes (in ' + outstandingArrays +
  1268. ' arrays) are outstanding on the BackChannel');
  1269. if (!this.shouldRetryBackChannel_(numOutstandingBackchannelBytes)) {
  1270. return;
  1271. }
  1272. if (!this.deadBackChannelTimerId_) {
  1273. // We expect to receive data within 2 RTTs or we retry the backchannel.
  1274. this.deadBackChannelTimerId_ = requestStats.setTimeout(
  1275. goog.bind(this.onBackChannelDead_, this),
  1276. 2 * WebChannelBase.RTT_ESTIMATE);
  1277. }
  1278. }
  1279. };
  1280. /**
  1281. * Handles a POST response from the server telling us that it has detected that
  1282. * we have no hanging GET connection.
  1283. * @param {!ChannelRequest} forwardReq The forward channel request that
  1284. * triggers this function call.
  1285. * @private
  1286. */
  1287. WebChannelBase.prototype.handleBackchannelMissing_ = function(forwardReq) {
  1288. // As long as the back channel was started before the POST was sent,
  1289. // we should retry the backchannel. We give a slight buffer of RTT_ESTIMATE
  1290. // so as not to excessively retry the backchannel
  1291. this.channelDebug_.debug('Server claims our backchannel is missing.');
  1292. if (this.backChannelTimerId_) {
  1293. this.channelDebug_.debug('But we are currently starting the request.');
  1294. return;
  1295. } else if (!this.backChannelRequest_) {
  1296. this.channelDebug_.warning('We do not have a BackChannel established');
  1297. } else if (
  1298. this.backChannelRequest_.getRequestStartTime() +
  1299. WebChannelBase.RTT_ESTIMATE <
  1300. forwardReq.getRequestStartTime()) {
  1301. this.clearDeadBackchannelTimer_();
  1302. this.backChannelRequest_.cancel();
  1303. this.backChannelRequest_ = null;
  1304. } else {
  1305. return;
  1306. }
  1307. this.maybeRetryBackChannel_();
  1308. requestStats.notifyStatEvent(requestStats.Stat.BACKCHANNEL_MISSING);
  1309. };
  1310. /**
  1311. * Determines whether we should start the process of retrying a possibly
  1312. * dead backchannel.
  1313. * @param {number} outstandingBytes The number of bytes for which the server has
  1314. * not yet received acknowledgement.
  1315. * @return {boolean} Whether to start the backchannel retry timer.
  1316. * @private
  1317. */
  1318. WebChannelBase.prototype.shouldRetryBackChannel_ = function(outstandingBytes) {
  1319. // Not too many outstanding bytes, not buffered and not after a retry.
  1320. return outstandingBytes <
  1321. WebChannelBase.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF &&
  1322. !this.isBuffered() && this.backChannelRetryCount_ == 0;
  1323. };
  1324. /**
  1325. * Decides which host prefix should be used, if any. If there is a handler,
  1326. * allows the handler to validate a host prefix provided by the server, and
  1327. * optionally override it.
  1328. * @param {?string} serverHostPrefix The host prefix provided by the server.
  1329. * @return {?string} The host prefix to actually use, if any. Will return null
  1330. * if the use of host prefixes was disabled via setAllowHostPrefix().
  1331. * @override
  1332. */
  1333. WebChannelBase.prototype.correctHostPrefix = function(serverHostPrefix) {
  1334. if (this.allowHostPrefix_) {
  1335. if (this.handler_) {
  1336. return this.handler_.correctHostPrefix(serverHostPrefix);
  1337. }
  1338. return serverHostPrefix;
  1339. }
  1340. return null;
  1341. };
  1342. /**
  1343. * Handles the timer that indicates that our backchannel is no longer able to
  1344. * successfully receive data from the server.
  1345. * @private
  1346. */
  1347. WebChannelBase.prototype.onBackChannelDead_ = function() {
  1348. if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {
  1349. this.deadBackChannelTimerId_ = null;
  1350. this.backChannelRequest_.cancel();
  1351. this.backChannelRequest_ = null;
  1352. this.maybeRetryBackChannel_();
  1353. requestStats.notifyStatEvent(requestStats.Stat.BACKCHANNEL_DEAD);
  1354. }
  1355. };
  1356. /**
  1357. * Clears the timer that indicates that our backchannel is no longer able to
  1358. * successfully receive data from the server.
  1359. * @private
  1360. */
  1361. WebChannelBase.prototype.clearDeadBackchannelTimer_ = function() {
  1362. if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {
  1363. goog.global.clearTimeout(this.deadBackChannelTimerId_);
  1364. this.deadBackChannelTimerId_ = null;
  1365. }
  1366. };
  1367. /**
  1368. * Returns whether or not the given error/status combination is fatal or not.
  1369. * On fatal errors we immediately close the session rather than retrying the
  1370. * failed request.
  1371. * @param {?ChannelRequest.Error} error The error code for the
  1372. * failed request.
  1373. * @param {number} statusCode The last HTTP status code.
  1374. * @return {boolean} Whether or not the error is fatal.
  1375. * @private
  1376. */
  1377. WebChannelBase.isFatalError_ = function(error, statusCode) {
  1378. return error == ChannelRequest.Error.UNKNOWN_SESSION_ID ||
  1379. (error == ChannelRequest.Error.STATUS && statusCode > 0);
  1380. };
  1381. /**
  1382. * @override
  1383. */
  1384. WebChannelBase.prototype.onRequestComplete = function(request) {
  1385. this.channelDebug_.debug('Request complete');
  1386. var type;
  1387. if (this.backChannelRequest_ == request) {
  1388. this.clearDeadBackchannelTimer_();
  1389. this.backChannelRequest_ = null;
  1390. type = WebChannelBase.ChannelType_.BACK_CHANNEL;
  1391. } else if (this.forwardChannelRequestPool_.hasRequest(request)) {
  1392. this.forwardChannelRequestPool_.removeRequest(request);
  1393. type = WebChannelBase.ChannelType_.FORWARD_CHANNEL;
  1394. } else {
  1395. // return if it was an old request from a previous session
  1396. return;
  1397. }
  1398. this.lastStatusCode_ = request.getLastStatusCode();
  1399. if (this.state_ == WebChannelBase.State.CLOSED) {
  1400. return;
  1401. }
  1402. if (request.getSuccess()) {
  1403. // Yay!
  1404. if (type == WebChannelBase.ChannelType_.FORWARD_CHANNEL) {
  1405. var size = request.getPostData() ? request.getPostData().length : 0;
  1406. requestStats.notifyTimingEvent(
  1407. size, goog.now() - request.getRequestStartTime(),
  1408. this.forwardChannelRetryCount_);
  1409. this.ensureForwardChannel_();
  1410. this.onSuccess_();
  1411. this.pendingMaps_.length = 0;
  1412. } else { // i.e., back-channel
  1413. this.ensureBackChannel_();
  1414. }
  1415. return;
  1416. }
  1417. // Else unsuccessful. Fall through.
  1418. var lastError = request.getLastError();
  1419. if (!WebChannelBase.isFatalError_(lastError, this.lastStatusCode_)) {
  1420. // Maybe retry.
  1421. this.channelDebug_.debug(
  1422. 'Maybe retrying, last error: ' +
  1423. ChannelRequest.errorStringFromCode(lastError, this.lastStatusCode_));
  1424. if (type == WebChannelBase.ChannelType_.FORWARD_CHANNEL) {
  1425. if (this.maybeRetryForwardChannel_(request)) {
  1426. return;
  1427. }
  1428. }
  1429. if (type == WebChannelBase.ChannelType_.BACK_CHANNEL) {
  1430. if (this.maybeRetryBackChannel_()) {
  1431. return;
  1432. }
  1433. }
  1434. // Else exceeded max retries. Fall through.
  1435. this.channelDebug_.debug('Exceeded max number of retries');
  1436. } else {
  1437. // Else fatal error. Fall through and mark the pending maps as failed.
  1438. this.channelDebug_.debug('Not retrying due to error type');
  1439. }
  1440. // Can't save this session. :(
  1441. this.channelDebug_.debug('Error: HTTP request failed');
  1442. switch (lastError) {
  1443. case ChannelRequest.Error.NO_DATA:
  1444. this.signalError_(WebChannelBase.Error.NO_DATA);
  1445. break;
  1446. case ChannelRequest.Error.BAD_DATA:
  1447. this.signalError_(WebChannelBase.Error.BAD_DATA);
  1448. break;
  1449. case ChannelRequest.Error.UNKNOWN_SESSION_ID:
  1450. this.signalError_(WebChannelBase.Error.UNKNOWN_SESSION_ID);
  1451. break;
  1452. default:
  1453. this.signalError_(WebChannelBase.Error.REQUEST_FAILED);
  1454. break;
  1455. }
  1456. };
  1457. /**
  1458. * @param {number} retryCount Number of retries so far.
  1459. * @return {number} Time in ms before firing next retry request.
  1460. * @private
  1461. */
  1462. WebChannelBase.prototype.getRetryTime_ = function(retryCount) {
  1463. var retryTime = this.baseRetryDelayMs_ +
  1464. Math.floor(Math.random() * this.retryDelaySeedMs_);
  1465. if (!this.isActive()) {
  1466. this.channelDebug_.debug('Inactive channel');
  1467. retryTime = retryTime * WebChannelBase.INACTIVE_CHANNEL_RETRY_FACTOR;
  1468. }
  1469. // Backoff for subsequent retries
  1470. retryTime *= retryCount;
  1471. return retryTime;
  1472. };
  1473. /**
  1474. * @param {number} baseDelayMs The base part of the retry delay, in ms.
  1475. * @param {number} delaySeedMs A random delay between 0 and this is added to
  1476. * the base part.
  1477. */
  1478. WebChannelBase.prototype.setRetryDelay = function(baseDelayMs, delaySeedMs) {
  1479. this.baseRetryDelayMs_ = baseDelayMs;
  1480. this.retryDelaySeedMs_ = delaySeedMs;
  1481. };
  1482. /**
  1483. * Apply any handshake control headers.
  1484. * @param {!ChannelRequest} request The underlying request object
  1485. * @private
  1486. */
  1487. WebChannelBase.prototype.applyControlHeaders_ = function(request) {
  1488. if (!this.backgroundChannelTest_) {
  1489. return;
  1490. }
  1491. var xhr = request.getXhr();
  1492. if (xhr) {
  1493. var clientProtocol =
  1494. xhr.getStreamingResponseHeader(WebChannel.X_CLIENT_WIRE_PROTOCOL);
  1495. if (clientProtocol) {
  1496. this.forwardChannelRequestPool_.applyClientProtocol(clientProtocol);
  1497. }
  1498. if (this.getHttpSessionIdParam()) {
  1499. var httpSessionIdHeader =
  1500. xhr.getStreamingResponseHeader(WebChannel.X_HTTP_SESSION_ID);
  1501. if (httpSessionIdHeader) {
  1502. this.setHttpSessionId(httpSessionIdHeader);
  1503. // update the cached uri
  1504. var httpSessionIdParam = this.getHttpSessionIdParam();
  1505. this.forwardChannelUri_.setParameterValue(
  1506. /** @type {string} */ (httpSessionIdParam), // never null
  1507. httpSessionIdHeader);
  1508. } else {
  1509. this.channelDebug_.warning(
  1510. 'Missing X_HTTP_SESSION_ID in the handshake response');
  1511. }
  1512. }
  1513. }
  1514. };
  1515. /**
  1516. * Processes the data returned by the server.
  1517. * @param {!Array<!Array<?>>} respArray The response array returned
  1518. * by the server.
  1519. * @param {!ChannelRequest} request The underlying request object
  1520. * @private
  1521. */
  1522. WebChannelBase.prototype.onInput_ = function(respArray, request) {
  1523. var batch =
  1524. this.handler_ && this.handler_.channelHandleMultipleArrays ? [] : null;
  1525. for (var i = 0; i < respArray.length; i++) {
  1526. var nextArray = respArray[i];
  1527. this.lastArrayId_ = nextArray[0];
  1528. nextArray = nextArray[1];
  1529. if (this.state_ == WebChannelBase.State.OPENING) {
  1530. if (nextArray[0] == 'c') {
  1531. this.sid_ = nextArray[1];
  1532. this.hostPrefix_ = this.correctHostPrefix(nextArray[2]);
  1533. var negotiatedVersion = nextArray[3];
  1534. if (goog.isDefAndNotNull(negotiatedVersion)) {
  1535. this.channelVersion_ = negotiatedVersion;
  1536. this.channelDebug_.info('VER=' + this.channelVersion_);
  1537. }
  1538. var negotiatedServerVersion = nextArray[4];
  1539. if (goog.isDefAndNotNull(negotiatedServerVersion)) {
  1540. this.serverVersion_ = negotiatedServerVersion;
  1541. this.channelDebug_.info('SVER=' + this.serverVersion_);
  1542. }
  1543. this.applyControlHeaders_(request);
  1544. this.state_ = WebChannelBase.State.OPENED;
  1545. if (this.handler_) {
  1546. this.handler_.channelOpened(this);
  1547. }
  1548. this.backChannelUri_ = this.getBackChannelUri(
  1549. this.hostPrefix_, /** @type {string} */ (this.path_));
  1550. // Open connection to receive data
  1551. this.ensureBackChannel_();
  1552. } else if (nextArray[0] == 'stop' || nextArray[0] == 'close') {
  1553. // treat close also as an abort
  1554. this.signalError_(WebChannelBase.Error.STOP);
  1555. }
  1556. } else if (this.state_ == WebChannelBase.State.OPENED) {
  1557. if (nextArray[0] == 'stop' || nextArray[0] == 'close') {
  1558. if (batch && !goog.array.isEmpty(batch)) {
  1559. this.handler_.channelHandleMultipleArrays(this, batch);
  1560. batch.length = 0;
  1561. }
  1562. if (nextArray[0] == 'stop') {
  1563. this.signalError_(WebChannelBase.Error.STOP);
  1564. } else {
  1565. this.disconnect();
  1566. }
  1567. } else if (nextArray[0] == 'noop') {
  1568. // ignore - noop to keep connection happy
  1569. } else {
  1570. if (batch) {
  1571. batch.push(nextArray);
  1572. } else if (this.handler_) {
  1573. this.handler_.channelHandleArray(this, nextArray);
  1574. }
  1575. }
  1576. // We have received useful data on the back-channel, so clear its retry
  1577. // count. We do this because back-channels by design do not complete
  1578. // quickly, so on a flaky connection we could have many fail to complete
  1579. // fully but still deliver a lot of data before they fail. We don't want
  1580. // to count such failures towards the retry limit, because we don't want
  1581. // to give up on a session if we can still receive data.
  1582. this.backChannelRetryCount_ = 0;
  1583. }
  1584. }
  1585. if (batch && !goog.array.isEmpty(batch)) {
  1586. this.handler_.channelHandleMultipleArrays(this, batch);
  1587. }
  1588. };
  1589. /**
  1590. * Helper to ensure the channel is in the expected state.
  1591. * @param {...number} var_args The channel must be in one of the indicated
  1592. * states.
  1593. * @private
  1594. */
  1595. WebChannelBase.prototype.ensureInState_ = function(var_args) {
  1596. goog.asserts.assert(
  1597. goog.array.contains(arguments, this.state_),
  1598. 'Unexpected channel state: %s', this.state_);
  1599. };
  1600. /**
  1601. * Signals an error has occurred.
  1602. * @param {WebChannelBase.Error} error The error code for the failure.
  1603. * @private
  1604. */
  1605. WebChannelBase.prototype.signalError_ = function(error) {
  1606. this.channelDebug_.info('Error code ' + error);
  1607. if (error == WebChannelBase.Error.REQUEST_FAILED) {
  1608. // Create a separate Internet connection to check
  1609. // if it's a server error or user's network error.
  1610. var imageUri = null;
  1611. if (this.handler_) {
  1612. imageUri = this.handler_.getNetworkTestImageUri(this);
  1613. }
  1614. netUtils.testNetwork(goog.bind(this.testNetworkCallback_, this), imageUri);
  1615. } else {
  1616. requestStats.notifyStatEvent(requestStats.Stat.ERROR_OTHER);
  1617. }
  1618. this.onError_(error);
  1619. };
  1620. /**
  1621. * Callback for netUtils.testNetwork during error handling.
  1622. * @param {boolean} networkUp Whether the network is up.
  1623. * @private
  1624. */
  1625. WebChannelBase.prototype.testNetworkCallback_ = function(networkUp) {
  1626. if (networkUp) {
  1627. this.channelDebug_.info('Successfully pinged google.com');
  1628. requestStats.notifyStatEvent(requestStats.Stat.ERROR_OTHER);
  1629. } else {
  1630. this.channelDebug_.info('Failed to ping google.com');
  1631. requestStats.notifyStatEvent(requestStats.Stat.ERROR_NETWORK);
  1632. // Do not call onError_ again to eliminate duplicated Error events.
  1633. }
  1634. };
  1635. /**
  1636. * Called when messages have been successfully sent from the queue.
  1637. * @private
  1638. */
  1639. WebChannelBase.prototype.onSuccess_ = function() {
  1640. // TODO(user): optimize for request pool (>1)
  1641. if (this.handler_) {
  1642. this.handler_.channelSuccess(this, this.pendingMaps_);
  1643. }
  1644. };
  1645. /**
  1646. * Called when we've determined the final error for a channel. It closes the
  1647. * notifiers the handler of the error and closes the channel.
  1648. * @param {WebChannelBase.Error} error The error code for the failure.
  1649. * @private
  1650. */
  1651. WebChannelBase.prototype.onError_ = function(error) {
  1652. this.channelDebug_.debug('HttpChannel: error - ' + error);
  1653. this.state_ = WebChannelBase.State.CLOSED;
  1654. if (this.handler_) {
  1655. this.handler_.channelError(this, error);
  1656. }
  1657. this.onClose_();
  1658. this.cancelRequests_();
  1659. };
  1660. /**
  1661. * Called when the channel has been closed. It notifiers the handler of the
  1662. * event, and reports any pending or undelivered maps.
  1663. * @private
  1664. */
  1665. WebChannelBase.prototype.onClose_ = function() {
  1666. this.state_ = WebChannelBase.State.CLOSED;
  1667. this.lastStatusCode_ = -1;
  1668. if (this.handler_) {
  1669. if (this.pendingMaps_.length == 0 && this.outgoingMaps_.length == 0) {
  1670. this.handler_.channelClosed(this);
  1671. } else {
  1672. this.channelDebug_.debug(
  1673. 'Number of undelivered maps' +
  1674. ', pending: ' + this.pendingMaps_.length + ', outgoing: ' +
  1675. this.outgoingMaps_.length);
  1676. var copyOfPendingMaps = goog.array.clone(this.pendingMaps_);
  1677. var copyOfUndeliveredMaps = goog.array.clone(this.outgoingMaps_);
  1678. this.pendingMaps_.length = 0;
  1679. this.outgoingMaps_.length = 0;
  1680. this.handler_.channelClosed(
  1681. this, copyOfPendingMaps, copyOfUndeliveredMaps);
  1682. }
  1683. }
  1684. };
  1685. /**
  1686. * @override
  1687. */
  1688. WebChannelBase.prototype.getForwardChannelUri = function(path) {
  1689. var uri = this.createDataUri(null, path);
  1690. this.channelDebug_.debug('GetForwardChannelUri: ' + uri);
  1691. return uri;
  1692. };
  1693. /**
  1694. * @override
  1695. */
  1696. WebChannelBase.prototype.getConnectionState = function() {
  1697. return this.connState_;
  1698. };
  1699. /**
  1700. * @override
  1701. */
  1702. WebChannelBase.prototype.getBackChannelUri = function(hostPrefix, path) {
  1703. var uri = this.createDataUri(
  1704. this.shouldUseSecondaryDomains() ? hostPrefix : null, path);
  1705. this.channelDebug_.debug('GetBackChannelUri: ' + uri);
  1706. return uri;
  1707. };
  1708. /**
  1709. * @override
  1710. */
  1711. WebChannelBase.prototype.createDataUri = function(
  1712. hostPrefix, path, opt_overridePort) {
  1713. var uri = goog.Uri.parse(path);
  1714. var uriAbsolute = (uri.getDomain() != '');
  1715. if (uriAbsolute) {
  1716. if (hostPrefix) {
  1717. uri.setDomain(hostPrefix + '.' + uri.getDomain());
  1718. }
  1719. uri.setPort(opt_overridePort || uri.getPort());
  1720. } else {
  1721. var locationPage = goog.global.location;
  1722. var hostName;
  1723. if (hostPrefix) {
  1724. hostName = hostPrefix + '.' + locationPage.hostname;
  1725. } else {
  1726. hostName = locationPage.hostname;
  1727. }
  1728. var port = opt_overridePort || locationPage.port;
  1729. uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path);
  1730. }
  1731. if (this.extraParams_) {
  1732. goog.object.forEach(this.extraParams_, function(value, key) {
  1733. uri.setParameterValue(key, value);
  1734. });
  1735. }
  1736. var param = this.getHttpSessionIdParam();
  1737. var value = this.getHttpSessionId();
  1738. if (param && value) {
  1739. uri.setParameterValue(param, value);
  1740. }
  1741. // Add the protocol version to the URI.
  1742. uri.setParameterValue('VER', this.channelVersion_);
  1743. // Add the reconnect parameters.
  1744. this.addAdditionalParams_(uri);
  1745. return uri;
  1746. };
  1747. /**
  1748. * @override
  1749. */
  1750. WebChannelBase.prototype.createXhrIo = function(hostPrefix) {
  1751. if (hostPrefix && !this.supportsCrossDomainXhrs_) {
  1752. throw Error('Can\'t create secondary domain capable XhrIo object.');
  1753. }
  1754. var xhr = new goog.net.XhrIo();
  1755. xhr.setWithCredentials(this.supportsCrossDomainXhrs_);
  1756. return xhr;
  1757. };
  1758. /**
  1759. * @override
  1760. */
  1761. WebChannelBase.prototype.isActive = function() {
  1762. return !!this.handler_ && this.handler_.isActive(this);
  1763. };
  1764. /**
  1765. * @override
  1766. */
  1767. WebChannelBase.prototype.shouldUseSecondaryDomains = function() {
  1768. return this.supportsCrossDomainXhrs_;
  1769. };
  1770. /**
  1771. * A LogSaver that can be used to accumulate all the debug logs so they
  1772. * can be sent to the server when a problem is detected.
  1773. */
  1774. WebChannelBase.LogSaver = {};
  1775. /**
  1776. * Buffer for accumulating the debug log
  1777. * @type {goog.structs.CircularBuffer}
  1778. * @private
  1779. */
  1780. WebChannelBase.LogSaver.buffer_ = new goog.structs.CircularBuffer(1000);
  1781. /**
  1782. * Whether we're currently accumulating the debug log.
  1783. * @type {boolean}
  1784. * @private
  1785. */
  1786. WebChannelBase.LogSaver.enabled_ = false;
  1787. /**
  1788. * Formatter for saving logs.
  1789. * @type {goog.debug.Formatter}
  1790. * @private
  1791. */
  1792. WebChannelBase.LogSaver.formatter_ = new goog.debug.TextFormatter();
  1793. /**
  1794. * Returns whether the LogSaver is enabled.
  1795. * @return {boolean} Whether saving is enabled or disabled.
  1796. */
  1797. WebChannelBase.LogSaver.isEnabled = function() {
  1798. return WebChannelBase.LogSaver.enabled_;
  1799. };
  1800. /**
  1801. * Enables of disables the LogSaver.
  1802. * @param {boolean} enable Whether to enable or disable saving.
  1803. */
  1804. WebChannelBase.LogSaver.setEnabled = function(enable) {
  1805. if (enable == WebChannelBase.LogSaver.enabled_) {
  1806. return;
  1807. }
  1808. var fn = WebChannelBase.LogSaver.addLogRecord;
  1809. var logger = goog.log.getLogger('goog.net');
  1810. if (enable) {
  1811. goog.log.addHandler(logger, fn);
  1812. } else {
  1813. goog.log.removeHandler(logger, fn);
  1814. }
  1815. };
  1816. /**
  1817. * Adds a log record.
  1818. * @param {goog.log.LogRecord} logRecord the LogRecord.
  1819. */
  1820. WebChannelBase.LogSaver.addLogRecord = function(logRecord) {
  1821. WebChannelBase.LogSaver.buffer_.add(
  1822. WebChannelBase.LogSaver.formatter_.formatRecord(logRecord));
  1823. };
  1824. /**
  1825. * Returns the log as a single string.
  1826. * @return {string} The log as a single string.
  1827. */
  1828. WebChannelBase.LogSaver.getBuffer = function() {
  1829. return WebChannelBase.LogSaver.buffer_.getValues().join('');
  1830. };
  1831. /**
  1832. * Clears the buffer
  1833. */
  1834. WebChannelBase.LogSaver.clearBuffer = function() {
  1835. WebChannelBase.LogSaver.buffer_.clear();
  1836. };
  1837. /**
  1838. * Abstract base class for the channel handler
  1839. * @constructor
  1840. * @struct
  1841. */
  1842. WebChannelBase.Handler = function() {};
  1843. /**
  1844. * Callback handler for when a batch of response arrays is received from the
  1845. * server. When null, batched dispatching is disabled.
  1846. * @type {?function(!WebChannelBase, !Array<!Array<?>>)}
  1847. */
  1848. WebChannelBase.Handler.prototype.channelHandleMultipleArrays = null;
  1849. /**
  1850. * Whether it's okay to make a request to the server. A handler can return
  1851. * false if the channel should fail. For example, if the user has logged out,
  1852. * the handler may want all requests to fail immediately.
  1853. * @param {WebChannelBase} channel The channel.
  1854. * @return {WebChannelBase.Error} An error code. The code should
  1855. * return WebChannelBase.Error.OK to indicate it's okay. Any other
  1856. * error code will cause a failure.
  1857. */
  1858. WebChannelBase.Handler.prototype.okToMakeRequest = function(channel) {
  1859. return WebChannelBase.Error.OK;
  1860. };
  1861. /**
  1862. * Indicates the WebChannel has successfully negotiated with the server
  1863. * and can now send and receive data.
  1864. * @param {WebChannelBase} channel The channel.
  1865. */
  1866. WebChannelBase.Handler.prototype.channelOpened = function(channel) {};
  1867. /**
  1868. * New input is available for the application to process.
  1869. *
  1870. * @param {WebChannelBase} channel The channel.
  1871. * @param {Array<?>} array The data array.
  1872. */
  1873. WebChannelBase.Handler.prototype.channelHandleArray = function(channel, array) {
  1874. };
  1875. /**
  1876. * Indicates maps were successfully sent on the channel.
  1877. *
  1878. * @param {WebChannelBase} channel The channel.
  1879. * @param {Array<Wire.QueuedMap>} deliveredMaps The
  1880. * array of maps that have been delivered to the server. This is a direct
  1881. * reference to the internal array, so a copy should be made
  1882. * if the caller desires a reference to the data.
  1883. */
  1884. WebChannelBase.Handler.prototype.channelSuccess = function(
  1885. channel, deliveredMaps) {};
  1886. /**
  1887. * Indicates an error occurred on the WebChannel.
  1888. *
  1889. * @param {WebChannelBase} channel The channel.
  1890. * @param {WebChannelBase.Error} error The error code.
  1891. */
  1892. WebChannelBase.Handler.prototype.channelError = function(channel, error) {};
  1893. /**
  1894. * Indicates the WebChannel is closed. Also notifies about which maps,
  1895. * if any, that may not have been delivered to the server.
  1896. * @param {WebChannelBase} channel The channel.
  1897. * @param {Array<Wire.QueuedMap>=} opt_pendingMaps The
  1898. * array of pending maps, which may or may not have been delivered to the
  1899. * server.
  1900. * @param {Array<Wire.QueuedMap>=} opt_undeliveredMaps
  1901. * The array of undelivered maps, which have definitely not been delivered
  1902. * to the server.
  1903. */
  1904. WebChannelBase.Handler.prototype.channelClosed = function(
  1905. channel, opt_pendingMaps, opt_undeliveredMaps) {};
  1906. /**
  1907. * Gets any parameters that should be added at the time another connection is
  1908. * made to the server.
  1909. * @param {WebChannelBase} channel The channel.
  1910. * @return {!Object} Extra parameter keys and values to add to the requests.
  1911. */
  1912. WebChannelBase.Handler.prototype.getAdditionalParams = function(channel) {
  1913. return {};
  1914. };
  1915. /**
  1916. * Gets the URI of an image that can be used to test network connectivity.
  1917. * @param {WebChannelBase} channel The channel.
  1918. * @return {goog.Uri?} A custom URI to load for the network test.
  1919. */
  1920. WebChannelBase.Handler.prototype.getNetworkTestImageUri = function(channel) {
  1921. return null;
  1922. };
  1923. /**
  1924. * Gets whether this channel is currently active. This is used to determine the
  1925. * length of time to wait before retrying.
  1926. * @param {WebChannelBase} channel The channel.
  1927. * @return {boolean} Whether the channel is currently active.
  1928. */
  1929. WebChannelBase.Handler.prototype.isActive = function(channel) {
  1930. return true;
  1931. };
  1932. /**
  1933. * Called by the channel if enumeration of the map throws an exception.
  1934. * @param {WebChannelBase} channel The channel.
  1935. * @param {Object} map The map that can't be enumerated.
  1936. */
  1937. WebChannelBase.Handler.prototype.badMapError = function(channel, map) {};
  1938. /**
  1939. * Allows the handler to override a host prefix provided by the server. Will
  1940. * be called whenever the channel has received such a prefix and is considering
  1941. * its use.
  1942. * @param {?string} serverHostPrefix The host prefix provided by the server.
  1943. * @return {?string} The host prefix the client should use.
  1944. */
  1945. WebChannelBase.Handler.prototype.correctHostPrefix = function(
  1946. serverHostPrefix) {
  1947. return serverHostPrefix;
  1948. };
  1949. }); // goog.scope