browserchannel.js 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  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 Definition of the BrowserChannel class. A BrowserChannel
  16. * simulates a bidirectional socket over HTTP. It is the basis of the
  17. * Gmail Chat IM connections to the server.
  18. *
  19. * Typical usage will look like
  20. * var handler = [handler object];
  21. * var channel = new BrowserChannel(clientVersion);
  22. * channel.setHandler(handler);
  23. * channel.connect('channel/test', 'channel/bind');
  24. *
  25. * See goog.net.BrowserChannel.Handler for the handler interface.
  26. *
  27. */
  28. goog.provide('goog.net.BrowserChannel');
  29. goog.provide('goog.net.BrowserChannel.Error');
  30. goog.provide('goog.net.BrowserChannel.Event');
  31. goog.provide('goog.net.BrowserChannel.Handler');
  32. goog.provide('goog.net.BrowserChannel.LogSaver');
  33. goog.provide('goog.net.BrowserChannel.QueuedMap');
  34. goog.provide('goog.net.BrowserChannel.ServerReachability');
  35. goog.provide('goog.net.BrowserChannel.ServerReachabilityEvent');
  36. goog.provide('goog.net.BrowserChannel.Stat');
  37. goog.provide('goog.net.BrowserChannel.StatEvent');
  38. goog.provide('goog.net.BrowserChannel.State');
  39. goog.provide('goog.net.BrowserChannel.TimingEvent');
  40. goog.require('goog.Uri');
  41. goog.require('goog.array');
  42. goog.require('goog.asserts');
  43. goog.require('goog.debug.TextFormatter');
  44. goog.require('goog.events.Event');
  45. goog.require('goog.events.EventTarget');
  46. goog.require('goog.json');
  47. goog.require('goog.json.NativeJsonProcessor');
  48. goog.require('goog.log');
  49. goog.require('goog.net.BrowserTestChannel');
  50. goog.require('goog.net.ChannelDebug');
  51. goog.require('goog.net.ChannelRequest');
  52. goog.require('goog.net.XhrIo');
  53. goog.require('goog.net.tmpnetwork');
  54. goog.require('goog.object');
  55. goog.require('goog.string');
  56. goog.require('goog.structs');
  57. goog.require('goog.structs.CircularBuffer');
  58. /**
  59. * Encapsulates the logic for a single BrowserChannel.
  60. *
  61. * @param {string=} opt_clientVersion An application-specific version number
  62. * that is sent to the server when connected.
  63. * @param {Array<string>=} opt_firstTestResults Previously determined results
  64. * of the first browser channel test.
  65. * @param {boolean=} opt_secondTestResults Previously determined results
  66. * of the second browser channel test.
  67. * @param {boolean=} opt_asyncTest Whether to perform the test requests
  68. * asynchronously. While the test is performed, we'll assume the worst
  69. * (connection is buffered), in order to avoid delaying the connection
  70. * until the test is performed.
  71. * @constructor
  72. */
  73. goog.net.BrowserChannel = function(
  74. opt_clientVersion, opt_firstTestResults, opt_secondTestResults,
  75. opt_asyncTest) {
  76. /**
  77. * The application specific version that is passed to the server.
  78. * @type {?string}
  79. * @private
  80. */
  81. this.clientVersion_ = opt_clientVersion || null;
  82. /**
  83. * The current state of the BrowserChannel. It should be one of the
  84. * goog.net.BrowserChannel.State constants.
  85. * @type {!goog.net.BrowserChannel.State}
  86. * @private
  87. */
  88. this.state_ = goog.net.BrowserChannel.State.INIT;
  89. /**
  90. * An array of queued maps that need to be sent to the server.
  91. * @type {Array<goog.net.BrowserChannel.QueuedMap>}
  92. * @private
  93. */
  94. this.outgoingMaps_ = [];
  95. /**
  96. * An array of dequeued maps that we have either received a non-successful
  97. * response for, or no response at all, and which therefore may or may not
  98. * have been received by the server.
  99. * @type {Array<goog.net.BrowserChannel.QueuedMap>}
  100. * @private
  101. */
  102. this.pendingMaps_ = [];
  103. /**
  104. * The channel debug used for browserchannel logging
  105. * @type {!goog.net.ChannelDebug}
  106. * @private
  107. */
  108. this.channelDebug_ = new goog.net.ChannelDebug();
  109. /**
  110. * Parser for a response payload. The parser should return an array.
  111. * @type {!goog.string.Parser}
  112. * @private
  113. */
  114. this.parser_ = new goog.json.NativeJsonProcessor();
  115. /**
  116. * An array of results for the first browser channel test call.
  117. * @type {Array<string>}
  118. * @private
  119. */
  120. this.firstTestResults_ = opt_firstTestResults || null;
  121. /**
  122. * The results of the second browser channel test. True implies the
  123. * connection is buffered, False means unbuffered, null means that
  124. * the results are not available.
  125. * @private
  126. */
  127. this.secondTestResults_ = goog.isDefAndNotNull(opt_secondTestResults) ?
  128. opt_secondTestResults :
  129. null;
  130. /**
  131. * Whether to perform the test requests asynchronously. While the test is
  132. * performed, we'll assume the worst (connection is buffered), in order to
  133. * avoid delaying the connection until the test is performed.
  134. * @private {boolean}
  135. */
  136. this.asyncTest_ = opt_asyncTest || false;
  137. };
  138. /**
  139. * Simple container class for a (mapId, map) pair.
  140. * @param {number} mapId The id for this map.
  141. * @param {Object|goog.structs.Map} map The map itself.
  142. * @param {Object=} opt_context The context associated with the map.
  143. * @constructor
  144. * @final
  145. */
  146. goog.net.BrowserChannel.QueuedMap = function(mapId, map, opt_context) {
  147. /**
  148. * The id for this map.
  149. * @type {number}
  150. */
  151. this.mapId = mapId;
  152. /**
  153. * The map itself.
  154. * @type {Object}
  155. */
  156. this.map = map;
  157. /**
  158. * The context for the map.
  159. * @type {Object}
  160. */
  161. this.context = opt_context || null;
  162. };
  163. /**
  164. * Extra HTTP headers to add to all the requests sent to the server.
  165. * @type {Object}
  166. * @private
  167. */
  168. goog.net.BrowserChannel.prototype.extraHeaders_ = null;
  169. /**
  170. * Extra parameters to add to all the requests sent to the server.
  171. * @type {Object}
  172. * @private
  173. */
  174. goog.net.BrowserChannel.prototype.extraParams_ = null;
  175. /**
  176. * The current ChannelRequest object for the forwardchannel.
  177. * @type {goog.net.ChannelRequest?}
  178. * @private
  179. */
  180. goog.net.BrowserChannel.prototype.forwardChannelRequest_ = null;
  181. /**
  182. * The ChannelRequest object for the backchannel.
  183. * @type {goog.net.ChannelRequest?}
  184. * @private
  185. */
  186. goog.net.BrowserChannel.prototype.backChannelRequest_ = null;
  187. /**
  188. * The relative path (in the context of the the page hosting the browser
  189. * channel) for making requests to the server.
  190. * @type {?string}
  191. * @private
  192. */
  193. goog.net.BrowserChannel.prototype.path_ = null;
  194. /**
  195. * The absolute URI for the forwardchannel request.
  196. * @type {goog.Uri}
  197. * @private
  198. */
  199. goog.net.BrowserChannel.prototype.forwardChannelUri_ = null;
  200. /**
  201. * The absolute URI for the backchannel request.
  202. * @type {goog.Uri}
  203. * @private
  204. */
  205. goog.net.BrowserChannel.prototype.backChannelUri_ = null;
  206. /**
  207. * A subdomain prefix for using a subdomain in IE for the backchannel
  208. * requests.
  209. * @type {?string}
  210. * @private
  211. */
  212. goog.net.BrowserChannel.prototype.hostPrefix_ = null;
  213. /**
  214. * Whether we allow the use of a subdomain in IE for the backchannel requests.
  215. * @private
  216. */
  217. goog.net.BrowserChannel.prototype.allowHostPrefix_ = true;
  218. /**
  219. * The next id to use for the RID (request identifier) parameter. This
  220. * identifier uniquely identifies the forward channel request.
  221. * @type {number}
  222. * @private
  223. */
  224. goog.net.BrowserChannel.prototype.nextRid_ = 0;
  225. /**
  226. * The id to use for the next outgoing map. This identifier uniquely
  227. * identifies a sent map.
  228. * @type {number}
  229. * @private
  230. */
  231. goog.net.BrowserChannel.prototype.nextMapId_ = 0;
  232. /**
  233. * Whether to fail forward-channel requests after one try, or after a few tries.
  234. * @type {boolean}
  235. * @private
  236. */
  237. goog.net.BrowserChannel.prototype.failFast_ = false;
  238. /**
  239. * The handler that receive callbacks for state changes and data.
  240. * @type {goog.net.BrowserChannel.Handler}
  241. * @private
  242. */
  243. goog.net.BrowserChannel.prototype.handler_ = null;
  244. /**
  245. * Timer identifier for asynchronously making a forward channel request.
  246. * @type {?number}
  247. * @private
  248. */
  249. goog.net.BrowserChannel.prototype.forwardChannelTimerId_ = null;
  250. /**
  251. * Timer identifier for asynchronously making a back channel request.
  252. * @type {?number}
  253. * @private
  254. */
  255. goog.net.BrowserChannel.prototype.backChannelTimerId_ = null;
  256. /**
  257. * Timer identifier for the timer that waits for us to retry the backchannel in
  258. * the case where it is dead and no longer receiving data.
  259. * @type {?number}
  260. * @private
  261. */
  262. goog.net.BrowserChannel.prototype.deadBackChannelTimerId_ = null;
  263. /**
  264. * The BrowserTestChannel object which encapsulates the logic for determining
  265. * interesting network conditions about the client.
  266. * @type {goog.net.BrowserTestChannel?}
  267. * @private
  268. */
  269. goog.net.BrowserChannel.prototype.connectionTest_ = null;
  270. /**
  271. * Whether the client's network conditions can support chunked responses.
  272. * @type {?boolean}
  273. * @private
  274. */
  275. goog.net.BrowserChannel.prototype.useChunked_ = null;
  276. /**
  277. * Whether chunked mode is allowed. In certain debugging situations, it's
  278. * useful to disable this.
  279. * @private
  280. */
  281. goog.net.BrowserChannel.prototype.allowChunkedMode_ = true;
  282. /**
  283. * The array identifier of the last array received from the server for the
  284. * backchannel request.
  285. * @type {number}
  286. * @private
  287. */
  288. goog.net.BrowserChannel.prototype.lastArrayId_ = -1;
  289. /**
  290. * The array identifier of the last array sent by the server that we know about.
  291. * @type {number}
  292. * @private
  293. */
  294. goog.net.BrowserChannel.prototype.lastPostResponseArrayId_ = -1;
  295. /**
  296. * The last status code received.
  297. * @type {number}
  298. * @private
  299. */
  300. goog.net.BrowserChannel.prototype.lastStatusCode_ = -1;
  301. /**
  302. * Number of times we have retried the current forward channel request.
  303. * @type {number}
  304. * @private
  305. */
  306. goog.net.BrowserChannel.prototype.forwardChannelRetryCount_ = 0;
  307. /**
  308. * Number of times it a row that we have retried the current back channel
  309. * request and received no data.
  310. * @type {number}
  311. * @private
  312. */
  313. goog.net.BrowserChannel.prototype.backChannelRetryCount_ = 0;
  314. /**
  315. * The attempt id for the current back channel request. Starts at 1 and
  316. * increments for each reconnect. The server uses this to log if our connection
  317. * is flaky or not.
  318. * @type {number}
  319. * @private
  320. */
  321. goog.net.BrowserChannel.prototype.backChannelAttemptId_;
  322. /**
  323. * The base part of the time before firing next retry request. Default is 5
  324. * seconds. Note that a random delay is added (see {@link retryDelaySeedMs_})
  325. * for all retries, and linear backoff is applied to the sum for subsequent
  326. * retries.
  327. * @type {number}
  328. * @private
  329. */
  330. goog.net.BrowserChannel.prototype.baseRetryDelayMs_ = 5 * 1000;
  331. /**
  332. * A random time between 0 and this number of MS is added to the
  333. * {@link baseRetryDelayMs_}. Default is 10 seconds.
  334. * @type {number}
  335. * @private
  336. */
  337. goog.net.BrowserChannel.prototype.retryDelaySeedMs_ = 10 * 1000;
  338. /**
  339. * Maximum number of attempts to connect to the server for forward channel
  340. * requests. Defaults to 2.
  341. * @type {number}
  342. * @private
  343. */
  344. goog.net.BrowserChannel.prototype.forwardChannelMaxRetries_ = 2;
  345. /**
  346. * The timeout in milliseconds for a forward channel request. Defaults to 20
  347. * seconds. Note that part of this timeout can be randomized.
  348. * @type {number}
  349. * @private
  350. */
  351. goog.net.BrowserChannel.prototype.forwardChannelRequestTimeoutMs_ = 20 * 1000;
  352. /**
  353. * A throttle time in ms for readystatechange events for the backchannel.
  354. * Useful for throttling when ready state is INTERACTIVE (partial data).
  355. *
  356. * This throttle is useful if the server sends large data chunks down the
  357. * backchannel. It prevents examining XHR partial data on every
  358. * readystate change event. This is useful because large chunks can
  359. * trigger hundreds of readystatechange events, each of which takes ~5ms
  360. * or so to handle, in turn making the UI unresponsive for a significant period.
  361. *
  362. * If set to zero no throttle is used.
  363. * @type {number}
  364. * @private
  365. */
  366. goog.net.BrowserChannel.prototype.readyStateChangeThrottleMs_ = 0;
  367. /**
  368. * Whether cross origin requests are supported for the browser channel.
  369. *
  370. * See {@link goog.net.XhrIo#setWithCredentials}.
  371. * @type {boolean}
  372. * @private
  373. */
  374. goog.net.BrowserChannel.prototype.supportsCrossDomainXhrs_ = false;
  375. /**
  376. * The latest protocol version that this class supports. We request this version
  377. * from the server when opening the connection. Should match
  378. * com.google.net.browserchannel.BrowserChannel.LATEST_CHANNEL_VERSION.
  379. * @type {number}
  380. */
  381. goog.net.BrowserChannel.LATEST_CHANNEL_VERSION = 8;
  382. /**
  383. * The channel version that we negotiated with the server for this session.
  384. * Starts out as the version we request, and then is changed to the negotiated
  385. * version after the initial open.
  386. * @type {number}
  387. * @private
  388. */
  389. goog.net.BrowserChannel.prototype.channelVersion_ =
  390. goog.net.BrowserChannel.LATEST_CHANNEL_VERSION;
  391. /**
  392. * Enum type for the browser channel state machine.
  393. * @enum {number}
  394. */
  395. goog.net.BrowserChannel.State = {
  396. /** The channel is closed. */
  397. CLOSED: 0,
  398. /** The channel has been initialized but hasn't yet initiated a connection. */
  399. INIT: 1,
  400. /** The channel is in the process of opening a connection to the server. */
  401. OPENING: 2,
  402. /** The channel is open. */
  403. OPENED: 3
  404. };
  405. /**
  406. * The timeout in milliseconds for a forward channel request.
  407. * @type {number}
  408. */
  409. goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;
  410. /**
  411. * Maximum number of attempts to connect to the server for back channel
  412. * requests.
  413. * @type {number}
  414. */
  415. goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES = 3;
  416. /**
  417. * A number in MS of how long we guess the maxmium amount of time a round trip
  418. * to the server should take. In the future this could be substituted with a
  419. * real measurement of the RTT.
  420. * @type {number}
  421. */
  422. goog.net.BrowserChannel.RTT_ESTIMATE = 3 * 1000;
  423. /**
  424. * When retrying for an inactive channel, we will multiply the total delay by
  425. * this number.
  426. * @type {number}
  427. */
  428. goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR = 2;
  429. /**
  430. * Enum type for identifying a BrowserChannel error.
  431. * @enum {number}
  432. */
  433. goog.net.BrowserChannel.Error = {
  434. /** Value that indicates no error has occurred. */
  435. OK: 0,
  436. /** An error due to a request failing. */
  437. REQUEST_FAILED: 2,
  438. /** An error due to the user being logged out. */
  439. LOGGED_OUT: 4,
  440. /** An error due to server response which contains no data. */
  441. NO_DATA: 5,
  442. /** An error due to a server response indicating an unknown session id */
  443. UNKNOWN_SESSION_ID: 6,
  444. /** An error due to a server response requesting to stop the channel. */
  445. STOP: 7,
  446. /** A general network error. */
  447. NETWORK: 8,
  448. /** An error due to the channel being blocked by a network administrator. */
  449. BLOCKED: 9,
  450. /** An error due to bad data being returned from the server. */
  451. BAD_DATA: 10,
  452. /** An error due to a response that doesn't start with the magic cookie. */
  453. BAD_RESPONSE: 11,
  454. /** ActiveX is blocked by the machine's admin settings. */
  455. ACTIVE_X_BLOCKED: 12
  456. };
  457. /**
  458. * Internal enum type for the two browser channel channel types.
  459. * @enum {number}
  460. * @private
  461. */
  462. goog.net.BrowserChannel.ChannelType_ = {
  463. FORWARD_CHANNEL: 1,
  464. BACK_CHANNEL: 2
  465. };
  466. /**
  467. * The maximum number of maps that can be sent in one POST. Should match
  468. * com.google.net.browserchannel.BrowserChannel.MAX_MAPS_PER_REQUEST.
  469. * @type {number}
  470. * @private
  471. */
  472. goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ = 1000;
  473. /**
  474. * Singleton event target for firing stat events
  475. * @type {goog.events.EventTarget}
  476. * @private
  477. */
  478. goog.net.BrowserChannel.statEventTarget_ = new goog.events.EventTarget();
  479. /**
  480. * Events fired by BrowserChannel and associated objects
  481. * @const
  482. */
  483. goog.net.BrowserChannel.Event = {};
  484. /**
  485. * Stat Event that fires when things of interest happen that may be useful for
  486. * applications to know about for stats or debugging purposes. This event fires
  487. * on the EventTarget returned by getStatEventTarget.
  488. */
  489. goog.net.BrowserChannel.Event.STAT_EVENT = 'statevent';
  490. /**
  491. * Event class for goog.net.BrowserChannel.Event.STAT_EVENT
  492. *
  493. * @param {goog.events.EventTarget} eventTarget The stat event target for
  494. the browser channel.
  495. * @param {goog.net.BrowserChannel.Stat} stat The stat.
  496. * @constructor
  497. * @extends {goog.events.Event}
  498. * @final
  499. */
  500. goog.net.BrowserChannel.StatEvent = function(eventTarget, stat) {
  501. goog.events.Event.call(
  502. this, goog.net.BrowserChannel.Event.STAT_EVENT, eventTarget);
  503. /**
  504. * The stat
  505. * @type {goog.net.BrowserChannel.Stat}
  506. */
  507. this.stat = stat;
  508. };
  509. goog.inherits(goog.net.BrowserChannel.StatEvent, goog.events.Event);
  510. /**
  511. * An event that fires when POST requests complete successfully, indicating
  512. * the size of the POST and the round trip time.
  513. * This event fires on the EventTarget returned by getStatEventTarget.
  514. */
  515. goog.net.BrowserChannel.Event.TIMING_EVENT = 'timingevent';
  516. /**
  517. * Event class for goog.net.BrowserChannel.Event.TIMING_EVENT
  518. *
  519. * @param {goog.events.EventTarget} target The stat event target for
  520. the browser channel.
  521. * @param {number} size The number of characters in the POST data.
  522. * @param {number} rtt The total round trip time from POST to response in MS.
  523. * @param {number} retries The number of times the POST had to be retried.
  524. * @constructor
  525. * @extends {goog.events.Event}
  526. * @final
  527. */
  528. goog.net.BrowserChannel.TimingEvent = function(target, size, rtt, retries) {
  529. goog.events.Event.call(
  530. this, goog.net.BrowserChannel.Event.TIMING_EVENT, target);
  531. /**
  532. * @type {number}
  533. */
  534. this.size = size;
  535. /**
  536. * @type {number}
  537. */
  538. this.rtt = rtt;
  539. /**
  540. * @type {number}
  541. */
  542. this.retries = retries;
  543. };
  544. goog.inherits(goog.net.BrowserChannel.TimingEvent, goog.events.Event);
  545. /**
  546. * The type of event that occurs every time some information about how reachable
  547. * the server is is discovered.
  548. */
  549. goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT = 'serverreachability';
  550. /**
  551. * Types of events which reveal information about the reachability of the
  552. * server.
  553. * @enum {number}
  554. */
  555. goog.net.BrowserChannel.ServerReachability = {
  556. REQUEST_MADE: 1,
  557. REQUEST_SUCCEEDED: 2,
  558. REQUEST_FAILED: 3,
  559. BACK_CHANNEL_ACTIVITY: 4
  560. };
  561. /**
  562. * Event class for goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT.
  563. *
  564. * @param {goog.events.EventTarget} target The stat event target for
  565. the browser channel.
  566. * @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The
  567. * reachability event type.
  568. * @constructor
  569. * @extends {goog.events.Event}
  570. * @final
  571. */
  572. goog.net.BrowserChannel.ServerReachabilityEvent = function(
  573. target, reachabilityType) {
  574. goog.events.Event.call(
  575. this, goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT, target);
  576. /**
  577. * @type {goog.net.BrowserChannel.ServerReachability}
  578. */
  579. this.reachabilityType = reachabilityType;
  580. };
  581. goog.inherits(
  582. goog.net.BrowserChannel.ServerReachabilityEvent, goog.events.Event);
  583. /**
  584. * Enum that identifies events for statistics that are interesting to track.
  585. * TODO(user) - Change name not to use Event or use EventTarget
  586. * @enum {number}
  587. */
  588. goog.net.BrowserChannel.Stat = {
  589. /** Event indicating a new connection attempt. */
  590. CONNECT_ATTEMPT: 0,
  591. /** Event indicating a connection error due to a general network problem. */
  592. ERROR_NETWORK: 1,
  593. /**
  594. * Event indicating a connection error that isn't due to a general network
  595. * problem.
  596. */
  597. ERROR_OTHER: 2,
  598. /** Event indicating the start of test stage one. */
  599. TEST_STAGE_ONE_START: 3,
  600. /** Event indicating the channel is blocked by a network administrator. */
  601. CHANNEL_BLOCKED: 4,
  602. /** Event indicating the start of test stage two. */
  603. TEST_STAGE_TWO_START: 5,
  604. /** Event indicating the first piece of test data was received. */
  605. TEST_STAGE_TWO_DATA_ONE: 6,
  606. /**
  607. * Event indicating that the second piece of test data was received and it was
  608. * received separately from the first.
  609. */
  610. TEST_STAGE_TWO_DATA_TWO: 7,
  611. /** Event indicating both pieces of test data were received simultaneously. */
  612. TEST_STAGE_TWO_DATA_BOTH: 8,
  613. /** Event indicating stage one of the test request failed. */
  614. TEST_STAGE_ONE_FAILED: 9,
  615. /** Event indicating stage two of the test request failed. */
  616. TEST_STAGE_TWO_FAILED: 10,
  617. /**
  618. * Event indicating that a buffering proxy is likely between the client and
  619. * the server.
  620. */
  621. PROXY: 11,
  622. /**
  623. * Event indicating that no buffering proxy is likely between the client and
  624. * the server.
  625. */
  626. NOPROXY: 12,
  627. /** Event indicating an unknown SID error. */
  628. REQUEST_UNKNOWN_SESSION_ID: 13,
  629. /** Event indicating a bad status code was received. */
  630. REQUEST_BAD_STATUS: 14,
  631. /** Event indicating incomplete data was received */
  632. REQUEST_INCOMPLETE_DATA: 15,
  633. /** Event indicating bad data was received */
  634. REQUEST_BAD_DATA: 16,
  635. /** Event indicating no data was received when data was expected. */
  636. REQUEST_NO_DATA: 17,
  637. /** Event indicating a request timeout. */
  638. REQUEST_TIMEOUT: 18,
  639. /**
  640. * Event indicating that the server never received our hanging GET and so it
  641. * is being retried.
  642. */
  643. BACKCHANNEL_MISSING: 19,
  644. /**
  645. * Event indicating that we have determined that our hanging GET is not
  646. * receiving data when it should be. Thus it is dead dead and will be retried.
  647. */
  648. BACKCHANNEL_DEAD: 20,
  649. /**
  650. * The browser declared itself offline during the lifetime of a request, or
  651. * was offline when a request was initially made.
  652. */
  653. BROWSER_OFFLINE: 21,
  654. /** ActiveX is blocked by the machine's admin settings. */
  655. ACTIVE_X_BLOCKED: 22
  656. };
  657. /**
  658. * A guess at a cutoff at which to no longer assume the backchannel is dead
  659. * when we are slow to receive data. Number in bytes.
  660. *
  661. * Assumption: The worst bandwidth we work on is 50 kilobits/sec
  662. * 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout
  663. * @type {number}
  664. */
  665. goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500;
  666. /**
  667. * Returns the browserchannel logger.
  668. *
  669. * @return {!goog.net.ChannelDebug} The channel debug object.
  670. */
  671. goog.net.BrowserChannel.prototype.getChannelDebug = function() {
  672. return this.channelDebug_;
  673. };
  674. /**
  675. * Set the browserchannel logger.
  676. * TODO(user): Add interface for channel loggers or remove this function.
  677. *
  678. * @param {goog.net.ChannelDebug} channelDebug The channel debug object.
  679. */
  680. goog.net.BrowserChannel.prototype.setChannelDebug = function(channelDebug) {
  681. if (goog.isDefAndNotNull(channelDebug)) {
  682. this.channelDebug_ = channelDebug;
  683. }
  684. };
  685. /**
  686. * Allows the application to set an execution hooks for when BrowserChannel
  687. * starts processing requests. This is useful to track timing or logging
  688. * special information. The function takes no parameters and return void.
  689. * @param {Function} startHook The function for the start hook.
  690. */
  691. goog.net.BrowserChannel.setStartThreadExecutionHook = function(startHook) {
  692. goog.net.BrowserChannel.startExecutionHook_ = startHook;
  693. };
  694. /**
  695. * Allows the application to set an execution hooks for when BrowserChannel
  696. * stops processing requests. This is useful to track timing or logging
  697. * special information. The function takes no parameters and return void.
  698. * @param {Function} endHook The function for the end hook.
  699. */
  700. goog.net.BrowserChannel.setEndThreadExecutionHook = function(endHook) {
  701. goog.net.BrowserChannel.endExecutionHook_ = endHook;
  702. };
  703. /**
  704. * Application provided execution hook for the start hook.
  705. *
  706. * @type {Function}
  707. * @private
  708. */
  709. goog.net.BrowserChannel.startExecutionHook_ = function() {};
  710. /**
  711. * Application provided execution hook for the end hook.
  712. *
  713. * @type {Function}
  714. * @private
  715. */
  716. goog.net.BrowserChannel.endExecutionHook_ = function() {};
  717. /**
  718. * Instantiates a ChannelRequest with the given parameters. Overidden in tests.
  719. *
  720. * @param {goog.net.BrowserChannel|goog.net.BrowserTestChannel} channel
  721. * The BrowserChannel that owns this request.
  722. * @param {goog.net.ChannelDebug} channelDebug A ChannelDebug to use for
  723. * logging.
  724. * @param {string=} opt_sessionId The session id for the channel.
  725. * @param {string|number=} opt_requestId The request id for this request.
  726. * @param {number=} opt_retryId The retry id for this request.
  727. * @return {!goog.net.ChannelRequest} The created channel request.
  728. */
  729. goog.net.BrowserChannel.createChannelRequest = function(
  730. channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) {
  731. return new goog.net.ChannelRequest(
  732. channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId);
  733. };
  734. /**
  735. * Starts the channel. This initiates connections to the server.
  736. *
  737. * @param {string} testPath The path for the test connection.
  738. * @param {string} channelPath The path for the channel connection.
  739. * @param {Object=} opt_extraParams Extra parameter keys and values to add to
  740. * the requests.
  741. * @param {string=} opt_oldSessionId Session ID from a previous session.
  742. * @param {number=} opt_oldArrayId The last array ID from a previous session.
  743. */
  744. goog.net.BrowserChannel.prototype.connect = function(
  745. testPath, channelPath, opt_extraParams, opt_oldSessionId, opt_oldArrayId) {
  746. this.channelDebug_.debug('connect()');
  747. goog.net.BrowserChannel.notifyStatEvent(
  748. goog.net.BrowserChannel.Stat.CONNECT_ATTEMPT);
  749. this.path_ = channelPath;
  750. this.extraParams_ = opt_extraParams || {};
  751. // Attach parameters about the previous session if reconnecting.
  752. if (opt_oldSessionId && goog.isDef(opt_oldArrayId)) {
  753. this.extraParams_['OSID'] = opt_oldSessionId;
  754. this.extraParams_['OAID'] = opt_oldArrayId;
  755. }
  756. if (this.asyncTest_) {
  757. goog.net.BrowserChannel.setTimeout(
  758. goog.bind(this.connectTest_, this, testPath), 100);
  759. this.connectChannel_();
  760. } else {
  761. this.connectTest_(testPath);
  762. }
  763. };
  764. /**
  765. * Disconnects and closes the channel.
  766. */
  767. goog.net.BrowserChannel.prototype.disconnect = function() {
  768. this.channelDebug_.debug('disconnect()');
  769. this.cancelRequests_();
  770. if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
  771. var rid = this.nextRid_++;
  772. var uri = this.forwardChannelUri_.clone();
  773. uri.setParameterValue('SID', this.sid_);
  774. uri.setParameterValue('RID', rid);
  775. uri.setParameterValue('TYPE', 'terminate');
  776. // Add the reconnect parameters.
  777. this.addAdditionalParams_(uri);
  778. var request = goog.net.BrowserChannel.createChannelRequest(
  779. this, this.channelDebug_, this.sid_, rid);
  780. request.sendUsingImgTag(uri);
  781. }
  782. this.onClose_();
  783. };
  784. /**
  785. * Returns the session id of the channel. Only available after the
  786. * channel has been opened.
  787. * @return {string} Session ID.
  788. */
  789. goog.net.BrowserChannel.prototype.getSessionId = function() {
  790. return this.sid_;
  791. };
  792. /**
  793. * Starts the test channel to determine network conditions.
  794. *
  795. * @param {string} testPath The relative PATH for the test connection.
  796. * @private
  797. */
  798. goog.net.BrowserChannel.prototype.connectTest_ = function(testPath) {
  799. this.channelDebug_.debug('connectTest_()');
  800. if (!this.okToMakeRequest_()) {
  801. return; // channel is cancelled
  802. }
  803. this.connectionTest_ =
  804. new goog.net.BrowserTestChannel(this, this.channelDebug_);
  805. this.connectionTest_.setExtraHeaders(this.extraHeaders_);
  806. this.connectionTest_.setParser(this.parser_);
  807. this.connectionTest_.connect(testPath);
  808. };
  809. /**
  810. * Starts the regular channel which is run after the test channel is complete.
  811. * @private
  812. */
  813. goog.net.BrowserChannel.prototype.connectChannel_ = function() {
  814. this.channelDebug_.debug('connectChannel_()');
  815. this.ensureInState_(
  816. goog.net.BrowserChannel.State.INIT, goog.net.BrowserChannel.State.CLOSED);
  817. this.forwardChannelUri_ =
  818. this.getForwardChannelUri(/** @type {string} */ (this.path_));
  819. this.ensureForwardChannel_();
  820. };
  821. /**
  822. * Cancels all outstanding requests.
  823. * @private
  824. */
  825. goog.net.BrowserChannel.prototype.cancelRequests_ = function() {
  826. if (this.connectionTest_) {
  827. this.connectionTest_.abort();
  828. this.connectionTest_ = null;
  829. }
  830. if (this.backChannelRequest_) {
  831. this.backChannelRequest_.cancel();
  832. this.backChannelRequest_ = null;
  833. }
  834. if (this.backChannelTimerId_) {
  835. goog.global.clearTimeout(this.backChannelTimerId_);
  836. this.backChannelTimerId_ = null;
  837. }
  838. this.clearDeadBackchannelTimer_();
  839. if (this.forwardChannelRequest_) {
  840. this.forwardChannelRequest_.cancel();
  841. this.forwardChannelRequest_ = null;
  842. }
  843. if (this.forwardChannelTimerId_) {
  844. goog.global.clearTimeout(this.forwardChannelTimerId_);
  845. this.forwardChannelTimerId_ = null;
  846. }
  847. };
  848. /**
  849. * Returns the extra HTTP headers to add to all the requests sent to the server.
  850. *
  851. * @return {Object} The HTTP headers, or null.
  852. */
  853. goog.net.BrowserChannel.prototype.getExtraHeaders = function() {
  854. return this.extraHeaders_;
  855. };
  856. /**
  857. * Sets extra HTTP headers to add to all the requests sent to the server.
  858. *
  859. * @param {Object} extraHeaders The HTTP headers, or null.
  860. */
  861. goog.net.BrowserChannel.prototype.setExtraHeaders = function(extraHeaders) {
  862. this.extraHeaders_ = extraHeaders;
  863. };
  864. /**
  865. * Sets the throttle for handling onreadystatechange events for the request.
  866. *
  867. * @param {number} throttle The throttle in ms. A value of zero indicates
  868. * no throttle.
  869. */
  870. goog.net.BrowserChannel.prototype.setReadyStateChangeThrottle = function(
  871. throttle) {
  872. this.readyStateChangeThrottleMs_ = throttle;
  873. };
  874. /**
  875. * Sets whether cross origin requests are supported for the browser channel.
  876. *
  877. * Setting this allows the creation of requests to secondary domains and
  878. * sends XHRs with the CORS withCredentials bit set to true.
  879. *
  880. * In order for cross-origin requests to work, the server will also need to set
  881. * CORS response headers as per:
  882. * https://developer.mozilla.org/en-US/docs/HTTP_access_control
  883. *
  884. * See {@link goog.net.XhrIo#setWithCredentials}.
  885. * @param {boolean} supportCrossDomain Whether cross domain XHRs are supported.
  886. */
  887. goog.net.BrowserChannel.prototype.setSupportsCrossDomainXhrs = function(
  888. supportCrossDomain) {
  889. this.supportsCrossDomainXhrs_ = supportCrossDomain;
  890. };
  891. /**
  892. * Returns the handler used for channel callback events.
  893. *
  894. * @return {goog.net.BrowserChannel.Handler} The handler.
  895. */
  896. goog.net.BrowserChannel.prototype.getHandler = function() {
  897. return this.handler_;
  898. };
  899. /**
  900. * Sets the handler used for channel callback events.
  901. * @param {goog.net.BrowserChannel.Handler} handler The handler to set.
  902. */
  903. goog.net.BrowserChannel.prototype.setHandler = function(handler) {
  904. this.handler_ = handler;
  905. };
  906. /**
  907. * Returns whether the channel allows the use of a subdomain. There may be
  908. * cases where this isn't allowed.
  909. * @return {boolean} Whether a host prefix is allowed.
  910. */
  911. goog.net.BrowserChannel.prototype.getAllowHostPrefix = function() {
  912. return this.allowHostPrefix_;
  913. };
  914. /**
  915. * Sets whether the channel allows the use of a subdomain. There may be cases
  916. * where this isn't allowed, for example, logging in with troutboard where
  917. * using a subdomain causes Apache to force the user to authenticate twice.
  918. * @param {boolean} allowHostPrefix Whether a host prefix is allowed.
  919. */
  920. goog.net.BrowserChannel.prototype.setAllowHostPrefix = function(
  921. allowHostPrefix) {
  922. this.allowHostPrefix_ = allowHostPrefix;
  923. };
  924. /**
  925. * Returns whether the channel is buffered or not. This state is valid for
  926. * querying only after the test connection has completed. This may be
  927. * queried in the goog.net.BrowserChannel.okToMakeRequest() callback.
  928. * A channel may be buffered if the test connection determines that
  929. * a chunked response could not be sent down within a suitable time.
  930. * @return {boolean} Whether the channel is buffered.
  931. */
  932. goog.net.BrowserChannel.prototype.isBuffered = function() {
  933. return !this.useChunked_;
  934. };
  935. /**
  936. * Returns whether chunked mode is allowed. In certain debugging situations,
  937. * it's useful for the application to have a way to disable chunked mode for a
  938. * user.
  939. * @return {boolean} Whether chunked mode is allowed.
  940. */
  941. goog.net.BrowserChannel.prototype.getAllowChunkedMode = function() {
  942. return this.allowChunkedMode_;
  943. };
  944. /**
  945. * Sets whether chunked mode is allowed. In certain debugging situations, it's
  946. * useful for the application to have a way to disable chunked mode for a user.
  947. * @param {boolean} allowChunkedMode Whether chunked mode is allowed.
  948. */
  949. goog.net.BrowserChannel.prototype.setAllowChunkedMode = function(
  950. allowChunkedMode) {
  951. this.allowChunkedMode_ = allowChunkedMode;
  952. };
  953. /**
  954. * Sends a request to the server. The format of the request is a Map data
  955. * structure of key/value pairs. These maps are then encoded in a format
  956. * suitable for the wire and then reconstituted as a Map data structure that
  957. * the server can process.
  958. * @param {Object} map The map to send.
  959. * @param {?Object=} opt_context The context associated with the map.
  960. */
  961. goog.net.BrowserChannel.prototype.sendMap = function(map, opt_context) {
  962. if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {
  963. throw Error('Invalid operation: sending map when state is closed');
  964. }
  965. // We can only send 1000 maps per POST, but typically we should never have
  966. // that much to send, so warn if we exceed that (we still send all the maps).
  967. if (this.outgoingMaps_.length ==
  968. goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_) {
  969. // severe() is temporary so that we get these uploaded and can figure out
  970. // what's causing them. Afterwards can change to warning().
  971. this.channelDebug_.severe(
  972. 'Already have ' + goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ +
  973. ' queued maps upon queueing ' + this.parser_.stringify(map));
  974. }
  975. this.outgoingMaps_.push(
  976. new goog.net.BrowserChannel.QueuedMap(
  977. this.nextMapId_++, map, opt_context));
  978. if (this.state_ == goog.net.BrowserChannel.State.OPENING ||
  979. this.state_ == goog.net.BrowserChannel.State.OPENED) {
  980. this.ensureForwardChannel_();
  981. }
  982. };
  983. /**
  984. * When set to true, this changes the behavior of the forward channel so it
  985. * will not retry requests; it will fail after one network failure, and if
  986. * there was already one network failure, the request will fail immediately.
  987. * @param {boolean} failFast Whether or not to fail fast.
  988. */
  989. goog.net.BrowserChannel.prototype.setFailFast = function(failFast) {
  990. this.failFast_ = failFast;
  991. this.channelDebug_.info('setFailFast: ' + failFast);
  992. if ((this.forwardChannelRequest_ || this.forwardChannelTimerId_) &&
  993. this.forwardChannelRetryCount_ > this.getForwardChannelMaxRetries()) {
  994. this.channelDebug_.info(
  995. 'Retry count ' + this.forwardChannelRetryCount_ + ' > new maxRetries ' +
  996. this.getForwardChannelMaxRetries() + '. Fail immediately!');
  997. if (this.forwardChannelRequest_) {
  998. this.forwardChannelRequest_.cancel();
  999. // Go through the standard onRequestComplete logic to expose the max-retry
  1000. // failure in the standard way.
  1001. this.onRequestComplete(this.forwardChannelRequest_);
  1002. } else { // i.e., this.forwardChannelTimerId_
  1003. goog.global.clearTimeout(this.forwardChannelTimerId_);
  1004. this.forwardChannelTimerId_ = null;
  1005. // The error code from the last failed request is gone, so just use a
  1006. // generic one.
  1007. this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
  1008. }
  1009. }
  1010. };
  1011. /**
  1012. * @return {number} The max number of forward-channel retries, which will be 0
  1013. * in fail-fast mode.
  1014. */
  1015. goog.net.BrowserChannel.prototype.getForwardChannelMaxRetries = function() {
  1016. return this.failFast_ ? 0 : this.forwardChannelMaxRetries_;
  1017. };
  1018. /**
  1019. * Sets the maximum number of attempts to connect to the server for forward
  1020. * channel requests.
  1021. * @param {number} retries The maximum number of attempts.
  1022. */
  1023. goog.net.BrowserChannel.prototype.setForwardChannelMaxRetries = function(
  1024. retries) {
  1025. this.forwardChannelMaxRetries_ = retries;
  1026. };
  1027. /**
  1028. * Sets the timeout for a forward channel request.
  1029. * @param {number} timeoutMs The timeout in milliseconds.
  1030. */
  1031. goog.net.BrowserChannel.prototype.setForwardChannelRequestTimeout = function(
  1032. timeoutMs) {
  1033. this.forwardChannelRequestTimeoutMs_ = timeoutMs;
  1034. };
  1035. /**
  1036. * @return {number} The max number of back-channel retries, which is a constant.
  1037. */
  1038. goog.net.BrowserChannel.prototype.getBackChannelMaxRetries = function() {
  1039. // Back-channel retries is a constant.
  1040. return goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES;
  1041. };
  1042. /**
  1043. * Returns whether the channel is closed
  1044. * @return {boolean} true if the channel is closed.
  1045. */
  1046. goog.net.BrowserChannel.prototype.isClosed = function() {
  1047. return this.state_ == goog.net.BrowserChannel.State.CLOSED;
  1048. };
  1049. /**
  1050. * Returns the browser channel state.
  1051. * @return {goog.net.BrowserChannel.State} The current state of the browser
  1052. * channel.
  1053. */
  1054. goog.net.BrowserChannel.prototype.getState = function() {
  1055. return this.state_;
  1056. };
  1057. /**
  1058. * Return the last status code received for a request.
  1059. * @return {number} The last status code received for a request.
  1060. */
  1061. goog.net.BrowserChannel.prototype.getLastStatusCode = function() {
  1062. return this.lastStatusCode_;
  1063. };
  1064. /**
  1065. * @return {number} The last array id received.
  1066. */
  1067. goog.net.BrowserChannel.prototype.getLastArrayId = function() {
  1068. return this.lastArrayId_;
  1069. };
  1070. /**
  1071. * Returns whether there are outstanding requests servicing the channel.
  1072. * @return {boolean} true if there are outstanding requests.
  1073. */
  1074. goog.net.BrowserChannel.prototype.hasOutstandingRequests = function() {
  1075. return this.outstandingRequests_() != 0;
  1076. };
  1077. /**
  1078. * Sets a new parser for the response payload.
  1079. * @param {!goog.string.Parser} parser Parser.
  1080. */
  1081. goog.net.BrowserChannel.prototype.setParser = function(parser) {
  1082. this.parser_ = parser;
  1083. };
  1084. /**
  1085. * Returns the number of outstanding requests.
  1086. * @return {number} The number of outstanding requests to the server.
  1087. * @private
  1088. */
  1089. goog.net.BrowserChannel.prototype.outstandingRequests_ = function() {
  1090. var count = 0;
  1091. if (this.backChannelRequest_) {
  1092. count++;
  1093. }
  1094. if (this.forwardChannelRequest_) {
  1095. count++;
  1096. }
  1097. return count;
  1098. };
  1099. /**
  1100. * Ensures that a forward channel request is scheduled.
  1101. * @private
  1102. */
  1103. goog.net.BrowserChannel.prototype.ensureForwardChannel_ = function() {
  1104. if (this.forwardChannelRequest_) {
  1105. // connection in process - no need to start a new request
  1106. return;
  1107. }
  1108. if (this.forwardChannelTimerId_) {
  1109. // no need to start a new request - one is already scheduled
  1110. return;
  1111. }
  1112. this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
  1113. goog.bind(this.onStartForwardChannelTimer_, this), 0);
  1114. this.forwardChannelRetryCount_ = 0;
  1115. };
  1116. /**
  1117. * Schedules a forward-channel retry for the specified request, unless the max
  1118. * retries has been reached.
  1119. * @param {goog.net.ChannelRequest} request The failed request to retry.
  1120. * @return {boolean} true iff a retry was scheduled.
  1121. * @private
  1122. */
  1123. goog.net.BrowserChannel.prototype.maybeRetryForwardChannel_ = function(
  1124. request) {
  1125. if (this.forwardChannelRequest_ || this.forwardChannelTimerId_) {
  1126. // Should be impossible to be called in this state.
  1127. this.channelDebug_.severe('Request already in progress');
  1128. return false;
  1129. }
  1130. if (this.state_ == goog.net.BrowserChannel.State.INIT || // no retry open_()
  1131. (this.forwardChannelRetryCount_ >= this.getForwardChannelMaxRetries())) {
  1132. return false;
  1133. }
  1134. this.channelDebug_.debug('Going to retry POST');
  1135. this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
  1136. goog.bind(this.onStartForwardChannelTimer_, this, request),
  1137. this.getRetryTime_(this.forwardChannelRetryCount_));
  1138. this.forwardChannelRetryCount_++;
  1139. return true;
  1140. };
  1141. /**
  1142. * Timer callback for ensureForwardChannel
  1143. * @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.
  1144. * @private
  1145. */
  1146. goog.net.BrowserChannel.prototype.onStartForwardChannelTimer_ = function(
  1147. opt_retryRequest) {
  1148. this.forwardChannelTimerId_ = null;
  1149. this.startForwardChannel_(opt_retryRequest);
  1150. };
  1151. /**
  1152. * Begins a new forward channel operation to the server.
  1153. * @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.
  1154. * @private
  1155. */
  1156. goog.net.BrowserChannel.prototype.startForwardChannel_ = function(
  1157. opt_retryRequest) {
  1158. this.channelDebug_.debug('startForwardChannel_');
  1159. if (!this.okToMakeRequest_()) {
  1160. return; // channel is cancelled
  1161. } else if (this.state_ == goog.net.BrowserChannel.State.INIT) {
  1162. if (opt_retryRequest) {
  1163. this.channelDebug_.severe('Not supposed to retry the open');
  1164. return;
  1165. }
  1166. this.open_();
  1167. this.state_ = goog.net.BrowserChannel.State.OPENING;
  1168. } else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
  1169. if (opt_retryRequest) {
  1170. this.makeForwardChannelRequest_(opt_retryRequest);
  1171. return;
  1172. }
  1173. if (this.outgoingMaps_.length == 0) {
  1174. this.channelDebug_.debug(
  1175. 'startForwardChannel_ returned: ' +
  1176. 'nothing to send');
  1177. // no need to start a new forward channel request
  1178. return;
  1179. }
  1180. if (this.forwardChannelRequest_) {
  1181. // Should be impossible to be called in this state.
  1182. this.channelDebug_.severe(
  1183. 'startForwardChannel_ returned: ' +
  1184. 'connection already in progress');
  1185. return;
  1186. }
  1187. this.makeForwardChannelRequest_();
  1188. this.channelDebug_.debug('startForwardChannel_ finished, sent request');
  1189. }
  1190. };
  1191. /**
  1192. * Establishes a new channel session with the the server.
  1193. * @private
  1194. */
  1195. goog.net.BrowserChannel.prototype.open_ = function() {
  1196. this.channelDebug_.debug('open_()');
  1197. this.nextRid_ = Math.floor(Math.random() * 100000);
  1198. var rid = this.nextRid_++;
  1199. var request = goog.net.BrowserChannel.createChannelRequest(
  1200. this, this.channelDebug_, '', rid);
  1201. request.setExtraHeaders(this.extraHeaders_);
  1202. var requestText = this.dequeueOutgoingMaps_();
  1203. var uri = this.forwardChannelUri_.clone();
  1204. uri.setParameterValue('RID', rid);
  1205. if (this.clientVersion_) {
  1206. uri.setParameterValue('CVER', this.clientVersion_);
  1207. }
  1208. // Add the reconnect parameters.
  1209. this.addAdditionalParams_(uri);
  1210. request.xmlHttpPost(uri, requestText, true);
  1211. this.forwardChannelRequest_ = request;
  1212. };
  1213. /**
  1214. * Makes a forward channel request using XMLHTTP.
  1215. * @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.
  1216. * @private
  1217. */
  1218. goog.net.BrowserChannel.prototype.makeForwardChannelRequest_ = function(
  1219. opt_retryRequest) {
  1220. var rid;
  1221. var requestText;
  1222. if (opt_retryRequest) {
  1223. if (this.channelVersion_ > 6) {
  1224. // In version 7 and up we can tack on new arrays to a retry.
  1225. this.requeuePendingMaps_();
  1226. rid = this.nextRid_ - 1; // Must use last RID
  1227. requestText = this.dequeueOutgoingMaps_();
  1228. } else {
  1229. // TODO(user): Remove this code and the opt_retryRequest passing
  1230. // once server-side support for ver 7 is ubiquitous.
  1231. rid = opt_retryRequest.getRequestId();
  1232. requestText = /** @type {string} */ (opt_retryRequest.getPostData());
  1233. }
  1234. } else {
  1235. rid = this.nextRid_++;
  1236. requestText = this.dequeueOutgoingMaps_();
  1237. }
  1238. var uri = this.forwardChannelUri_.clone();
  1239. uri.setParameterValue('SID', this.sid_);
  1240. uri.setParameterValue('RID', rid);
  1241. uri.setParameterValue('AID', this.lastArrayId_);
  1242. // Add the additional reconnect parameters.
  1243. this.addAdditionalParams_(uri);
  1244. var request = goog.net.BrowserChannel.createChannelRequest(
  1245. this, this.channelDebug_, this.sid_, rid,
  1246. this.forwardChannelRetryCount_ + 1);
  1247. request.setExtraHeaders(this.extraHeaders_);
  1248. // randomize from 50%-100% of the forward channel timeout to avoid
  1249. // a big hit if servers happen to die at once.
  1250. request.setTimeout(
  1251. Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50) +
  1252. Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50 * Math.random()));
  1253. this.forwardChannelRequest_ = request;
  1254. request.xmlHttpPost(uri, requestText, true);
  1255. };
  1256. /**
  1257. * Adds the additional parameters from the handler to the given URI.
  1258. * @param {goog.Uri} uri The URI to add the parameters to.
  1259. * @private
  1260. */
  1261. goog.net.BrowserChannel.prototype.addAdditionalParams_ = function(uri) {
  1262. // Add the additional reconnect parameters as needed.
  1263. if (this.handler_) {
  1264. var params = this.handler_.getAdditionalParams(this);
  1265. if (params) {
  1266. goog.object.forEach(
  1267. params, function(value, key) { uri.setParameterValue(key, value); });
  1268. }
  1269. }
  1270. };
  1271. /**
  1272. * Returns the request text from the outgoing maps and resets it.
  1273. * @return {string} The encoded request text created from all the currently
  1274. * queued outgoing maps.
  1275. * @private
  1276. */
  1277. goog.net.BrowserChannel.prototype.dequeueOutgoingMaps_ = function() {
  1278. var count = Math.min(
  1279. this.outgoingMaps_.length, goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_);
  1280. var sb = ['count=' + count];
  1281. var offset;
  1282. if (this.channelVersion_ > 6 && count > 0) {
  1283. // To save a bit of bandwidth, specify the base mapId and the rest as
  1284. // offsets from it.
  1285. offset = this.outgoingMaps_[0].mapId;
  1286. sb.push('ofs=' + offset);
  1287. } else {
  1288. offset = 0;
  1289. }
  1290. for (var i = 0; i < count; i++) {
  1291. var mapId = this.outgoingMaps_[i].mapId;
  1292. var map = this.outgoingMaps_[i].map;
  1293. if (this.channelVersion_ <= 6) {
  1294. // Map IDs were not used in ver 6 and before, just indexes in the request.
  1295. mapId = i;
  1296. } else {
  1297. mapId -= offset;
  1298. }
  1299. try {
  1300. goog.object.forEach(map, function(value, key, coll) {
  1301. sb.push('req' + mapId + '_' + key + '=' + encodeURIComponent(value));
  1302. });
  1303. } catch (ex) {
  1304. // We send a map here because lots of the retry logic relies on map IDs,
  1305. // so we have to send something.
  1306. sb.push(
  1307. 'req' + mapId + '_' +
  1308. 'type' +
  1309. '=' + encodeURIComponent('_badmap'));
  1310. if (this.handler_) {
  1311. this.handler_.badMapError(this, map);
  1312. }
  1313. }
  1314. }
  1315. this.pendingMaps_ =
  1316. this.pendingMaps_.concat(this.outgoingMaps_.splice(0, count));
  1317. return sb.join('&');
  1318. };
  1319. /**
  1320. * Requeues unacknowledged sent arrays for retransmission in the next forward
  1321. * channel request.
  1322. * @private
  1323. */
  1324. goog.net.BrowserChannel.prototype.requeuePendingMaps_ = function() {
  1325. this.outgoingMaps_ = this.pendingMaps_.concat(this.outgoingMaps_);
  1326. this.pendingMaps_.length = 0;
  1327. };
  1328. /**
  1329. * Ensures there is a backchannel request for receiving data from the server.
  1330. * @private
  1331. */
  1332. goog.net.BrowserChannel.prototype.ensureBackChannel_ = function() {
  1333. if (this.backChannelRequest_) {
  1334. // already have one
  1335. return;
  1336. }
  1337. if (this.backChannelTimerId_) {
  1338. // no need to start a new request - one is already scheduled
  1339. return;
  1340. }
  1341. this.backChannelAttemptId_ = 1;
  1342. this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
  1343. goog.bind(this.onStartBackChannelTimer_, this), 0);
  1344. this.backChannelRetryCount_ = 0;
  1345. };
  1346. /**
  1347. * Schedules a back-channel retry, unless the max retries has been reached.
  1348. * @return {boolean} true iff a retry was scheduled.
  1349. * @private
  1350. */
  1351. goog.net.BrowserChannel.prototype.maybeRetryBackChannel_ = function() {
  1352. if (this.backChannelRequest_ || this.backChannelTimerId_) {
  1353. // Should be impossible to be called in this state.
  1354. this.channelDebug_.severe('Request already in progress');
  1355. return false;
  1356. }
  1357. if (this.backChannelRetryCount_ >= this.getBackChannelMaxRetries()) {
  1358. return false;
  1359. }
  1360. this.channelDebug_.debug('Going to retry GET');
  1361. this.backChannelAttemptId_++;
  1362. this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
  1363. goog.bind(this.onStartBackChannelTimer_, this),
  1364. this.getRetryTime_(this.backChannelRetryCount_));
  1365. this.backChannelRetryCount_++;
  1366. return true;
  1367. };
  1368. /**
  1369. * Timer callback for ensureBackChannel_.
  1370. * @private
  1371. */
  1372. goog.net.BrowserChannel.prototype.onStartBackChannelTimer_ = function() {
  1373. this.backChannelTimerId_ = null;
  1374. this.startBackChannel_();
  1375. };
  1376. /**
  1377. * Begins a new back channel operation to the server.
  1378. * @private
  1379. */
  1380. goog.net.BrowserChannel.prototype.startBackChannel_ = function() {
  1381. if (!this.okToMakeRequest_()) {
  1382. // channel is cancelled
  1383. return;
  1384. }
  1385. this.channelDebug_.debug('Creating new HttpRequest');
  1386. this.backChannelRequest_ = goog.net.BrowserChannel.createChannelRequest(
  1387. this, this.channelDebug_, this.sid_, 'rpc', this.backChannelAttemptId_);
  1388. this.backChannelRequest_.setExtraHeaders(this.extraHeaders_);
  1389. this.backChannelRequest_.setReadyStateChangeThrottle(
  1390. this.readyStateChangeThrottleMs_);
  1391. var uri = this.backChannelUri_.clone();
  1392. uri.setParameterValue('RID', 'rpc');
  1393. uri.setParameterValue('SID', this.sid_);
  1394. uri.setParameterValue('CI', this.useChunked_ ? '0' : '1');
  1395. uri.setParameterValue('AID', this.lastArrayId_);
  1396. // Add the reconnect parameters.
  1397. this.addAdditionalParams_(uri);
  1398. if (!goog.net.ChannelRequest.supportsXhrStreaming()) {
  1399. uri.setParameterValue('TYPE', 'html');
  1400. this.backChannelRequest_.tridentGet(uri, Boolean(this.hostPrefix_));
  1401. } else {
  1402. uri.setParameterValue('TYPE', 'xmlhttp');
  1403. this.backChannelRequest_.xmlHttpGet(
  1404. uri, true /* decodeChunks */, this.hostPrefix_,
  1405. false /* opt_noClose */);
  1406. }
  1407. this.channelDebug_.debug('New Request created');
  1408. };
  1409. /**
  1410. * Gives the handler a chance to return an error code and stop channel
  1411. * execution. A handler might want to do this to check that the user is still
  1412. * logged in, for example.
  1413. * @private
  1414. * @return {boolean} If it's OK to make a request.
  1415. */
  1416. goog.net.BrowserChannel.prototype.okToMakeRequest_ = function() {
  1417. if (this.handler_) {
  1418. var result = this.handler_.okToMakeRequest(this);
  1419. if (result != goog.net.BrowserChannel.Error.OK) {
  1420. this.channelDebug_.debug(
  1421. 'Handler returned error code from ' +
  1422. 'okToMakeRequest');
  1423. this.signalError_(result);
  1424. return false;
  1425. }
  1426. }
  1427. return true;
  1428. };
  1429. /**
  1430. * Callback from BrowserTestChannel for when the channel is finished.
  1431. * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
  1432. * @param {boolean} useChunked Whether we can chunk responses.
  1433. */
  1434. goog.net.BrowserChannel.prototype.testConnectionFinished = function(
  1435. testChannel, useChunked) {
  1436. this.channelDebug_.debug('Test Connection Finished');
  1437. this.useChunked_ = this.allowChunkedMode_ && useChunked;
  1438. this.lastStatusCode_ = testChannel.getLastStatusCode();
  1439. // When using asynchronous test, the channel is already open by connect().
  1440. if (!this.asyncTest_) {
  1441. this.connectChannel_();
  1442. }
  1443. };
  1444. /**
  1445. * Callback from BrowserTestChannel for when the channel has an error.
  1446. * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
  1447. * @param {goog.net.ChannelRequest.Error} errorCode The error code of the
  1448. failure.
  1449. */
  1450. goog.net.BrowserChannel.prototype.testConnectionFailure = function(
  1451. testChannel, errorCode) {
  1452. this.channelDebug_.debug('Test Connection Failed');
  1453. this.lastStatusCode_ = testChannel.getLastStatusCode();
  1454. this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
  1455. };
  1456. /**
  1457. * Callback from BrowserTestChannel for when the channel is blocked.
  1458. * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
  1459. */
  1460. goog.net.BrowserChannel.prototype.testConnectionBlocked = function(
  1461. testChannel) {
  1462. this.channelDebug_.debug('Test Connection Blocked');
  1463. this.lastStatusCode_ = this.connectionTest_.getLastStatusCode();
  1464. this.signalError_(goog.net.BrowserChannel.Error.BLOCKED);
  1465. };
  1466. /**
  1467. * Callback from ChannelRequest for when new data is received
  1468. * @param {goog.net.ChannelRequest} request The request object.
  1469. * @param {string} responseText The text of the response.
  1470. */
  1471. goog.net.BrowserChannel.prototype.onRequestData = function(
  1472. request, responseText) {
  1473. if (this.state_ == goog.net.BrowserChannel.State.CLOSED ||
  1474. (this.backChannelRequest_ != request &&
  1475. this.forwardChannelRequest_ != request)) {
  1476. // either CLOSED or a request we don't know about (perhaps an old request)
  1477. return;
  1478. }
  1479. this.lastStatusCode_ = request.getLastStatusCode();
  1480. if (this.forwardChannelRequest_ == request &&
  1481. this.state_ == goog.net.BrowserChannel.State.OPENED) {
  1482. if (this.channelVersion_ > 7) {
  1483. var response;
  1484. try {
  1485. response = this.parser_.parse(responseText);
  1486. } catch (ex) {
  1487. response = null;
  1488. }
  1489. if (goog.isArray(response) && response.length == 3) {
  1490. this.handlePostResponse_(response);
  1491. } else {
  1492. this.channelDebug_.debug('Bad POST response data returned');
  1493. this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);
  1494. }
  1495. } else if (responseText != goog.net.ChannelDebug.MAGIC_RESPONSE_COOKIE) {
  1496. this.channelDebug_.debug(
  1497. 'Bad data returned - missing/invald ' +
  1498. 'magic cookie');
  1499. this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);
  1500. }
  1501. } else {
  1502. if (this.backChannelRequest_ == request) {
  1503. this.clearDeadBackchannelTimer_();
  1504. }
  1505. if (!goog.string.isEmptyOrWhitespace(responseText)) {
  1506. var response = this.parser_.parse(responseText);
  1507. goog.asserts.assert(goog.isArray(response));
  1508. this.onInput_(/** @type {!Array<?>} */ (response));
  1509. }
  1510. }
  1511. };
  1512. /**
  1513. * Handles a POST response from the server.
  1514. * @param {Array<number>} responseValues The key value pairs in the POST
  1515. * response.
  1516. * @private
  1517. */
  1518. goog.net.BrowserChannel.prototype.handlePostResponse_ = function(
  1519. responseValues) {
  1520. // The first response value is set to 0 if server is missing backchannel.
  1521. if (responseValues[0] == 0) {
  1522. this.handleBackchannelMissing_();
  1523. return;
  1524. }
  1525. this.lastPostResponseArrayId_ = responseValues[1];
  1526. var outstandingArrays = this.lastPostResponseArrayId_ - this.lastArrayId_;
  1527. if (0 < outstandingArrays) {
  1528. var numOutstandingBackchannelBytes = responseValues[2];
  1529. this.channelDebug_.debug(
  1530. numOutstandingBackchannelBytes + ' bytes (in ' + outstandingArrays +
  1531. ' arrays) are outstanding on the BackChannel');
  1532. if (!this.shouldRetryBackChannel_(numOutstandingBackchannelBytes)) {
  1533. return;
  1534. }
  1535. if (!this.deadBackChannelTimerId_) {
  1536. // We expect to receive data within 2 RTTs or we retry the backchannel.
  1537. this.deadBackChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
  1538. goog.bind(this.onBackChannelDead_, this),
  1539. 2 * goog.net.BrowserChannel.RTT_ESTIMATE);
  1540. }
  1541. }
  1542. };
  1543. /**
  1544. * Handles a POST response from the server telling us that it has detected that
  1545. * we have no hanging GET connection.
  1546. * @private
  1547. */
  1548. goog.net.BrowserChannel.prototype.handleBackchannelMissing_ = function() {
  1549. // As long as the back channel was started before the POST was sent,
  1550. // we should retry the backchannel. We give a slight buffer of RTT_ESTIMATE
  1551. // so as not to excessively retry the backchannel
  1552. this.channelDebug_.debug('Server claims our backchannel is missing.');
  1553. if (this.backChannelTimerId_) {
  1554. this.channelDebug_.debug('But we are currently starting the request.');
  1555. return;
  1556. } else if (!this.backChannelRequest_) {
  1557. this.channelDebug_.warning('We do not have a BackChannel established');
  1558. } else if (
  1559. this.backChannelRequest_.getRequestStartTime() +
  1560. goog.net.BrowserChannel.RTT_ESTIMATE <
  1561. this.forwardChannelRequest_.getRequestStartTime()) {
  1562. this.clearDeadBackchannelTimer_();
  1563. this.backChannelRequest_.cancel();
  1564. this.backChannelRequest_ = null;
  1565. } else {
  1566. return;
  1567. }
  1568. this.maybeRetryBackChannel_();
  1569. goog.net.BrowserChannel.notifyStatEvent(
  1570. goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING);
  1571. };
  1572. /**
  1573. * Determines whether we should start the process of retrying a possibly
  1574. * dead backchannel.
  1575. * @param {number} outstandingBytes The number of bytes for which the server has
  1576. * not yet received acknowledgement.
  1577. * @return {boolean} Whether to start the backchannel retry timer.
  1578. * @private
  1579. */
  1580. goog.net.BrowserChannel.prototype.shouldRetryBackChannel_ = function(
  1581. outstandingBytes) {
  1582. // Not too many outstanding bytes, not buffered and not after a retry.
  1583. return outstandingBytes <
  1584. goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF &&
  1585. !this.isBuffered() && this.backChannelRetryCount_ == 0;
  1586. };
  1587. /**
  1588. * Decides which host prefix should be used, if any. If there is a handler,
  1589. * allows the handler to validate a host prefix provided by the server, and
  1590. * optionally override it.
  1591. * @param {?string} serverHostPrefix The host prefix provided by the server.
  1592. * @return {?string} The host prefix to actually use, if any. Will return null
  1593. * if the use of host prefixes was disabled via setAllowHostPrefix().
  1594. */
  1595. goog.net.BrowserChannel.prototype.correctHostPrefix = function(
  1596. serverHostPrefix) {
  1597. if (this.allowHostPrefix_) {
  1598. if (this.handler_) {
  1599. return this.handler_.correctHostPrefix(serverHostPrefix);
  1600. }
  1601. return serverHostPrefix;
  1602. }
  1603. return null;
  1604. };
  1605. /**
  1606. * Handles the timer that indicates that our backchannel is no longer able to
  1607. * successfully receive data from the server.
  1608. * @private
  1609. */
  1610. goog.net.BrowserChannel.prototype.onBackChannelDead_ = function() {
  1611. if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {
  1612. this.deadBackChannelTimerId_ = null;
  1613. this.backChannelRequest_.cancel();
  1614. this.backChannelRequest_ = null;
  1615. this.maybeRetryBackChannel_();
  1616. goog.net.BrowserChannel.notifyStatEvent(
  1617. goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD);
  1618. }
  1619. };
  1620. /**
  1621. * Clears the timer that indicates that our backchannel is no longer able to
  1622. * successfully receive data from the server.
  1623. * @private
  1624. */
  1625. goog.net.BrowserChannel.prototype.clearDeadBackchannelTimer_ = function() {
  1626. if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {
  1627. goog.global.clearTimeout(this.deadBackChannelTimerId_);
  1628. this.deadBackChannelTimerId_ = null;
  1629. }
  1630. };
  1631. /**
  1632. * Returns whether or not the given error/status combination is fatal or not.
  1633. * On fatal errors we immediately close the session rather than retrying the
  1634. * failed request.
  1635. * @param {goog.net.ChannelRequest.Error?} error The error code for the failed
  1636. * request.
  1637. * @param {number} statusCode The last HTTP status code.
  1638. * @return {boolean} Whether or not the error is fatal.
  1639. * @private
  1640. */
  1641. goog.net.BrowserChannel.isFatalError_ = function(error, statusCode) {
  1642. return error == goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID ||
  1643. error == goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED ||
  1644. (error == goog.net.ChannelRequest.Error.STATUS && statusCode > 0);
  1645. };
  1646. /**
  1647. * Callback from ChannelRequest that indicates a request has completed.
  1648. * @param {goog.net.ChannelRequest} request The request object.
  1649. */
  1650. goog.net.BrowserChannel.prototype.onRequestComplete = function(request) {
  1651. this.channelDebug_.debug('Request complete');
  1652. var type;
  1653. if (this.backChannelRequest_ == request) {
  1654. this.clearDeadBackchannelTimer_();
  1655. this.backChannelRequest_ = null;
  1656. type = goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL;
  1657. } else if (this.forwardChannelRequest_ == request) {
  1658. this.forwardChannelRequest_ = null;
  1659. type = goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL;
  1660. } else {
  1661. // return if it was an old request from a previous session
  1662. return;
  1663. }
  1664. this.lastStatusCode_ = request.getLastStatusCode();
  1665. if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {
  1666. return;
  1667. }
  1668. if (request.getSuccess()) {
  1669. // Yay!
  1670. if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) {
  1671. var size = request.getPostData() ? request.getPostData().length : 0;
  1672. goog.net.BrowserChannel.notifyTimingEvent(
  1673. size, goog.now() - request.getRequestStartTime(),
  1674. this.forwardChannelRetryCount_);
  1675. this.ensureForwardChannel_();
  1676. this.onSuccess_();
  1677. this.pendingMaps_.length = 0;
  1678. } else { // i.e., back-channel
  1679. this.ensureBackChannel_();
  1680. }
  1681. return;
  1682. }
  1683. // Else unsuccessful. Fall through.
  1684. var lastError = request.getLastError();
  1685. if (!goog.net.BrowserChannel.isFatalError_(lastError, this.lastStatusCode_)) {
  1686. // Maybe retry.
  1687. this.channelDebug_.debug(
  1688. 'Maybe retrying, last error: ' +
  1689. goog.net.ChannelRequest.errorStringFromCode(
  1690. /** @type {goog.net.ChannelRequest.Error} */ (lastError),
  1691. this.lastStatusCode_));
  1692. if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) {
  1693. if (this.maybeRetryForwardChannel_(request)) {
  1694. return;
  1695. }
  1696. }
  1697. if (type == goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL) {
  1698. if (this.maybeRetryBackChannel_()) {
  1699. return;
  1700. }
  1701. }
  1702. // Else exceeded max retries. Fall through.
  1703. this.channelDebug_.debug('Exceeded max number of retries');
  1704. } else {
  1705. // Else fatal error. Fall through and mark the pending maps as failed.
  1706. this.channelDebug_.debug('Not retrying due to error type');
  1707. }
  1708. // Can't save this session. :(
  1709. this.channelDebug_.debug('Error: HTTP request failed');
  1710. switch (lastError) {
  1711. case goog.net.ChannelRequest.Error.NO_DATA:
  1712. this.signalError_(goog.net.BrowserChannel.Error.NO_DATA);
  1713. break;
  1714. case goog.net.ChannelRequest.Error.BAD_DATA:
  1715. this.signalError_(goog.net.BrowserChannel.Error.BAD_DATA);
  1716. break;
  1717. case goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID:
  1718. this.signalError_(goog.net.BrowserChannel.Error.UNKNOWN_SESSION_ID);
  1719. break;
  1720. case goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED:
  1721. this.signalError_(goog.net.BrowserChannel.Error.ACTIVE_X_BLOCKED);
  1722. break;
  1723. default:
  1724. this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
  1725. break;
  1726. }
  1727. };
  1728. /**
  1729. * @param {number} retryCount Number of retries so far.
  1730. * @return {number} Time in ms before firing next retry request.
  1731. * @private
  1732. */
  1733. goog.net.BrowserChannel.prototype.getRetryTime_ = function(retryCount) {
  1734. var retryTime = this.baseRetryDelayMs_ +
  1735. Math.floor(Math.random() * this.retryDelaySeedMs_);
  1736. if (!this.isActive()) {
  1737. this.channelDebug_.debug('Inactive channel');
  1738. retryTime =
  1739. retryTime * goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR;
  1740. }
  1741. // Backoff for subsequent retries
  1742. retryTime = retryTime * retryCount;
  1743. return retryTime;
  1744. };
  1745. /**
  1746. * @param {number} baseDelayMs The base part of the retry delay, in ms.
  1747. * @param {number} delaySeedMs A random delay between 0 and this is added to
  1748. * the base part.
  1749. */
  1750. goog.net.BrowserChannel.prototype.setRetryDelay = function(
  1751. baseDelayMs, delaySeedMs) {
  1752. this.baseRetryDelayMs_ = baseDelayMs;
  1753. this.retryDelaySeedMs_ = delaySeedMs;
  1754. };
  1755. /**
  1756. * Processes the data returned by the server.
  1757. * @param {!Array<!Array<?>>} respArray The response array returned
  1758. * by the server.
  1759. * @private
  1760. */
  1761. goog.net.BrowserChannel.prototype.onInput_ = function(respArray) {
  1762. var batch =
  1763. this.handler_ && this.handler_.channelHandleMultipleArrays ? [] : null;
  1764. for (var i = 0; i < respArray.length; i++) {
  1765. var nextArray = respArray[i];
  1766. this.lastArrayId_ = nextArray[0];
  1767. nextArray = nextArray[1];
  1768. if (this.state_ == goog.net.BrowserChannel.State.OPENING) {
  1769. if (nextArray[0] == 'c') {
  1770. this.sid_ = nextArray[1];
  1771. this.hostPrefix_ = this.correctHostPrefix(nextArray[2]);
  1772. var negotiatedVersion = nextArray[3];
  1773. if (goog.isDefAndNotNull(negotiatedVersion)) {
  1774. this.channelVersion_ = negotiatedVersion;
  1775. } else {
  1776. // Servers prior to version 7 did not send this, so assume version 6.
  1777. this.channelVersion_ = 6;
  1778. }
  1779. this.state_ = goog.net.BrowserChannel.State.OPENED;
  1780. if (this.handler_) {
  1781. this.handler_.channelOpened(this);
  1782. }
  1783. this.backChannelUri_ = this.getBackChannelUri(
  1784. this.hostPrefix_, /** @type {string} */ (this.path_));
  1785. // Open connection to receive data
  1786. this.ensureBackChannel_();
  1787. } else if (nextArray[0] == 'stop') {
  1788. this.signalError_(goog.net.BrowserChannel.Error.STOP);
  1789. }
  1790. } else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
  1791. if (nextArray[0] == 'stop') {
  1792. if (batch && !goog.array.isEmpty(batch)) {
  1793. this.handler_.channelHandleMultipleArrays(this, batch);
  1794. batch.length = 0;
  1795. }
  1796. this.signalError_(goog.net.BrowserChannel.Error.STOP);
  1797. } else if (nextArray[0] == 'noop') {
  1798. // ignore - noop to keep connection happy
  1799. } else {
  1800. if (batch) {
  1801. batch.push(nextArray);
  1802. } else if (this.handler_) {
  1803. this.handler_.channelHandleArray(this, nextArray);
  1804. }
  1805. }
  1806. // We have received useful data on the back-channel, so clear its retry
  1807. // count. We do this because back-channels by design do not complete
  1808. // quickly, so on a flaky connection we could have many fail to complete
  1809. // fully but still deliver a lot of data before they fail. We don't want
  1810. // to count such failures towards the retry limit, because we don't want
  1811. // to give up on a session if we can still receive data.
  1812. this.backChannelRetryCount_ = 0;
  1813. }
  1814. }
  1815. if (batch && !goog.array.isEmpty(batch)) {
  1816. this.handler_.channelHandleMultipleArrays(this, batch);
  1817. }
  1818. };
  1819. /**
  1820. * Helper to ensure the BrowserChannel is in the expected state.
  1821. * @param {...number} var_args The channel must be in one of the indicated
  1822. * states.
  1823. * @private
  1824. */
  1825. goog.net.BrowserChannel.prototype.ensureInState_ = function(var_args) {
  1826. if (!goog.array.contains(arguments, this.state_)) {
  1827. throw Error('Unexpected channel state: ' + this.state_);
  1828. }
  1829. };
  1830. /**
  1831. * Signals an error has occurred.
  1832. * @param {goog.net.BrowserChannel.Error} error The error code for the failure.
  1833. * @private
  1834. */
  1835. goog.net.BrowserChannel.prototype.signalError_ = function(error) {
  1836. this.channelDebug_.info('Error code ' + error);
  1837. if (error == goog.net.BrowserChannel.Error.REQUEST_FAILED ||
  1838. error == goog.net.BrowserChannel.Error.BLOCKED) {
  1839. // Ping google to check if it's a server error or user's network error.
  1840. var imageUri = null;
  1841. if (this.handler_) {
  1842. imageUri = this.handler_.getNetworkTestImageUri(this);
  1843. }
  1844. goog.net.tmpnetwork.testGoogleCom(
  1845. goog.bind(this.testGoogleComCallback_, this), imageUri);
  1846. } else {
  1847. goog.net.BrowserChannel.notifyStatEvent(
  1848. goog.net.BrowserChannel.Stat.ERROR_OTHER);
  1849. }
  1850. this.onError_(error);
  1851. };
  1852. /**
  1853. * Callback for testGoogleCom during error handling.
  1854. * @param {boolean} networkUp Whether the network is up.
  1855. * @private
  1856. */
  1857. goog.net.BrowserChannel.prototype.testGoogleComCallback_ = function(networkUp) {
  1858. if (networkUp) {
  1859. this.channelDebug_.info('Successfully pinged google.com');
  1860. goog.net.BrowserChannel.notifyStatEvent(
  1861. goog.net.BrowserChannel.Stat.ERROR_OTHER);
  1862. } else {
  1863. this.channelDebug_.info('Failed to ping google.com');
  1864. goog.net.BrowserChannel.notifyStatEvent(
  1865. goog.net.BrowserChannel.Stat.ERROR_NETWORK);
  1866. // We call onError_ here instead of signalError_ because the latter just
  1867. // calls notifyStatEvent, and we don't want to have another stat event.
  1868. this.onError_(goog.net.BrowserChannel.Error.NETWORK);
  1869. }
  1870. };
  1871. /**
  1872. * Called when messages have been successfully sent from the queue.
  1873. * @private
  1874. */
  1875. goog.net.BrowserChannel.prototype.onSuccess_ = function() {
  1876. if (this.handler_) {
  1877. this.handler_.channelSuccess(this, this.pendingMaps_);
  1878. }
  1879. };
  1880. /**
  1881. * Called when we've determined the final error for a channel. It closes the
  1882. * notifiers the handler of the error and closes the channel.
  1883. * @param {goog.net.BrowserChannel.Error} error The error code for the failure.
  1884. * @private
  1885. */
  1886. goog.net.BrowserChannel.prototype.onError_ = function(error) {
  1887. this.channelDebug_.debug('HttpChannel: error - ' + error);
  1888. this.state_ = goog.net.BrowserChannel.State.CLOSED;
  1889. if (this.handler_) {
  1890. this.handler_.channelError(this, error);
  1891. }
  1892. this.onClose_();
  1893. this.cancelRequests_();
  1894. };
  1895. /**
  1896. * Called when the channel has been closed. It notifiers the handler of the
  1897. * event, and reports any pending or undelivered maps.
  1898. * @private
  1899. */
  1900. goog.net.BrowserChannel.prototype.onClose_ = function() {
  1901. this.state_ = goog.net.BrowserChannel.State.CLOSED;
  1902. this.lastStatusCode_ = -1;
  1903. if (this.handler_) {
  1904. if (this.pendingMaps_.length == 0 && this.outgoingMaps_.length == 0) {
  1905. this.handler_.channelClosed(this);
  1906. } else {
  1907. this.channelDebug_.debug(
  1908. 'Number of undelivered maps' +
  1909. ', pending: ' + this.pendingMaps_.length + ', outgoing: ' +
  1910. this.outgoingMaps_.length);
  1911. var copyOfPendingMaps = goog.array.clone(this.pendingMaps_);
  1912. var copyOfUndeliveredMaps = goog.array.clone(this.outgoingMaps_);
  1913. this.pendingMaps_.length = 0;
  1914. this.outgoingMaps_.length = 0;
  1915. this.handler_.channelClosed(
  1916. this, copyOfPendingMaps, copyOfUndeliveredMaps);
  1917. }
  1918. }
  1919. };
  1920. /**
  1921. * Gets the Uri used for the connection that sends data to the server.
  1922. * @param {string} path The path on the host.
  1923. * @return {!goog.Uri} The forward channel URI.
  1924. */
  1925. goog.net.BrowserChannel.prototype.getForwardChannelUri = function(path) {
  1926. var uri = this.createDataUri(null, path);
  1927. this.channelDebug_.debug('GetForwardChannelUri: ' + uri);
  1928. return uri;
  1929. };
  1930. /**
  1931. * Gets the results for the first browser channel test
  1932. * @return {Array<string>} The results.
  1933. */
  1934. goog.net.BrowserChannel.prototype.getFirstTestResults = function() {
  1935. return this.firstTestResults_;
  1936. };
  1937. /**
  1938. * Gets the results for the second browser channel test
  1939. * @return {?boolean} The results. True -> buffered connection,
  1940. * False -> unbuffered, null -> unknown.
  1941. */
  1942. goog.net.BrowserChannel.prototype.getSecondTestResults = function() {
  1943. return this.secondTestResults_;
  1944. };
  1945. /**
  1946. * Gets the Uri used for the connection that receives data from the server.
  1947. * @param {?string} hostPrefix The host prefix.
  1948. * @param {string} path The path on the host.
  1949. * @return {!goog.Uri} The back channel URI.
  1950. */
  1951. goog.net.BrowserChannel.prototype.getBackChannelUri = function(
  1952. hostPrefix, path) {
  1953. var uri = this.createDataUri(
  1954. this.shouldUseSecondaryDomains() ? hostPrefix : null, path);
  1955. this.channelDebug_.debug('GetBackChannelUri: ' + uri);
  1956. return uri;
  1957. };
  1958. /**
  1959. * Creates a data Uri applying logic for secondary hostprefix, port
  1960. * overrides, and versioning.
  1961. * @param {?string} hostPrefix The host prefix.
  1962. * @param {string} path The path on the host (may be absolute or relative).
  1963. * @param {number=} opt_overridePort Optional override port.
  1964. * @return {!goog.Uri} The data URI.
  1965. */
  1966. goog.net.BrowserChannel.prototype.createDataUri = function(
  1967. hostPrefix, path, opt_overridePort) {
  1968. var uri = goog.Uri.parse(path);
  1969. var uriAbsolute = (uri.getDomain() != '');
  1970. if (uriAbsolute) {
  1971. if (hostPrefix) {
  1972. uri.setDomain(hostPrefix + '.' + uri.getDomain());
  1973. }
  1974. uri.setPort(opt_overridePort || uri.getPort());
  1975. } else {
  1976. var locationPage = window.location;
  1977. var hostName;
  1978. if (hostPrefix) {
  1979. hostName = hostPrefix + '.' + locationPage.hostname;
  1980. } else {
  1981. hostName = locationPage.hostname;
  1982. }
  1983. var port = opt_overridePort || locationPage.port;
  1984. uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path);
  1985. }
  1986. if (this.extraParams_) {
  1987. goog.object.forEach(this.extraParams_, function(value, key) {
  1988. uri.setParameterValue(key, value);
  1989. });
  1990. }
  1991. // Add the protocol version to the URI.
  1992. uri.setParameterValue('VER', this.channelVersion_);
  1993. // Add the reconnect parameters.
  1994. this.addAdditionalParams_(uri);
  1995. return uri;
  1996. };
  1997. /**
  1998. * Called when BC needs to create an XhrIo object. Override in a subclass if
  1999. * you need to customize the behavior, for example to enable the creation of
  2000. * XHR's capable of calling a secondary domain. Will also allow calling
  2001. * a secondary domain if withCredentials (CORS) is enabled.
  2002. * @param {?string} hostPrefix The host prefix, if we need an XhrIo object
  2003. * capable of calling a secondary domain.
  2004. * @return {!goog.net.XhrIo} A new XhrIo object.
  2005. */
  2006. goog.net.BrowserChannel.prototype.createXhrIo = function(hostPrefix) {
  2007. if (hostPrefix && !this.supportsCrossDomainXhrs_) {
  2008. throw Error('Can\'t create secondary domain capable XhrIo object.');
  2009. }
  2010. var xhr = new goog.net.XhrIo();
  2011. xhr.setWithCredentials(this.supportsCrossDomainXhrs_);
  2012. return xhr;
  2013. };
  2014. /**
  2015. * Gets whether this channel is currently active. This is used to determine the
  2016. * length of time to wait before retrying. This call delegates to the handler.
  2017. * @return {boolean} Whether the channel is currently active.
  2018. */
  2019. goog.net.BrowserChannel.prototype.isActive = function() {
  2020. return !!this.handler_ && this.handler_.isActive(this);
  2021. };
  2022. /**
  2023. * Wrapper around SafeTimeout which calls the start and end execution hooks
  2024. * with a try...finally block.
  2025. * @param {Function} fn The callback function.
  2026. * @param {number} ms The time in MS for the timer.
  2027. * @return {number} The ID of the timer.
  2028. */
  2029. goog.net.BrowserChannel.setTimeout = function(fn, ms) {
  2030. if (!goog.isFunction(fn)) {
  2031. throw Error('Fn must not be null and must be a function');
  2032. }
  2033. return goog.global.setTimeout(function() {
  2034. goog.net.BrowserChannel.onStartExecution();
  2035. try {
  2036. fn();
  2037. } finally {
  2038. goog.net.BrowserChannel.onEndExecution();
  2039. }
  2040. }, ms);
  2041. };
  2042. /**
  2043. * Helper function to call the start hook
  2044. */
  2045. goog.net.BrowserChannel.onStartExecution = function() {
  2046. goog.net.BrowserChannel.startExecutionHook_();
  2047. };
  2048. /**
  2049. * Helper function to call the end hook
  2050. */
  2051. goog.net.BrowserChannel.onEndExecution = function() {
  2052. goog.net.BrowserChannel.endExecutionHook_();
  2053. };
  2054. /**
  2055. * Returns the singleton event target for stat events.
  2056. * @return {goog.events.EventTarget} The event target for stat events.
  2057. */
  2058. goog.net.BrowserChannel.getStatEventTarget = function() {
  2059. return goog.net.BrowserChannel.statEventTarget_;
  2060. };
  2061. /**
  2062. * Notify the channel that a particular fine grained network event has occurred.
  2063. * Should be considered package-private.
  2064. * @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The
  2065. * reachability event type.
  2066. */
  2067. goog.net.BrowserChannel.prototype.notifyServerReachabilityEvent = function(
  2068. reachabilityType) {
  2069. var target = goog.net.BrowserChannel.statEventTarget_;
  2070. target.dispatchEvent(
  2071. new goog.net.BrowserChannel.ServerReachabilityEvent(
  2072. target, reachabilityType));
  2073. };
  2074. /**
  2075. * Helper function to call the stat event callback.
  2076. * @param {goog.net.BrowserChannel.Stat} stat The stat.
  2077. */
  2078. goog.net.BrowserChannel.notifyStatEvent = function(stat) {
  2079. var target = goog.net.BrowserChannel.statEventTarget_;
  2080. target.dispatchEvent(new goog.net.BrowserChannel.StatEvent(target, stat));
  2081. };
  2082. /**
  2083. * Helper function to notify listeners about POST request performance.
  2084. *
  2085. * @param {number} size Number of characters in the POST data.
  2086. * @param {number} rtt The amount of time from POST start to response.
  2087. * @param {number} retries The number of times the POST had to be retried.
  2088. */
  2089. goog.net.BrowserChannel.notifyTimingEvent = function(size, rtt, retries) {
  2090. var target = goog.net.BrowserChannel.statEventTarget_;
  2091. target.dispatchEvent(
  2092. new goog.net.BrowserChannel.TimingEvent(target, size, rtt, retries));
  2093. };
  2094. /**
  2095. * Determines whether to use a secondary domain when the server gives us
  2096. * a host prefix. This allows us to work around browser per-domain
  2097. * connection limits.
  2098. *
  2099. * Currently, we use secondary domains when using Trident's ActiveXObject,
  2100. * because it supports cross-domain requests out of the box. Note that in IE10
  2101. * we no longer use ActiveX since it's not supported in Metro mode and IE10
  2102. * supports XHR streaming.
  2103. *
  2104. * If you need to use secondary domains on other browsers and IE10,
  2105. * you have two choices:
  2106. * 1) If you only care about browsers that support CORS
  2107. * (https://developer.mozilla.org/en-US/docs/HTTP_access_control), you
  2108. * can use {@link #setSupportsCrossDomainXhrs} and set the appropriate
  2109. * CORS response headers on the server.
  2110. * 2) Or, override this method in a subclass, and make sure that those
  2111. * browsers use some messaging mechanism that works cross-domain (e.g
  2112. * iframes and window.postMessage).
  2113. *
  2114. * @return {boolean} Whether to use secondary domains.
  2115. * @see http://code.google.com/p/closure-library/issues/detail?id=339
  2116. */
  2117. goog.net.BrowserChannel.prototype.shouldUseSecondaryDomains = function() {
  2118. return this.supportsCrossDomainXhrs_ ||
  2119. !goog.net.ChannelRequest.supportsXhrStreaming();
  2120. };
  2121. /**
  2122. * A LogSaver that can be used to accumulate all the debug logs for
  2123. * BrowserChannels so they can be sent to the server when a problem is
  2124. * detected.
  2125. * @const
  2126. */
  2127. goog.net.BrowserChannel.LogSaver = {};
  2128. /**
  2129. * Buffer for accumulating the debug log
  2130. * @type {goog.structs.CircularBuffer}
  2131. * @private
  2132. */
  2133. goog.net.BrowserChannel.LogSaver.buffer_ =
  2134. new goog.structs.CircularBuffer(1000);
  2135. /**
  2136. * Whether we're currently accumulating the debug log.
  2137. * @type {boolean}
  2138. * @private
  2139. */
  2140. goog.net.BrowserChannel.LogSaver.enabled_ = false;
  2141. /**
  2142. * Formatter for saving logs.
  2143. * @type {goog.debug.Formatter}
  2144. * @private
  2145. */
  2146. goog.net.BrowserChannel.LogSaver.formatter_ = new goog.debug.TextFormatter();
  2147. /**
  2148. * Returns whether the LogSaver is enabled.
  2149. * @return {boolean} Whether saving is enabled or disabled.
  2150. */
  2151. goog.net.BrowserChannel.LogSaver.isEnabled = function() {
  2152. return goog.net.BrowserChannel.LogSaver.enabled_;
  2153. };
  2154. /**
  2155. * Enables of disables the LogSaver.
  2156. * @param {boolean} enable Whether to enable or disable saving.
  2157. */
  2158. goog.net.BrowserChannel.LogSaver.setEnabled = function(enable) {
  2159. if (enable == goog.net.BrowserChannel.LogSaver.enabled_) {
  2160. return;
  2161. }
  2162. var fn = goog.net.BrowserChannel.LogSaver.addLogRecord;
  2163. var logger = goog.log.getLogger('goog.net');
  2164. if (enable) {
  2165. goog.log.addHandler(logger, fn);
  2166. } else {
  2167. goog.log.removeHandler(logger, fn);
  2168. }
  2169. };
  2170. /**
  2171. * Adds a log record.
  2172. * @param {goog.log.LogRecord} logRecord the LogRecord.
  2173. */
  2174. goog.net.BrowserChannel.LogSaver.addLogRecord = function(logRecord) {
  2175. goog.net.BrowserChannel.LogSaver.buffer_.add(
  2176. goog.net.BrowserChannel.LogSaver.formatter_.formatRecord(logRecord));
  2177. };
  2178. /**
  2179. * Returns the log as a single string.
  2180. * @return {string} The log as a single string.
  2181. */
  2182. goog.net.BrowserChannel.LogSaver.getBuffer = function() {
  2183. return goog.net.BrowserChannel.LogSaver.buffer_.getValues().join('');
  2184. };
  2185. /**
  2186. * Clears the buffer
  2187. */
  2188. goog.net.BrowserChannel.LogSaver.clearBuffer = function() {
  2189. goog.net.BrowserChannel.LogSaver.buffer_.clear();
  2190. };
  2191. /**
  2192. * Abstract base class for the browser channel handler
  2193. * @constructor
  2194. */
  2195. goog.net.BrowserChannel.Handler = function() {};
  2196. /**
  2197. * Callback handler for when a batch of response arrays is received from the
  2198. * server.
  2199. * @type {?function(!goog.net.BrowserChannel, !Array<!Array<?>>)}
  2200. */
  2201. goog.net.BrowserChannel.Handler.prototype.channelHandleMultipleArrays = null;
  2202. /**
  2203. * Whether it's okay to make a request to the server. A handler can return
  2204. * false if the channel should fail. For example, if the user has logged out,
  2205. * the handler may want all requests to fail immediately.
  2206. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2207. * @return {goog.net.BrowserChannel.Error} An error code. The code should
  2208. * return goog.net.BrowserChannel.Error.OK to indicate it's okay. Any other
  2209. * error code will cause a failure.
  2210. */
  2211. goog.net.BrowserChannel.Handler.prototype.okToMakeRequest = function(
  2212. browserChannel) {
  2213. return goog.net.BrowserChannel.Error.OK;
  2214. };
  2215. /**
  2216. * Indicates the BrowserChannel has successfully negotiated with the server
  2217. * and can now send and receive data.
  2218. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2219. */
  2220. goog.net.BrowserChannel.Handler.prototype.channelOpened = function(
  2221. browserChannel) {};
  2222. /**
  2223. * New input is available for the application to process.
  2224. *
  2225. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2226. * @param {Array<?>} array The data array.
  2227. */
  2228. goog.net.BrowserChannel.Handler.prototype.channelHandleArray = function(
  2229. browserChannel, array) {};
  2230. /**
  2231. * Indicates maps were successfully sent on the BrowserChannel.
  2232. *
  2233. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2234. * @param {Array<goog.net.BrowserChannel.QueuedMap>} deliveredMaps The
  2235. * array of maps that have been delivered to the server. This is a direct
  2236. * reference to the internal BrowserChannel array, so a copy should be made
  2237. * if the caller desires a reference to the data.
  2238. */
  2239. goog.net.BrowserChannel.Handler.prototype.channelSuccess = function(
  2240. browserChannel, deliveredMaps) {};
  2241. /**
  2242. * Indicates an error occurred on the BrowserChannel.
  2243. *
  2244. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2245. * @param {goog.net.BrowserChannel.Error} error The error code.
  2246. */
  2247. goog.net.BrowserChannel.Handler.prototype.channelError = function(
  2248. browserChannel, error) {};
  2249. /**
  2250. * Indicates the BrowserChannel is closed. Also notifies about which maps,
  2251. * if any, that may not have been delivered to the server.
  2252. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2253. * @param {Array<goog.net.BrowserChannel.QueuedMap>=} opt_pendingMaps The
  2254. * array of pending maps, which may or may not have been delivered to the
  2255. * server.
  2256. * @param {Array<goog.net.BrowserChannel.QueuedMap>=} opt_undeliveredMaps
  2257. * The array of undelivered maps, which have definitely not been delivered
  2258. * to the server.
  2259. */
  2260. goog.net.BrowserChannel.Handler.prototype.channelClosed = function(
  2261. browserChannel, opt_pendingMaps, opt_undeliveredMaps) {};
  2262. /**
  2263. * Gets any parameters that should be added at the time another connection is
  2264. * made to the server.
  2265. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2266. * @return {!Object} Extra parameter keys and values to add to the
  2267. * requests.
  2268. */
  2269. goog.net.BrowserChannel.Handler.prototype.getAdditionalParams = function(
  2270. browserChannel) {
  2271. return {};
  2272. };
  2273. /**
  2274. * Gets the URI of an image that can be used to test network connectivity.
  2275. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2276. * @return {goog.Uri?} A custom URI to load for the network test.
  2277. */
  2278. goog.net.BrowserChannel.Handler.prototype.getNetworkTestImageUri = function(
  2279. browserChannel) {
  2280. return null;
  2281. };
  2282. /**
  2283. * Gets whether this channel is currently active. This is used to determine the
  2284. * length of time to wait before retrying.
  2285. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2286. * @return {boolean} Whether the channel is currently active.
  2287. */
  2288. goog.net.BrowserChannel.Handler.prototype.isActive = function(browserChannel) {
  2289. return true;
  2290. };
  2291. /**
  2292. * Called by the channel if enumeration of the map throws an exception.
  2293. * @param {goog.net.BrowserChannel} browserChannel The browser channel.
  2294. * @param {Object} map The map that can't be enumerated.
  2295. */
  2296. goog.net.BrowserChannel.Handler.prototype.badMapError = function(
  2297. browserChannel, map) {
  2298. return;
  2299. };
  2300. /**
  2301. * Allows the handler to override a host prefix provided by the server. Will
  2302. * be called whenever the channel has received such a prefix and is considering
  2303. * its use.
  2304. * @param {?string} serverHostPrefix The host prefix provided by the server.
  2305. * @return {?string} The host prefix the client should use.
  2306. */
  2307. goog.net.BrowserChannel.Handler.prototype.correctHostPrefix = function(
  2308. serverHostPrefix) {
  2309. return serverHostPrefix;
  2310. };