Debian Codegraph @

closure-library/closure/goog/labs/net/webchannel/netutils.js

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