Path: blob/trunk/third_party/closure/goog/labs/net/webchannel/netutils.js
1865 views
// Copyright 2013 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 Utility functions for managing networking, such as16* testing network connectivity.17*18* @visibility {:internal}19*/202122goog.provide('goog.labs.net.webChannel.netUtils');2324goog.require('goog.Uri');25goog.require('goog.labs.net.webChannel.WebChannelDebug');2627goog.scope(function() {28var netUtils = goog.labs.net.webChannel.netUtils;29var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug;303132/**33* Default timeout to allow for URI pings.34* @type {number}35*/36netUtils.NETWORK_TIMEOUT = 10000;373839/**40* Pings the network with an image URI to check if an error is a server error41* or user's network error.42*43* The caller needs to add a 'rand' parameter to make sure the response is44* not fulfilled by browser cache.45*46* @param {function(boolean)} callback The function to call back with results.47* @param {goog.Uri=} opt_imageUri The URI (of an image) to use for the network48* test.49*/50netUtils.testNetwork = function(callback, opt_imageUri) {51var uri = opt_imageUri;52if (!uri) {53// default google.com image54uri = new goog.Uri('//www.google.com/images/cleardot.gif');5556if (!(goog.global.location && goog.global.location.protocol == 'http')) {57uri.setScheme('https'); // e.g. chrome-extension58}59uri.makeUnique();60}6162netUtils.testLoadImage(uri.toString(), netUtils.NETWORK_TIMEOUT, callback);63};646566/**67* Test loading the given image, retrying if necessary.68* @param {string} url URL to the image.69* @param {number} timeout Milliseconds before giving up.70* @param {function(boolean)} callback Function to call with results.71* @param {number} retries The number of times to retry.72* @param {number=} opt_pauseBetweenRetriesMS Optional number of milliseconds73* between retries - defaults to 0.74*/75netUtils.testLoadImageWithRetries = function(76url, timeout, callback, retries, opt_pauseBetweenRetriesMS) {77var channelDebug = new WebChannelDebug();78channelDebug.debug('TestLoadImageWithRetries: ' + opt_pauseBetweenRetriesMS);79if (retries == 0) {80// no more retries, give up81callback(false);82return;83}8485var pauseBetweenRetries = opt_pauseBetweenRetriesMS || 0;86retries--;87netUtils.testLoadImage(url, timeout, function(succeeded) {88if (succeeded) {89callback(true);90} else {91// try again92goog.global.setTimeout(function() {93netUtils.testLoadImageWithRetries(94url, timeout, callback, retries, pauseBetweenRetries);95}, pauseBetweenRetries);96}97});98};99100101/**102* Test loading the given image.103* @param {string} url URL to the image.104* @param {number} timeout Milliseconds before giving up.105* @param {function(boolean)} callback Function to call with results.106*/107netUtils.testLoadImage = function(url, timeout, callback) {108var channelDebug = new WebChannelDebug();109channelDebug.debug('TestLoadImage: loading ' + url);110var img = new Image();111img.onload = goog.partial(112netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: loaded', true,113callback);114img.onerror = goog.partial(115netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: error', false,116callback);117img.onabort = goog.partial(118netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: abort', false,119callback);120img.ontimeout = goog.partial(121netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: timeout',122false, callback);123124goog.global.setTimeout(function() {125if (img.ontimeout) {126img.ontimeout();127}128}, timeout);129img.src = url;130};131132133/**134* Wrap the image callback with debug and cleanup logic.135* @param {!WebChannelDebug} channelDebug The WebChannelDebug object.136* @param {!Image} img The image element.137* @param {string} debugText The debug text.138* @param {boolean} result The result of image loading.139* @param {function(boolean)} callback The image callback.140* @private141*/142netUtils.imageCallback_ = function(143channelDebug, img, debugText, result, callback) {144try {145channelDebug.debug(debugText);146netUtils.clearImageCallbacks_(img);147callback(result);148} catch (e) {149channelDebug.dumpException(e);150}151};152153154/**155* Clears handlers to avoid memory leaks.156* @param {Image} img The image to clear handlers from.157* @private158*/159netUtils.clearImageCallbacks_ = function(img) {160img.onload = null;161img.onerror = null;162img.onabort = null;163img.ontimeout = null;164};165}); // goog.scope166167168