Debian Codegraph @

closure-library/closure/goog/labs/net/webchannel/webchannelbasetransport.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 Implementation of a WebChannel transport using WebChannelBase.
17 *
18 * When WebChannelBase is used as the underlying transport, the capabilities
19 * of the WebChannel are limited to what's supported by the implementation.
20 * Particularly, multiplexing is not possible, and only strings are
21 * supported as message types.
22 *
23 */
24
25 goog.provide('goog.labs.net.webChannel.WebChannelBaseTransport');
26
27 goog.require('goog.asserts');
28 goog.require('goog.events.EventTarget');
29 goog.require('goog.json');
30 goog.require('goog.labs.net.webChannel.ChannelRequest');
31 goog.require('goog.labs.net.webChannel.WebChannelBase');
32 goog.require('goog.log');
33 goog.require('goog.net.WebChannel');
34 goog.require('goog.net.WebChannelTransport');
35 goog.require('goog.object');
36 goog.require('goog.string');
37 goog.require('goog.string.path');
38
39
40
41 /**
42 * Implementation of {@link goog.net.WebChannelTransport} with
43 * {@link goog.labs.net.webChannel.WebChannelBase} as the underlying channel
44 * implementation.
45 *
46 * @constructor
47 * @struct
48 * @implements {goog.net.WebChannelTransport}
49 * @final
50 */
51 goog.labs.net.webChannel.WebChannelBaseTransport = function() {
52 if (!goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming()) {
53 throw new Error('Environmental error: no available transport.');
54 }
55 };
56
57
58 goog.scope(function() {
59 var WebChannelBaseTransport = goog.labs.net.webChannel.WebChannelBaseTransport;
60 var WebChannelBase = goog.labs.net.webChannel.WebChannelBase;
61
62
63 /**
64 * @override
65 */
66 WebChannelBaseTransport.prototype.createWebChannel = function(
67 url, opt_options) {
68 return new WebChannelBaseTransport.Channel(url, opt_options);
69 };
70
71
72
73 /**
74 * Implementation of the {@link goog.net.WebChannel} interface.
75 *
76 * @param {string} url The URL path for the new WebChannel instance.
77 * @param {!goog.net.WebChannel.Options=} opt_options Configuration for the
78 * new WebChannel instance.
79 *
80 * @constructor
81 * @implements {goog.net.WebChannel}
82 * @extends {goog.events.EventTarget}
83 * @final
84 */
85 WebChannelBaseTransport.Channel = function(url, opt_options) {
86 WebChannelBaseTransport.Channel.base(this, 'constructor');
87
88 /**
89 * @private {!WebChannelBase} The underlying channel object.
90 */
91 this.channel_ = new WebChannelBase(
92 opt_options, goog.net.WebChannelTransport.CLIENT_VERSION);
93
94 /**
95 * @private {string} The URL of the target server end-point.
96 */
97 this.url_ = url;
98
99 /**
100 * The test URL of the target server end-point. This value defaults to
101 * this.url_ + '/test'.
102 *
103 * @private {string}
104 */
105 this.testUrl_ = (opt_options && opt_options.testUrl) ?
106 opt_options.testUrl :
107 goog.string.path.join(this.url_, 'test');
108
109 /**
110 * @private {goog.log.Logger} The logger for this class.
111 */
112 this.logger_ =
113 goog.log.getLogger('goog.labs.net.webChannel.WebChannelBaseTransport');
114
115 /**
116 * @private {Object<string, string>} Extra URL parameters
117 * to be added to each HTTP request.
118 */
119 this.messageUrlParams_ =
120 (opt_options && opt_options.messageUrlParams) || null;
121
122 var messageHeaders = (opt_options && opt_options.messageHeaders) || null;
123
124 // default is false
125 if (opt_options && opt_options.clientProtocolHeaderRequired) {
126 if (messageHeaders) {
127 goog.object.set(
128 messageHeaders, goog.net.WebChannel.X_CLIENT_PROTOCOL,
129 goog.net.WebChannel.X_CLIENT_PROTOCOL_WEB_CHANNEL);
130 } else {
131 messageHeaders = goog.object.create(
132 goog.net.WebChannel.X_CLIENT_PROTOCOL,
133 goog.net.WebChannel.X_CLIENT_PROTOCOL_WEB_CHANNEL);
134 }
135 }
136
137 this.channel_.setExtraHeaders(messageHeaders);
138
139 var initHeaders = (opt_options && opt_options.initMessageHeaders) || null;
140
141 if (opt_options && opt_options.messageContentType) {
142 if (initHeaders) {
143 goog.object.set(
144 initHeaders, goog.net.WebChannel.X_WEBCHANNEL_CONTENT_TYPE,
145 opt_options.messageContentType);
146 } else {
147 initHeaders = goog.object.create(
148 goog.net.WebChannel.X_WEBCHANNEL_CONTENT_TYPE,
149 opt_options.messageContentType);
150 }
151 }
152
153 if (opt_options && opt_options.clientProfile) {
154 if (initHeaders) {
155 goog.object.set(
156 initHeaders, goog.net.WebChannel.X_WEBCHANNEL_CLIENT_PROFILE,
157 opt_options.clientProfile);
158 } else {
159 initHeaders = goog.object.create(
160 goog.net.WebChannel.X_WEBCHANNEL_CLIENT_PROFILE,
161 opt_options.clientProfile);
162 }
163 }
164
165 this.channel_.setInitHeaders(initHeaders);
166
167 var httpHeadersOverwriteParam =
168 opt_options && opt_options.httpHeadersOverwriteParam;
169 if (httpHeadersOverwriteParam &&
170 !goog.string.isEmptyOrWhitespace(httpHeadersOverwriteParam)) {
171 this.channel_.setHttpHeadersOverwriteParam(httpHeadersOverwriteParam);
172 }
173
174 /**
175 * @private {boolean} Whether to enable CORS.
176 */
177 this.supportsCrossDomainXhr_ =
178 (opt_options && opt_options.supportsCrossDomainXhr) || false;
179
180 /**
181 * @private {boolean} Whether to send raw Json and bypass v8 wire format.
182 */
183 this.sendRawJson_ = (opt_options && opt_options.sendRawJson) || false;
184
185 // Note that httpSessionIdParam will be ignored if the same parameter name
186 // has already been specified with messageUrlParams
187 var httpSessionIdParam = opt_options && opt_options.httpSessionIdParam;
188 if (httpSessionIdParam &&
189 !goog.string.isEmptyOrWhitespace(httpSessionIdParam)) {
190 this.channel_.setHttpSessionIdParam(httpSessionIdParam);
191 if (goog.object.containsKey(this.messageUrlParams_, httpSessionIdParam)) {
192 goog.object.remove(this.messageUrlParams_, httpSessionIdParam);
193 goog.log.warning(this.logger_,
194 'Ignore httpSessionIdParam also specified with messageUrlParams: '
195 + httpSessionIdParam);
196 }
197 }
198
199 /**
200 * The channel handler.
201 *
202 * @private {!WebChannelBaseTransport.Channel.Handler_}
203 */
204 this.channelHandler_ = new WebChannelBaseTransport.Channel.Handler_(this);
205 };
206 goog.inherits(WebChannelBaseTransport.Channel, goog.events.EventTarget);
207
208
209 /**
210 * @override
211 * @suppress {checkTypes}
212 */
213 WebChannelBaseTransport.Channel.prototype.addEventListener = function(
214 type, handler, /** boolean= */ opt_capture, opt_handlerScope) {
215 WebChannelBaseTransport.Channel.base(
216 this, 'addEventListener', type, handler, opt_capture, opt_handlerScope);
217 };
218
219
220 /**
221 * @override
222 * @suppress {checkTypes}
223 */
224 WebChannelBaseTransport.Channel.prototype.removeEventListener = function(
225 type, handler, /** boolean= */ opt_capture, opt_handlerScope) {
226 WebChannelBaseTransport.Channel.base(
227 this, 'removeEventListener', type, handler, opt_capture,
228 opt_handlerScope);
229 };
230
231
232 /**
233 * Test path is always set to "/url/test".
234 *
235 * @override
236 */
237 WebChannelBaseTransport.Channel.prototype.open = function() {
238 this.channel_.setHandler(this.channelHandler_);
239 if (this.supportsCrossDomainXhr_) {
240 this.channel_.setSupportsCrossDomainXhrs(true);
241 }
242 this.channel_.connect(
243 this.testUrl_, this.url_, (this.messageUrlParams_ || undefined));
244 };
245
246
247 /**
248 * @override
249 */
250 WebChannelBaseTransport.Channel.prototype.close = function() {
251 this.channel_.disconnect();
252 };
253
254
255 /**
256 * @override
257 */
258 WebChannelBaseTransport.Channel.prototype.halfClose = function() {
259 // to be implemented
260 this.close();
261 };
262
263
264 /**
265 * The WebChannelBase only supports object types.
266 *
267 * @param {!goog.net.WebChannel.MessageData} message The message to send.
268 *
269 * @override
270 */
271 WebChannelBaseTransport.Channel.prototype.send = function(message) {
272 goog.asserts.assert(
273 goog.isObject(message) || goog.isString(message),
274 'only object type or raw string is supported');
275
276 if (goog.isString(message)) {
277 var rawJson = {};
278 rawJson['__data__'] = message;
279 this.channel_.sendMap(rawJson);
280 } else if (this.sendRawJson_) {
281 var rawJson = {};
282 rawJson['__data__'] = goog.json.serialize(message);
283 this.channel_.sendMap(rawJson);
284 } else {
285 this.channel_.sendMap(message);
286 }
287 };
288
289
290 /**
291 * @override
292 */
293 WebChannelBaseTransport.Channel.prototype.disposeInternal = function() {
294 this.channel_.setHandler(null);
295 delete this.channelHandler_;
296 this.channel_.disconnect();
297 delete this.channel_;
298
299 WebChannelBaseTransport.Channel.base(this, 'disposeInternal');
300 };
301
302
303
304 /**
305 * The message event.
306 *
307 * @param {!Array<?>|!Object} array The data array from the underlying channel.
308 * @constructor
309 * @extends {goog.net.WebChannel.MessageEvent}
310 * @final
311 */
312 WebChannelBaseTransport.Channel.MessageEvent = function(array) {
313 WebChannelBaseTransport.Channel.MessageEvent.base(this, 'constructor');
314
315 // single-metadata only
316 var metadata = array['__sm__'];
317 if (metadata) {
318 this.metadataKey = goog.object.getAnyKey(metadata);
319 if (this.metadataKey) {
320 this.data = goog.object.get(metadata, this.metadataKey);
321 } else {
322 this.data = metadata; // empty
323 }
324 } else {
325 this.data = array;
326 }
327 };
328 goog.inherits(
329 WebChannelBaseTransport.Channel.MessageEvent,
330 goog.net.WebChannel.MessageEvent);
331
332
333
334 /**
335 * The error event.
336 *
337 * @param {WebChannelBase.Error} error The error code.
338 * @constructor
339 * @extends {goog.net.WebChannel.ErrorEvent}
340 * @final
341 */
342 WebChannelBaseTransport.Channel.ErrorEvent = function(error) {
343 WebChannelBaseTransport.Channel.ErrorEvent.base(this, 'constructor');
344
345 /**
346 * High-level status code.
347 */
348 this.status = goog.net.WebChannel.ErrorStatus.NETWORK_ERROR;
349
350 /**
351 * @const {WebChannelBase.Error} Internal error code, for debugging use only.
352 */
353 this.errorCode = error;
354 };
355 goog.inherits(
356 WebChannelBaseTransport.Channel.ErrorEvent, goog.net.WebChannel.ErrorEvent);
357
358
359
360 /**
361 * Implementation of {@link WebChannelBase.Handler} interface.
362 *
363 * @param {!WebChannelBaseTransport.Channel} channel The enclosing WebChannel.
364 *
365 * @constructor
366 * @extends {WebChannelBase.Handler}
367 * @private
368 */
369 WebChannelBaseTransport.Channel.Handler_ = function(channel) {
370 WebChannelBaseTransport.Channel.Handler_.base(this, 'constructor');
371
372 /**
373 * @type {!WebChannelBaseTransport.Channel}
374 * @private
375 */
376 this.channel_ = channel;
377 };
378 goog.inherits(WebChannelBaseTransport.Channel.Handler_, WebChannelBase.Handler);
379
380
381 /**
382 * @override
383 */
384 WebChannelBaseTransport.Channel.Handler_.prototype.channelOpened = function(
385 channel) {
386 goog.log.info(
387 this.channel_.logger_, 'WebChannel opened on ' + this.channel_.url_);
388 this.channel_.dispatchEvent(goog.net.WebChannel.EventType.OPEN);
389 };
390
391
392 /**
393 * @override
394 */
395 WebChannelBaseTransport.Channel.Handler_.prototype.channelHandleArray =
396 function(channel, array) {
397 goog.asserts.assert(array, 'array expected to be defined');
398 this.channel_.dispatchEvent(
399 new WebChannelBaseTransport.Channel.MessageEvent(array));
400 };
401
402
403 /**
404 * @override
405 */
406 WebChannelBaseTransport.Channel.Handler_.prototype.channelError = function(
407 channel, error) {
408 goog.log.info(
409 this.channel_.logger_, 'WebChannel aborted on ' + this.channel_.url_ +
410 ' due to channel error: ' + error);
411 this.channel_.dispatchEvent(
412 new WebChannelBaseTransport.Channel.ErrorEvent(error));
413 };
414
415
416 /**
417 * @override
418 */
419 WebChannelBaseTransport.Channel.Handler_.prototype.channelClosed = function(
420 channel, opt_pendingMaps, opt_undeliveredMaps) {
421 goog.log.info(
422 this.channel_.logger_, 'WebChannel closed on ' + this.channel_.url_);
423 this.channel_.dispatchEvent(goog.net.WebChannel.EventType.CLOSE);
424 };
425
426
427 /**
428 * @override
429 */
430 WebChannelBaseTransport.Channel.prototype.getRuntimeProperties = function() {
431 return new WebChannelBaseTransport.ChannelProperties(this.channel_);
432 };
433
434
435
436 /**
437 * Implementation of the {@link goog.net.WebChannel.RuntimeProperties}.
438 *
439 * @param {!WebChannelBase} channel The underlying channel object.
440 *
441 * @constructor
442 * @implements {goog.net.WebChannel.RuntimeProperties}
443 * @final
444 */
445 WebChannelBaseTransport.ChannelProperties = function(channel) {
446 /**
447 * The underlying channel object.
448 *
449 * @private {!WebChannelBase}
450 */
451 this.channel_ = channel;
452
453 };
454
455
456 /**
457 * @override
458 */
459 WebChannelBaseTransport.ChannelProperties.prototype.getConcurrentRequestLimit =
460 function() {
461 return this.channel_.getForwardChannelRequestPool().getMaxSize();
462 };
463
464
465 /**
466 * @override
467 */
468 WebChannelBaseTransport.ChannelProperties.prototype.isSpdyEnabled = function() {
469 return this.getConcurrentRequestLimit() > 1;
470 };
471
472
473 /**
474 * @override
475 */
476 WebChannelBaseTransport.ChannelProperties.prototype.getPendingRequestCount =
477 function() {
478 return this.channel_.getForwardChannelRequestPool().getRequestCount();
479 };
480
481
482 /**
483 * @override
484 */
485 WebChannelBaseTransport.ChannelProperties.prototype.getHttpSessionId =
486 function() {
487 return this.channel_.getHttpSessionId();
488 };
489
490
491 /**
492 * @override
493 */
494 WebChannelBaseTransport.ChannelProperties.prototype.commit =
495 goog.abstractMethod;
496
497
498 /**
499 * @override
500 */
501 WebChannelBaseTransport.ChannelProperties.prototype.getNonAckedMessageCount =
502 goog.abstractMethod;
503
504
505 /**
506 * @override
507 */
508 WebChannelBaseTransport.ChannelProperties.prototype.notifyNonAckedMessageCount =
509 goog.abstractMethod;
510
511
512 /**
513 * @override
514 */
515 WebChannelBaseTransport.ChannelProperties.prototype.onCommit =
516 goog.abstractMethod;
517
518
519 /**
520 * @override
521 */
522 WebChannelBaseTransport.ChannelProperties.prototype.ackCommit =
523 goog.abstractMethod;
524
525
526 /** @override */
527 WebChannelBaseTransport.ChannelProperties.prototype.getLastStatusCode =
528 function() {
529 return this.channel_.getLastStatusCode();
530 };
531 }); // goog.scope