1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Definition of goog.net.NetworkTester.
*/
goog.provide('goog.net.NetworkTester');
goog.require('goog.Timer');
goog.require('goog.Uri');
goog.require('goog.log');
/**
* Creates an instance of goog.net.NetworkTester which can be used to test
* for internet connectivity by seeing if an image can be loaded from
* google.com. It can also be tested with other URLs.
* @param {Function} callback Callback that is called when the test completes.
* The callback takes a single boolean parameter. True indicates the URL
* was reachable, false indicates it wasn't.
* @param {Object=} opt_handler Handler object for the callback.
* @param {goog.Uri=} opt_uri URI to use for testing.
* @constructor @struct
* @final
*/
goog.net.NetworkTester = function(callback, opt_handler, opt_uri) {
/**
* Callback that is called when the test completes.
* The callback takes a single boolean parameter. True indicates the URL was
* reachable, false indicates it wasn't.
* @type {Function}
* @private
*/
this.callback_ = callback;
/**
* Handler object for the callback.
* @type {Object|undefined}
* @private
*/
this.handler_ = opt_handler;
if (!opt_uri) {
// set the default URI to be based on the cleardot image at google.com
// We need to add a 'rand' to make sure the response is not fulfilled
// by browser cache. Use protocol-relative URLs to avoid insecure content
// warnings in IE.
opt_uri = new goog.Uri('//www.google.com/images/cleardot.gif');
opt_uri.makeUnique();
}
/**
* Uri to use for test. Defaults to using an image off of google.com
* @type {goog.Uri}
* @private
*/
this.uri_ = opt_uri;
};
/**
* Default timeout
* @type {number}
*/
goog.net.NetworkTester.DEFAULT_TIMEOUT_MS = 10000;
/**
* Logger object
* @type {goog.log.Logger}
* @private
*/
goog.net.NetworkTester.prototype.logger_ =
goog.log.getLogger('goog.net.NetworkTester');
/**
* Timeout for test
* @type {number}
* @private
*/
goog.net.NetworkTester.prototype.timeoutMs_ =
goog.net.NetworkTester.DEFAULT_TIMEOUT_MS;
/**
* Whether we've already started running.
* @type {boolean}
* @private
*/
goog.net.NetworkTester.prototype.running_ = false;
/**
* Number of retries to attempt
* @type {number}
* @private
*/
goog.net.NetworkTester.prototype.retries_ = 0;
/**
* Attempt number we're on
* @type {number}
* @private
*/
goog.net.NetworkTester.prototype.attempt_ = 0;
/**
* Pause between retries in milliseconds.
* @type {number}
* @private
*/
goog.net.NetworkTester.prototype.pauseBetweenRetriesMs_ = 0;
/**
* Timer for timeouts.
* @type {?number}
* @private
*/
goog.net.NetworkTester.prototype.timeoutTimer_ = null;
/**
* Timer for pauses between retries.
* @type {?number}
* @private
*/
goog.net.NetworkTester.prototype.pauseTimer_ = null;
/** @private {?Image} */
goog.net.NetworkTester.prototype.image_;
/**
* Returns the timeout in milliseconds.
* @return {number} Timeout in milliseconds.
*/
goog.net.NetworkTester.prototype.getTimeout = function() {
return this.timeoutMs_;
};
/**
* Sets the timeout in milliseconds.
* @param {number} timeoutMs Timeout in milliseconds.
*/
goog.net.NetworkTester.prototype.setTimeout = function(timeoutMs) {
this.timeoutMs_ = timeoutMs;
};
/**
* Returns the numer of retries to attempt.
* @return {number} Number of retries to attempt.
*/
goog.net.NetworkTester.prototype.getNumRetries = function() {
return this.retries_;
};
/**
* Sets the timeout in milliseconds.
* @param {number} retries Number of retries to attempt.
*/
goog.net.NetworkTester.prototype.setNumRetries = function(retries) {
this.retries_ = retries;
};
/**
* Returns the pause between retries in milliseconds.
* @return {number} Pause between retries in milliseconds.
*/
goog.net.NetworkTester.prototype.getPauseBetweenRetries = function() {
return this.pauseBetweenRetriesMs_;
};
/**
* Sets the pause between retries in milliseconds.
* @param {number} pauseMs Pause between retries in milliseconds.
*/
goog.net.NetworkTester.prototype.setPauseBetweenRetries = function(pauseMs) {
this.pauseBetweenRetriesMs_ = pauseMs;
};
/**
* Returns the uri to use for the test.
* @return {goog.Uri} The uri for the test.
*/
goog.net.NetworkTester.prototype.getUri = function() {
return this.uri_;
};
/**
* Returns the current attempt count.
* @return {number} The attempt count.
*/
goog.net.NetworkTester.prototype.getAttemptCount = function() {
return this.attempt_;
};
/**
* Sets the uri to use for the test.
* @param {goog.Uri} uri The uri for the test.
*/
goog.net.NetworkTester.prototype.setUri = function(uri) {
this.uri_ = uri;
};
/**
* Returns whether the tester is currently running.
* @return {boolean} True if it's running, false if it's not running.
*/
goog.net.NetworkTester.prototype.isRunning = function() {
return this.running_;
};
/**
* Starts the process of testing the network.
*/
goog.net.NetworkTester.prototype.start = function() {
if (this.running_) {
throw new Error('NetworkTester.start called when already running');
}
this.running_ = true;
goog.log.info(this.logger_, 'Starting');
this.attempt_ = 0;
this.startNextAttempt_();
};
/**
* Stops the testing of the network. This is a noop if not running.
*/
goog.net.NetworkTester.prototype.stop = function() {
this.cleanupCallbacks_();
this.running_ = false;
};
/**
* Starts the next attempt to load an image.
* @private
*/
goog.net.NetworkTester.prototype.startNextAttempt_ = function() {
this.attempt_++;
if (goog.net.NetworkTester.getNavigatorOffline_()) {
goog.log.info(this.logger_, 'Browser is set to work offline.');
// Call in a timeout to make async like the rest.
goog.Timer.callOnce(goog.bind(this.onResult, this, false), 0);
} else {
goog.log.info(
this.logger_,
'Loading image (attempt ' + this.attempt_ + ') at ' + this.uri_);
this.image_ = new Image();
this.image_.onload = goog.bind(this.onImageLoad_, this);
this.image_.onerror = goog.bind(this.onImageError_, this);
this.image_.onabort = goog.bind(this.onImageAbort_, this);
this.timeoutTimer_ =
goog.Timer.callOnce(this.onImageTimeout_, this.timeoutMs_, this);
this.image_.src = String(this.uri_);
}
};
/**
* @return {boolean} Whether navigator.onLine returns false.
* @private
*/
goog.net.NetworkTester.getNavigatorOffline_ = function() {
return navigator !== null && 'onLine' in navigator && !navigator.onLine;
};
/**
* Callback for the image successfully loading.
* @private
*/
goog.net.NetworkTester.prototype.onImageLoad_ = function() {
goog.log.info(this.logger_, 'Image loaded');
this.onResult(true);
};
/**
* Callback for the image failing to load.
* @private
*/
goog.net.NetworkTester.prototype.onImageError_ = function() {
goog.log.info(this.logger_, 'Image load error');
this.onResult(false);
};
/**
* Callback for the image load being aborted.
* @private
*/
goog.net.NetworkTester.prototype.onImageAbort_ = function() {
goog.log.info(this.logger_, 'Image load aborted');
this.onResult(false);
};
/**
* Callback for the image load timing out.
* @private
*/
goog.net.NetworkTester.prototype.onImageTimeout_ = function() {
goog.log.info(this.logger_, 'Image load timed out');
this.onResult(false);
};
/**
* Handles a successful or failed result.
* @param {boolean} succeeded Whether the image load succeeded.
*/
goog.net.NetworkTester.prototype.onResult = function(succeeded) {
this.cleanupCallbacks_();
if (succeeded) {
this.running_ = false;
this.callback_.call(this.handler_, true);
} else {
if (this.attempt_ <= this.retries_) {
if (this.pauseBetweenRetriesMs_) {
this.pauseTimer_ = goog.Timer.callOnce(
this.onPauseFinished_, this.pauseBetweenRetriesMs_, this);
} else {
this.startNextAttempt_();
}
} else {
this.running_ = false;
this.callback_.call(this.handler_, false);
}
}
};
/**
* Callback for the pause between retry timer.
* @private
*/
goog.net.NetworkTester.prototype.onPauseFinished_ = function() {
this.pauseTimer_ = null;
this.startNextAttempt_();
};
/**
* Cleans up the handlers and timer associated with the image.
* @private
*/
goog.net.NetworkTester.prototype.cleanupCallbacks_ = function() {
// clear handlers to avoid memory leaks
// NOTE(user): Nullified individually to avoid compiler warnings
// (BUG 658126)
if (this.image_) {
this.image_.onload = null;
this.image_.onerror = null;
this.image_.onabort = null;
this.image_ = null;
}
if (this.timeoutTimer_) {
goog.Timer.clear(this.timeoutTimer_);
this.timeoutTimer_ = null;
}
if (this.pauseTimer_) {
goog.Timer.clear(this.pauseTimer_);
this.pauseTimer_ = null;
}
};
|