tmpnetwork.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview tmpnetwork.js contains some temporary networking functions
  16. * for browserchannel which will be moved at a later date.
  17. */
  18. /**
  19. * Namespace for BrowserChannel
  20. */
  21. goog.provide('goog.net.tmpnetwork');
  22. goog.require('goog.Uri');
  23. goog.require('goog.net.ChannelDebug');
  24. /**
  25. * Default timeout to allow for google.com pings.
  26. * @type {number}
  27. */
  28. goog.net.tmpnetwork.GOOGLECOM_TIMEOUT = 10000;
  29. /**
  30. * @define {string} url to use to test for internet connectivity.
  31. * Use protocol-relative URLs to avoid insecure content warnings in IE.
  32. */
  33. goog.define(
  34. 'goog.net.tmpnetwork.TEST_URL', '//www.google.com/images/cleardot.gif');
  35. /**
  36. * Pings the network to check if an error is a server error or user's network
  37. * error.
  38. *
  39. * @param {Function} callback The function to call back with results.
  40. * @param {goog.Uri?=} opt_imageUri The URI of an image to use for the network
  41. * test. You *must* provide an image URI; the default behavior is provided
  42. * for compatibility with existing code, but the search team does not want
  43. * people using images served off of google.com for this purpose. The
  44. * default will go away when all usages have been changed.
  45. */
  46. goog.net.tmpnetwork.testGoogleCom = function(callback, opt_imageUri) {
  47. // We need to add a 'rand' to make sure the response is not fulfilled
  48. // by browser cache.
  49. var uri = opt_imageUri;
  50. if (!uri) {
  51. uri = new goog.Uri(goog.net.tmpnetwork.TEST_URL);
  52. uri.makeUnique();
  53. }
  54. goog.net.tmpnetwork.testLoadImage(
  55. uri.toString(), goog.net.tmpnetwork.GOOGLECOM_TIMEOUT, callback);
  56. };
  57. /**
  58. * Test loading the given image, retrying if necessary.
  59. * @param {string} url URL to the iamge.
  60. * @param {number} timeout Milliseconds before giving up.
  61. * @param {Function} callback Function to call with results.
  62. * @param {number} retries The number of times to retry.
  63. * @param {number=} opt_pauseBetweenRetriesMS Optional number of milliseconds
  64. * between retries - defaults to 0.
  65. */
  66. goog.net.tmpnetwork.testLoadImageWithRetries = function(
  67. url, timeout, callback, retries, opt_pauseBetweenRetriesMS) {
  68. var channelDebug = new goog.net.ChannelDebug();
  69. channelDebug.debug('TestLoadImageWithRetries: ' + opt_pauseBetweenRetriesMS);
  70. if (retries == 0) {
  71. // no more retries, give up
  72. callback(false);
  73. return;
  74. }
  75. var pauseBetweenRetries = opt_pauseBetweenRetriesMS || 0;
  76. retries--;
  77. goog.net.tmpnetwork.testLoadImage(url, timeout, function(succeeded) {
  78. if (succeeded) {
  79. callback(true);
  80. } else {
  81. // try again
  82. goog.global.setTimeout(function() {
  83. goog.net.tmpnetwork.testLoadImageWithRetries(
  84. url, timeout, callback, retries, pauseBetweenRetries);
  85. }, pauseBetweenRetries);
  86. }
  87. });
  88. };
  89. /**
  90. * Test loading the given image.
  91. * @param {string} url URL to the iamge.
  92. * @param {number} timeout Milliseconds before giving up.
  93. * @param {Function} callback Function to call with results.
  94. */
  95. goog.net.tmpnetwork.testLoadImage = function(url, timeout, callback) {
  96. var channelDebug = new goog.net.ChannelDebug();
  97. channelDebug.debug('TestLoadImage: loading ' + url);
  98. var img = new Image();
  99. img.onload = function() {
  100. try {
  101. channelDebug.debug('TestLoadImage: loaded');
  102. goog.net.tmpnetwork.clearImageCallbacks_(img);
  103. callback(true);
  104. } catch (e) {
  105. channelDebug.dumpException(e);
  106. }
  107. };
  108. img.onerror = function() {
  109. try {
  110. channelDebug.debug('TestLoadImage: error');
  111. goog.net.tmpnetwork.clearImageCallbacks_(img);
  112. callback(false);
  113. } catch (e) {
  114. channelDebug.dumpException(e);
  115. }
  116. };
  117. img.onabort = function() {
  118. try {
  119. channelDebug.debug('TestLoadImage: abort');
  120. goog.net.tmpnetwork.clearImageCallbacks_(img);
  121. callback(false);
  122. } catch (e) {
  123. channelDebug.dumpException(e);
  124. }
  125. };
  126. img.ontimeout = function() {
  127. try {
  128. channelDebug.debug('TestLoadImage: timeout');
  129. goog.net.tmpnetwork.clearImageCallbacks_(img);
  130. callback(false);
  131. } catch (e) {
  132. channelDebug.dumpException(e);
  133. }
  134. };
  135. goog.global.setTimeout(function() {
  136. if (img.ontimeout) {
  137. img.ontimeout();
  138. }
  139. }, timeout);
  140. img.src = url;
  141. };
  142. /**
  143. * Clear handlers to avoid memory leaks.
  144. * @param {Image} img The image to clear handlers from.
  145. * @private
  146. */
  147. goog.net.tmpnetwork.clearImageCallbacks_ = function(img) {
  148. // NOTE(user): Nullified individually to avoid compiler warnings
  149. // (BUG 658126)
  150. img.onload = null;
  151. img.onerror = null;
  152. img.onabort = null;
  153. img.ontimeout = null;
  154. };