Path: blob/trunk/third_party/closure/goog/labs/net/webchannel/webchannelbase.js
1865 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Base WebChannel implementation.16*17*/181920goog.provide('goog.labs.net.webChannel.WebChannelBase');2122goog.require('goog.Uri');23goog.require('goog.array');24goog.require('goog.asserts');25goog.require('goog.debug.TextFormatter');26goog.require('goog.json');27goog.require('goog.labs.net.webChannel.BaseTestChannel');28goog.require('goog.labs.net.webChannel.Channel');29goog.require('goog.labs.net.webChannel.ChannelRequest');30goog.require('goog.labs.net.webChannel.ConnectionState');31goog.require('goog.labs.net.webChannel.ForwardChannelRequestPool');32goog.require('goog.labs.net.webChannel.WebChannelDebug');33goog.require('goog.labs.net.webChannel.Wire');34goog.require('goog.labs.net.webChannel.WireV8');35goog.require('goog.labs.net.webChannel.netUtils');36goog.require('goog.labs.net.webChannel.requestStats');37goog.require('goog.labs.net.webChannel.requestStats.Stat');38goog.require('goog.log');39goog.require('goog.net.WebChannel');40goog.require('goog.net.XhrIo');41goog.require('goog.net.rpc.HttpCors');42goog.require('goog.object');43goog.require('goog.string');44goog.require('goog.structs');45goog.require('goog.structs.CircularBuffer');4647goog.scope(function() {48var WebChannel = goog.net.WebChannel;49var BaseTestChannel = goog.labs.net.webChannel.BaseTestChannel;50var ChannelRequest = goog.labs.net.webChannel.ChannelRequest;51var ConnectionState = goog.labs.net.webChannel.ConnectionState;52var ForwardChannelRequestPool =53goog.labs.net.webChannel.ForwardChannelRequestPool;54var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug;55var Wire = goog.labs.net.webChannel.Wire;56var WireV8 = goog.labs.net.webChannel.WireV8;57var netUtils = goog.labs.net.webChannel.netUtils;58var requestStats = goog.labs.net.webChannel.requestStats;5960var httpCors = goog.module.get('goog.net.rpc.HttpCors');616263/**64* This WebChannel implementation is branched off goog.net.BrowserChannel65* for now. Ongoing changes to goog.net.BrowserChannel will be back66* ported to this implementation as needed.67*68* @param {!goog.net.WebChannel.Options=} opt_options Configuration for the69* WebChannel instance.70* @param {number=} opt_clientVersion An application-specific version number71* that is sent to the server when connected.72* @param {!ConnectionState=} opt_conn Previously determined connection73* conditions.74* @constructor75* @struct76* @implements {goog.labs.net.webChannel.Channel}77*/78goog.labs.net.webChannel.WebChannelBase = function(79opt_options, opt_clientVersion, opt_conn) {80/**81* The client library version (capabilities).82* @private {number}83*/84this.clientVersion_ = opt_clientVersion || 0;8586/**87* The server library version (capabilities).88* @private {number}89*/90this.serverVersion_ = 0;919293/**94* An array of queued maps that need to be sent to the server.95* @private {!Array<Wire.QueuedMap>}96*/97this.outgoingMaps_ = [];9899/**100* An array of dequeued maps that we have either received a non-successful101* response for, or no response at all, and which therefore may or may not102* have been received by the server.103* @private {!Array<Wire.QueuedMap>}104*/105this.pendingMaps_ = [];106107/**108* The channel debug used for logging109* @private {!WebChannelDebug}110*/111this.channelDebug_ = new WebChannelDebug();112113/**114* Previous connectivity test results.115* @private {!ConnectionState}116*/117this.connState_ = opt_conn || new ConnectionState();118119/**120* Extra HTTP headers to add to all the requests sent to the server.121* @private {Object}122*/123this.extraHeaders_ = null;124125/**126* Extra HTTP headers to add to the init request(s) sent to the server.127* @private {Object}128*/129this.initHeaders_ = null;130131/**132* @private {?string} The URL param name to overwrite custom HTTP headers133* to bypass CORS preflight.134*/135this.httpHeadersOverwriteParam_ = null;136137/**138* Extra parameters to add to all the requests sent to the server.139* @private {Object}140*/141this.extraParams_ = null;142143/**144* Parameter name for the http session id.145* @private {?string}146*/147this.httpSessionIdParam_ = null;148149/**150* The http session id, to be sent with httpSessionIdParam_ with each151* request after the initial handshake.152* @private {?string}153*/154this.httpSessionId_ = null;155156/**157* The ChannelRequest object for the backchannel.158* @private {ChannelRequest}159*/160this.backChannelRequest_ = null;161162/**163* The relative path (in the context of the the page hosting the browser164* channel) for making requests to the server.165* @private {?string}166*/167this.path_ = null;168169/**170* The absolute URI for the forwardchannel request.171* @private {goog.Uri}172*/173this.forwardChannelUri_ = null;174175/**176* The absolute URI for the backchannel request.177* @private {goog.Uri}178*/179this.backChannelUri_ = null;180181/**182* A subdomain prefix for using a subdomain in IE for the backchannel183* requests.184* @private {?string}185*/186this.hostPrefix_ = null;187188/**189* Whether we allow the use of a subdomain in IE for the backchannel requests.190* @private {boolean}191*/192this.allowHostPrefix_ = true;193194/**195* The next id to use for the RID (request identifier) parameter. This196* identifier uniquely identifies the forward channel request.197* @private {number}198*/199this.nextRid_ = 0;200201/**202* The id to use for the next outgoing map. This identifier uniquely203* identifies a sent map.204* @private {number}205*/206this.nextMapId_ = 0;207208/**209* Whether to fail forward-channel requests after one try or a few tries.210* @private {boolean}211*/212this.failFast_ = false;213214/**215* The handler that receive callbacks for state changes and data.216* @private {goog.labs.net.webChannel.WebChannelBase.Handler}217*/218this.handler_ = null;219220/**221* Timer identifier for asynchronously making a forward channel request.222* @private {?number}223*/224this.forwardChannelTimerId_ = null;225226/**227* Timer identifier for asynchronously making a back channel request.228* @private {?number}229*/230this.backChannelTimerId_ = null;231232/**233* Timer identifier for the timer that waits for us to retry the backchannel234* in the case where it is dead and no longer receiving data.235* @private {?number}236*/237this.deadBackChannelTimerId_ = null;238239/**240* The TestChannel object which encapsulates the logic for determining241* interesting network conditions about the client.242* @private {BaseTestChannel}243*/244this.connectionTest_ = null;245246/**247* Whether the client's network conditions can support chunked responses.248* @private {?boolean}249*/250this.useChunked_ = null;251252/**253* Whether chunked mode is allowed. In certain debugging situations, it's254* useful to disable this.255* @private {boolean}256*/257this.allowChunkedMode_ = true;258259/**260* The array identifier of the last array received from the server for the261* backchannel request.262* @private {number}263*/264this.lastArrayId_ = -1;265266/**267* The array id of the last array sent by the server that we know about.268* @private {number}269*/270this.lastPostResponseArrayId_ = -1;271272/**273* The last status code received.274* @private {number}275*/276this.lastStatusCode_ = -1;277278/**279* Number of times we have retried the current forward channel request.280* @private {number}281*/282this.forwardChannelRetryCount_ = 0;283284/**285* Number of times in a row that we have retried the current back channel286* request and received no data.287* @private {number}288*/289this.backChannelRetryCount_ = 0;290291/**292* The attempt id for the current back channel request. Starts at 1 and293* increments for each reconnect. The server uses this to log if our294* connection is flaky or not.295* @private {number}296*/297this.backChannelAttemptId_ = 0;298299/**300* The base part of the time before firing next retry request. Default is 5301* seconds. Note that a random delay is added (see {@link retryDelaySeedMs_})302* for all retries, and linear backoff is applied to the sum for subsequent303* retries.304* @private {number}305*/306this.baseRetryDelayMs_ = 5 * 1000;307308/**309* A random time between 0 and this number of MS is added to the310* {@link baseRetryDelayMs_}. Default is 10 seconds.311* @private {number}312*/313this.retryDelaySeedMs_ = 10 * 1000;314315/**316* Maximum number of attempts to connect to the server for forward channel317* requests. Defaults to 2.318* @private {number}319*/320this.forwardChannelMaxRetries_ = 2;321322/**323* The timeout in milliseconds for a forward channel request. Defaults to 20324* seconds. Note that part of this timeout can be randomized.325* @private {number}326*/327this.forwardChannelRequestTimeoutMs_ = 20 * 1000;328329/**330* A throttle time in ms for readystatechange events for the backchannel.331* Useful for throttling when ready state is INTERACTIVE (partial data).332*333* This throttle is useful if the server sends large data chunks down the334* backchannel. It prevents examining XHR partial data on every readystate335* change event. This is useful because large chunks can trigger hundreds336* of readystatechange events, each of which takes ~5ms or so to handle,337* in turn making the UI unresponsive for a significant period.338*339* If set to zero no throttle is used.340* @private {number}341*/342this.readyStateChangeThrottleMs_ = 0;343344/**345* Whether cross origin requests are supported for the channel.346*347* See {@link goog.net.XhrIo#setWithCredentials}.348* @private {boolean}349*/350this.supportsCrossDomainXhrs_ =351(opt_options && opt_options.supportsCrossDomainXhr) || false;352353/**354* The current session id.355* @private {string}356*/357this.sid_ = '';358359/**360* The current ChannelRequest pool for the forward channel.361* @private {!ForwardChannelRequestPool}362*/363this.forwardChannelRequestPool_ = new ForwardChannelRequestPool(364opt_options && opt_options.concurrentRequestLimit);365366/**367* The V8 codec.368* @private {!WireV8}369*/370this.wireCodec_ = new WireV8();371372/**373* Whether to run the channel test as a background process to not block374* the OPEN event.375*376* @private {boolean}377*/378this.backgroundChannelTest_ =379(opt_options && opt_options.backgroundChannelTest) || false;380};381382var WebChannelBase = goog.labs.net.webChannel.WebChannelBase;383384385/**386* The channel version that we negotiated with the server for this session.387* Starts out as the version we request, and then is changed to the negotiated388* version after the initial open.389* @private {number}390*/391WebChannelBase.prototype.channelVersion_ = Wire.LATEST_CHANNEL_VERSION;392393394/**395* Enum type for the channel state machine.396* @enum {number}397*/398WebChannelBase.State = {399/** The channel is closed. */400CLOSED: 0,401402/** The channel has been initialized but hasn't yet initiated a connection. */403INIT: 1,404405/** The channel is in the process of opening a connection to the server. */406OPENING: 2,407408/** The channel is open. */409OPENED: 3410};411412413/**414* The current state of the WebChannel.415* @private {!WebChannelBase.State}416*/417WebChannelBase.prototype.state_ = WebChannelBase.State.INIT;418419420/**421* The timeout in milliseconds for a forward channel request.422* @type {number}423*/424WebChannelBase.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;425426427/**428* Maximum number of attempts to connect to the server for back channel429* requests.430* @type {number}431*/432WebChannelBase.BACK_CHANNEL_MAX_RETRIES = 3;433434435/**436* A number in MS of how long we guess the maxmium amount of time a round trip437* to the server should take. In the future this could be substituted with a438* real measurement of the RTT.439* @type {number}440*/441WebChannelBase.RTT_ESTIMATE = 3 * 1000;442443444/**445* When retrying for an inactive channel, we will multiply the total delay by446* this number.447* @type {number}448*/449WebChannelBase.INACTIVE_CHANNEL_RETRY_FACTOR = 2;450451452/**453* Enum type for identifying an error.454* @enum {number}455*/456WebChannelBase.Error = {457/** Value that indicates no error has occurred. */458OK: 0,459460/** An error due to a request failing. */461REQUEST_FAILED: 2,462463/** An error due to the user being logged out. */464LOGGED_OUT: 4,465466/** An error due to server response which contains no data. */467NO_DATA: 5,468469/** An error due to a server response indicating an unknown session id */470UNKNOWN_SESSION_ID: 6,471472/** An error due to a server response requesting to stop the channel. */473STOP: 7,474475/** A general network error. */476NETWORK: 8,477478/** An error due to bad data being returned from the server. */479BAD_DATA: 10,480481/** An error due to a response that is not parsable. */482BAD_RESPONSE: 11483};484485486/**487* Internal enum type for the two channel types.488* @enum {number}489* @private490*/491WebChannelBase.ChannelType_ = {492FORWARD_CHANNEL: 1,493494BACK_CHANNEL: 2495};496497498/**499* The maximum number of maps that can be sent in one POST. Should match500* MAX_MAPS_PER_REQUEST on the server code.501* @type {number}502* @private503*/504WebChannelBase.MAX_MAPS_PER_REQUEST_ = 1000;505506507/**508* A guess at a cutoff at which to no longer assume the backchannel is dead509* when we are slow to receive data. Number in bytes.510*511* Assumption: The worst bandwidth we work on is 50 kilobits/sec512* 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout513* @type {number}514*/515WebChannelBase.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500;516517518/**519* @return {!ForwardChannelRequestPool} The forward channel request pool.520*/521WebChannelBase.prototype.getForwardChannelRequestPool = function() {522return this.forwardChannelRequestPool_;523};524525526/**527* @return {!Object} The codec object, to be used for the test channel.528*/529WebChannelBase.prototype.getWireCodec = function() {530return this.wireCodec_;531};532533534/**535* Returns the logger.536*537* @return {!WebChannelDebug} The channel debug object.538*/539WebChannelBase.prototype.getChannelDebug = function() {540return this.channelDebug_;541};542543544/**545* Sets the logger.546*547* @param {!WebChannelDebug} channelDebug The channel debug object.548*/549WebChannelBase.prototype.setChannelDebug = function(channelDebug) {550this.channelDebug_ = channelDebug;551};552553554/**555* Starts the channel. This initiates connections to the server.556*557* @param {string} testPath The path for the test connection.558* @param {string} channelPath The path for the channel connection.559* @param {!Object=} opt_extraParams Extra parameter keys and values to add to560* the requests.561* @param {string=} opt_oldSessionId Session ID from a previous session.562* @param {number=} opt_oldArrayId The last array ID from a previous session.563*/564WebChannelBase.prototype.connect = function(565testPath, channelPath, opt_extraParams, opt_oldSessionId, opt_oldArrayId) {566this.channelDebug_.debug('connect()');567568requestStats.notifyStatEvent(requestStats.Stat.CONNECT_ATTEMPT);569570this.path_ = channelPath;571this.extraParams_ = opt_extraParams || {};572573// Attach parameters about the previous session if reconnecting.574if (opt_oldSessionId && goog.isDef(opt_oldArrayId)) {575this.extraParams_['OSID'] = opt_oldSessionId;576this.extraParams_['OAID'] = opt_oldArrayId;577}578579if (this.backgroundChannelTest_) {580this.channelDebug_.debug('connect() bypassed channel-test.');581this.connState_.handshakeResult = [];582this.connState_.bufferingProxyResult = false;583584// TODO(user): merge states with background channel test585// requestStats.setTimeout(goog.bind(this.connectTest_, this, testPath), 0);586// this.connectChannel_();587}588589this.connectTest_(testPath);590};591592593/**594* Disconnects and closes the channel.595*/596WebChannelBase.prototype.disconnect = function() {597this.channelDebug_.debug('disconnect()');598599this.cancelRequests_();600601if (this.state_ == WebChannelBase.State.OPENED) {602var rid = this.nextRid_++;603var uri = this.forwardChannelUri_.clone();604uri.setParameterValue('SID', this.sid_);605uri.setParameterValue('RID', rid);606uri.setParameterValue('TYPE', 'terminate');607608// Add the reconnect parameters.609this.addAdditionalParams_(uri);610611var request = ChannelRequest.createChannelRequest(612this, this.channelDebug_, this.sid_, rid);613request.sendCloseRequest(uri);614}615616this.onClose_();617};618619620/**621* Returns the session id of the channel. Only available after the622* channel has been opened.623* @return {string} Session ID.624*/625WebChannelBase.prototype.getSessionId = function() {626return this.sid_;627};628629630/**631* Starts the test channel to determine network conditions.632*633* @param {string} testPath The relative PATH for the test connection.634* @private635*/636WebChannelBase.prototype.connectTest_ = function(testPath) {637this.channelDebug_.debug('connectTest_()');638if (!this.okToMakeRequest_()) {639return; // channel is cancelled640}641this.connectionTest_ = new BaseTestChannel(this, this.channelDebug_);642643if (this.httpHeadersOverwriteParam_ === null) {644this.connectionTest_.setExtraHeaders(this.extraHeaders_);645}646647var urlPath = testPath;648if (this.httpHeadersOverwriteParam_ && this.extraHeaders_) {649urlPath = httpCors.setHttpHeadersWithOverwriteParam(650testPath, this.httpHeadersOverwriteParam_, this.extraHeaders_);651}652653this.connectionTest_.connect(/** @type {string} */ (urlPath));654};655656657/**658* Starts the regular channel which is run after the test channel is complete.659* @private660*/661WebChannelBase.prototype.connectChannel_ = function() {662this.channelDebug_.debug('connectChannel_()');663this.ensureInState_(WebChannelBase.State.INIT, WebChannelBase.State.CLOSED);664this.forwardChannelUri_ =665this.getForwardChannelUri(/** @type {string} */ (this.path_));666this.ensureForwardChannel_();667};668669670/**671* Cancels all outstanding requests.672* @private673*/674WebChannelBase.prototype.cancelRequests_ = function() {675if (this.connectionTest_) {676this.connectionTest_.abort();677this.connectionTest_ = null;678}679680if (this.backChannelRequest_) {681this.backChannelRequest_.cancel();682this.backChannelRequest_ = null;683}684685if (this.backChannelTimerId_) {686goog.global.clearTimeout(this.backChannelTimerId_);687this.backChannelTimerId_ = null;688}689690this.clearDeadBackchannelTimer_();691692this.forwardChannelRequestPool_.cancel();693694if (this.forwardChannelTimerId_) {695goog.global.clearTimeout(this.forwardChannelTimerId_);696this.forwardChannelTimerId_ = null;697}698};699700701/**702* Returns the extra HTTP headers to add to all the requests sent to the server.703*704* @return {Object} The HTTP headers, or null.705*/706WebChannelBase.prototype.getExtraHeaders = function() {707return this.extraHeaders_;708};709710711/**712* Sets extra HTTP headers to add to all the requests sent to the server.713*714* @param {Object} extraHeaders The HTTP headers, or null.715*/716WebChannelBase.prototype.setExtraHeaders = function(extraHeaders) {717this.extraHeaders_ = extraHeaders;718};719720721/**722* Returns the extra HTTP headers to add to the init requests723* sent to the server.724*725* @return {Object} The HTTP headers, or null.726*/727WebChannelBase.prototype.getInitHeaders = function() {728return this.initHeaders_;729};730731732/**733* Sets extra HTTP headers to add to the init requests sent to the server.734*735* @param {Object} initHeaders The HTTP headers, or null.736*/737WebChannelBase.prototype.setInitHeaders = function(initHeaders) {738this.initHeaders_ = initHeaders;739};740741742/**743* Sets the URL param name to overwrite custom HTTP headers.744*745* @param {string} httpHeadersOverwriteParam The URL param name.746*/747WebChannelBase.prototype.setHttpHeadersOverwriteParam = function(748httpHeadersOverwriteParam) {749this.httpHeadersOverwriteParam_ = httpHeadersOverwriteParam;750};751752753/**754* @override755*/756WebChannelBase.prototype.setHttpSessionIdParam = function(httpSessionIdParam) {757this.httpSessionIdParam_ = httpSessionIdParam;758};759760761/**762* @override763*/764WebChannelBase.prototype.getHttpSessionIdParam = function() {765return this.httpSessionIdParam_;766};767768769/**770* @override771*/772WebChannelBase.prototype.setHttpSessionId = function(httpSessionId) {773this.httpSessionId_ = httpSessionId;774};775776777/**778* @override779*/780WebChannelBase.prototype.getHttpSessionId = function() {781return this.httpSessionId_;782};783784785/**786* @override787*/788WebChannelBase.prototype.getBackgroundChannelTest = function() {789return this.backgroundChannelTest_;790};791792793/**794* Sets the throttle for handling onreadystatechange events for the request.795*796* @param {number} throttle The throttle in ms. A value of zero indicates797* no throttle.798*/799WebChannelBase.prototype.setReadyStateChangeThrottle = function(throttle) {800this.readyStateChangeThrottleMs_ = throttle;801};802803804/**805* Sets whether cross origin requests are supported for the channel.806*807* Setting this allows the creation of requests to secondary domains and808* sends XHRs with the CORS withCredentials bit set to true.809*810* In order for cross-origin requests to work, the server will also need to set811* CORS response headers as per:812* https://developer.mozilla.org/en-US/docs/HTTP_access_control813*814* See {@link goog.net.XhrIo#setWithCredentials}.815* @param {boolean} supportCrossDomain Whether cross domain XHRs are supported.816*/817WebChannelBase.prototype.setSupportsCrossDomainXhrs = function(818supportCrossDomain) {819this.supportsCrossDomainXhrs_ = supportCrossDomain;820};821822823/**824* Returns the handler used for channel callback events.825*826* @return {WebChannelBase.Handler} The handler.827*/828WebChannelBase.prototype.getHandler = function() {829return this.handler_;830};831832833/**834* Sets the handler used for channel callback events.835* @param {WebChannelBase.Handler} handler The handler to set.836*/837WebChannelBase.prototype.setHandler = function(handler) {838this.handler_ = handler;839};840841842/**843* Returns whether the channel allows the use of a subdomain. There may be844* cases where this isn't allowed.845* @return {boolean} Whether a host prefix is allowed.846*/847WebChannelBase.prototype.getAllowHostPrefix = function() {848return this.allowHostPrefix_;849};850851852/**853* Sets whether the channel allows the use of a subdomain. There may be cases854* where this isn't allowed, for example, logging in with troutboard where855* using a subdomain causes Apache to force the user to authenticate twice.856* @param {boolean} allowHostPrefix Whether a host prefix is allowed.857*/858WebChannelBase.prototype.setAllowHostPrefix = function(allowHostPrefix) {859this.allowHostPrefix_ = allowHostPrefix;860};861862863/**864* Returns whether the channel is buffered or not. This state is valid for865* querying only after the test connection has completed. This may be866* queried in the WebChannelBase.okToMakeRequest() callback.867* A channel may be buffered if the test connection determines that868* a chunked response could not be sent down within a suitable time.869* @return {boolean} Whether the channel is buffered.870*/871WebChannelBase.prototype.isBuffered = function() {872return !this.useChunked_;873};874875876/**877* Returns whether chunked mode is allowed. In certain debugging situations,878* it's useful for the application to have a way to disable chunked mode for a879* user.880881* @return {boolean} Whether chunked mode is allowed.882*/883WebChannelBase.prototype.getAllowChunkedMode = function() {884return this.allowChunkedMode_;885};886887888/**889* Sets whether chunked mode is allowed. In certain debugging situations, it's890* useful for the application to have a way to disable chunked mode for a user.891* @param {boolean} allowChunkedMode Whether chunked mode is allowed.892*/893WebChannelBase.prototype.setAllowChunkedMode = function(allowChunkedMode) {894this.allowChunkedMode_ = allowChunkedMode;895};896897898/**899* Sends a request to the server. The format of the request is a Map data900* structure of key/value pairs. These maps are then encoded in a format901* suitable for the wire and then reconstituted as a Map data structure that902* the server can process.903* @param {!Object|!goog.structs.Map} map The map to send.904* @param {!Object=} opt_context The context associated with the map.905*/906WebChannelBase.prototype.sendMap = function(map, opt_context) {907goog.asserts.assert(908this.state_ != WebChannelBase.State.CLOSED,909'Invalid operation: sending map when state is closed');910911// We can only send 1000 maps per POST, but typically we should never have912// that much to send, so warn if we exceed that (we still send all the maps).913if (this.outgoingMaps_.length == WebChannelBase.MAX_MAPS_PER_REQUEST_) {914// severe() is temporary so that we get these uploaded and can figure out915// what's causing them. Afterwards can change to warning().916this.channelDebug_.severe(917'Already have ' + WebChannelBase.MAX_MAPS_PER_REQUEST_ +918' queued maps upon queueing ' + goog.json.serialize(map));919}920921this.outgoingMaps_.push(922new Wire.QueuedMap(this.nextMapId_++, map, opt_context));923if (this.state_ == WebChannelBase.State.OPENING ||924this.state_ == WebChannelBase.State.OPENED) {925this.ensureForwardChannel_();926}927};928929930/**931* When set to true, this changes the behavior of the forward channel so it932* will not retry requests; it will fail after one network failure, and if933* there was already one network failure, the request will fail immediately.934* @param {boolean} failFast Whether or not to fail fast.935*/936WebChannelBase.prototype.setFailFast = function(failFast) {937this.failFast_ = failFast;938this.channelDebug_.info('setFailFast: ' + failFast);939if ((this.forwardChannelRequestPool_.hasPendingRequest() ||940this.forwardChannelTimerId_) &&941this.forwardChannelRetryCount_ > this.getForwardChannelMaxRetries()) {942this.channelDebug_.info(943'Retry count ' + this.forwardChannelRetryCount_ + ' > new maxRetries ' +944this.getForwardChannelMaxRetries() + '. Fail immediately!');945946if (!this.forwardChannelRequestPool_.forceComplete(947goog.bind(this.onRequestComplete, this))) {948// i.e., this.forwardChannelTimerId_949goog.global.clearTimeout(this.forwardChannelTimerId_);950this.forwardChannelTimerId_ = null;951// The error code from the last failed request is gone, so just use a952// generic one.953this.signalError_(WebChannelBase.Error.REQUEST_FAILED);954}955}956};957958959/**960* @return {number} The max number of forward-channel retries, which will be 0961* in fail-fast mode.962*/963WebChannelBase.prototype.getForwardChannelMaxRetries = function() {964return this.failFast_ ? 0 : this.forwardChannelMaxRetries_;965};966967968/**969* Sets the maximum number of attempts to connect to the server for forward970* channel requests.971* @param {number} retries The maximum number of attempts.972*/973WebChannelBase.prototype.setForwardChannelMaxRetries = function(retries) {974this.forwardChannelMaxRetries_ = retries;975};976977978/**979* Sets the timeout for a forward channel request.980* @param {number} timeoutMs The timeout in milliseconds.981*/982WebChannelBase.prototype.setForwardChannelRequestTimeout = function(timeoutMs) {983this.forwardChannelRequestTimeoutMs_ = timeoutMs;984};985986987/**988* @return {number} The max number of back-channel retries, which is a constant.989*/990WebChannelBase.prototype.getBackChannelMaxRetries = function() {991// Back-channel retries is a constant.992return WebChannelBase.BACK_CHANNEL_MAX_RETRIES;993};994995996/**997* @override998*/999WebChannelBase.prototype.isClosed = function() {1000return this.state_ == WebChannelBase.State.CLOSED;1001};100210031004/**1005* Returns the channel state.1006* @return {WebChannelBase.State} The current state of the channel.1007*/1008WebChannelBase.prototype.getState = function() {1009return this.state_;1010};101110121013/**1014* Return the last status code received for a request.1015* @return {number} The last status code received for a request.1016*/1017WebChannelBase.prototype.getLastStatusCode = function() {1018return this.lastStatusCode_;1019};102010211022/**1023* @return {number} The last array id received.1024*/1025WebChannelBase.prototype.getLastArrayId = function() {1026return this.lastArrayId_;1027};102810291030/**1031* Returns whether there are outstanding requests servicing the channel.1032* @return {boolean} true if there are outstanding requests.1033*/1034WebChannelBase.prototype.hasOutstandingRequests = function() {1035return this.getOutstandingRequests_() != 0;1036};103710381039/**1040* Returns the number of outstanding requests.1041* @return {number} The number of outstanding requests to the server.1042* @private1043*/1044WebChannelBase.prototype.getOutstandingRequests_ = function() {1045var count = 0;1046if (this.backChannelRequest_) {1047count++;1048}1049count += this.forwardChannelRequestPool_.getRequestCount();1050return count;1051};105210531054/**1055* Ensures that a forward channel request is scheduled.1056* @private1057*/1058WebChannelBase.prototype.ensureForwardChannel_ = function() {1059if (this.forwardChannelRequestPool_.isFull()) {1060// enough connection in process - no need to start a new request1061return;1062}10631064if (this.forwardChannelTimerId_) {1065// no need to start a new request - one is already scheduled1066return;1067}10681069this.forwardChannelTimerId_ = requestStats.setTimeout(1070goog.bind(this.onStartForwardChannelTimer_, this), 0);1071this.forwardChannelRetryCount_ = 0;1072};107310741075/**1076* Schedules a forward-channel retry for the specified request, unless the max1077* retries has been reached.1078* @param {ChannelRequest} request The failed request to retry.1079* @return {boolean} true iff a retry was scheduled.1080* @private1081*/1082WebChannelBase.prototype.maybeRetryForwardChannel_ = function(request) {1083if (this.forwardChannelRequestPool_.isFull() || this.forwardChannelTimerId_) {1084// Should be impossible to be called in this state.1085this.channelDebug_.severe('Request already in progress');1086return false;1087}10881089if (this.state_ == WebChannelBase.State.INIT || // no retry open_()1090(this.forwardChannelRetryCount_ >= this.getForwardChannelMaxRetries())) {1091return false;1092}10931094this.channelDebug_.debug('Going to retry POST');10951096this.forwardChannelTimerId_ = requestStats.setTimeout(1097goog.bind(this.onStartForwardChannelTimer_, this, request),1098this.getRetryTime_(this.forwardChannelRetryCount_));1099this.forwardChannelRetryCount_++;1100return true;1101};110211031104/**1105* Timer callback for ensureForwardChannel1106* @param {ChannelRequest=} opt_retryRequest A failed request1107* to retry.1108* @private1109*/1110WebChannelBase.prototype.onStartForwardChannelTimer_ = function(1111opt_retryRequest) {1112this.forwardChannelTimerId_ = null;1113this.startForwardChannel_(opt_retryRequest);1114};111511161117/**1118* Begins a new forward channel operation to the server.1119* @param {ChannelRequest=} opt_retryRequest A failed request to retry.1120* @private1121*/1122WebChannelBase.prototype.startForwardChannel_ = function(opt_retryRequest) {1123this.channelDebug_.debug('startForwardChannel_');1124if (!this.okToMakeRequest_()) {1125return; // channel is cancelled1126} else if (this.state_ == WebChannelBase.State.INIT) {1127if (opt_retryRequest) {1128this.channelDebug_.severe('Not supposed to retry the open');1129return;1130}1131this.open_();1132this.state_ = WebChannelBase.State.OPENING;1133} else if (this.state_ == WebChannelBase.State.OPENED) {1134if (opt_retryRequest) {1135this.makeForwardChannelRequest_(opt_retryRequest);1136return;1137}11381139if (this.outgoingMaps_.length == 0) {1140this.channelDebug_.debug(1141'startForwardChannel_ returned: ' +1142'nothing to send');1143// no need to start a new forward channel request1144return;1145}11461147if (this.forwardChannelRequestPool_.isFull()) {1148// Should be impossible to be called in this state.1149this.channelDebug_.severe(1150'startForwardChannel_ returned: ' +1151'connection already in progress');1152return;1153}11541155this.makeForwardChannelRequest_();1156this.channelDebug_.debug('startForwardChannel_ finished, sent request');1157}1158};115911601161/**1162* Establishes a new channel session with the the server.1163* @private1164*/1165WebChannelBase.prototype.open_ = function() {1166this.channelDebug_.debug('open_()');1167this.nextRid_ = Math.floor(Math.random() * 100000);11681169var rid = this.nextRid_++;1170var request =1171ChannelRequest.createChannelRequest(this, this.channelDebug_, '', rid);11721173// mix the init headers1174var extraHeaders = this.extraHeaders_;1175if (this.initHeaders_) {1176if (extraHeaders) {1177extraHeaders = goog.object.clone(extraHeaders);1178goog.object.extend(extraHeaders, this.initHeaders_);1179} else {1180extraHeaders = this.initHeaders_;1181}1182}11831184if (this.httpHeadersOverwriteParam_ === null) {1185request.setExtraHeaders(extraHeaders);1186}11871188var requestText = this.dequeueOutgoingMaps_();1189var uri = this.forwardChannelUri_.clone();1190uri.setParameterValue('RID', rid);11911192if (this.clientVersion_ > 0) {1193uri.setParameterValue('CVER', this.clientVersion_);1194}11951196// http-session-id to be generated as the response1197if (this.getBackgroundChannelTest() && this.getHttpSessionIdParam()) {1198uri.setParameterValues(1199WebChannel.X_HTTP_SESSION_ID, this.getHttpSessionIdParam());1200}12011202// Add the reconnect parameters.1203this.addAdditionalParams_(uri);12041205if (this.httpHeadersOverwriteParam_ && extraHeaders) {1206httpCors.setHttpHeadersWithOverwriteParam(1207uri, this.httpHeadersOverwriteParam_, extraHeaders);1208}12091210this.forwardChannelRequestPool_.addRequest(request);1211request.xmlHttpPost(uri, requestText, true);1212};121312141215/**1216* Makes a forward channel request using XMLHTTP.1217* @param {!ChannelRequest=} opt_retryRequest A failed request to retry.1218* @private1219*/1220WebChannelBase.prototype.makeForwardChannelRequest_ = function(1221opt_retryRequest) {1222var rid;1223var requestText;1224if (opt_retryRequest) {1225this.requeuePendingMaps_();1226rid = this.nextRid_ - 1; // Must use last RID1227requestText = this.dequeueOutgoingMaps_();1228} else {1229rid = this.nextRid_++;1230requestText = this.dequeueOutgoingMaps_();1231}12321233var uri = this.forwardChannelUri_.clone();1234uri.setParameterValue('SID', this.sid_);1235uri.setParameterValue('RID', rid);1236uri.setParameterValue('AID', this.lastArrayId_);1237// Add the additional reconnect parameters.1238this.addAdditionalParams_(uri);12391240if (this.httpHeadersOverwriteParam_ && this.extraHeaders_) {1241httpCors.setHttpHeadersWithOverwriteParam(1242uri, this.httpHeadersOverwriteParam_, this.extraHeaders_);1243}12441245var request = ChannelRequest.createChannelRequest(1246this, this.channelDebug_, this.sid_, rid,1247this.forwardChannelRetryCount_ + 1);12481249if (this.httpHeadersOverwriteParam_ === null) {1250request.setExtraHeaders(this.extraHeaders_);1251}12521253// Randomize from 50%-100% of the forward channel timeout to avoid1254// a big hit if servers happen to die at once.1255request.setTimeout(1256Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50) +1257Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50 * Math.random()));1258this.forwardChannelRequestPool_.addRequest(request);1259request.xmlHttpPost(uri, requestText, true);1260};126112621263/**1264* Adds the additional parameters from the handler to the given URI.1265* @param {!goog.Uri} uri The URI to add the parameters to.1266* @private1267*/1268WebChannelBase.prototype.addAdditionalParams_ = function(uri) {1269// Add the additional reconnect parameters as needed.1270if (this.handler_) {1271var params = this.handler_.getAdditionalParams(this);1272if (params) {1273goog.structs.forEach(params, function(value, key, coll) {1274uri.setParameterValue(key, value);1275});1276}1277}1278};127912801281/**1282* Returns the request text from the outgoing maps and resets it.1283* @return {string} The encoded request text created from all the currently1284* queued outgoing maps.1285* @private1286*/1287WebChannelBase.prototype.dequeueOutgoingMaps_ = function() {1288var count =1289Math.min(this.outgoingMaps_.length, WebChannelBase.MAX_MAPS_PER_REQUEST_);1290var badMapHandler = this.handler_ ?1291goog.bind(this.handler_.badMapError, this.handler_, this) :1292null;1293var result = this.wireCodec_.encodeMessageQueue(1294this.outgoingMaps_, count, badMapHandler);1295this.pendingMaps_ =1296this.pendingMaps_.concat(this.outgoingMaps_.splice(0, count));1297return result;1298};129913001301/**1302* Requeues unacknowledged sent arrays for retransmission in the next forward1303* channel request.1304* @private1305*/1306WebChannelBase.prototype.requeuePendingMaps_ = function() {1307this.outgoingMaps_ = this.pendingMaps_.concat(this.outgoingMaps_);1308this.pendingMaps_.length = 0;1309};131013111312/**1313* Ensures there is a backchannel request for receiving data from the server.1314* @private1315*/1316WebChannelBase.prototype.ensureBackChannel_ = function() {1317if (this.backChannelRequest_) {1318// already have one1319return;1320}13211322if (this.backChannelTimerId_) {1323// no need to start a new request - one is already scheduled1324return;1325}13261327this.backChannelAttemptId_ = 1;1328this.backChannelTimerId_ = requestStats.setTimeout(1329goog.bind(this.onStartBackChannelTimer_, this), 0);1330this.backChannelRetryCount_ = 0;1331};133213331334/**1335* Schedules a back-channel retry, unless the max retries has been reached.1336* @return {boolean} true iff a retry was scheduled.1337* @private1338*/1339WebChannelBase.prototype.maybeRetryBackChannel_ = function() {1340if (this.backChannelRequest_ || this.backChannelTimerId_) {1341// Should be impossible to be called in this state.1342this.channelDebug_.severe('Request already in progress');1343return false;1344}13451346if (this.backChannelRetryCount_ >= this.getBackChannelMaxRetries()) {1347return false;1348}13491350this.channelDebug_.debug('Going to retry GET');13511352this.backChannelAttemptId_++;1353this.backChannelTimerId_ = requestStats.setTimeout(1354goog.bind(this.onStartBackChannelTimer_, this),1355this.getRetryTime_(this.backChannelRetryCount_));1356this.backChannelRetryCount_++;1357return true;1358};135913601361/**1362* Timer callback for ensureBackChannel_.1363* @private1364*/1365WebChannelBase.prototype.onStartBackChannelTimer_ = function() {1366this.backChannelTimerId_ = null;1367this.startBackChannel_();1368};136913701371/**1372* Begins a new back channel operation to the server.1373* @private1374*/1375WebChannelBase.prototype.startBackChannel_ = function() {1376if (!this.okToMakeRequest_()) {1377// channel is cancelled1378return;1379}13801381this.channelDebug_.debug('Creating new HttpRequest');1382this.backChannelRequest_ = ChannelRequest.createChannelRequest(1383this, this.channelDebug_, this.sid_, 'rpc', this.backChannelAttemptId_);13841385if (this.httpHeadersOverwriteParam_ === null) {1386this.backChannelRequest_.setExtraHeaders(this.extraHeaders_);1387}13881389this.backChannelRequest_.setReadyStateChangeThrottle(1390this.readyStateChangeThrottleMs_);1391var uri = this.backChannelUri_.clone();1392uri.setParameterValue('RID', 'rpc');1393uri.setParameterValue('SID', this.sid_);1394uri.setParameterValue('CI', this.useChunked_ ? '0' : '1');1395uri.setParameterValue('AID', this.lastArrayId_);13961397// Add the reconnect parameters.1398this.addAdditionalParams_(uri);13991400uri.setParameterValue('TYPE', 'xmlhttp');14011402if (this.httpHeadersOverwriteParam_ && this.extraHeaders_) {1403httpCors.setHttpHeadersWithOverwriteParam(1404uri, this.httpHeadersOverwriteParam_, this.extraHeaders_);1405}14061407this.backChannelRequest_.xmlHttpGet(1408uri, true /* decodeChunks */, this.hostPrefix_, false /* opt_noClose */);14091410this.channelDebug_.debug('New Request created');1411};141214131414/**1415* Gives the handler a chance to return an error code and stop channel1416* execution. A handler might want to do this to check that the user is still1417* logged in, for example.1418* @private1419* @return {boolean} If it's OK to make a request.1420*/1421WebChannelBase.prototype.okToMakeRequest_ = function() {1422if (this.handler_) {1423var result = this.handler_.okToMakeRequest(this);1424if (result != WebChannelBase.Error.OK) {1425this.channelDebug_.debug(1426'Handler returned error code from okToMakeRequest');1427this.signalError_(result);1428return false;1429}1430}1431return true;1432};143314341435/**1436* @override1437*/1438WebChannelBase.prototype.testConnectionFinished = function(1439testChannel, useChunked) {1440this.channelDebug_.debug('Test Connection Finished');14411442// Forward channel will not be used prior to this method is called1443var clientProtocol = testChannel.getClientProtocol();1444if (clientProtocol) {1445this.forwardChannelRequestPool_.applyClientProtocol(clientProtocol);1446}14471448this.useChunked_ = this.allowChunkedMode_ && useChunked;1449this.lastStatusCode_ = testChannel.getLastStatusCode();14501451this.connectChannel_();1452};145314541455/**1456* @override1457*/1458WebChannelBase.prototype.testConnectionFailure = function(1459testChannel, errorCode) {1460this.channelDebug_.debug('Test Connection Failed');1461this.lastStatusCode_ = testChannel.getLastStatusCode();1462this.signalError_(WebChannelBase.Error.REQUEST_FAILED);1463};146414651466/**1467* @override1468*/1469WebChannelBase.prototype.onRequestData = function(request, responseText) {1470if (this.state_ == WebChannelBase.State.CLOSED ||1471(this.backChannelRequest_ != request &&1472!this.forwardChannelRequestPool_.hasRequest(request))) {1473// either CLOSED or a request we don't know about (perhaps an old request)1474return;1475}1476this.lastStatusCode_ = request.getLastStatusCode();14771478if (this.forwardChannelRequestPool_.hasRequest(request) &&1479this.state_ == WebChannelBase.State.OPENED) {1480var response;1481try {1482response = this.wireCodec_.decodeMessage(responseText);1483} catch (ex) {1484response = null;1485}1486if (goog.isArray(response) && response.length == 3) {1487this.handlePostResponse_(/** @type {!Array<?>} */ (response), request);1488} else {1489this.channelDebug_.debug('Bad POST response data returned');1490this.signalError_(WebChannelBase.Error.BAD_RESPONSE);1491}1492} else {1493if (this.backChannelRequest_ == request) {1494this.clearDeadBackchannelTimer_();1495}1496if (!goog.string.isEmptyOrWhitespace(responseText)) {1497var response = this.wireCodec_.decodeMessage(responseText);1498this.onInput_(/** @type {!Array<?>} */ (response), request);1499}1500}1501};150215031504/**1505* Handles a POST response from the server.1506* @param {Array<number>} responseValues The key value pairs in1507* the POST response.1508* @param {!ChannelRequest} forwardReq The forward channel request that1509* triggers this function call.1510* @private1511*/1512WebChannelBase.prototype.handlePostResponse_ = function(1513responseValues, forwardReq) {1514// The first response value is set to 0 if server is missing backchannel.1515if (responseValues[0] == 0) {1516this.handleBackchannelMissing_(forwardReq);1517return;1518}1519this.lastPostResponseArrayId_ = responseValues[1];1520var outstandingArrays = this.lastPostResponseArrayId_ - this.lastArrayId_;1521if (0 < outstandingArrays) {1522var numOutstandingBackchannelBytes = responseValues[2];1523this.channelDebug_.debug(1524numOutstandingBackchannelBytes + ' bytes (in ' + outstandingArrays +1525' arrays) are outstanding on the BackChannel');1526if (!this.shouldRetryBackChannel_(numOutstandingBackchannelBytes)) {1527return;1528}1529if (!this.deadBackChannelTimerId_) {1530// We expect to receive data within 2 RTTs or we retry the backchannel.1531this.deadBackChannelTimerId_ = requestStats.setTimeout(1532goog.bind(this.onBackChannelDead_, this),15332 * WebChannelBase.RTT_ESTIMATE);1534}1535}1536};153715381539/**1540* Handles a POST response from the server telling us that it has detected that1541* we have no hanging GET connection.1542* @param {!ChannelRequest} forwardReq The forward channel request that1543* triggers this function call.1544* @private1545*/1546WebChannelBase.prototype.handleBackchannelMissing_ = function(forwardReq) {1547// As long as the back channel was started before the POST was sent,1548// we should retry the backchannel. We give a slight buffer of RTT_ESTIMATE1549// so as not to excessively retry the backchannel1550this.channelDebug_.debug('Server claims our backchannel is missing.');1551if (this.backChannelTimerId_) {1552this.channelDebug_.debug('But we are currently starting the request.');1553return;1554} else if (!this.backChannelRequest_) {1555this.channelDebug_.warning('We do not have a BackChannel established');1556} else if (1557this.backChannelRequest_.getRequestStartTime() +1558WebChannelBase.RTT_ESTIMATE <1559forwardReq.getRequestStartTime()) {1560this.clearDeadBackchannelTimer_();1561this.backChannelRequest_.cancel();1562this.backChannelRequest_ = null;1563} else {1564return;1565}1566this.maybeRetryBackChannel_();1567requestStats.notifyStatEvent(requestStats.Stat.BACKCHANNEL_MISSING);1568};156915701571/**1572* Determines whether we should start the process of retrying a possibly1573* dead backchannel.1574* @param {number} outstandingBytes The number of bytes for which the server has1575* not yet received acknowledgement.1576* @return {boolean} Whether to start the backchannel retry timer.1577* @private1578*/1579WebChannelBase.prototype.shouldRetryBackChannel_ = function(outstandingBytes) {1580// Not too many outstanding bytes, not buffered and not after a retry.1581return outstandingBytes <1582WebChannelBase.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF &&1583!this.isBuffered() && this.backChannelRetryCount_ == 0;1584};158515861587/**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, and1590* 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 null1593* if the use of host prefixes was disabled via setAllowHostPrefix().1594* @override1595*/1596WebChannelBase.prototype.correctHostPrefix = function(serverHostPrefix) {1597if (this.allowHostPrefix_) {1598if (this.handler_) {1599return this.handler_.correctHostPrefix(serverHostPrefix);1600}1601return serverHostPrefix;1602}1603return null;1604};160516061607/**1608* Handles the timer that indicates that our backchannel is no longer able to1609* successfully receive data from the server.1610* @private1611*/1612WebChannelBase.prototype.onBackChannelDead_ = function() {1613if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {1614this.deadBackChannelTimerId_ = null;1615this.backChannelRequest_.cancel();1616this.backChannelRequest_ = null;1617this.maybeRetryBackChannel_();1618requestStats.notifyStatEvent(requestStats.Stat.BACKCHANNEL_DEAD);1619}1620};162116221623/**1624* Clears the timer that indicates that our backchannel is no longer able to1625* successfully receive data from the server.1626* @private1627*/1628WebChannelBase.prototype.clearDeadBackchannelTimer_ = function() {1629if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {1630goog.global.clearTimeout(this.deadBackChannelTimerId_);1631this.deadBackChannelTimerId_ = null;1632}1633};163416351636/**1637* Returns whether or not the given error/status combination is fatal or not.1638* On fatal errors we immediately close the session rather than retrying the1639* failed request.1640* @param {?ChannelRequest.Error} error The error code for the1641* failed request.1642* @param {number} statusCode The last HTTP status code.1643* @return {boolean} Whether or not the error is fatal.1644* @private1645*/1646WebChannelBase.isFatalError_ = function(error, statusCode) {1647return error == ChannelRequest.Error.UNKNOWN_SESSION_ID ||1648(error == ChannelRequest.Error.STATUS && statusCode > 0);1649};165016511652/**1653* @override1654*/1655WebChannelBase.prototype.onRequestComplete = function(request) {1656this.channelDebug_.debug('Request complete');1657var type;1658if (this.backChannelRequest_ == request) {1659this.clearDeadBackchannelTimer_();1660this.backChannelRequest_ = null;1661type = WebChannelBase.ChannelType_.BACK_CHANNEL;1662} else if (this.forwardChannelRequestPool_.hasRequest(request)) {1663this.forwardChannelRequestPool_.removeRequest(request);1664type = WebChannelBase.ChannelType_.FORWARD_CHANNEL;1665} else {1666// return if it was an old request from a previous session1667return;1668}16691670this.lastStatusCode_ = request.getLastStatusCode();16711672if (this.state_ == WebChannelBase.State.CLOSED) {1673return;1674}16751676if (request.getSuccess()) {1677// Yay!1678if (type == WebChannelBase.ChannelType_.FORWARD_CHANNEL) {1679var size = request.getPostData() ? request.getPostData().length : 0;1680requestStats.notifyTimingEvent(1681size, goog.now() - request.getRequestStartTime(),1682this.forwardChannelRetryCount_);1683this.ensureForwardChannel_();1684this.onSuccess_();1685this.pendingMaps_.length = 0;1686} else { // i.e., back-channel1687this.ensureBackChannel_();1688}1689return;1690}1691// Else unsuccessful. Fall through.16921693var lastError = request.getLastError();1694if (!WebChannelBase.isFatalError_(lastError, this.lastStatusCode_)) {1695// Maybe retry.1696this.channelDebug_.debug(1697'Maybe retrying, last error: ' +1698ChannelRequest.errorStringFromCode(lastError, this.lastStatusCode_));1699if (type == WebChannelBase.ChannelType_.FORWARD_CHANNEL) {1700if (this.maybeRetryForwardChannel_(request)) {1701return;1702}1703}1704if (type == WebChannelBase.ChannelType_.BACK_CHANNEL) {1705if (this.maybeRetryBackChannel_()) {1706return;1707}1708}1709// Else exceeded max retries. Fall through.1710this.channelDebug_.debug('Exceeded max number of retries');1711} else {1712// Else fatal error. Fall through and mark the pending maps as failed.1713this.channelDebug_.debug('Not retrying due to error type');1714}171517161717// Can't save this session. :(1718this.channelDebug_.debug('Error: HTTP request failed');1719switch (lastError) {1720case ChannelRequest.Error.NO_DATA:1721this.signalError_(WebChannelBase.Error.NO_DATA);1722break;1723case ChannelRequest.Error.BAD_DATA:1724this.signalError_(WebChannelBase.Error.BAD_DATA);1725break;1726case ChannelRequest.Error.UNKNOWN_SESSION_ID:1727this.signalError_(WebChannelBase.Error.UNKNOWN_SESSION_ID);1728break;1729default:1730this.signalError_(WebChannelBase.Error.REQUEST_FAILED);1731break;1732}1733};173417351736/**1737* @param {number} retryCount Number of retries so far.1738* @return {number} Time in ms before firing next retry request.1739* @private1740*/1741WebChannelBase.prototype.getRetryTime_ = function(retryCount) {1742var retryTime = this.baseRetryDelayMs_ +1743Math.floor(Math.random() * this.retryDelaySeedMs_);1744if (!this.isActive()) {1745this.channelDebug_.debug('Inactive channel');1746retryTime = retryTime * WebChannelBase.INACTIVE_CHANNEL_RETRY_FACTOR;1747}1748// Backoff for subsequent retries1749retryTime *= retryCount;1750return retryTime;1751};175217531754/**1755* @param {number} baseDelayMs The base part of the retry delay, in ms.1756* @param {number} delaySeedMs A random delay between 0 and this is added to1757* the base part.1758*/1759WebChannelBase.prototype.setRetryDelay = function(baseDelayMs, delaySeedMs) {1760this.baseRetryDelayMs_ = baseDelayMs;1761this.retryDelaySeedMs_ = delaySeedMs;1762};176317641765/**1766* Apply any handshake control headers.1767* @param {!ChannelRequest} request The underlying request object1768* @private1769*/1770WebChannelBase.prototype.applyControlHeaders_ = function(request) {1771if (!this.backgroundChannelTest_) {1772return;1773}17741775var xhr = request.getXhr();1776if (xhr) {1777var clientProtocol =1778xhr.getStreamingResponseHeader(WebChannel.X_CLIENT_WIRE_PROTOCOL);1779if (clientProtocol) {1780this.forwardChannelRequestPool_.applyClientProtocol(clientProtocol);1781}17821783if (this.getHttpSessionIdParam()) {1784var httpSessionIdHeader =1785xhr.getStreamingResponseHeader(WebChannel.X_HTTP_SESSION_ID);1786if (httpSessionIdHeader) {1787this.setHttpSessionId(httpSessionIdHeader);1788// update the cached uri1789var httpSessionIdParam = this.getHttpSessionIdParam();17901791this.forwardChannelUri_.setParameterValue(1792/** @type {string} */ (httpSessionIdParam), // never null1793httpSessionIdHeader);1794} else {1795this.channelDebug_.warning(1796'Missing X_HTTP_SESSION_ID in the handshake response');1797}1798}1799}1800};180118021803/**1804* Processes the data returned by the server.1805* @param {!Array<!Array<?>>} respArray The response array returned1806* by the server.1807* @param {!ChannelRequest} request The underlying request object1808* @private1809*/1810WebChannelBase.prototype.onInput_ = function(respArray, request) {1811var batch =1812this.handler_ && this.handler_.channelHandleMultipleArrays ? [] : null;1813for (var i = 0; i < respArray.length; i++) {1814var nextArray = respArray[i];1815this.lastArrayId_ = nextArray[0];1816nextArray = nextArray[1];1817if (this.state_ == WebChannelBase.State.OPENING) {1818if (nextArray[0] == 'c') {1819this.sid_ = nextArray[1];1820this.hostPrefix_ = this.correctHostPrefix(nextArray[2]);18211822var negotiatedVersion = nextArray[3];1823if (goog.isDefAndNotNull(negotiatedVersion)) {1824this.channelVersion_ = negotiatedVersion;1825this.channelDebug_.info('VER=' + this.channelVersion_);1826}18271828var negotiatedServerVersion = nextArray[4];1829if (goog.isDefAndNotNull(negotiatedServerVersion)) {1830this.serverVersion_ = negotiatedServerVersion;1831this.channelDebug_.info('SVER=' + this.serverVersion_);1832}18331834this.applyControlHeaders_(request);18351836this.state_ = WebChannelBase.State.OPENED;1837if (this.handler_) {1838this.handler_.channelOpened(this);1839}1840this.backChannelUri_ = this.getBackChannelUri(1841this.hostPrefix_, /** @type {string} */ (this.path_));1842// Open connection to receive data1843this.ensureBackChannel_();1844} else if (nextArray[0] == 'stop' || nextArray[0] == 'close') {1845// treat close also as an abort1846this.signalError_(WebChannelBase.Error.STOP);1847}1848} else if (this.state_ == WebChannelBase.State.OPENED) {1849if (nextArray[0] == 'stop' || nextArray[0] == 'close') {1850if (batch && !goog.array.isEmpty(batch)) {1851this.handler_.channelHandleMultipleArrays(this, batch);1852batch.length = 0;1853}1854if (nextArray[0] == 'stop') {1855this.signalError_(WebChannelBase.Error.STOP);1856} else {1857this.disconnect();1858}1859} else if (nextArray[0] == 'noop') {1860// ignore - noop to keep connection happy1861} else {1862if (batch) {1863batch.push(nextArray);1864} else if (this.handler_) {1865this.handler_.channelHandleArray(this, nextArray);1866}1867}1868// We have received useful data on the back-channel, so clear its retry1869// count. We do this because back-channels by design do not complete1870// quickly, so on a flaky connection we could have many fail to complete1871// fully but still deliver a lot of data before they fail. We don't want1872// to count such failures towards the retry limit, because we don't want1873// to give up on a session if we can still receive data.1874this.backChannelRetryCount_ = 0;1875}1876}1877if (batch && !goog.array.isEmpty(batch)) {1878this.handler_.channelHandleMultipleArrays(this, batch);1879}1880};188118821883/**1884* Helper to ensure the channel is in the expected state.1885* @param {...number} var_args The channel must be in one of the indicated1886* states.1887* @private1888*/1889WebChannelBase.prototype.ensureInState_ = function(var_args) {1890goog.asserts.assert(1891goog.array.contains(arguments, this.state_),1892'Unexpected channel state: %s', this.state_);1893};189418951896/**1897* Signals an error has occurred.1898* @param {WebChannelBase.Error} error The error code for the failure.1899* @private1900*/1901WebChannelBase.prototype.signalError_ = function(error) {1902this.channelDebug_.info('Error code ' + error);1903if (error == WebChannelBase.Error.REQUEST_FAILED) {1904// Create a separate Internet connection to check1905// if it's a server error or user's network error.1906var imageUri = null;1907if (this.handler_) {1908imageUri = this.handler_.getNetworkTestImageUri(this);1909}1910netUtils.testNetwork(goog.bind(this.testNetworkCallback_, this), imageUri);1911} else {1912requestStats.notifyStatEvent(requestStats.Stat.ERROR_OTHER);1913}1914this.onError_(error);1915};191619171918/**1919* Callback for netUtils.testNetwork during error handling.1920* @param {boolean} networkUp Whether the network is up.1921* @private1922*/1923WebChannelBase.prototype.testNetworkCallback_ = function(networkUp) {1924if (networkUp) {1925this.channelDebug_.info('Successfully pinged google.com');1926requestStats.notifyStatEvent(requestStats.Stat.ERROR_OTHER);1927} else {1928this.channelDebug_.info('Failed to ping google.com');1929requestStats.notifyStatEvent(requestStats.Stat.ERROR_NETWORK);1930// Do not call onError_ again to eliminate duplicated Error events.1931}1932};193319341935/**1936* Called when messages have been successfully sent from the queue.1937* @private1938*/1939WebChannelBase.prototype.onSuccess_ = function() {1940// TODO(user): optimize for request pool (>1)1941if (this.handler_) {1942this.handler_.channelSuccess(this, this.pendingMaps_);1943}1944};194519461947/**1948* Called when we've determined the final error for a channel. It closes the1949* notifiers the handler of the error and closes the channel.1950* @param {WebChannelBase.Error} error The error code for the failure.1951* @private1952*/1953WebChannelBase.prototype.onError_ = function(error) {1954this.channelDebug_.debug('HttpChannel: error - ' + error);1955this.state_ = WebChannelBase.State.CLOSED;1956if (this.handler_) {1957this.handler_.channelError(this, error);1958}1959this.onClose_();1960this.cancelRequests_();1961};196219631964/**1965* Called when the channel has been closed. It notifiers the handler of the1966* event, and reports any pending or undelivered maps.1967* @private1968*/1969WebChannelBase.prototype.onClose_ = function() {1970this.state_ = WebChannelBase.State.CLOSED;1971this.lastStatusCode_ = -1;1972if (this.handler_) {1973if (this.pendingMaps_.length == 0 && this.outgoingMaps_.length == 0) {1974this.handler_.channelClosed(this);1975} else {1976this.channelDebug_.debug(1977'Number of undelivered maps' +1978', pending: ' + this.pendingMaps_.length + ', outgoing: ' +1979this.outgoingMaps_.length);19801981var copyOfPendingMaps = goog.array.clone(this.pendingMaps_);1982var copyOfUndeliveredMaps = goog.array.clone(this.outgoingMaps_);1983this.pendingMaps_.length = 0;1984this.outgoingMaps_.length = 0;19851986this.handler_.channelClosed(1987this, copyOfPendingMaps, copyOfUndeliveredMaps);1988}1989}1990};199119921993/**1994* @override1995*/1996WebChannelBase.prototype.getForwardChannelUri = function(path) {1997var uri = this.createDataUri(null, path);1998this.channelDebug_.debug('GetForwardChannelUri: ' + uri);1999return uri;2000};200120022003/**2004* @override2005*/2006WebChannelBase.prototype.getConnectionState = function() {2007return this.connState_;2008};200920102011/**2012* @override2013*/2014WebChannelBase.prototype.getBackChannelUri = function(hostPrefix, path) {2015var uri = this.createDataUri(2016this.shouldUseSecondaryDomains() ? hostPrefix : null, path);2017this.channelDebug_.debug('GetBackChannelUri: ' + uri);2018return uri;2019};202020212022/**2023* @override2024*/2025WebChannelBase.prototype.createDataUri = function(2026hostPrefix, path, opt_overridePort) {2027var uri = goog.Uri.parse(path);2028var uriAbsolute = (uri.getDomain() != '');2029if (uriAbsolute) {2030if (hostPrefix) {2031uri.setDomain(hostPrefix + '.' + uri.getDomain());2032}20332034uri.setPort(opt_overridePort || uri.getPort());2035} else {2036var locationPage = goog.global.location;2037var hostName;2038if (hostPrefix) {2039hostName = hostPrefix + '.' + locationPage.hostname;2040} else {2041hostName = locationPage.hostname;2042}20432044var port = opt_overridePort || locationPage.port;20452046uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path);2047}20482049if (this.extraParams_) {2050goog.object.forEach(this.extraParams_, function(value, key) {2051uri.setParameterValue(key, value);2052});2053}20542055var param = this.getHttpSessionIdParam();2056var value = this.getHttpSessionId();2057if (param && value) {2058uri.setParameterValue(param, value);2059}20602061// Add the protocol version to the URI.2062uri.setParameterValue('VER', this.channelVersion_);20632064// Add the reconnect parameters.2065this.addAdditionalParams_(uri);20662067return uri;2068};206920702071/**2072* @override2073*/2074WebChannelBase.prototype.createXhrIo = function(hostPrefix) {2075if (hostPrefix && !this.supportsCrossDomainXhrs_) {2076throw Error('Can\'t create secondary domain capable XhrIo object.');2077}2078var xhr = new goog.net.XhrIo();2079xhr.setWithCredentials(this.supportsCrossDomainXhrs_);2080return xhr;2081};208220832084/**2085* @override2086*/2087WebChannelBase.prototype.isActive = function() {2088return !!this.handler_ && this.handler_.isActive(this);2089};209020912092/**2093* @override2094*/2095WebChannelBase.prototype.shouldUseSecondaryDomains = function() {2096return this.supportsCrossDomainXhrs_;2097};209820992100/**2101* A LogSaver that can be used to accumulate all the debug logs so they2102* can be sent to the server when a problem is detected.2103*/2104WebChannelBase.LogSaver = {};210521062107/**2108* Buffer for accumulating the debug log2109* @type {goog.structs.CircularBuffer}2110* @private2111*/2112WebChannelBase.LogSaver.buffer_ = new goog.structs.CircularBuffer(1000);211321142115/**2116* Whether we're currently accumulating the debug log.2117* @type {boolean}2118* @private2119*/2120WebChannelBase.LogSaver.enabled_ = false;212121222123/**2124* Formatter for saving logs.2125* @type {goog.debug.Formatter}2126* @private2127*/2128WebChannelBase.LogSaver.formatter_ = new goog.debug.TextFormatter();212921302131/**2132* Returns whether the LogSaver is enabled.2133* @return {boolean} Whether saving is enabled or disabled.2134*/2135WebChannelBase.LogSaver.isEnabled = function() {2136return WebChannelBase.LogSaver.enabled_;2137};213821392140/**2141* Enables of disables the LogSaver.2142* @param {boolean} enable Whether to enable or disable saving.2143*/2144WebChannelBase.LogSaver.setEnabled = function(enable) {2145if (enable == WebChannelBase.LogSaver.enabled_) {2146return;2147}21482149var fn = WebChannelBase.LogSaver.addLogRecord;2150var logger = goog.log.getLogger('goog.net');2151if (enable) {2152goog.log.addHandler(logger, fn);2153} else {2154goog.log.removeHandler(logger, fn);2155}2156};215721582159/**2160* Adds a log record.2161* @param {goog.log.LogRecord} logRecord the LogRecord.2162*/2163WebChannelBase.LogSaver.addLogRecord = function(logRecord) {2164WebChannelBase.LogSaver.buffer_.add(2165WebChannelBase.LogSaver.formatter_.formatRecord(logRecord));2166};216721682169/**2170* Returns the log as a single string.2171* @return {string} The log as a single string.2172*/2173WebChannelBase.LogSaver.getBuffer = function() {2174return WebChannelBase.LogSaver.buffer_.getValues().join('');2175};217621772178/**2179* Clears the buffer2180*/2181WebChannelBase.LogSaver.clearBuffer = function() {2182WebChannelBase.LogSaver.buffer_.clear();2183};2184218521862187/**2188* Abstract base class for the channel handler2189* @constructor2190* @struct2191*/2192WebChannelBase.Handler = function() {};219321942195/**2196* Callback handler for when a batch of response arrays is received from the2197* server. When null, batched dispatching is disabled.2198* @type {?function(!WebChannelBase, !Array<!Array<?>>)}2199*/2200WebChannelBase.Handler.prototype.channelHandleMultipleArrays = null;220122022203/**2204* Whether it's okay to make a request to the server. A handler can return2205* false if the channel should fail. For example, if the user has logged out,2206* the handler may want all requests to fail immediately.2207* @param {WebChannelBase} channel The channel.2208* @return {WebChannelBase.Error} An error code. The code should2209* return WebChannelBase.Error.OK to indicate it's okay. Any other2210* error code will cause a failure.2211*/2212WebChannelBase.Handler.prototype.okToMakeRequest = function(channel) {2213return WebChannelBase.Error.OK;2214};221522162217/**2218* Indicates the WebChannel has successfully negotiated with the server2219* and can now send and receive data.2220* @param {WebChannelBase} channel The channel.2221*/2222WebChannelBase.Handler.prototype.channelOpened = function(channel) {};222322242225/**2226* New input is available for the application to process.2227*2228* @param {WebChannelBase} channel The channel.2229* @param {Array<?>} array The data array.2230*/2231WebChannelBase.Handler.prototype.channelHandleArray = function(channel, array) {2232};223322342235/**2236* Indicates maps were successfully sent on the channel.2237*2238* @param {WebChannelBase} channel The channel.2239* @param {Array<Wire.QueuedMap>} deliveredMaps The2240* array of maps that have been delivered to the server. This is a direct2241* reference to the internal array, so a copy should be made2242* if the caller desires a reference to the data.2243*/2244WebChannelBase.Handler.prototype.channelSuccess = function(2245channel, deliveredMaps) {};224622472248/**2249* Indicates an error occurred on the WebChannel.2250*2251* @param {WebChannelBase} channel The channel.2252* @param {WebChannelBase.Error} error The error code.2253*/2254WebChannelBase.Handler.prototype.channelError = function(channel, error) {};225522562257/**2258* Indicates the WebChannel is closed. Also notifies about which maps,2259* if any, that may not have been delivered to the server.2260* @param {WebChannelBase} channel The channel.2261* @param {Array<Wire.QueuedMap>=} opt_pendingMaps The2262* array of pending maps, which may or may not have been delivered to the2263* server.2264* @param {Array<Wire.QueuedMap>=} opt_undeliveredMaps2265* The array of undelivered maps, which have definitely not been delivered2266* to the server.2267*/2268WebChannelBase.Handler.prototype.channelClosed = function(2269channel, opt_pendingMaps, opt_undeliveredMaps) {};227022712272/**2273* Gets any parameters that should be added at the time another connection is2274* made to the server.2275* @param {WebChannelBase} channel The channel.2276* @return {!Object} Extra parameter keys and values to add to the requests.2277*/2278WebChannelBase.Handler.prototype.getAdditionalParams = function(channel) {2279return {};2280};228122822283/**2284* Gets the URI of an image that can be used to test network connectivity.2285* @param {WebChannelBase} channel The channel.2286* @return {goog.Uri?} A custom URI to load for the network test.2287*/2288WebChannelBase.Handler.prototype.getNetworkTestImageUri = function(channel) {2289return null;2290};229122922293/**2294* Gets whether this channel is currently active. This is used to determine the2295* length of time to wait before retrying.2296* @param {WebChannelBase} channel The channel.2297* @return {boolean} Whether the channel is currently active.2298*/2299WebChannelBase.Handler.prototype.isActive = function(channel) {2300return true;2301};230223032304/**2305* Called by the channel if enumeration of the map throws an exception.2306* @param {WebChannelBase} channel The channel.2307* @param {Object} map The map that can't be enumerated.2308*/2309WebChannelBase.Handler.prototype.badMapError = function(channel, map) {};231023112312/**2313* Allows the handler to override a host prefix provided by the server. Will2314* be called whenever the channel has received such a prefix and is considering2315* its use.2316* @param {?string} serverHostPrefix The host prefix provided by the server.2317* @return {?string} The host prefix the client should use.2318*/2319WebChannelBase.Handler.prototype.correctHostPrefix = function(2320serverHostPrefix) {2321return serverHostPrefix;2322};2323}); // goog.scope232423252326