Path: blob/trunk/third_party/closure/goog/net/tmpnetwork.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 tmpnetwork.js contains some temporary networking functions16* for browserchannel which will be moved at a later date.17*/181920/**21* Namespace for BrowserChannel22*/23goog.provide('goog.net.tmpnetwork');2425goog.require('goog.Uri');26goog.require('goog.net.ChannelDebug');272829/**30* Default timeout to allow for google.com pings.31* @type {number}32*/33goog.net.tmpnetwork.GOOGLECOM_TIMEOUT = 10000;343536/**37* @define {string} url to use to test for internet connectivity.38* Use protocol-relative URLs to avoid insecure content warnings in IE.39*/40goog.define(41'goog.net.tmpnetwork.TEST_URL', '//www.google.com/images/cleardot.gif');424344/**45* Pings the network to check if an error is a server error or user's network46* error.47*48* @param {Function} callback The function to call back with results.49* @param {goog.Uri?=} opt_imageUri The URI of an image to use for the network50* test. You *must* provide an image URI; the default behavior is provided51* for compatibility with existing code, but the search team does not want52* people using images served off of google.com for this purpose. The53* default will go away when all usages have been changed.54*/55goog.net.tmpnetwork.testGoogleCom = function(callback, opt_imageUri) {56// We need to add a 'rand' to make sure the response is not fulfilled57// by browser cache.58var uri = opt_imageUri;59if (!uri) {60uri = new goog.Uri(goog.net.tmpnetwork.TEST_URL);61uri.makeUnique();62}63goog.net.tmpnetwork.testLoadImage(64uri.toString(), goog.net.tmpnetwork.GOOGLECOM_TIMEOUT, callback);65};666768/**69* Test loading the given image, retrying if necessary.70* @param {string} url URL to the iamge.71* @param {number} timeout Milliseconds before giving up.72* @param {Function} callback Function to call with results.73* @param {number} retries The number of times to retry.74* @param {number=} opt_pauseBetweenRetriesMS Optional number of milliseconds75* between retries - defaults to 0.76*/77goog.net.tmpnetwork.testLoadImageWithRetries = function(78url, timeout, callback, retries, opt_pauseBetweenRetriesMS) {79var channelDebug = new goog.net.ChannelDebug();80channelDebug.debug('TestLoadImageWithRetries: ' + opt_pauseBetweenRetriesMS);81if (retries == 0) {82// no more retries, give up83callback(false);84return;85}8687var pauseBetweenRetries = opt_pauseBetweenRetriesMS || 0;88retries--;89goog.net.tmpnetwork.testLoadImage(url, timeout, function(succeeded) {90if (succeeded) {91callback(true);92} else {93// try again94goog.global.setTimeout(function() {95goog.net.tmpnetwork.testLoadImageWithRetries(96url, timeout, callback, retries, pauseBetweenRetries);97}, pauseBetweenRetries);98}99});100};101102103/**104* Test loading the given image.105* @param {string} url URL to the iamge.106* @param {number} timeout Milliseconds before giving up.107* @param {Function} callback Function to call with results.108*/109goog.net.tmpnetwork.testLoadImage = function(url, timeout, callback) {110var channelDebug = new goog.net.ChannelDebug();111channelDebug.debug('TestLoadImage: loading ' + url);112var img = new Image();113img.onload = function() {114try {115channelDebug.debug('TestLoadImage: loaded');116goog.net.tmpnetwork.clearImageCallbacks_(img);117callback(true);118} catch (e) {119channelDebug.dumpException(e);120}121};122img.onerror = function() {123try {124channelDebug.debug('TestLoadImage: error');125goog.net.tmpnetwork.clearImageCallbacks_(img);126callback(false);127} catch (e) {128channelDebug.dumpException(e);129}130};131img.onabort = function() {132try {133channelDebug.debug('TestLoadImage: abort');134goog.net.tmpnetwork.clearImageCallbacks_(img);135callback(false);136} catch (e) {137channelDebug.dumpException(e);138}139};140img.ontimeout = function() {141try {142channelDebug.debug('TestLoadImage: timeout');143goog.net.tmpnetwork.clearImageCallbacks_(img);144callback(false);145} catch (e) {146channelDebug.dumpException(e);147}148};149150goog.global.setTimeout(function() {151if (img.ontimeout) {152img.ontimeout();153}154}, timeout);155img.src = url;156};157158159/**160* Clear handlers to avoid memory leaks.161* @param {Image} img The image to clear handlers from.162* @private163*/164goog.net.tmpnetwork.clearImageCallbacks_ = function(img) {165// NOTE(user): Nullified individually to avoid compiler warnings166// (BUG 658126)167img.onload = null;168img.onerror = null;169img.onabort = null;170img.ontimeout = null;171};172173174