Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/net/tmpnetwork.js
2868 views
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
/**
16
* @fileoverview tmpnetwork.js contains some temporary networking functions
17
* for browserchannel which will be moved at a later date.
18
*/
19
20
21
/**
22
* Namespace for BrowserChannel
23
*/
24
goog.provide('goog.net.tmpnetwork');
25
26
goog.require('goog.Uri');
27
goog.require('goog.net.ChannelDebug');
28
29
30
/**
31
* Default timeout to allow for google.com pings.
32
* @type {number}
33
*/
34
goog.net.tmpnetwork.GOOGLECOM_TIMEOUT = 10000;
35
36
37
/**
38
* @define {string} url to use to test for internet connectivity.
39
* Use protocol-relative URLs to avoid insecure content warnings in IE.
40
*/
41
goog.define(
42
'goog.net.tmpnetwork.TEST_URL', '//www.google.com/images/cleardot.gif');
43
44
45
/**
46
* Pings the network to check if an error is a server error or user's network
47
* error.
48
*
49
* @param {Function} callback The function to call back with results.
50
* @param {goog.Uri?=} opt_imageUri The URI of an image to use for the network
51
* test. You *must* provide an image URI; the default behavior is provided
52
* for compatibility with existing code, but the search team does not want
53
* people using images served off of google.com for this purpose. The
54
* default will go away when all usages have been changed.
55
*/
56
goog.net.tmpnetwork.testGoogleCom = function(callback, opt_imageUri) {
57
// We need to add a 'rand' to make sure the response is not fulfilled
58
// by browser cache.
59
var uri = opt_imageUri;
60
if (!uri) {
61
uri = new goog.Uri(goog.net.tmpnetwork.TEST_URL);
62
uri.makeUnique();
63
}
64
goog.net.tmpnetwork.testLoadImage(
65
uri.toString(), goog.net.tmpnetwork.GOOGLECOM_TIMEOUT, callback);
66
};
67
68
69
/**
70
* Test loading the given image, retrying if necessary.
71
* @param {string} url URL to the iamge.
72
* @param {number} timeout Milliseconds before giving up.
73
* @param {Function} callback Function to call with results.
74
* @param {number} retries The number of times to retry.
75
* @param {number=} opt_pauseBetweenRetriesMS Optional number of milliseconds
76
* between retries - defaults to 0.
77
*/
78
goog.net.tmpnetwork.testLoadImageWithRetries = function(
79
url, timeout, callback, retries, opt_pauseBetweenRetriesMS) {
80
var channelDebug = new goog.net.ChannelDebug();
81
channelDebug.debug('TestLoadImageWithRetries: ' + opt_pauseBetweenRetriesMS);
82
if (retries == 0) {
83
// no more retries, give up
84
callback(false);
85
return;
86
}
87
88
var pauseBetweenRetries = opt_pauseBetweenRetriesMS || 0;
89
retries--;
90
goog.net.tmpnetwork.testLoadImage(url, timeout, function(succeeded) {
91
if (succeeded) {
92
callback(true);
93
} else {
94
// try again
95
goog.global.setTimeout(function() {
96
goog.net.tmpnetwork.testLoadImageWithRetries(
97
url, timeout, callback, retries, pauseBetweenRetries);
98
}, pauseBetweenRetries);
99
}
100
});
101
};
102
103
104
/**
105
* Test loading the given image.
106
* @param {string} url URL to the iamge.
107
* @param {number} timeout Milliseconds before giving up.
108
* @param {Function} callback Function to call with results.
109
*/
110
goog.net.tmpnetwork.testLoadImage = function(url, timeout, callback) {
111
var channelDebug = new goog.net.ChannelDebug();
112
channelDebug.debug('TestLoadImage: loading ' + url);
113
var img = new Image();
114
img.onload = function() {
115
try {
116
channelDebug.debug('TestLoadImage: loaded');
117
goog.net.tmpnetwork.clearImageCallbacks_(img);
118
callback(true);
119
} catch (e) {
120
channelDebug.dumpException(e);
121
}
122
};
123
img.onerror = function() {
124
try {
125
channelDebug.debug('TestLoadImage: error');
126
goog.net.tmpnetwork.clearImageCallbacks_(img);
127
callback(false);
128
} catch (e) {
129
channelDebug.dumpException(e);
130
}
131
};
132
img.onabort = function() {
133
try {
134
channelDebug.debug('TestLoadImage: abort');
135
goog.net.tmpnetwork.clearImageCallbacks_(img);
136
callback(false);
137
} catch (e) {
138
channelDebug.dumpException(e);
139
}
140
};
141
img.ontimeout = function() {
142
try {
143
channelDebug.debug('TestLoadImage: timeout');
144
goog.net.tmpnetwork.clearImageCallbacks_(img);
145
callback(false);
146
} catch (e) {
147
channelDebug.dumpException(e);
148
}
149
};
150
151
goog.global.setTimeout(function() {
152
if (img.ontimeout) {
153
img.ontimeout();
154
}
155
}, timeout);
156
img.src = url;
157
};
158
159
160
/**
161
* Clear handlers to avoid memory leaks.
162
* @param {Image} img The image to clear handlers from.
163
* @private
164
*/
165
goog.net.tmpnetwork.clearImageCallbacks_ = function(img) {
166
// NOTE(user): Nullified individually to avoid compiler warnings
167
// (BUG 658126)
168
img.onload = null;
169
img.onerror = null;
170
img.onabort = null;
171
img.ontimeout = null;
172
};
173
174