Debian Codegraph @

closure-library/closure/goog/labs/net/webchannel/forwardchannelrequestpool.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 A pool of forward channel requests to enable real-time
17 * messaging from the client to server.
18 *
19 * @visibility {:internal}
20 */
21
22 goog.module('goog.labs.net.webChannel.ForwardChannelRequestPool');
23
24 goog.module.declareLegacyNamespace();
25
26 var ChannelRequest = goog.require('goog.labs.net.webChannel.ChannelRequest');
27 var Set = goog.require('goog.structs.Set');
28 var Wire = goog.require('goog.labs.net.webChannel.Wire');
29 var array = goog.require('goog.array');
30 var googString = goog.require('goog.string');
31
32
33 /**
34 * This class represents the state of all forward channel requests.
35 *
36 * @param {number=} opt_maxPoolSize The maximum pool size.
37 *
38 * @struct @constructor @final
39 */
40 var ForwardChannelRequestPool = function(opt_maxPoolSize) {
41 /**
42 * The max pool size as configured.
43 *
44 * @private {number}
45 */
46 this.maxPoolSizeConfigured_ =
47 opt_maxPoolSize || ForwardChannelRequestPool.MAX_POOL_SIZE_;
48
49 /**
50 * The current size limit of the request pool. This limit is meant to be
51 * read-only after the channel is fully opened.
52 *
53 * If SPDY is enabled, set it to the max pool size, which is also
54 * configurable.
55 *
56 * @private {number}
57 */
58 this.maxSize_ = ForwardChannelRequestPool.isSpdyEnabled_() ?
59 this.maxPoolSizeConfigured_ :
60 1;
61
62 /**
63 * The container for all the pending request objects.
64 *
65 * @private {Set<ChannelRequest>}
66 */
67 this.requestPool_ = null;
68
69 if (this.maxSize_ > 1) {
70 this.requestPool_ = new Set();
71 }
72
73 /**
74 * The single request object when the pool size is limited to one.
75 *
76 * @private {ChannelRequest}
77 */
78 this.request_ = null;
79
80 /**
81 * Saved pending messages when the pool is cancelled.
82 *
83 * @private {!Array<Wire.QueuedMap>}
84 */
85 this.pendingMessages_ = [];
86 };
87
88
89 /**
90 * The default size limit of the request pool.
91 *
92 * @private {number}
93 */
94 ForwardChannelRequestPool.MAX_POOL_SIZE_ = 10;
95
96
97 /**
98 * @return {boolean} True if SPDY is enabled for the current page using
99 * chrome specific APIs.
100 * @private
101 */
102 ForwardChannelRequestPool.isSpdyEnabled_ = function() {
103 return !!(
104 goog.global.chrome && goog.global.chrome.loadTimes &&
105 goog.global.chrome.loadTimes() &&
106 goog.global.chrome.loadTimes().wasFetchedViaSpdy);
107 };
108
109
110 /**
111 * Once we know the client protocol (from the handshake), check if we need
112 * enable the request pool accordingly. This is more robust than using
113 * browser-internal APIs (specific to Chrome).
114 *
115 * @param {string} clientProtocol The client protocol
116 */
117 ForwardChannelRequestPool.prototype.applyClientProtocol = function(
118 clientProtocol) {
119 if (this.requestPool_) {
120 return;
121 }
122
123 if (googString.contains(clientProtocol, 'spdy') ||
124 googString.contains(clientProtocol, 'quic') ||
125 googString.contains(clientProtocol, 'h2')) {
126 this.maxSize_ = this.maxPoolSizeConfigured_;
127 this.requestPool_ = new Set();
128 if (this.request_) {
129 this.addRequest(this.request_);
130 this.request_ = null;
131 }
132 }
133 };
134
135
136 /**
137 * @return {boolean} True if the pool is full.
138 */
139 ForwardChannelRequestPool.prototype.isFull = function() {
140 if (this.request_) {
141 return true;
142 }
143
144 if (this.requestPool_) {
145 return this.requestPool_.getCount() >= this.maxSize_;
146 }
147
148 return false;
149 };
150
151
152 /**
153 * @return {number} The current size limit.
154 */
155 ForwardChannelRequestPool.prototype.getMaxSize = function() {
156 return this.maxSize_;
157 };
158
159
160 /**
161 * @return {number} The number of pending requests in the pool.
162 */
163 ForwardChannelRequestPool.prototype.getRequestCount = function() {
164 if (this.request_) {
165 return 1;
166 }
167
168 if (this.requestPool_) {
169 return this.requestPool_.getCount();
170 }
171
172 return 0;
173 };
174
175
176 /**
177 * @param {ChannelRequest} req The channel request.
178 * @return {boolean} True if the request is a included inside the pool.
179 */
180 ForwardChannelRequestPool.prototype.hasRequest = function(req) {
181 if (this.request_) {
182 return this.request_ == req;
183 }
184
185 if (this.requestPool_) {
186 return this.requestPool_.contains(req);
187 }
188
189 return false;
190 };
191
192
193 /**
194 * Adds a new request to the pool.
195 *
196 * @param {!ChannelRequest} req The new channel request.
197 */
198 ForwardChannelRequestPool.prototype.addRequest = function(req) {
199 if (this.requestPool_) {
200 this.requestPool_.add(req);
201 } else {
202 this.request_ = req;
203 }
204 };
205
206
207 /**
208 * Removes the given request from the pool.
209 *
210 * @param {ChannelRequest} req The channel request.
211 * @return {boolean} Whether the request has been removed from the pool.
212 */
213 ForwardChannelRequestPool.prototype.removeRequest = function(req) {
214 if (this.request_ && this.request_ == req) {
215 this.request_ = null;
216 return true;
217 }
218
219 if (this.requestPool_ && this.requestPool_.contains(req)) {
220 this.requestPool_.remove(req);
221 return true;
222 }
223
224 return false;
225 };
226
227
228 /**
229 * Clears the pool and cancel all the pending requests.
230 */
231 ForwardChannelRequestPool.prototype.cancel = function() {
232 // save any pending messages
233 this.pendingMessages_ = this.getPendingMessages();
234
235 if (this.request_) {
236 this.request_.cancel();
237 this.request_ = null;
238 return;
239 }
240
241 if (this.requestPool_ && !this.requestPool_.isEmpty()) {
242 array.forEach(this.requestPool_.getValues(), function(val) {
243 val.cancel();
244 });
245 this.requestPool_.clear();
246 }
247 };
248
249
250 /**
251 * @return {boolean} Whether there are any pending requests.
252 */
253 ForwardChannelRequestPool.prototype.hasPendingRequest = function() {
254 return (this.request_ != null) ||
255 (this.requestPool_ != null && !this.requestPool_.isEmpty());
256 };
257
258
259 /**
260 * @return {!Array<Wire.QueuedMap>} All the pending messages from the pool,
261 * as a new array.
262 */
263 ForwardChannelRequestPool.prototype.getPendingMessages = function() {
264 if (this.request_ != null) {
265 return this.pendingMessages_.concat(this.request_.getPendingMessages());
266 }
267
268 if (this.requestPool_ != null && !this.requestPool_.isEmpty()) {
269 var result = this.pendingMessages_;
270 array.forEach(this.requestPool_.getValues(), function(val) {
271 result = result.concat(val.getPendingMessages());
272 });
273 return result;
274 }
275
276 return array.clone(this.pendingMessages_);
277 };
278
279
280 /**
281 * Records pending messages, e.g. when a request receives a failed response.
282 *
283 * @param {!Array<Wire.QueuedMap>} messages Pending messages.
284 */
285 ForwardChannelRequestPool.prototype.addPendingMessages = function(messages) {
286 this.pendingMessages_ = this.pendingMessages_.concat(messages);
287 };
288
289
290 /**
291 * Clears any recorded pending messages.
292 */
293 ForwardChannelRequestPool.prototype.clearPendingMessages = function() {
294 this.pendingMessages_.length = 0;
295 };
296
297
298 /**
299 * Cancels all pending requests and force the completion of channel requests.
300 *
301 * Need go through the standard onRequestComplete logic to expose the max-retry
302 * failure in the standard way.
303 *
304 * @param {function(!ChannelRequest)} onComplete The completion callback.
305 * @return {boolean} true if any request has been forced to complete.
306 */
307 ForwardChannelRequestPool.prototype.forceComplete = function(onComplete) {
308 if (this.request_ != null) {
309 this.request_.cancel();
310 onComplete(this.request_);
311 return true;
312 }
313
314 if (this.requestPool_ && !this.requestPool_.isEmpty()) {
315 array.forEach(this.requestPool_.getValues(), function(val) {
316 val.cancel();
317 onComplete(val);
318 });
319 return true;
320 }
321
322 return false;
323 };
324
325 exports = ForwardChannelRequestPool;