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