Path: blob/trunk/third_party/closure/goog/net/browserchannel.js
2868 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 Definition of the BrowserChannel class. A BrowserChannel16* simulates a bidirectional socket over HTTP. It is the basis of the17* Gmail Chat IM connections to the server.18*19* Typical usage will look like20* 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*/282930goog.provide('goog.net.BrowserChannel');31goog.provide('goog.net.BrowserChannel.Error');32goog.provide('goog.net.BrowserChannel.Event');33goog.provide('goog.net.BrowserChannel.Handler');34goog.provide('goog.net.BrowserChannel.LogSaver');35goog.provide('goog.net.BrowserChannel.QueuedMap');36goog.provide('goog.net.BrowserChannel.ServerReachability');37goog.provide('goog.net.BrowserChannel.ServerReachabilityEvent');38goog.provide('goog.net.BrowserChannel.Stat');39goog.provide('goog.net.BrowserChannel.StatEvent');40goog.provide('goog.net.BrowserChannel.State');41goog.provide('goog.net.BrowserChannel.TimingEvent');4243goog.require('goog.Uri');44goog.require('goog.array');45goog.require('goog.asserts');46goog.require('goog.debug.TextFormatter');47goog.require('goog.events.Event');48goog.require('goog.events.EventTarget');49goog.require('goog.json');50goog.require('goog.json.NativeJsonProcessor');51goog.require('goog.log');52goog.require('goog.net.BrowserTestChannel');53goog.require('goog.net.ChannelDebug');54goog.require('goog.net.ChannelRequest');55goog.require('goog.net.XhrIo');56goog.require('goog.net.tmpnetwork');57goog.require('goog.object');58goog.require('goog.string');59goog.require('goog.structs');60goog.require('goog.structs.CircularBuffer');61626364/**65* Encapsulates the logic for a single BrowserChannel.66*67* @param {string=} opt_clientVersion An application-specific version number68* that is sent to the server when connected.69* @param {Array<string>=} opt_firstTestResults Previously determined results70* of the first browser channel test.71* @param {boolean=} opt_secondTestResults Previously determined results72* of the second browser channel test.73* @param {boolean=} opt_asyncTest Whether to perform the test requests74* asynchronously. While the test is performed, we'll assume the worst75* (connection is buffered), in order to avoid delaying the connection76* until the test is performed.77* @constructor78*/79goog.net.BrowserChannel = function(80opt_clientVersion, opt_firstTestResults, opt_secondTestResults,81opt_asyncTest) {82/**83* The application specific version that is passed to the server.84* @type {?string}85* @private86*/87this.clientVersion_ = opt_clientVersion || null;8889/**90* The current state of the BrowserChannel. It should be one of the91* goog.net.BrowserChannel.State constants.92* @type {!goog.net.BrowserChannel.State}93* @private94*/95this.state_ = goog.net.BrowserChannel.State.INIT;9697/**98* An array of queued maps that need to be sent to the server.99* @type {Array<goog.net.BrowserChannel.QueuedMap>}100* @private101*/102this.outgoingMaps_ = [];103104/**105* An array of dequeued maps that we have either received a non-successful106* response for, or no response at all, and which therefore may or may not107* have been received by the server.108* @type {Array<goog.net.BrowserChannel.QueuedMap>}109* @private110*/111this.pendingMaps_ = [];112113/**114* The channel debug used for browserchannel logging115* @type {!goog.net.ChannelDebug}116* @private117*/118this.channelDebug_ = new goog.net.ChannelDebug();119120/**121* Parser for a response payload. The parser should return an array.122* @type {!goog.string.Parser}123* @private124*/125this.parser_ = new goog.json.NativeJsonProcessor();126127/**128* An array of results for the first browser channel test call.129* @type {Array<string>}130* @private131*/132this.firstTestResults_ = opt_firstTestResults || null;133134/**135* The results of the second browser channel test. True implies the136* connection is buffered, False means unbuffered, null means that137* the results are not available.138* @private139*/140this.secondTestResults_ = goog.isDefAndNotNull(opt_secondTestResults) ?141opt_secondTestResults :142null;143144/**145* Whether to perform the test requests asynchronously. While the test is146* performed, we'll assume the worst (connection is buffered), in order to147* avoid delaying the connection until the test is performed.148* @private {boolean}149*/150this.asyncTest_ = opt_asyncTest || false;151};152153154155/**156* Simple container class for a (mapId, map) pair.157* @param {number} mapId The id for this map.158* @param {Object|goog.structs.Map} map The map itself.159* @param {Object=} opt_context The context associated with the map.160* @constructor161* @final162*/163goog.net.BrowserChannel.QueuedMap = function(mapId, map, opt_context) {164/**165* The id for this map.166* @type {number}167*/168this.mapId = mapId;169170/**171* The map itself.172* @type {Object}173*/174this.map = map;175176/**177* The context for the map.178* @type {Object}179*/180this.context = opt_context || null;181};182183184/**185* Extra HTTP headers to add to all the requests sent to the server.186* @type {Object}187* @private188*/189goog.net.BrowserChannel.prototype.extraHeaders_ = null;190191192/**193* Extra parameters to add to all the requests sent to the server.194* @type {Object}195* @private196*/197goog.net.BrowserChannel.prototype.extraParams_ = null;198199200/**201* The current ChannelRequest object for the forwardchannel.202* @type {goog.net.ChannelRequest?}203* @private204*/205goog.net.BrowserChannel.prototype.forwardChannelRequest_ = null;206207208/**209* The ChannelRequest object for the backchannel.210* @type {goog.net.ChannelRequest?}211* @private212*/213goog.net.BrowserChannel.prototype.backChannelRequest_ = null;214215216/**217* The relative path (in the context of the the page hosting the browser218* channel) for making requests to the server.219* @type {?string}220* @private221*/222goog.net.BrowserChannel.prototype.path_ = null;223224225/**226* The absolute URI for the forwardchannel request.227* @type {goog.Uri}228* @private229*/230goog.net.BrowserChannel.prototype.forwardChannelUri_ = null;231232233/**234* The absolute URI for the backchannel request.235* @type {goog.Uri}236* @private237*/238goog.net.BrowserChannel.prototype.backChannelUri_ = null;239240241/**242* A subdomain prefix for using a subdomain in IE for the backchannel243* requests.244* @type {?string}245* @private246*/247goog.net.BrowserChannel.prototype.hostPrefix_ = null;248249250/**251* Whether we allow the use of a subdomain in IE for the backchannel requests.252* @private253*/254goog.net.BrowserChannel.prototype.allowHostPrefix_ = true;255256257/**258* The next id to use for the RID (request identifier) parameter. This259* identifier uniquely identifies the forward channel request.260* @type {number}261* @private262*/263goog.net.BrowserChannel.prototype.nextRid_ = 0;264265266/**267* The id to use for the next outgoing map. This identifier uniquely268* identifies a sent map.269* @type {number}270* @private271*/272goog.net.BrowserChannel.prototype.nextMapId_ = 0;273274275/**276* Whether to fail forward-channel requests after one try, or after a few tries.277* @type {boolean}278* @private279*/280goog.net.BrowserChannel.prototype.failFast_ = false;281282283/**284* The handler that receive callbacks for state changes and data.285* @type {goog.net.BrowserChannel.Handler}286* @private287*/288goog.net.BrowserChannel.prototype.handler_ = null;289290291/**292* Timer identifier for asynchronously making a forward channel request.293* @type {?number}294* @private295*/296goog.net.BrowserChannel.prototype.forwardChannelTimerId_ = null;297298299/**300* Timer identifier for asynchronously making a back channel request.301* @type {?number}302* @private303*/304goog.net.BrowserChannel.prototype.backChannelTimerId_ = null;305306307/**308* Timer identifier for the timer that waits for us to retry the backchannel in309* the case where it is dead and no longer receiving data.310* @type {?number}311* @private312*/313goog.net.BrowserChannel.prototype.deadBackChannelTimerId_ = null;314315316/**317* The BrowserTestChannel object which encapsulates the logic for determining318* interesting network conditions about the client.319* @type {goog.net.BrowserTestChannel?}320* @private321*/322goog.net.BrowserChannel.prototype.connectionTest_ = null;323324325/**326* Whether the client's network conditions can support chunked responses.327* @type {?boolean}328* @private329*/330goog.net.BrowserChannel.prototype.useChunked_ = null;331332333/**334* Whether chunked mode is allowed. In certain debugging situations, it's335* useful to disable this.336* @private337*/338goog.net.BrowserChannel.prototype.allowChunkedMode_ = true;339340341/**342* The array identifier of the last array received from the server for the343* backchannel request.344* @type {number}345* @private346*/347goog.net.BrowserChannel.prototype.lastArrayId_ = -1;348349350/**351* The array identifier of the last array sent by the server that we know about.352* @type {number}353* @private354*/355goog.net.BrowserChannel.prototype.lastPostResponseArrayId_ = -1;356357358/**359* The last status code received.360* @type {number}361* @private362*/363goog.net.BrowserChannel.prototype.lastStatusCode_ = -1;364365366/**367* Number of times we have retried the current forward channel request.368* @type {number}369* @private370*/371goog.net.BrowserChannel.prototype.forwardChannelRetryCount_ = 0;372373374/**375* Number of times it a row that we have retried the current back channel376* request and received no data.377* @type {number}378* @private379*/380goog.net.BrowserChannel.prototype.backChannelRetryCount_ = 0;381382383/**384* The attempt id for the current back channel request. Starts at 1 and385* increments for each reconnect. The server uses this to log if our connection386* is flaky or not.387* @type {number}388* @private389*/390goog.net.BrowserChannel.prototype.backChannelAttemptId_;391392393/**394* The base part of the time before firing next retry request. Default is 5395* seconds. Note that a random delay is added (see {@link retryDelaySeedMs_})396* for all retries, and linear backoff is applied to the sum for subsequent397* retries.398* @type {number}399* @private400*/401goog.net.BrowserChannel.prototype.baseRetryDelayMs_ = 5 * 1000;402403404/**405* A random time between 0 and this number of MS is added to the406* {@link baseRetryDelayMs_}. Default is 10 seconds.407* @type {number}408* @private409*/410goog.net.BrowserChannel.prototype.retryDelaySeedMs_ = 10 * 1000;411412413/**414* Maximum number of attempts to connect to the server for forward channel415* requests. Defaults to 2.416* @type {number}417* @private418*/419goog.net.BrowserChannel.prototype.forwardChannelMaxRetries_ = 2;420421422/**423* The timeout in milliseconds for a forward channel request. Defaults to 20424* seconds. Note that part of this timeout can be randomized.425* @type {number}426* @private427*/428goog.net.BrowserChannel.prototype.forwardChannelRequestTimeoutMs_ = 20 * 1000;429430431/**432* A throttle time in ms for readystatechange events for the backchannel.433* Useful for throttling when ready state is INTERACTIVE (partial data).434*435* This throttle is useful if the server sends large data chunks down the436* backchannel. It prevents examining XHR partial data on every437* readystate change event. This is useful because large chunks can438* trigger hundreds of readystatechange events, each of which takes ~5ms439* or so to handle, in turn making the UI unresponsive for a significant period.440*441* If set to zero no throttle is used.442* @type {number}443* @private444*/445goog.net.BrowserChannel.prototype.readyStateChangeThrottleMs_ = 0;446447448/**449* Whether cross origin requests are supported for the browser channel.450*451* See {@link goog.net.XhrIo#setWithCredentials}.452* @type {boolean}453* @private454*/455goog.net.BrowserChannel.prototype.supportsCrossDomainXhrs_ = false;456457458/**459* The latest protocol version that this class supports. We request this version460* from the server when opening the connection. Should match461* com.google.net.browserchannel.BrowserChannel.LATEST_CHANNEL_VERSION.462* @type {number}463*/464goog.net.BrowserChannel.LATEST_CHANNEL_VERSION = 8;465466467/**468* The channel version that we negotiated with the server for this session.469* Starts out as the version we request, and then is changed to the negotiated470* version after the initial open.471* @type {number}472* @private473*/474goog.net.BrowserChannel.prototype.channelVersion_ =475goog.net.BrowserChannel.LATEST_CHANNEL_VERSION;476477478/**479* Enum type for the browser channel state machine.480* @enum {number}481*/482goog.net.BrowserChannel.State = {483/** The channel is closed. */484CLOSED: 0,485486/** The channel has been initialized but hasn't yet initiated a connection. */487INIT: 1,488489/** The channel is in the process of opening a connection to the server. */490OPENING: 2,491492/** The channel is open. */493OPENED: 3494};495496497/**498* The timeout in milliseconds for a forward channel request.499* @type {number}500*/501goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;502503504/**505* Maximum number of attempts to connect to the server for back channel506* requests.507* @type {number}508*/509goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES = 3;510511512/**513* A number in MS of how long we guess the maxmium amount of time a round trip514* to the server should take. In the future this could be substituted with a515* real measurement of the RTT.516* @type {number}517*/518goog.net.BrowserChannel.RTT_ESTIMATE = 3 * 1000;519520521/**522* When retrying for an inactive channel, we will multiply the total delay by523* this number.524* @type {number}525*/526goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR = 2;527528529/**530* Enum type for identifying a BrowserChannel error.531* @enum {number}532*/533goog.net.BrowserChannel.Error = {534/** Value that indicates no error has occurred. */535OK: 0,536537/** An error due to a request failing. */538REQUEST_FAILED: 2,539540/** An error due to the user being logged out. */541LOGGED_OUT: 4,542543/** An error due to server response which contains no data. */544NO_DATA: 5,545546/** An error due to a server response indicating an unknown session id */547UNKNOWN_SESSION_ID: 6,548549/** An error due to a server response requesting to stop the channel. */550STOP: 7,551552/** A general network error. */553NETWORK: 8,554555/** An error due to the channel being blocked by a network administrator. */556BLOCKED: 9,557558/** An error due to bad data being returned from the server. */559BAD_DATA: 10,560561/** An error due to a response that doesn't start with the magic cookie. */562BAD_RESPONSE: 11,563564/** ActiveX is blocked by the machine's admin settings. */565ACTIVE_X_BLOCKED: 12566};567568569/**570* Internal enum type for the two browser channel channel types.571* @enum {number}572* @private573*/574goog.net.BrowserChannel.ChannelType_ = {575FORWARD_CHANNEL: 1,576577BACK_CHANNEL: 2578};579580581/**582* The maximum number of maps that can be sent in one POST. Should match583* com.google.net.browserchannel.BrowserChannel.MAX_MAPS_PER_REQUEST.584* @type {number}585* @private586*/587goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ = 1000;588589590/**591* Singleton event target for firing stat events592* @type {goog.events.EventTarget}593* @private594*/595goog.net.BrowserChannel.statEventTarget_ = new goog.events.EventTarget();596597598/**599* Events fired by BrowserChannel and associated objects600* @const601*/602goog.net.BrowserChannel.Event = {};603604605/**606* Stat Event that fires when things of interest happen that may be useful for607* applications to know about for stats or debugging purposes. This event fires608* on the EventTarget returned by getStatEventTarget.609*/610goog.net.BrowserChannel.Event.STAT_EVENT = 'statevent';611612613614/**615* Event class for goog.net.BrowserChannel.Event.STAT_EVENT616*617* @param {goog.events.EventTarget} eventTarget The stat event target for618the browser channel.619* @param {goog.net.BrowserChannel.Stat} stat The stat.620* @constructor621* @extends {goog.events.Event}622* @final623*/624goog.net.BrowserChannel.StatEvent = function(eventTarget, stat) {625goog.events.Event.call(626this, goog.net.BrowserChannel.Event.STAT_EVENT, eventTarget);627628/**629* The stat630* @type {goog.net.BrowserChannel.Stat}631*/632this.stat = stat;633634};635goog.inherits(goog.net.BrowserChannel.StatEvent, goog.events.Event);636637638/**639* An event that fires when POST requests complete successfully, indicating640* the size of the POST and the round trip time.641* This event fires on the EventTarget returned by getStatEventTarget.642*/643goog.net.BrowserChannel.Event.TIMING_EVENT = 'timingevent';644645646647/**648* Event class for goog.net.BrowserChannel.Event.TIMING_EVENT649*650* @param {goog.events.EventTarget} target The stat event target for651the browser channel.652* @param {number} size The number of characters in the POST data.653* @param {number} rtt The total round trip time from POST to response in MS.654* @param {number} retries The number of times the POST had to be retried.655* @constructor656* @extends {goog.events.Event}657* @final658*/659goog.net.BrowserChannel.TimingEvent = function(target, size, rtt, retries) {660goog.events.Event.call(661this, goog.net.BrowserChannel.Event.TIMING_EVENT, target);662663/**664* @type {number}665*/666this.size = size;667668/**669* @type {number}670*/671this.rtt = rtt;672673/**674* @type {number}675*/676this.retries = retries;677678};679goog.inherits(goog.net.BrowserChannel.TimingEvent, goog.events.Event);680681682/**683* The type of event that occurs every time some information about how reachable684* the server is is discovered.685*/686goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT = 'serverreachability';687688689/**690* Types of events which reveal information about the reachability of the691* server.692* @enum {number}693*/694goog.net.BrowserChannel.ServerReachability = {695REQUEST_MADE: 1,696REQUEST_SUCCEEDED: 2,697REQUEST_FAILED: 3,698BACK_CHANNEL_ACTIVITY: 4699};700701702703/**704* Event class for goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT.705*706* @param {goog.events.EventTarget} target The stat event target for707the browser channel.708* @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The709* reachability event type.710* @constructor711* @extends {goog.events.Event}712* @final713*/714goog.net.BrowserChannel.ServerReachabilityEvent = function(715target, reachabilityType) {716goog.events.Event.call(717this, goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT, target);718719/**720* @type {goog.net.BrowserChannel.ServerReachability}721*/722this.reachabilityType = reachabilityType;723};724goog.inherits(725goog.net.BrowserChannel.ServerReachabilityEvent, goog.events.Event);726727728/**729* Enum that identifies events for statistics that are interesting to track.730* TODO(user) - Change name not to use Event or use EventTarget731* @enum {number}732*/733goog.net.BrowserChannel.Stat = {734/** Event indicating a new connection attempt. */735CONNECT_ATTEMPT: 0,736737/** Event indicating a connection error due to a general network problem. */738ERROR_NETWORK: 1,739740/**741* Event indicating a connection error that isn't due to a general network742* problem.743*/744ERROR_OTHER: 2,745746/** Event indicating the start of test stage one. */747TEST_STAGE_ONE_START: 3,748749750/** Event indicating the channel is blocked by a network administrator. */751CHANNEL_BLOCKED: 4,752753/** Event indicating the start of test stage two. */754TEST_STAGE_TWO_START: 5,755756/** Event indicating the first piece of test data was received. */757TEST_STAGE_TWO_DATA_ONE: 6,758759/**760* Event indicating that the second piece of test data was received and it was761* received separately from the first.762*/763TEST_STAGE_TWO_DATA_TWO: 7,764765/** Event indicating both pieces of test data were received simultaneously. */766TEST_STAGE_TWO_DATA_BOTH: 8,767768/** Event indicating stage one of the test request failed. */769TEST_STAGE_ONE_FAILED: 9,770771/** Event indicating stage two of the test request failed. */772TEST_STAGE_TWO_FAILED: 10,773774/**775* Event indicating that a buffering proxy is likely between the client and776* the server.777*/778PROXY: 11,779780/**781* Event indicating that no buffering proxy is likely between the client and782* the server.783*/784NOPROXY: 12,785786/** Event indicating an unknown SID error. */787REQUEST_UNKNOWN_SESSION_ID: 13,788789/** Event indicating a bad status code was received. */790REQUEST_BAD_STATUS: 14,791792/** Event indicating incomplete data was received */793REQUEST_INCOMPLETE_DATA: 15,794795/** Event indicating bad data was received */796REQUEST_BAD_DATA: 16,797798/** Event indicating no data was received when data was expected. */799REQUEST_NO_DATA: 17,800801/** Event indicating a request timeout. */802REQUEST_TIMEOUT: 18,803804/**805* Event indicating that the server never received our hanging GET and so it806* is being retried.807*/808BACKCHANNEL_MISSING: 19,809810/**811* Event indicating that we have determined that our hanging GET is not812* receiving data when it should be. Thus it is dead dead and will be retried.813*/814BACKCHANNEL_DEAD: 20,815816/**817* The browser declared itself offline during the lifetime of a request, or818* was offline when a request was initially made.819*/820BROWSER_OFFLINE: 21,821822/** ActiveX is blocked by the machine's admin settings. */823ACTIVE_X_BLOCKED: 22824};825826827/**828* A guess at a cutoff at which to no longer assume the backchannel is dead829* when we are slow to receive data. Number in bytes.830*831* Assumption: The worst bandwidth we work on is 50 kilobits/sec832* 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout833* @type {number}834*/835goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500;836837838/**839* Returns the browserchannel logger.840*841* @return {!goog.net.ChannelDebug} The channel debug object.842*/843goog.net.BrowserChannel.prototype.getChannelDebug = function() {844return this.channelDebug_;845};846847848/**849* Set the browserchannel logger.850* TODO(user): Add interface for channel loggers or remove this function.851*852* @param {goog.net.ChannelDebug} channelDebug The channel debug object.853*/854goog.net.BrowserChannel.prototype.setChannelDebug = function(channelDebug) {855if (goog.isDefAndNotNull(channelDebug)) {856this.channelDebug_ = channelDebug;857}858};859860861/**862* Allows the application to set an execution hooks for when BrowserChannel863* starts processing requests. This is useful to track timing or logging864* special information. The function takes no parameters and return void.865* @param {Function} startHook The function for the start hook.866*/867goog.net.BrowserChannel.setStartThreadExecutionHook = function(startHook) {868goog.net.BrowserChannel.startExecutionHook_ = startHook;869};870871872/**873* Allows the application to set an execution hooks for when BrowserChannel874* stops processing requests. This is useful to track timing or logging875* special information. The function takes no parameters and return void.876* @param {Function} endHook The function for the end hook.877*/878goog.net.BrowserChannel.setEndThreadExecutionHook = function(endHook) {879goog.net.BrowserChannel.endExecutionHook_ = endHook;880};881882883/**884* Application provided execution hook for the start hook.885*886* @type {Function}887* @private888*/889goog.net.BrowserChannel.startExecutionHook_ = function() {};890891892/**893* Application provided execution hook for the end hook.894*895* @type {Function}896* @private897*/898goog.net.BrowserChannel.endExecutionHook_ = function() {};899900901/**902* Instantiates a ChannelRequest with the given parameters. Overidden in tests.903*904* @param {goog.net.BrowserChannel|goog.net.BrowserTestChannel} channel905* The BrowserChannel that owns this request.906* @param {goog.net.ChannelDebug} channelDebug A ChannelDebug to use for907* logging.908* @param {string=} opt_sessionId The session id for the channel.909* @param {string|number=} opt_requestId The request id for this request.910* @param {number=} opt_retryId The retry id for this request.911* @return {!goog.net.ChannelRequest} The created channel request.912*/913goog.net.BrowserChannel.createChannelRequest = function(914channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId) {915return new goog.net.ChannelRequest(916channel, channelDebug, opt_sessionId, opt_requestId, opt_retryId);917};918919920/**921* Starts the channel. This initiates connections to the server.922*923* @param {string} testPath The path for the test connection.924* @param {string} channelPath The path for the channel connection.925* @param {Object=} opt_extraParams Extra parameter keys and values to add to926* the requests.927* @param {string=} opt_oldSessionId Session ID from a previous session.928* @param {number=} opt_oldArrayId The last array ID from a previous session.929*/930goog.net.BrowserChannel.prototype.connect = function(931testPath, channelPath, opt_extraParams, opt_oldSessionId, opt_oldArrayId) {932this.channelDebug_.debug('connect()');933934goog.net.BrowserChannel.notifyStatEvent(935goog.net.BrowserChannel.Stat.CONNECT_ATTEMPT);936937this.path_ = channelPath;938this.extraParams_ = opt_extraParams || {};939940// Attach parameters about the previous session if reconnecting.941if (opt_oldSessionId && goog.isDef(opt_oldArrayId)) {942this.extraParams_['OSID'] = opt_oldSessionId;943this.extraParams_['OAID'] = opt_oldArrayId;944}945946if (this.asyncTest_) {947goog.net.BrowserChannel.setTimeout(948goog.bind(this.connectTest_, this, testPath), 100);949this.connectChannel_();950} else {951this.connectTest_(testPath);952}953};954955956/**957* Disconnects and closes the channel.958*/959goog.net.BrowserChannel.prototype.disconnect = function() {960this.channelDebug_.debug('disconnect()');961962this.cancelRequests_();963964if (this.state_ == goog.net.BrowserChannel.State.OPENED) {965var rid = this.nextRid_++;966var uri = this.forwardChannelUri_.clone();967uri.setParameterValue('SID', this.sid_);968uri.setParameterValue('RID', rid);969uri.setParameterValue('TYPE', 'terminate');970971// Add the reconnect parameters.972this.addAdditionalParams_(uri);973974var request = goog.net.BrowserChannel.createChannelRequest(975this, this.channelDebug_, this.sid_, rid);976request.sendUsingImgTag(uri);977}978979this.onClose_();980};981982983/**984* Returns the session id of the channel. Only available after the985* channel has been opened.986* @return {string} Session ID.987*/988goog.net.BrowserChannel.prototype.getSessionId = function() {989return this.sid_;990};991992993/**994* Starts the test channel to determine network conditions.995*996* @param {string} testPath The relative PATH for the test connection.997* @private998*/999goog.net.BrowserChannel.prototype.connectTest_ = function(testPath) {1000this.channelDebug_.debug('connectTest_()');1001if (!this.okToMakeRequest_()) {1002return; // channel is cancelled1003}1004this.connectionTest_ =1005new goog.net.BrowserTestChannel(this, this.channelDebug_);1006this.connectionTest_.setExtraHeaders(this.extraHeaders_);1007this.connectionTest_.setParser(this.parser_);1008this.connectionTest_.connect(testPath);1009};101010111012/**1013* Starts the regular channel which is run after the test channel is complete.1014* @private1015*/1016goog.net.BrowserChannel.prototype.connectChannel_ = function() {1017this.channelDebug_.debug('connectChannel_()');1018this.ensureInState_(1019goog.net.BrowserChannel.State.INIT, goog.net.BrowserChannel.State.CLOSED);1020this.forwardChannelUri_ =1021this.getForwardChannelUri(/** @type {string} */ (this.path_));1022this.ensureForwardChannel_();1023};102410251026/**1027* Cancels all outstanding requests.1028* @private1029*/1030goog.net.BrowserChannel.prototype.cancelRequests_ = function() {1031if (this.connectionTest_) {1032this.connectionTest_.abort();1033this.connectionTest_ = null;1034}10351036if (this.backChannelRequest_) {1037this.backChannelRequest_.cancel();1038this.backChannelRequest_ = null;1039}10401041if (this.backChannelTimerId_) {1042goog.global.clearTimeout(this.backChannelTimerId_);1043this.backChannelTimerId_ = null;1044}10451046this.clearDeadBackchannelTimer_();10471048if (this.forwardChannelRequest_) {1049this.forwardChannelRequest_.cancel();1050this.forwardChannelRequest_ = null;1051}10521053if (this.forwardChannelTimerId_) {1054goog.global.clearTimeout(this.forwardChannelTimerId_);1055this.forwardChannelTimerId_ = null;1056}1057};105810591060/**1061* Returns the extra HTTP headers to add to all the requests sent to the server.1062*1063* @return {Object} The HTTP headers, or null.1064*/1065goog.net.BrowserChannel.prototype.getExtraHeaders = function() {1066return this.extraHeaders_;1067};106810691070/**1071* Sets extra HTTP headers to add to all the requests sent to the server.1072*1073* @param {Object} extraHeaders The HTTP headers, or null.1074*/1075goog.net.BrowserChannel.prototype.setExtraHeaders = function(extraHeaders) {1076this.extraHeaders_ = extraHeaders;1077};107810791080/**1081* Sets the throttle for handling onreadystatechange events for the request.1082*1083* @param {number} throttle The throttle in ms. A value of zero indicates1084* no throttle.1085*/1086goog.net.BrowserChannel.prototype.setReadyStateChangeThrottle = function(1087throttle) {1088this.readyStateChangeThrottleMs_ = throttle;1089};109010911092/**1093* Sets whether cross origin requests are supported for the browser channel.1094*1095* Setting this allows the creation of requests to secondary domains and1096* sends XHRs with the CORS withCredentials bit set to true.1097*1098* In order for cross-origin requests to work, the server will also need to set1099* CORS response headers as per:1100* https://developer.mozilla.org/en-US/docs/HTTP_access_control1101*1102* See {@link goog.net.XhrIo#setWithCredentials}.1103* @param {boolean} supportCrossDomain Whether cross domain XHRs are supported.1104*/1105goog.net.BrowserChannel.prototype.setSupportsCrossDomainXhrs = function(1106supportCrossDomain) {1107this.supportsCrossDomainXhrs_ = supportCrossDomain;1108};110911101111/**1112* Returns the handler used for channel callback events.1113*1114* @return {goog.net.BrowserChannel.Handler} The handler.1115*/1116goog.net.BrowserChannel.prototype.getHandler = function() {1117return this.handler_;1118};111911201121/**1122* Sets the handler used for channel callback events.1123* @param {goog.net.BrowserChannel.Handler} handler The handler to set.1124*/1125goog.net.BrowserChannel.prototype.setHandler = function(handler) {1126this.handler_ = handler;1127};112811291130/**1131* Returns whether the channel allows the use of a subdomain. There may be1132* cases where this isn't allowed.1133* @return {boolean} Whether a host prefix is allowed.1134*/1135goog.net.BrowserChannel.prototype.getAllowHostPrefix = function() {1136return this.allowHostPrefix_;1137};113811391140/**1141* Sets whether the channel allows the use of a subdomain. There may be cases1142* where this isn't allowed, for example, logging in with troutboard where1143* using a subdomain causes Apache to force the user to authenticate twice.1144* @param {boolean} allowHostPrefix Whether a host prefix is allowed.1145*/1146goog.net.BrowserChannel.prototype.setAllowHostPrefix = function(1147allowHostPrefix) {1148this.allowHostPrefix_ = allowHostPrefix;1149};115011511152/**1153* Returns whether the channel is buffered or not. This state is valid for1154* querying only after the test connection has completed. This may be1155* queried in the goog.net.BrowserChannel.okToMakeRequest() callback.1156* A channel may be buffered if the test connection determines that1157* a chunked response could not be sent down within a suitable time.1158* @return {boolean} Whether the channel is buffered.1159*/1160goog.net.BrowserChannel.prototype.isBuffered = function() {1161return !this.useChunked_;1162};116311641165/**1166* Returns whether chunked mode is allowed. In certain debugging situations,1167* it's useful for the application to have a way to disable chunked mode for a1168* user.11691170* @return {boolean} Whether chunked mode is allowed.1171*/1172goog.net.BrowserChannel.prototype.getAllowChunkedMode = function() {1173return this.allowChunkedMode_;1174};117511761177/**1178* Sets whether chunked mode is allowed. In certain debugging situations, it's1179* useful for the application to have a way to disable chunked mode for a user.1180* @param {boolean} allowChunkedMode Whether chunked mode is allowed.1181*/1182goog.net.BrowserChannel.prototype.setAllowChunkedMode = function(1183allowChunkedMode) {1184this.allowChunkedMode_ = allowChunkedMode;1185};118611871188/**1189* Sends a request to the server. The format of the request is a Map data1190* structure of key/value pairs. These maps are then encoded in a format1191* suitable for the wire and then reconstituted as a Map data structure that1192* the server can process.1193* @param {Object} map The map to send.1194* @param {?Object=} opt_context The context associated with the map.1195*/1196goog.net.BrowserChannel.prototype.sendMap = function(map, opt_context) {1197if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {1198throw Error('Invalid operation: sending map when state is closed');1199}12001201// We can only send 1000 maps per POST, but typically we should never have1202// that much to send, so warn if we exceed that (we still send all the maps).1203if (this.outgoingMaps_.length ==1204goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_) {1205// severe() is temporary so that we get these uploaded and can figure out1206// what's causing them. Afterwards can change to warning().1207this.channelDebug_.severe(1208'Already have ' + goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ +1209' queued maps upon queueing ' + this.parser_.stringify(map));1210}12111212this.outgoingMaps_.push(1213new goog.net.BrowserChannel.QueuedMap(1214this.nextMapId_++, map, opt_context));1215if (this.state_ == goog.net.BrowserChannel.State.OPENING ||1216this.state_ == goog.net.BrowserChannel.State.OPENED) {1217this.ensureForwardChannel_();1218}1219};122012211222/**1223* When set to true, this changes the behavior of the forward channel so it1224* will not retry requests; it will fail after one network failure, and if1225* there was already one network failure, the request will fail immediately.1226* @param {boolean} failFast Whether or not to fail fast.1227*/1228goog.net.BrowserChannel.prototype.setFailFast = function(failFast) {1229this.failFast_ = failFast;1230this.channelDebug_.info('setFailFast: ' + failFast);1231if ((this.forwardChannelRequest_ || this.forwardChannelTimerId_) &&1232this.forwardChannelRetryCount_ > this.getForwardChannelMaxRetries()) {1233this.channelDebug_.info(1234'Retry count ' + this.forwardChannelRetryCount_ + ' > new maxRetries ' +1235this.getForwardChannelMaxRetries() + '. Fail immediately!');1236if (this.forwardChannelRequest_) {1237this.forwardChannelRequest_.cancel();1238// Go through the standard onRequestComplete logic to expose the max-retry1239// failure in the standard way.1240this.onRequestComplete(this.forwardChannelRequest_);1241} else { // i.e., this.forwardChannelTimerId_1242goog.global.clearTimeout(this.forwardChannelTimerId_);1243this.forwardChannelTimerId_ = null;1244// The error code from the last failed request is gone, so just use a1245// generic one.1246this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);1247}1248}1249};125012511252/**1253* @return {number} The max number of forward-channel retries, which will be 01254* in fail-fast mode.1255*/1256goog.net.BrowserChannel.prototype.getForwardChannelMaxRetries = function() {1257return this.failFast_ ? 0 : this.forwardChannelMaxRetries_;1258};125912601261/**1262* Sets the maximum number of attempts to connect to the server for forward1263* channel requests.1264* @param {number} retries The maximum number of attempts.1265*/1266goog.net.BrowserChannel.prototype.setForwardChannelMaxRetries = function(1267retries) {1268this.forwardChannelMaxRetries_ = retries;1269};127012711272/**1273* Sets the timeout for a forward channel request.1274* @param {number} timeoutMs The timeout in milliseconds.1275*/1276goog.net.BrowserChannel.prototype.setForwardChannelRequestTimeout = function(1277timeoutMs) {1278this.forwardChannelRequestTimeoutMs_ = timeoutMs;1279};128012811282/**1283* @return {number} The max number of back-channel retries, which is a constant.1284*/1285goog.net.BrowserChannel.prototype.getBackChannelMaxRetries = function() {1286// Back-channel retries is a constant.1287return goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES;1288};128912901291/**1292* Returns whether the channel is closed1293* @return {boolean} true if the channel is closed.1294*/1295goog.net.BrowserChannel.prototype.isClosed = function() {1296return this.state_ == goog.net.BrowserChannel.State.CLOSED;1297};129812991300/**1301* Returns the browser channel state.1302* @return {goog.net.BrowserChannel.State} The current state of the browser1303* channel.1304*/1305goog.net.BrowserChannel.prototype.getState = function() {1306return this.state_;1307};130813091310/**1311* Return the last status code received for a request.1312* @return {number} The last status code received for a request.1313*/1314goog.net.BrowserChannel.prototype.getLastStatusCode = function() {1315return this.lastStatusCode_;1316};131713181319/**1320* @return {number} The last array id received.1321*/1322goog.net.BrowserChannel.prototype.getLastArrayId = function() {1323return this.lastArrayId_;1324};132513261327/**1328* Returns whether there are outstanding requests servicing the channel.1329* @return {boolean} true if there are outstanding requests.1330*/1331goog.net.BrowserChannel.prototype.hasOutstandingRequests = function() {1332return this.outstandingRequests_() != 0;1333};133413351336/**1337* Sets a new parser for the response payload.1338* @param {!goog.string.Parser} parser Parser.1339*/1340goog.net.BrowserChannel.prototype.setParser = function(parser) {1341this.parser_ = parser;1342};134313441345/**1346* Returns the number of outstanding requests.1347* @return {number} The number of outstanding requests to the server.1348* @private1349*/1350goog.net.BrowserChannel.prototype.outstandingRequests_ = function() {1351var count = 0;1352if (this.backChannelRequest_) {1353count++;1354}1355if (this.forwardChannelRequest_) {1356count++;1357}1358return count;1359};136013611362/**1363* Ensures that a forward channel request is scheduled.1364* @private1365*/1366goog.net.BrowserChannel.prototype.ensureForwardChannel_ = function() {1367if (this.forwardChannelRequest_) {1368// connection in process - no need to start a new request1369return;1370}13711372if (this.forwardChannelTimerId_) {1373// no need to start a new request - one is already scheduled1374return;1375}13761377this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(1378goog.bind(this.onStartForwardChannelTimer_, this), 0);1379this.forwardChannelRetryCount_ = 0;1380};138113821383/**1384* Schedules a forward-channel retry for the specified request, unless the max1385* retries has been reached.1386* @param {goog.net.ChannelRequest} request The failed request to retry.1387* @return {boolean} true iff a retry was scheduled.1388* @private1389*/1390goog.net.BrowserChannel.prototype.maybeRetryForwardChannel_ = function(1391request) {1392if (this.forwardChannelRequest_ || this.forwardChannelTimerId_) {1393// Should be impossible to be called in this state.1394this.channelDebug_.severe('Request already in progress');1395return false;1396}13971398if (this.state_ == goog.net.BrowserChannel.State.INIT || // no retry open_()1399(this.forwardChannelRetryCount_ >= this.getForwardChannelMaxRetries())) {1400return false;1401}14021403this.channelDebug_.debug('Going to retry POST');14041405this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(1406goog.bind(this.onStartForwardChannelTimer_, this, request),1407this.getRetryTime_(this.forwardChannelRetryCount_));1408this.forwardChannelRetryCount_++;1409return true;1410};141114121413/**1414* Timer callback for ensureForwardChannel1415* @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.1416* @private1417*/1418goog.net.BrowserChannel.prototype.onStartForwardChannelTimer_ = function(1419opt_retryRequest) {1420this.forwardChannelTimerId_ = null;1421this.startForwardChannel_(opt_retryRequest);1422};142314241425/**1426* Begins a new forward channel operation to the server.1427* @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.1428* @private1429*/1430goog.net.BrowserChannel.prototype.startForwardChannel_ = function(1431opt_retryRequest) {1432this.channelDebug_.debug('startForwardChannel_');1433if (!this.okToMakeRequest_()) {1434return; // channel is cancelled1435} else if (this.state_ == goog.net.BrowserChannel.State.INIT) {1436if (opt_retryRequest) {1437this.channelDebug_.severe('Not supposed to retry the open');1438return;1439}1440this.open_();1441this.state_ = goog.net.BrowserChannel.State.OPENING;1442} else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {1443if (opt_retryRequest) {1444this.makeForwardChannelRequest_(opt_retryRequest);1445return;1446}14471448if (this.outgoingMaps_.length == 0) {1449this.channelDebug_.debug(1450'startForwardChannel_ returned: ' +1451'nothing to send');1452// no need to start a new forward channel request1453return;1454}14551456if (this.forwardChannelRequest_) {1457// Should be impossible to be called in this state.1458this.channelDebug_.severe(1459'startForwardChannel_ returned: ' +1460'connection already in progress');1461return;1462}14631464this.makeForwardChannelRequest_();1465this.channelDebug_.debug('startForwardChannel_ finished, sent request');1466}1467};146814691470/**1471* Establishes a new channel session with the the server.1472* @private1473*/1474goog.net.BrowserChannel.prototype.open_ = function() {1475this.channelDebug_.debug('open_()');1476this.nextRid_ = Math.floor(Math.random() * 100000);14771478var rid = this.nextRid_++;1479var request = goog.net.BrowserChannel.createChannelRequest(1480this, this.channelDebug_, '', rid);1481request.setExtraHeaders(this.extraHeaders_);1482var requestText = this.dequeueOutgoingMaps_();1483var uri = this.forwardChannelUri_.clone();1484uri.setParameterValue('RID', rid);1485if (this.clientVersion_) {1486uri.setParameterValue('CVER', this.clientVersion_);1487}14881489// Add the reconnect parameters.1490this.addAdditionalParams_(uri);14911492request.xmlHttpPost(uri, requestText, true);1493this.forwardChannelRequest_ = request;1494};149514961497/**1498* Makes a forward channel request using XMLHTTP.1499* @param {goog.net.ChannelRequest=} opt_retryRequest A failed request to retry.1500* @private1501*/1502goog.net.BrowserChannel.prototype.makeForwardChannelRequest_ = function(1503opt_retryRequest) {1504var rid;1505var requestText;1506if (opt_retryRequest) {1507if (this.channelVersion_ > 6) {1508// In version 7 and up we can tack on new arrays to a retry.1509this.requeuePendingMaps_();1510rid = this.nextRid_ - 1; // Must use last RID1511requestText = this.dequeueOutgoingMaps_();1512} else {1513// TODO(user): Remove this code and the opt_retryRequest passing1514// once server-side support for ver 7 is ubiquitous.1515rid = opt_retryRequest.getRequestId();1516requestText = /** @type {string} */ (opt_retryRequest.getPostData());1517}1518} else {1519rid = this.nextRid_++;1520requestText = this.dequeueOutgoingMaps_();1521}15221523var uri = this.forwardChannelUri_.clone();1524uri.setParameterValue('SID', this.sid_);1525uri.setParameterValue('RID', rid);1526uri.setParameterValue('AID', this.lastArrayId_);1527// Add the additional reconnect parameters.1528this.addAdditionalParams_(uri);15291530var request = goog.net.BrowserChannel.createChannelRequest(1531this, this.channelDebug_, this.sid_, rid,1532this.forwardChannelRetryCount_ + 1);1533request.setExtraHeaders(this.extraHeaders_);15341535// randomize from 50%-100% of the forward channel timeout to avoid1536// a big hit if servers happen to die at once.1537request.setTimeout(1538Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50) +1539Math.round(this.forwardChannelRequestTimeoutMs_ * 0.50 * Math.random()));1540this.forwardChannelRequest_ = request;1541request.xmlHttpPost(uri, requestText, true);1542};154315441545/**1546* Adds the additional parameters from the handler to the given URI.1547* @param {goog.Uri} uri The URI to add the parameters to.1548* @private1549*/1550goog.net.BrowserChannel.prototype.addAdditionalParams_ = function(uri) {1551// Add the additional reconnect parameters as needed.1552if (this.handler_) {1553var params = this.handler_.getAdditionalParams(this);1554if (params) {1555goog.object.forEach(1556params, function(value, key) { uri.setParameterValue(key, value); });1557}1558}1559};156015611562/**1563* Returns the request text from the outgoing maps and resets it.1564* @return {string} The encoded request text created from all the currently1565* queued outgoing maps.1566* @private1567*/1568goog.net.BrowserChannel.prototype.dequeueOutgoingMaps_ = function() {1569var count = Math.min(1570this.outgoingMaps_.length, goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_);1571var sb = ['count=' + count];1572var offset;1573if (this.channelVersion_ > 6 && count > 0) {1574// To save a bit of bandwidth, specify the base mapId and the rest as1575// offsets from it.1576offset = this.outgoingMaps_[0].mapId;1577sb.push('ofs=' + offset);1578} else {1579offset = 0;1580}1581for (var i = 0; i < count; i++) {1582var mapId = this.outgoingMaps_[i].mapId;1583var map = this.outgoingMaps_[i].map;1584if (this.channelVersion_ <= 6) {1585// Map IDs were not used in ver 6 and before, just indexes in the request.1586mapId = i;1587} else {1588mapId -= offset;1589}1590try {1591goog.object.forEach(map, function(value, key, coll) {1592sb.push('req' + mapId + '_' + key + '=' + encodeURIComponent(value));1593});1594} catch (ex) {1595// We send a map here because lots of the retry logic relies on map IDs,1596// so we have to send something.1597sb.push(1598'req' + mapId + '_' +1599'type' +1600'=' + encodeURIComponent('_badmap'));1601if (this.handler_) {1602this.handler_.badMapError(this, map);1603}1604}1605}1606this.pendingMaps_ =1607this.pendingMaps_.concat(this.outgoingMaps_.splice(0, count));1608return sb.join('&');1609};161016111612/**1613* Requeues unacknowledged sent arrays for retransmission in the next forward1614* channel request.1615* @private1616*/1617goog.net.BrowserChannel.prototype.requeuePendingMaps_ = function() {1618this.outgoingMaps_ = this.pendingMaps_.concat(this.outgoingMaps_);1619this.pendingMaps_.length = 0;1620};162116221623/**1624* Ensures there is a backchannel request for receiving data from the server.1625* @private1626*/1627goog.net.BrowserChannel.prototype.ensureBackChannel_ = function() {1628if (this.backChannelRequest_) {1629// already have one1630return;1631}16321633if (this.backChannelTimerId_) {1634// no need to start a new request - one is already scheduled1635return;1636}16371638this.backChannelAttemptId_ = 1;1639this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(1640goog.bind(this.onStartBackChannelTimer_, this), 0);1641this.backChannelRetryCount_ = 0;1642};164316441645/**1646* Schedules a back-channel retry, unless the max retries has been reached.1647* @return {boolean} true iff a retry was scheduled.1648* @private1649*/1650goog.net.BrowserChannel.prototype.maybeRetryBackChannel_ = function() {1651if (this.backChannelRequest_ || this.backChannelTimerId_) {1652// Should be impossible to be called in this state.1653this.channelDebug_.severe('Request already in progress');1654return false;1655}16561657if (this.backChannelRetryCount_ >= this.getBackChannelMaxRetries()) {1658return false;1659}16601661this.channelDebug_.debug('Going to retry GET');16621663this.backChannelAttemptId_++;1664this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(1665goog.bind(this.onStartBackChannelTimer_, this),1666this.getRetryTime_(this.backChannelRetryCount_));1667this.backChannelRetryCount_++;1668return true;1669};167016711672/**1673* Timer callback for ensureBackChannel_.1674* @private1675*/1676goog.net.BrowserChannel.prototype.onStartBackChannelTimer_ = function() {1677this.backChannelTimerId_ = null;1678this.startBackChannel_();1679};168016811682/**1683* Begins a new back channel operation to the server.1684* @private1685*/1686goog.net.BrowserChannel.prototype.startBackChannel_ = function() {1687if (!this.okToMakeRequest_()) {1688// channel is cancelled1689return;1690}16911692this.channelDebug_.debug('Creating new HttpRequest');1693this.backChannelRequest_ = goog.net.BrowserChannel.createChannelRequest(1694this, this.channelDebug_, this.sid_, 'rpc', this.backChannelAttemptId_);1695this.backChannelRequest_.setExtraHeaders(this.extraHeaders_);1696this.backChannelRequest_.setReadyStateChangeThrottle(1697this.readyStateChangeThrottleMs_);1698var uri = this.backChannelUri_.clone();1699uri.setParameterValue('RID', 'rpc');1700uri.setParameterValue('SID', this.sid_);1701uri.setParameterValue('CI', this.useChunked_ ? '0' : '1');1702uri.setParameterValue('AID', this.lastArrayId_);17031704// Add the reconnect parameters.1705this.addAdditionalParams_(uri);17061707if (!goog.net.ChannelRequest.supportsXhrStreaming()) {1708uri.setParameterValue('TYPE', 'html');1709this.backChannelRequest_.tridentGet(uri, Boolean(this.hostPrefix_));1710} else {1711uri.setParameterValue('TYPE', 'xmlhttp');1712this.backChannelRequest_.xmlHttpGet(1713uri, true /* decodeChunks */, this.hostPrefix_,1714false /* opt_noClose */);1715}1716this.channelDebug_.debug('New Request created');1717};171817191720/**1721* Gives the handler a chance to return an error code and stop channel1722* execution. A handler might want to do this to check that the user is still1723* logged in, for example.1724* @private1725* @return {boolean} If it's OK to make a request.1726*/1727goog.net.BrowserChannel.prototype.okToMakeRequest_ = function() {1728if (this.handler_) {1729var result = this.handler_.okToMakeRequest(this);1730if (result != goog.net.BrowserChannel.Error.OK) {1731this.channelDebug_.debug(1732'Handler returned error code from ' +1733'okToMakeRequest');1734this.signalError_(result);1735return false;1736}1737}1738return true;1739};174017411742/**1743* Callback from BrowserTestChannel for when the channel is finished.1744* @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.1745* @param {boolean} useChunked Whether we can chunk responses.1746*/1747goog.net.BrowserChannel.prototype.testConnectionFinished = function(1748testChannel, useChunked) {1749this.channelDebug_.debug('Test Connection Finished');17501751this.useChunked_ = this.allowChunkedMode_ && useChunked;1752this.lastStatusCode_ = testChannel.getLastStatusCode();1753// When using asynchronous test, the channel is already open by connect().1754if (!this.asyncTest_) {1755this.connectChannel_();1756}1757};175817591760/**1761* Callback from BrowserTestChannel for when the channel has an error.1762* @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.1763* @param {goog.net.ChannelRequest.Error} errorCode The error code of the1764failure.1765*/1766goog.net.BrowserChannel.prototype.testConnectionFailure = function(1767testChannel, errorCode) {1768this.channelDebug_.debug('Test Connection Failed');1769this.lastStatusCode_ = testChannel.getLastStatusCode();1770this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);1771};177217731774/**1775* Callback from BrowserTestChannel for when the channel is blocked.1776* @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.1777*/1778goog.net.BrowserChannel.prototype.testConnectionBlocked = function(1779testChannel) {1780this.channelDebug_.debug('Test Connection Blocked');1781this.lastStatusCode_ = this.connectionTest_.getLastStatusCode();1782this.signalError_(goog.net.BrowserChannel.Error.BLOCKED);1783};178417851786/**1787* Callback from ChannelRequest for when new data is received1788* @param {goog.net.ChannelRequest} request The request object.1789* @param {string} responseText The text of the response.1790*/1791goog.net.BrowserChannel.prototype.onRequestData = function(1792request, responseText) {1793if (this.state_ == goog.net.BrowserChannel.State.CLOSED ||1794(this.backChannelRequest_ != request &&1795this.forwardChannelRequest_ != request)) {1796// either CLOSED or a request we don't know about (perhaps an old request)1797return;1798}1799this.lastStatusCode_ = request.getLastStatusCode();18001801if (this.forwardChannelRequest_ == request &&1802this.state_ == goog.net.BrowserChannel.State.OPENED) {1803if (this.channelVersion_ > 7) {1804var response;1805try {1806response = this.parser_.parse(responseText);1807} catch (ex) {1808response = null;1809}1810if (goog.isArray(response) && response.length == 3) {1811this.handlePostResponse_(response);1812} else {1813this.channelDebug_.debug('Bad POST response data returned');1814this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);1815}1816} else if (responseText != goog.net.ChannelDebug.MAGIC_RESPONSE_COOKIE) {1817this.channelDebug_.debug(1818'Bad data returned - missing/invald ' +1819'magic cookie');1820this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);1821}1822} else {1823if (this.backChannelRequest_ == request) {1824this.clearDeadBackchannelTimer_();1825}1826if (!goog.string.isEmptyOrWhitespace(responseText)) {1827var response = this.parser_.parse(responseText);1828goog.asserts.assert(goog.isArray(response));1829this.onInput_(/** @type {!Array<?>} */ (response));1830}1831}1832};183318341835/**1836* Handles a POST response from the server.1837* @param {Array<number>} responseValues The key value pairs in the POST1838* response.1839* @private1840*/1841goog.net.BrowserChannel.prototype.handlePostResponse_ = function(1842responseValues) {1843// The first response value is set to 0 if server is missing backchannel.1844if (responseValues[0] == 0) {1845this.handleBackchannelMissing_();1846return;1847}1848this.lastPostResponseArrayId_ = responseValues[1];1849var outstandingArrays = this.lastPostResponseArrayId_ - this.lastArrayId_;1850if (0 < outstandingArrays) {1851var numOutstandingBackchannelBytes = responseValues[2];1852this.channelDebug_.debug(1853numOutstandingBackchannelBytes + ' bytes (in ' + outstandingArrays +1854' arrays) are outstanding on the BackChannel');1855if (!this.shouldRetryBackChannel_(numOutstandingBackchannelBytes)) {1856return;1857}1858if (!this.deadBackChannelTimerId_) {1859// We expect to receive data within 2 RTTs or we retry the backchannel.1860this.deadBackChannelTimerId_ = goog.net.BrowserChannel.setTimeout(1861goog.bind(this.onBackChannelDead_, this),18622 * goog.net.BrowserChannel.RTT_ESTIMATE);1863}1864}1865};186618671868/**1869* Handles a POST response from the server telling us that it has detected that1870* we have no hanging GET connection.1871* @private1872*/1873goog.net.BrowserChannel.prototype.handleBackchannelMissing_ = function() {1874// As long as the back channel was started before the POST was sent,1875// we should retry the backchannel. We give a slight buffer of RTT_ESTIMATE1876// so as not to excessively retry the backchannel1877this.channelDebug_.debug('Server claims our backchannel is missing.');1878if (this.backChannelTimerId_) {1879this.channelDebug_.debug('But we are currently starting the request.');1880return;1881} else if (!this.backChannelRequest_) {1882this.channelDebug_.warning('We do not have a BackChannel established');1883} else if (1884this.backChannelRequest_.getRequestStartTime() +1885goog.net.BrowserChannel.RTT_ESTIMATE <1886this.forwardChannelRequest_.getRequestStartTime()) {1887this.clearDeadBackchannelTimer_();1888this.backChannelRequest_.cancel();1889this.backChannelRequest_ = null;1890} else {1891return;1892}1893this.maybeRetryBackChannel_();1894goog.net.BrowserChannel.notifyStatEvent(1895goog.net.BrowserChannel.Stat.BACKCHANNEL_MISSING);1896};189718981899/**1900* Determines whether we should start the process of retrying a possibly1901* dead backchannel.1902* @param {number} outstandingBytes The number of bytes for which the server has1903* not yet received acknowledgement.1904* @return {boolean} Whether to start the backchannel retry timer.1905* @private1906*/1907goog.net.BrowserChannel.prototype.shouldRetryBackChannel_ = function(1908outstandingBytes) {1909// Not too many outstanding bytes, not buffered and not after a retry.1910return outstandingBytes <1911goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF &&1912!this.isBuffered() && this.backChannelRetryCount_ == 0;1913};191419151916/**1917* Decides which host prefix should be used, if any. If there is a handler,1918* allows the handler to validate a host prefix provided by the server, and1919* optionally override it.1920* @param {?string} serverHostPrefix The host prefix provided by the server.1921* @return {?string} The host prefix to actually use, if any. Will return null1922* if the use of host prefixes was disabled via setAllowHostPrefix().1923*/1924goog.net.BrowserChannel.prototype.correctHostPrefix = function(1925serverHostPrefix) {1926if (this.allowHostPrefix_) {1927if (this.handler_) {1928return this.handler_.correctHostPrefix(serverHostPrefix);1929}1930return serverHostPrefix;1931}1932return null;1933};193419351936/**1937* Handles the timer that indicates that our backchannel is no longer able to1938* successfully receive data from the server.1939* @private1940*/1941goog.net.BrowserChannel.prototype.onBackChannelDead_ = function() {1942if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {1943this.deadBackChannelTimerId_ = null;1944this.backChannelRequest_.cancel();1945this.backChannelRequest_ = null;1946this.maybeRetryBackChannel_();1947goog.net.BrowserChannel.notifyStatEvent(1948goog.net.BrowserChannel.Stat.BACKCHANNEL_DEAD);1949}1950};195119521953/**1954* Clears the timer that indicates that our backchannel is no longer able to1955* successfully receive data from the server.1956* @private1957*/1958goog.net.BrowserChannel.prototype.clearDeadBackchannelTimer_ = function() {1959if (goog.isDefAndNotNull(this.deadBackChannelTimerId_)) {1960goog.global.clearTimeout(this.deadBackChannelTimerId_);1961this.deadBackChannelTimerId_ = null;1962}1963};196419651966/**1967* Returns whether or not the given error/status combination is fatal or not.1968* On fatal errors we immediately close the session rather than retrying the1969* failed request.1970* @param {goog.net.ChannelRequest.Error?} error The error code for the failed1971* request.1972* @param {number} statusCode The last HTTP status code.1973* @return {boolean} Whether or not the error is fatal.1974* @private1975*/1976goog.net.BrowserChannel.isFatalError_ = function(error, statusCode) {1977return error == goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID ||1978error == goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED ||1979(error == goog.net.ChannelRequest.Error.STATUS && statusCode > 0);1980};198119821983/**1984* Callback from ChannelRequest that indicates a request has completed.1985* @param {goog.net.ChannelRequest} request The request object.1986*/1987goog.net.BrowserChannel.prototype.onRequestComplete = function(request) {1988this.channelDebug_.debug('Request complete');1989var type;1990if (this.backChannelRequest_ == request) {1991this.clearDeadBackchannelTimer_();1992this.backChannelRequest_ = null;1993type = goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL;1994} else if (this.forwardChannelRequest_ == request) {1995this.forwardChannelRequest_ = null;1996type = goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL;1997} else {1998// return if it was an old request from a previous session1999return;2000}20012002this.lastStatusCode_ = request.getLastStatusCode();20032004if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {2005return;2006}20072008if (request.getSuccess()) {2009// Yay!2010if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) {2011var size = request.getPostData() ? request.getPostData().length : 0;2012goog.net.BrowserChannel.notifyTimingEvent(2013size, goog.now() - request.getRequestStartTime(),2014this.forwardChannelRetryCount_);2015this.ensureForwardChannel_();2016this.onSuccess_();2017this.pendingMaps_.length = 0;2018} else { // i.e., back-channel2019this.ensureBackChannel_();2020}2021return;2022}2023// Else unsuccessful. Fall through.20242025var lastError = request.getLastError();2026if (!goog.net.BrowserChannel.isFatalError_(lastError, this.lastStatusCode_)) {2027// Maybe retry.2028this.channelDebug_.debug(2029'Maybe retrying, last error: ' +2030goog.net.ChannelRequest.errorStringFromCode(2031/** @type {goog.net.ChannelRequest.Error} */ (lastError),2032this.lastStatusCode_));2033if (type == goog.net.BrowserChannel.ChannelType_.FORWARD_CHANNEL) {2034if (this.maybeRetryForwardChannel_(request)) {2035return;2036}2037}2038if (type == goog.net.BrowserChannel.ChannelType_.BACK_CHANNEL) {2039if (this.maybeRetryBackChannel_()) {2040return;2041}2042}2043// Else exceeded max retries. Fall through.2044this.channelDebug_.debug('Exceeded max number of retries');2045} else {2046// Else fatal error. Fall through and mark the pending maps as failed.2047this.channelDebug_.debug('Not retrying due to error type');2048}204920502051// Can't save this session. :(2052this.channelDebug_.debug('Error: HTTP request failed');2053switch (lastError) {2054case goog.net.ChannelRequest.Error.NO_DATA:2055this.signalError_(goog.net.BrowserChannel.Error.NO_DATA);2056break;2057case goog.net.ChannelRequest.Error.BAD_DATA:2058this.signalError_(goog.net.BrowserChannel.Error.BAD_DATA);2059break;2060case goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID:2061this.signalError_(goog.net.BrowserChannel.Error.UNKNOWN_SESSION_ID);2062break;2063case goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED:2064this.signalError_(goog.net.BrowserChannel.Error.ACTIVE_X_BLOCKED);2065break;2066default:2067this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);2068break;2069}2070};207120722073/**2074* @param {number} retryCount Number of retries so far.2075* @return {number} Time in ms before firing next retry request.2076* @private2077*/2078goog.net.BrowserChannel.prototype.getRetryTime_ = function(retryCount) {2079var retryTime = this.baseRetryDelayMs_ +2080Math.floor(Math.random() * this.retryDelaySeedMs_);2081if (!this.isActive()) {2082this.channelDebug_.debug('Inactive channel');2083retryTime =2084retryTime * goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR;2085}2086// Backoff for subsequent retries2087retryTime = retryTime * retryCount;2088return retryTime;2089};209020912092/**2093* @param {number} baseDelayMs The base part of the retry delay, in ms.2094* @param {number} delaySeedMs A random delay between 0 and this is added to2095* the base part.2096*/2097goog.net.BrowserChannel.prototype.setRetryDelay = function(2098baseDelayMs, delaySeedMs) {2099this.baseRetryDelayMs_ = baseDelayMs;2100this.retryDelaySeedMs_ = delaySeedMs;2101};210221032104/**2105* Processes the data returned by the server.2106* @param {!Array<!Array<?>>} respArray The response array returned2107* by the server.2108* @private2109*/2110goog.net.BrowserChannel.prototype.onInput_ = function(respArray) {2111var batch =2112this.handler_ && this.handler_.channelHandleMultipleArrays ? [] : null;2113for (var i = 0; i < respArray.length; i++) {2114var nextArray = respArray[i];2115this.lastArrayId_ = nextArray[0];2116nextArray = nextArray[1];2117if (this.state_ == goog.net.BrowserChannel.State.OPENING) {2118if (nextArray[0] == 'c') {2119this.sid_ = nextArray[1];2120this.hostPrefix_ = this.correctHostPrefix(nextArray[2]);2121var negotiatedVersion = nextArray[3];2122if (goog.isDefAndNotNull(negotiatedVersion)) {2123this.channelVersion_ = negotiatedVersion;2124} else {2125// Servers prior to version 7 did not send this, so assume version 6.2126this.channelVersion_ = 6;2127}2128this.state_ = goog.net.BrowserChannel.State.OPENED;2129if (this.handler_) {2130this.handler_.channelOpened(this);2131}2132this.backChannelUri_ = this.getBackChannelUri(2133this.hostPrefix_, /** @type {string} */ (this.path_));2134// Open connection to receive data2135this.ensureBackChannel_();2136} else if (nextArray[0] == 'stop') {2137this.signalError_(goog.net.BrowserChannel.Error.STOP);2138}2139} else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {2140if (nextArray[0] == 'stop') {2141if (batch && !goog.array.isEmpty(batch)) {2142this.handler_.channelHandleMultipleArrays(this, batch);2143batch.length = 0;2144}2145this.signalError_(goog.net.BrowserChannel.Error.STOP);2146} else if (nextArray[0] == 'noop') {2147// ignore - noop to keep connection happy2148} else {2149if (batch) {2150batch.push(nextArray);2151} else if (this.handler_) {2152this.handler_.channelHandleArray(this, nextArray);2153}2154}2155// We have received useful data on the back-channel, so clear its retry2156// count. We do this because back-channels by design do not complete2157// quickly, so on a flaky connection we could have many fail to complete2158// fully but still deliver a lot of data before they fail. We don't want2159// to count such failures towards the retry limit, because we don't want2160// to give up on a session if we can still receive data.2161this.backChannelRetryCount_ = 0;2162}2163}2164if (batch && !goog.array.isEmpty(batch)) {2165this.handler_.channelHandleMultipleArrays(this, batch);2166}2167};216821692170/**2171* Helper to ensure the BrowserChannel is in the expected state.2172* @param {...number} var_args The channel must be in one of the indicated2173* states.2174* @private2175*/2176goog.net.BrowserChannel.prototype.ensureInState_ = function(var_args) {2177if (!goog.array.contains(arguments, this.state_)) {2178throw Error('Unexpected channel state: ' + this.state_);2179}2180};218121822183/**2184* Signals an error has occurred.2185* @param {goog.net.BrowserChannel.Error} error The error code for the failure.2186* @private2187*/2188goog.net.BrowserChannel.prototype.signalError_ = function(error) {2189this.channelDebug_.info('Error code ' + error);2190if (error == goog.net.BrowserChannel.Error.REQUEST_FAILED ||2191error == goog.net.BrowserChannel.Error.BLOCKED) {2192// Ping google to check if it's a server error or user's network error.2193var imageUri = null;2194if (this.handler_) {2195imageUri = this.handler_.getNetworkTestImageUri(this);2196}2197goog.net.tmpnetwork.testGoogleCom(2198goog.bind(this.testGoogleComCallback_, this), imageUri);2199} else {2200goog.net.BrowserChannel.notifyStatEvent(2201goog.net.BrowserChannel.Stat.ERROR_OTHER);2202}2203this.onError_(error);2204};220522062207/**2208* Callback for testGoogleCom during error handling.2209* @param {boolean} networkUp Whether the network is up.2210* @private2211*/2212goog.net.BrowserChannel.prototype.testGoogleComCallback_ = function(networkUp) {2213if (networkUp) {2214this.channelDebug_.info('Successfully pinged google.com');2215goog.net.BrowserChannel.notifyStatEvent(2216goog.net.BrowserChannel.Stat.ERROR_OTHER);2217} else {2218this.channelDebug_.info('Failed to ping google.com');2219goog.net.BrowserChannel.notifyStatEvent(2220goog.net.BrowserChannel.Stat.ERROR_NETWORK);2221// We call onError_ here instead of signalError_ because the latter just2222// calls notifyStatEvent, and we don't want to have another stat event.2223this.onError_(goog.net.BrowserChannel.Error.NETWORK);2224}2225};222622272228/**2229* Called when messages have been successfully sent from the queue.2230* @private2231*/2232goog.net.BrowserChannel.prototype.onSuccess_ = function() {2233if (this.handler_) {2234this.handler_.channelSuccess(this, this.pendingMaps_);2235}2236};223722382239/**2240* Called when we've determined the final error for a channel. It closes the2241* notifiers the handler of the error and closes the channel.2242* @param {goog.net.BrowserChannel.Error} error The error code for the failure.2243* @private2244*/2245goog.net.BrowserChannel.prototype.onError_ = function(error) {2246this.channelDebug_.debug('HttpChannel: error - ' + error);2247this.state_ = goog.net.BrowserChannel.State.CLOSED;2248if (this.handler_) {2249this.handler_.channelError(this, error);2250}2251this.onClose_();2252this.cancelRequests_();2253};225422552256/**2257* Called when the channel has been closed. It notifiers the handler of the2258* event, and reports any pending or undelivered maps.2259* @private2260*/2261goog.net.BrowserChannel.prototype.onClose_ = function() {2262this.state_ = goog.net.BrowserChannel.State.CLOSED;2263this.lastStatusCode_ = -1;2264if (this.handler_) {2265if (this.pendingMaps_.length == 0 && this.outgoingMaps_.length == 0) {2266this.handler_.channelClosed(this);2267} else {2268this.channelDebug_.debug(2269'Number of undelivered maps' +2270', pending: ' + this.pendingMaps_.length + ', outgoing: ' +2271this.outgoingMaps_.length);22722273var copyOfPendingMaps = goog.array.clone(this.pendingMaps_);2274var copyOfUndeliveredMaps = goog.array.clone(this.outgoingMaps_);2275this.pendingMaps_.length = 0;2276this.outgoingMaps_.length = 0;22772278this.handler_.channelClosed(2279this, copyOfPendingMaps, copyOfUndeliveredMaps);2280}2281}2282};228322842285/**2286* Gets the Uri used for the connection that sends data to the server.2287* @param {string} path The path on the host.2288* @return {!goog.Uri} The forward channel URI.2289*/2290goog.net.BrowserChannel.prototype.getForwardChannelUri = function(path) {2291var uri = this.createDataUri(null, path);2292this.channelDebug_.debug('GetForwardChannelUri: ' + uri);2293return uri;2294};229522962297/**2298* Gets the results for the first browser channel test2299* @return {Array<string>} The results.2300*/2301goog.net.BrowserChannel.prototype.getFirstTestResults = function() {2302return this.firstTestResults_;2303};230423052306/**2307* Gets the results for the second browser channel test2308* @return {?boolean} The results. True -> buffered connection,2309* False -> unbuffered, null -> unknown.2310*/2311goog.net.BrowserChannel.prototype.getSecondTestResults = function() {2312return this.secondTestResults_;2313};231423152316/**2317* Gets the Uri used for the connection that receives data from the server.2318* @param {?string} hostPrefix The host prefix.2319* @param {string} path The path on the host.2320* @return {!goog.Uri} The back channel URI.2321*/2322goog.net.BrowserChannel.prototype.getBackChannelUri = function(2323hostPrefix, path) {2324var uri = this.createDataUri(2325this.shouldUseSecondaryDomains() ? hostPrefix : null, path);2326this.channelDebug_.debug('GetBackChannelUri: ' + uri);2327return uri;2328};232923302331/**2332* Creates a data Uri applying logic for secondary hostprefix, port2333* overrides, and versioning.2334* @param {?string} hostPrefix The host prefix.2335* @param {string} path The path on the host (may be absolute or relative).2336* @param {number=} opt_overridePort Optional override port.2337* @return {!goog.Uri} The data URI.2338*/2339goog.net.BrowserChannel.prototype.createDataUri = function(2340hostPrefix, path, opt_overridePort) {2341var uri = goog.Uri.parse(path);2342var uriAbsolute = (uri.getDomain() != '');2343if (uriAbsolute) {2344if (hostPrefix) {2345uri.setDomain(hostPrefix + '.' + uri.getDomain());2346}23472348uri.setPort(opt_overridePort || uri.getPort());2349} else {2350var locationPage = window.location;2351var hostName;2352if (hostPrefix) {2353hostName = hostPrefix + '.' + locationPage.hostname;2354} else {2355hostName = locationPage.hostname;2356}23572358var port = opt_overridePort || locationPage.port;23592360uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path);2361}23622363if (this.extraParams_) {2364goog.object.forEach(this.extraParams_, function(value, key) {2365uri.setParameterValue(key, value);2366});2367}23682369// Add the protocol version to the URI.2370uri.setParameterValue('VER', this.channelVersion_);23712372// Add the reconnect parameters.2373this.addAdditionalParams_(uri);23742375return uri;2376};237723782379/**2380* Called when BC needs to create an XhrIo object. Override in a subclass if2381* you need to customize the behavior, for example to enable the creation of2382* XHR's capable of calling a secondary domain. Will also allow calling2383* a secondary domain if withCredentials (CORS) is enabled.2384* @param {?string} hostPrefix The host prefix, if we need an XhrIo object2385* capable of calling a secondary domain.2386* @return {!goog.net.XhrIo} A new XhrIo object.2387*/2388goog.net.BrowserChannel.prototype.createXhrIo = function(hostPrefix) {2389if (hostPrefix && !this.supportsCrossDomainXhrs_) {2390throw Error('Can\'t create secondary domain capable XhrIo object.');2391}2392var xhr = new goog.net.XhrIo();2393xhr.setWithCredentials(this.supportsCrossDomainXhrs_);2394return xhr;2395};239623972398/**2399* Gets whether this channel is currently active. This is used to determine the2400* length of time to wait before retrying. This call delegates to the handler.2401* @return {boolean} Whether the channel is currently active.2402*/2403goog.net.BrowserChannel.prototype.isActive = function() {2404return !!this.handler_ && this.handler_.isActive(this);2405};240624072408/**2409* Wrapper around SafeTimeout which calls the start and end execution hooks2410* with a try...finally block.2411* @param {Function} fn The callback function.2412* @param {number} ms The time in MS for the timer.2413* @return {number} The ID of the timer.2414*/2415goog.net.BrowserChannel.setTimeout = function(fn, ms) {2416if (!goog.isFunction(fn)) {2417throw Error('Fn must not be null and must be a function');2418}2419return goog.global.setTimeout(function() {2420goog.net.BrowserChannel.onStartExecution();2421try {2422fn();2423} finally {2424goog.net.BrowserChannel.onEndExecution();2425}2426}, ms);2427};242824292430/**2431* Helper function to call the start hook2432*/2433goog.net.BrowserChannel.onStartExecution = function() {2434goog.net.BrowserChannel.startExecutionHook_();2435};243624372438/**2439* Helper function to call the end hook2440*/2441goog.net.BrowserChannel.onEndExecution = function() {2442goog.net.BrowserChannel.endExecutionHook_();2443};244424452446/**2447* Returns the singleton event target for stat events.2448* @return {goog.events.EventTarget} The event target for stat events.2449*/2450goog.net.BrowserChannel.getStatEventTarget = function() {2451return goog.net.BrowserChannel.statEventTarget_;2452};245324542455/**2456* Notify the channel that a particular fine grained network event has occurred.2457* Should be considered package-private.2458* @param {goog.net.BrowserChannel.ServerReachability} reachabilityType The2459* reachability event type.2460*/2461goog.net.BrowserChannel.prototype.notifyServerReachabilityEvent = function(2462reachabilityType) {2463var target = goog.net.BrowserChannel.statEventTarget_;2464target.dispatchEvent(2465new goog.net.BrowserChannel.ServerReachabilityEvent(2466target, reachabilityType));2467};246824692470/**2471* Helper function to call the stat event callback.2472* @param {goog.net.BrowserChannel.Stat} stat The stat.2473*/2474goog.net.BrowserChannel.notifyStatEvent = function(stat) {2475var target = goog.net.BrowserChannel.statEventTarget_;2476target.dispatchEvent(new goog.net.BrowserChannel.StatEvent(target, stat));2477};247824792480/**2481* Helper function to notify listeners about POST request performance.2482*2483* @param {number} size Number of characters in the POST data.2484* @param {number} rtt The amount of time from POST start to response.2485* @param {number} retries The number of times the POST had to be retried.2486*/2487goog.net.BrowserChannel.notifyTimingEvent = function(size, rtt, retries) {2488var target = goog.net.BrowserChannel.statEventTarget_;2489target.dispatchEvent(2490new goog.net.BrowserChannel.TimingEvent(target, size, rtt, retries));2491};249224932494/**2495* Determines whether to use a secondary domain when the server gives us2496* a host prefix. This allows us to work around browser per-domain2497* connection limits.2498*2499* Currently, we use secondary domains when using Trident's ActiveXObject,2500* because it supports cross-domain requests out of the box. Note that in IE102501* we no longer use ActiveX since it's not supported in Metro mode and IE102502* supports XHR streaming.2503*2504* If you need to use secondary domains on other browsers and IE10,2505* you have two choices:2506* 1) If you only care about browsers that support CORS2507* (https://developer.mozilla.org/en-US/docs/HTTP_access_control), you2508* can use {@link #setSupportsCrossDomainXhrs} and set the appropriate2509* CORS response headers on the server.2510* 2) Or, override this method in a subclass, and make sure that those2511* browsers use some messaging mechanism that works cross-domain (e.g2512* iframes and window.postMessage).2513*2514* @return {boolean} Whether to use secondary domains.2515* @see http://code.google.com/p/closure-library/issues/detail?id=3392516*/2517goog.net.BrowserChannel.prototype.shouldUseSecondaryDomains = function() {2518return this.supportsCrossDomainXhrs_ ||2519!goog.net.ChannelRequest.supportsXhrStreaming();2520};252125222523/**2524* A LogSaver that can be used to accumulate all the debug logs for2525* BrowserChannels so they can be sent to the server when a problem is2526* detected.2527* @const2528*/2529goog.net.BrowserChannel.LogSaver = {};253025312532/**2533* Buffer for accumulating the debug log2534* @type {goog.structs.CircularBuffer}2535* @private2536*/2537goog.net.BrowserChannel.LogSaver.buffer_ =2538new goog.structs.CircularBuffer(1000);253925402541/**2542* Whether we're currently accumulating the debug log.2543* @type {boolean}2544* @private2545*/2546goog.net.BrowserChannel.LogSaver.enabled_ = false;254725482549/**2550* Formatter for saving logs.2551* @type {goog.debug.Formatter}2552* @private2553*/2554goog.net.BrowserChannel.LogSaver.formatter_ = new goog.debug.TextFormatter();255525562557/**2558* Returns whether the LogSaver is enabled.2559* @return {boolean} Whether saving is enabled or disabled.2560*/2561goog.net.BrowserChannel.LogSaver.isEnabled = function() {2562return goog.net.BrowserChannel.LogSaver.enabled_;2563};256425652566/**2567* Enables of disables the LogSaver.2568* @param {boolean} enable Whether to enable or disable saving.2569*/2570goog.net.BrowserChannel.LogSaver.setEnabled = function(enable) {2571if (enable == goog.net.BrowserChannel.LogSaver.enabled_) {2572return;2573}25742575var fn = goog.net.BrowserChannel.LogSaver.addLogRecord;2576var logger = goog.log.getLogger('goog.net');2577if (enable) {2578goog.log.addHandler(logger, fn);2579} else {2580goog.log.removeHandler(logger, fn);2581}2582};258325842585/**2586* Adds a log record.2587* @param {goog.log.LogRecord} logRecord the LogRecord.2588*/2589goog.net.BrowserChannel.LogSaver.addLogRecord = function(logRecord) {2590goog.net.BrowserChannel.LogSaver.buffer_.add(2591goog.net.BrowserChannel.LogSaver.formatter_.formatRecord(logRecord));2592};259325942595/**2596* Returns the log as a single string.2597* @return {string} The log as a single string.2598*/2599goog.net.BrowserChannel.LogSaver.getBuffer = function() {2600return goog.net.BrowserChannel.LogSaver.buffer_.getValues().join('');2601};260226032604/**2605* Clears the buffer2606*/2607goog.net.BrowserChannel.LogSaver.clearBuffer = function() {2608goog.net.BrowserChannel.LogSaver.buffer_.clear();2609};2610261126122613/**2614* Abstract base class for the browser channel handler2615* @constructor2616*/2617goog.net.BrowserChannel.Handler = function() {};261826192620/**2621* Callback handler for when a batch of response arrays is received from the2622* server.2623* @type {?function(!goog.net.BrowserChannel, !Array<!Array<?>>)}2624*/2625goog.net.BrowserChannel.Handler.prototype.channelHandleMultipleArrays = null;262626272628/**2629* Whether it's okay to make a request to the server. A handler can return2630* false if the channel should fail. For example, if the user has logged out,2631* the handler may want all requests to fail immediately.2632* @param {goog.net.BrowserChannel} browserChannel The browser channel.2633* @return {goog.net.BrowserChannel.Error} An error code. The code should2634* return goog.net.BrowserChannel.Error.OK to indicate it's okay. Any other2635* error code will cause a failure.2636*/2637goog.net.BrowserChannel.Handler.prototype.okToMakeRequest = function(2638browserChannel) {2639return goog.net.BrowserChannel.Error.OK;2640};264126422643/**2644* Indicates the BrowserChannel has successfully negotiated with the server2645* and can now send and receive data.2646* @param {goog.net.BrowserChannel} browserChannel The browser channel.2647*/2648goog.net.BrowserChannel.Handler.prototype.channelOpened = function(2649browserChannel) {};265026512652/**2653* New input is available for the application to process.2654*2655* @param {goog.net.BrowserChannel} browserChannel The browser channel.2656* @param {Array<?>} array The data array.2657*/2658goog.net.BrowserChannel.Handler.prototype.channelHandleArray = function(2659browserChannel, array) {};266026612662/**2663* Indicates maps were successfully sent on the BrowserChannel.2664*2665* @param {goog.net.BrowserChannel} browserChannel The browser channel.2666* @param {Array<goog.net.BrowserChannel.QueuedMap>} deliveredMaps The2667* array of maps that have been delivered to the server. This is a direct2668* reference to the internal BrowserChannel array, so a copy should be made2669* if the caller desires a reference to the data.2670*/2671goog.net.BrowserChannel.Handler.prototype.channelSuccess = function(2672browserChannel, deliveredMaps) {};267326742675/**2676* Indicates an error occurred on the BrowserChannel.2677*2678* @param {goog.net.BrowserChannel} browserChannel The browser channel.2679* @param {goog.net.BrowserChannel.Error} error The error code.2680*/2681goog.net.BrowserChannel.Handler.prototype.channelError = function(2682browserChannel, error) {};268326842685/**2686* Indicates the BrowserChannel is closed. Also notifies about which maps,2687* if any, that may not have been delivered to the server.2688* @param {goog.net.BrowserChannel} browserChannel The browser channel.2689* @param {Array<goog.net.BrowserChannel.QueuedMap>=} opt_pendingMaps The2690* array of pending maps, which may or may not have been delivered to the2691* server.2692* @param {Array<goog.net.BrowserChannel.QueuedMap>=} opt_undeliveredMaps2693* The array of undelivered maps, which have definitely not been delivered2694* to the server.2695*/2696goog.net.BrowserChannel.Handler.prototype.channelClosed = function(2697browserChannel, opt_pendingMaps, opt_undeliveredMaps) {};269826992700/**2701* Gets any parameters that should be added at the time another connection is2702* made to the server.2703* @param {goog.net.BrowserChannel} browserChannel The browser channel.2704* @return {!Object} Extra parameter keys and values to add to the2705* requests.2706*/2707goog.net.BrowserChannel.Handler.prototype.getAdditionalParams = function(2708browserChannel) {2709return {};2710};271127122713/**2714* Gets the URI of an image that can be used to test network connectivity.2715* @param {goog.net.BrowserChannel} browserChannel The browser channel.2716* @return {goog.Uri?} A custom URI to load for the network test.2717*/2718goog.net.BrowserChannel.Handler.prototype.getNetworkTestImageUri = function(2719browserChannel) {2720return null;2721};272227232724/**2725* Gets whether this channel is currently active. This is used to determine the2726* length of time to wait before retrying.2727* @param {goog.net.BrowserChannel} browserChannel The browser channel.2728* @return {boolean} Whether the channel is currently active.2729*/2730goog.net.BrowserChannel.Handler.prototype.isActive = function(browserChannel) {2731return true;2732};273327342735/**2736* Called by the channel if enumeration of the map throws an exception.2737* @param {goog.net.BrowserChannel} browserChannel The browser channel.2738* @param {Object} map The map that can't be enumerated.2739*/2740goog.net.BrowserChannel.Handler.prototype.badMapError = function(2741browserChannel, map) {2742return;2743};274427452746/**2747* Allows the handler to override a host prefix provided by the server. Will2748* be called whenever the channel has received such a prefix and is considering2749* its use.2750* @param {?string} serverHostPrefix The host prefix provided by the server.2751* @return {?string} The host prefix the client should use.2752*/2753goog.net.BrowserChannel.Handler.prototype.correctHostPrefix = function(2754serverHostPrefix) {2755return serverHostPrefix;2756};275727582759