123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- goog.provide('goog.net.BrowserTestChannel');
- goog.require('goog.json.NativeJsonProcessor');
- goog.require('goog.net.ChannelRequest');
- goog.require('goog.net.ChannelRequest.Error');
- goog.require('goog.net.tmpnetwork');
- goog.require('goog.string.Parser');
- goog.net.BrowserTestChannel = function(channel, channelDebug) {
-
- this.channel_ = channel;
-
- this.channelDebug_ = channelDebug;
-
- this.parser_ = new goog.json.NativeJsonProcessor();
- };
- goog.net.BrowserTestChannel.prototype.extraHeaders_ = null;
- goog.net.BrowserTestChannel.prototype.request_ = null;
- goog.net.BrowserTestChannel.prototype.receivedIntermediateResult_ = false;
- goog.net.BrowserTestChannel.prototype.startTime_ = null;
- goog.net.BrowserTestChannel.prototype.firstTime_ = null;
- goog.net.BrowserTestChannel.prototype.lastTime_ = null;
- goog.net.BrowserTestChannel.prototype.path_ = null;
- goog.net.BrowserTestChannel.prototype.state_ = null;
- goog.net.BrowserTestChannel.prototype.lastStatusCode_ = -1;
- goog.net.BrowserTestChannel.prototype.hostPrefix_ = null;
- goog.net.BrowserTestChannel.prototype.blockedPrefix_ = null;
- goog.net.BrowserTestChannel.State_ = {
-
- INIT: 0,
-
- CHECKING_BLOCKED: 1,
-
- CONNECTION_TESTING: 2
- };
- goog.net.BrowserTestChannel.BLOCKED_TIMEOUT_ = 5000;
- goog.net.BrowserTestChannel.BLOCKED_RETRIES_ = 3;
- goog.net.BrowserTestChannel.BLOCKED_PAUSE_BETWEEN_RETRIES_ = 2000;
- goog.net.BrowserTestChannel.MIN_TIME_EXPECTED_BETWEEN_DATA_ = 500;
- goog.net.BrowserTestChannel.prototype.setExtraHeaders = function(extraHeaders) {
- this.extraHeaders_ = extraHeaders;
- };
- goog.net.BrowserTestChannel.prototype.setParser = function(parser) {
- this.parser_ = parser;
- };
- goog.net.BrowserTestChannel.prototype.connect = function(path) {
- this.path_ = path;
- var sendDataUri = this.channel_.getForwardChannelUri(this.path_);
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_ONE_START);
- this.startTime_ = goog.now();
-
- var firstTestResults = this.channel_.getFirstTestResults();
- if (goog.isDefAndNotNull(firstTestResults)) {
- this.hostPrefix_ = this.channel_.correctHostPrefix(firstTestResults[0]);
- this.blockedPrefix_ = firstTestResults[1];
- if (this.blockedPrefix_) {
- this.state_ = goog.net.BrowserTestChannel.State_.CHECKING_BLOCKED;
- this.checkBlocked_();
- } else {
- this.state_ = goog.net.BrowserTestChannel.State_.CONNECTION_TESTING;
- this.connectStage2_();
- }
- return;
- }
-
- sendDataUri.setParameterValues('MODE', 'init');
- this.request_ =
- goog.net.BrowserChannel.createChannelRequest(this, this.channelDebug_);
- this.request_.setExtraHeaders(this.extraHeaders_);
- this.request_.xmlHttpGet(
- sendDataUri, false , null ,
- true );
- this.state_ = goog.net.BrowserTestChannel.State_.INIT;
- };
- goog.net.BrowserTestChannel.prototype.checkBlocked_ = function() {
- var uri = this.channel_.createDataUri(
- this.blockedPrefix_, '/mail/images/cleardot.gif');
- uri.makeUnique();
- goog.net.tmpnetwork.testLoadImageWithRetries(
- uri.toString(), goog.net.BrowserTestChannel.BLOCKED_TIMEOUT_,
- goog.bind(this.checkBlockedCallback_, this),
- goog.net.BrowserTestChannel.BLOCKED_RETRIES_,
- goog.net.BrowserTestChannel.BLOCKED_PAUSE_BETWEEN_RETRIES_);
- this.notifyServerReachabilityEvent(
- goog.net.BrowserChannel.ServerReachability.REQUEST_MADE);
- };
- goog.net.BrowserTestChannel.prototype.checkBlockedCallback_ = function(
- succeeded) {
- if (succeeded) {
- this.state_ = goog.net.BrowserTestChannel.State_.CONNECTION_TESTING;
- this.connectStage2_();
- } else {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.CHANNEL_BLOCKED);
- this.channel_.testConnectionBlocked(this);
- }
-
-
-
- if (succeeded) {
- this.notifyServerReachabilityEvent(
- goog.net.BrowserChannel.ServerReachability.REQUEST_SUCCEEDED);
- }
- };
- goog.net.BrowserTestChannel.prototype.connectStage2_ = function() {
- this.channelDebug_.debug('TestConnection: starting stage 2');
-
- var secondTestResults = this.channel_.getSecondTestResults();
- if (goog.isDefAndNotNull(secondTestResults)) {
- this.channelDebug_.debug(
- 'TestConnection: skipping stage 2, precomputed result is ' +
- secondTestResults ?
- 'Buffered' :
- 'Unbuffered');
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_TWO_START);
- if (secondTestResults) {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.PROXY);
- this.channel_.testConnectionFinished(this, false);
- } else {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.NOPROXY);
- this.channel_.testConnectionFinished(this, true);
- }
- return;
- }
-
- this.request_ =
- goog.net.BrowserChannel.createChannelRequest(this, this.channelDebug_);
- this.request_.setExtraHeaders(this.extraHeaders_);
- var recvDataUri = this.channel_.getBackChannelUri(
- this.hostPrefix_,
- (this.path_));
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_TWO_START);
- if (!goog.net.ChannelRequest.supportsXhrStreaming()) {
- recvDataUri.setParameterValues('TYPE', 'html');
- this.request_.tridentGet(recvDataUri, Boolean(this.hostPrefix_));
- } else {
- recvDataUri.setParameterValues('TYPE', 'xmlhttp');
- this.request_.xmlHttpGet(
- recvDataUri, false , this.hostPrefix_,
- false );
- }
- };
- goog.net.BrowserTestChannel.prototype.createXhrIo = function(hostPrefix) {
- return this.channel_.createXhrIo(hostPrefix);
- };
- goog.net.BrowserTestChannel.prototype.abort = function() {
- if (this.request_) {
- this.request_.cancel();
- this.request_ = null;
- }
- this.lastStatusCode_ = -1;
- };
- goog.net.BrowserTestChannel.prototype.isClosed = function() {
- return false;
- };
- goog.net.BrowserTestChannel.prototype.onRequestData = function(
- req, responseText) {
- this.lastStatusCode_ = req.getLastStatusCode();
- if (this.state_ == goog.net.BrowserTestChannel.State_.INIT) {
- this.channelDebug_.debug('TestConnection: Got data for stage 1');
- if (!responseText) {
- this.channelDebug_.debug('TestConnection: Null responseText');
-
- this.channel_.testConnectionFailure(
- this, goog.net.ChannelRequest.Error.BAD_DATA);
- return;
- }
- try {
- var respArray = this.parser_.parse(responseText);
- } catch (e) {
- this.channelDebug_.dumpException(e);
- this.channel_.testConnectionFailure(
- this, goog.net.ChannelRequest.Error.BAD_DATA);
- return;
- }
- this.hostPrefix_ = this.channel_.correctHostPrefix(respArray[0]);
- this.blockedPrefix_ = respArray[1];
- } else if (
- this.state_ == goog.net.BrowserTestChannel.State_.CONNECTION_TESTING) {
- if (this.receivedIntermediateResult_) {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_TWO_DATA_TWO);
- this.lastTime_ = goog.now();
- } else {
-
-
- if (responseText == '11111') {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_TWO_DATA_ONE);
- this.receivedIntermediateResult_ = true;
- this.firstTime_ = goog.now();
- if (this.checkForEarlyNonBuffered_()) {
-
-
- this.lastStatusCode_ = 200;
- this.request_.cancel();
- this.channelDebug_.debug(
- 'Test connection succeeded; using streaming connection');
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.NOPROXY);
- this.channel_.testConnectionFinished(this, true);
- }
- } else {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_TWO_DATA_BOTH);
- this.firstTime_ = this.lastTime_ = goog.now();
- this.receivedIntermediateResult_ = false;
- }
- }
- }
- };
- goog.net.BrowserTestChannel.prototype.onRequestComplete = function(req) {
- this.lastStatusCode_ = this.request_.getLastStatusCode();
- if (!this.request_.getSuccess()) {
- this.channelDebug_.debug(
- 'TestConnection: request failed, in state ' + this.state_);
- if (this.state_ == goog.net.BrowserTestChannel.State_.INIT) {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_ONE_FAILED);
- } else if (
- this.state_ == goog.net.BrowserTestChannel.State_.CONNECTION_TESTING) {
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.TEST_STAGE_TWO_FAILED);
- }
- this.channel_.testConnectionFailure(
- this,
-
- (this.request_.getLastError()));
- return;
- }
- if (this.state_ == goog.net.BrowserTestChannel.State_.INIT) {
- this.channelDebug_.debug(
- 'TestConnection: request complete for initial check');
- if (this.blockedPrefix_) {
- this.state_ = goog.net.BrowserTestChannel.State_.CHECKING_BLOCKED;
- this.checkBlocked_();
- } else {
- this.state_ = goog.net.BrowserTestChannel.State_.CONNECTION_TESTING;
- this.connectStage2_();
- }
- } else if (
- this.state_ == goog.net.BrowserTestChannel.State_.CONNECTION_TESTING) {
- this.channelDebug_.debug('TestConnection: request complete for stage 2');
- var goodConn = false;
- if (!goog.net.ChannelRequest.supportsXhrStreaming()) {
-
-
- var ms = this.lastTime_ - this.firstTime_;
- if (ms < 200) {
-
-
- goodConn = false;
- } else {
- goodConn = true;
- }
- } else {
- goodConn = this.receivedIntermediateResult_;
- }
- if (goodConn) {
- this.channelDebug_.debug(
- 'Test connection succeeded; using streaming connection');
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.NOPROXY);
- this.channel_.testConnectionFinished(this, true);
- } else {
- this.channelDebug_.debug('Test connection failed; not using streaming');
-
- goog.net.BrowserChannel.notifyStatEvent(
- goog.net.BrowserChannel.Stat.PROXY);
- this.channel_.testConnectionFinished(this, false);
- }
- }
- };
- goog.net.BrowserTestChannel.prototype.getLastStatusCode = function() {
- return this.lastStatusCode_;
- };
- goog.net.BrowserTestChannel.prototype.shouldUseSecondaryDomains = function() {
- return this.channel_.shouldUseSecondaryDomains();
- };
- goog.net.BrowserTestChannel.prototype.isActive = function(browserChannel) {
- return this.channel_.isActive();
- };
- goog.net.BrowserTestChannel.prototype.checkForEarlyNonBuffered_ = function() {
- var ms = this.firstTime_ - this.startTime_;
-
-
-
-
- return goog.net.ChannelRequest.supportsXhrStreaming() ||
- ms < goog.net.BrowserTestChannel.MIN_TIME_EXPECTED_BETWEEN_DATA_;
- };
- goog.net.BrowserTestChannel.prototype.notifyServerReachabilityEvent = function(
- reachabilityType) {
- this.channel_.notifyServerReachabilityEvent(reachabilityType);
- };
|