netutils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2013 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 Utility functions for managing networking, such as
  16. * testing network connectivity.
  17. *
  18. * @visibility {:internal}
  19. */
  20. goog.provide('goog.labs.net.webChannel.netUtils');
  21. goog.require('goog.Uri');
  22. goog.require('goog.labs.net.webChannel.WebChannelDebug');
  23. goog.scope(function() {
  24. var netUtils = goog.labs.net.webChannel.netUtils;
  25. var WebChannelDebug = goog.labs.net.webChannel.WebChannelDebug;
  26. /**
  27. * Default timeout to allow for URI pings.
  28. * @type {number}
  29. */
  30. netUtils.NETWORK_TIMEOUT = 10000;
  31. /**
  32. * Pings the network with an image URI to check if an error is a server error
  33. * or user's network error.
  34. *
  35. * The caller needs to add a 'rand' parameter to make sure the response is
  36. * not fulfilled by browser cache.
  37. *
  38. * @param {function(boolean)} callback The function to call back with results.
  39. * @param {goog.Uri=} opt_imageUri The URI (of an image) to use for the network
  40. * test.
  41. */
  42. netUtils.testNetwork = function(callback, opt_imageUri) {
  43. var uri = opt_imageUri;
  44. if (!uri) {
  45. // default google.com image
  46. uri = new goog.Uri('//www.google.com/images/cleardot.gif');
  47. if (!(goog.global.location && goog.global.location.protocol == 'http')) {
  48. uri.setScheme('https'); // e.g. chrome-extension
  49. }
  50. uri.makeUnique();
  51. }
  52. netUtils.testLoadImage(uri.toString(), netUtils.NETWORK_TIMEOUT, callback);
  53. };
  54. /**
  55. * Test loading the given image, retrying if necessary.
  56. * @param {string} url URL to the image.
  57. * @param {number} timeout Milliseconds before giving up.
  58. * @param {function(boolean)} callback Function to call with results.
  59. * @param {number} retries The number of times to retry.
  60. * @param {number=} opt_pauseBetweenRetriesMS Optional number of milliseconds
  61. * between retries - defaults to 0.
  62. */
  63. netUtils.testLoadImageWithRetries = function(
  64. url, timeout, callback, retries, opt_pauseBetweenRetriesMS) {
  65. var channelDebug = new WebChannelDebug();
  66. channelDebug.debug('TestLoadImageWithRetries: ' + opt_pauseBetweenRetriesMS);
  67. if (retries == 0) {
  68. // no more retries, give up
  69. callback(false);
  70. return;
  71. }
  72. var pauseBetweenRetries = opt_pauseBetweenRetriesMS || 0;
  73. retries--;
  74. netUtils.testLoadImage(url, timeout, function(succeeded) {
  75. if (succeeded) {
  76. callback(true);
  77. } else {
  78. // try again
  79. goog.global.setTimeout(function() {
  80. netUtils.testLoadImageWithRetries(
  81. url, timeout, callback, retries, pauseBetweenRetries);
  82. }, pauseBetweenRetries);
  83. }
  84. });
  85. };
  86. /**
  87. * Test loading the given image.
  88. * @param {string} url URL to the image.
  89. * @param {number} timeout Milliseconds before giving up.
  90. * @param {function(boolean)} callback Function to call with results.
  91. */
  92. netUtils.testLoadImage = function(url, timeout, callback) {
  93. var channelDebug = new WebChannelDebug();
  94. channelDebug.debug('TestLoadImage: loading ' + url);
  95. var img = new Image();
  96. img.onload = goog.partial(
  97. netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: loaded', true,
  98. callback);
  99. img.onerror = goog.partial(
  100. netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: error', false,
  101. callback);
  102. img.onabort = goog.partial(
  103. netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: abort', false,
  104. callback);
  105. img.ontimeout = goog.partial(
  106. netUtils.imageCallback_, channelDebug, img, 'TestLoadImage: timeout',
  107. false, callback);
  108. goog.global.setTimeout(function() {
  109. if (img.ontimeout) {
  110. img.ontimeout();
  111. }
  112. }, timeout);
  113. img.src = url;
  114. };
  115. /**
  116. * Wrap the image callback with debug and cleanup logic.
  117. * @param {!WebChannelDebug} channelDebug The WebChannelDebug object.
  118. * @param {!Image} img The image element.
  119. * @param {string} debugText The debug text.
  120. * @param {boolean} result The result of image loading.
  121. * @param {function(boolean)} callback The image callback.
  122. * @private
  123. */
  124. netUtils.imageCallback_ = function(
  125. channelDebug, img, debugText, result, callback) {
  126. try {
  127. channelDebug.debug(debugText);
  128. netUtils.clearImageCallbacks_(img);
  129. callback(result);
  130. } catch (e) {
  131. channelDebug.dumpException(e);
  132. }
  133. };
  134. /**
  135. * Clears handlers to avoid memory leaks.
  136. * @param {Image} img The image to clear handlers from.
  137. * @private
  138. */
  139. netUtils.clearImageCallbacks_ = function(img) {
  140. img.onload = null;
  141. img.onerror = null;
  142. img.onabort = null;
  143. img.ontimeout = null;
  144. };
  145. }); // goog.scope