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