This repository was archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathbrowserchannel.js
2675 lines (2281 loc) · 79.6 KB
/
browserchannel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Definition of the BrowserChannel class. A BrowserChannel
* simulates a bidirectional socket over HTTP. It is the basis of the
* Gmail Chat IM connections to the server.
*
* Typical usage will look like
* const handler = [handler object];
* const channel = new BrowserChannel(clientVersion);
* channel.setHandler(handler);
* channel.connect('channel/test', 'channel/bind');
*
* See goog.net.BrowserChannel.Handler for the handler interface.
*/
goog.provide('goog.net.BrowserChannel');
goog.provide('goog.net.BrowserChannel.Error');
goog.provide('goog.net.BrowserChannel.Event');
goog.provide('goog.net.BrowserChannel.Handler');
goog.provide('goog.net.BrowserChannel.Hooks');
goog.provide('goog.net.BrowserChannel.LogSaver');
goog.provide('goog.net.BrowserChannel.QueuedMap');
goog.provide('goog.net.BrowserChannel.ServerReachability');
goog.provide('goog.net.BrowserChannel.ServerReachabilityEvent');
goog.provide('goog.net.BrowserChannel.Stat');
goog.provide('goog.net.BrowserChannel.StatEvent');
goog.provide('goog.net.BrowserChannel.State');
goog.provide('goog.net.BrowserChannel.TimingEvent');
goog.require('goog.Uri');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.debug.TextFormatter');
goog.require('goog.events.Event');
goog.require('goog.events.EventTarget');
goog.require('goog.json');
goog.require('goog.json.NativeJsonProcessor');
goog.require('goog.log');
goog.require('goog.net.BrowserTestChannel');
goog.require('goog.net.ChannelDebug');
goog.require('goog.net.ChannelRequest');
goog.require('goog.net.XhrIo');
goog.require('goog.net.browserchannelinternal.ServerReachability');
goog.require('goog.net.browserchannelinternal.hooks');
goog.require('goog.net.browserchannelinternal.stats');
goog.require('goog.net.tmpnetwork');
goog.require('goog.object');
goog.require('goog.string');
goog.require('goog.structs');
goog.require('goog.structs.CircularBuffer');
goog.requireType('goog.debug.Formatter');
goog.requireType('goog.string.Parser');
goog.requireType('goog.structs.Map');
/**
* Encapsulates the logic for a single BrowserChannel.
*
* @param {string=} opt_clientVersion An application-specific version number
* that is sent to the server when connected.
* @param {Array<string>=} opt_firstTestResults Previously determined results
* of the first browser channel test.
* @param {boolean=} opt_secondTestResults Previously determined results
* of the second browser channel test.
* @param {boolean=} opt_asyncTest Whether to perform the test requests
* asynchronously. While the test is performed, we'll assume the worst
* (connection is buffered), in order to avoid delaying the connection
* until the test is performed.
* @constructor
*/
goog.net.BrowserChannel = function(
opt_clientVersion, opt_firstTestResults, opt_secondTestResults,
opt_asyncTest) {
'use strict';
/**
* The application specific version that is passed to the server.
* @type {?string}
* @private
*/
this.clientVersion_ = opt_clientVersion || null;
/**
* The current state of the BrowserChannel. It should be one of the
* goog.net.BrowserChannel.State constants.
* @type {!goog.net.BrowserChannel.State}
* @private
*/
this.state_ = goog.net.BrowserChannel.State.INIT;
/**
* An array of queued maps that need to be sent to the server.
* @type {Array<goog.net.BrowserChannel.QueuedMap>}
* @private
*/
this.outgoingMaps_ = [];
/**
* An array of dequeued maps that we have either received a non-successful
* response for, or no response at all, and which therefore may or may not
* have been received by the server.
* @type {Array<goog.net.BrowserChannel.QueuedMap>}
* @private
*/
this.pendingMaps_ = [];
/**
* The channel debug used for browserchannel logging
* @type {!goog.net.ChannelDebug}
* @private
*/
this.channelDebug_ = new goog.net.ChannelDebug();
/**
* Parser for a response payload. The parser should return an array.
* @type {!goog.string.Parser}
* @private
*/
this.parser_ = new goog.json.NativeJsonProcessor();
/**
* An array of results for the first browser channel test call.
* @type {Array<string>}
* @private
*/
this.firstTestResults_ = opt_firstTestResults || null;
/**
* The results of the second browser channel test. True implies the
* connection is buffered, False means unbuffered, null means that
* the results are not available.
* @private
*/
this.secondTestResults_ =
(opt_secondTestResults != null) ? opt_secondTestResults : null;
/**
* Whether to perform the test requests asynchronously. While the test is
* performed, we'll assume the worst (connection is buffered), in order to
* avoid delaying the connection until the test is performed.
* @private {boolean}
*/
this.asyncTest_ = opt_asyncTest || false;
};
/**
* Alias the internal Stat module to be used from the main BrowserChannel
* module.
* @const
*/
goog.net.BrowserChannel.Stat = goog.net.browserchannelinternal.stats.Stat;
/**
* Alias getStatEventTarget as it used to be provided from the main
* BrowserChannel module.
* @const
*/
goog.net.BrowserChannel.getStatEventTarget =
goog.net.browserchannelinternal.stats.getStatEventTarget;
/**
* Simple container class for a (mapId, map) pair.
* @param {number} mapId The id for this map.
* @param {Object|goog.structs.Map} map The map itself.
* @param {Object=} opt_context The context associated with the map.
* @constructor
* @final
*/
goog.net.BrowserChannel.QueuedMap = function(mapId, map, opt_context) {
'use strict';
/**
* The id for this map.
* @type {number}
*/
this.mapId = mapId;
/**
* The map itself.
* @type {Object}
*/
this.map = map;
/**
* The context for the map.
* @type {Object}
*/
this.context = opt_context || null;
};
/**
* Extra HTTP headers to add to all the requests sent to the server.
* @type {?Object}
* @private
*/
goog.net.BrowserChannel.prototype.extraHeaders_ = null;
/**
* Extra parameters to add to all the requests sent to the server.
* @type {?Object}
* @private
*/
goog.net.BrowserChannel.prototype.extraParams_ = null;
/**
* The current ChannelRequest object for the forwardchannel.
* @type {goog.net.ChannelRequest?}
* @private
*/
goog.net.BrowserChannel.prototype.forwardChannelRequest_ = null;
/**
* The ChannelRequest object for the backchannel.
* @type {goog.net.ChannelRequest?}
* @private
*/
goog.net.BrowserChannel.prototype.backChannelRequest_ = null;
/**
* The relative path (in the context of the page hosting the browser channel)
* for making requests to the server.
* @type {?string}
* @private
*/
goog.net.BrowserChannel.prototype.path_ = null;
/**
* The absolute URI for the forwardchannel request.
* @type {?goog.Uri}
* @private
*/
goog.net.BrowserChannel.prototype.forwardChannelUri_ = null;
/**
* The absolute URI for the backchannel request.
* @type {?goog.Uri}
* @private
*/
goog.net.BrowserChannel.prototype.backChannelUri_ = null;
/**
* A subdomain prefix for using a subdomain in IE for the backchannel
* requests.
* @type {?string}
* @private
*/
goog.net.BrowserChannel.prototype.hostPrefix_ = null;
/**
* Whether we allow the use of a subdomain in IE for the backchannel requests.
* @private
*/
goog.net.BrowserChannel.prototype.allowHostPrefix_ = true;
/**
* The next id to use for the RID (request identifier) parameter. This
* identifier uniquely identifies the forward channel request.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.nextRid_ = 0;
/**
* The id to use for the next outgoing map. This identifier uniquely
* identifies a sent map.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.nextMapId_ = 0;
/**
* Whether to fail forward-channel requests after one try, or after a few tries.
* @type {boolean}
* @private
*/
goog.net.BrowserChannel.prototype.failFast_ = false;
/**
* The handler that receive callbacks for state changes and data.
* @type {?goog.net.BrowserChannel.Handler}
* @private
*/
goog.net.BrowserChannel.prototype.handler_ = null;
/**
* Timer identifier for asynchronously making a forward channel request.
* @type {?number}
* @private
*/
goog.net.BrowserChannel.prototype.forwardChannelTimerId_ = null;
/**
* Timer identifier for asynchronously making a back channel request.
* @type {?number}
* @private
*/
goog.net.BrowserChannel.prototype.backChannelTimerId_ = null;
/**
* Timer identifier for the timer that waits for us to retry the backchannel in
* the case where it is dead and no longer receiving data.
* @type {?number}
* @private
*/
goog.net.BrowserChannel.prototype.deadBackChannelTimerId_ = null;
/**
* The BrowserTestChannel object which encapsulates the logic for determining
* interesting network conditions about the client.
* @type {goog.net.BrowserTestChannel?}
* @private
*/
goog.net.BrowserChannel.prototype.connectionTest_ = null;
/**
* Whether the client's network conditions can support chunked responses.
* @type {?boolean}
* @private
*/
goog.net.BrowserChannel.prototype.useChunked_ = null;
/**
* Whether chunked mode is allowed. In certain debugging situations, it's
* useful to disable this.
* @private
*/
goog.net.BrowserChannel.prototype.allowChunkedMode_ = true;
/**
* The array identifier of the last array received from the server for the
* backchannel request.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.lastArrayId_ = -1;
/**
* The array identifier of the last array sent by the server that we know about.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.lastPostResponseArrayId_ = -1;
/**
* The last status code received.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.lastStatusCode_ = -1;
/**
* Number of times we have retried the current forward channel request.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.forwardChannelRetryCount_ = 0;
/**
* Number of times it a row that we have retried the current back channel
* request and received no data.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.backChannelRetryCount_ = 0;
/**
* The attempt id for the current back channel request. Starts at 1 and
* increments for each reconnect. The server uses this to log if our connection
* is flaky or not.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.backChannelAttemptId_;
/**
* The base part of the time before firing next retry request. Default is 5
* seconds. Note that a random delay is added (see {@link retryDelaySeedMs_})
* for all retries, and linear backoff is applied to the sum for subsequent
* retries.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.baseRetryDelayMs_ = 5 * 1000;
/**
* A random time between 0 and this number of MS is added to the
* {@link baseRetryDelayMs_}. Default is 10 seconds.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.retryDelaySeedMs_ = 10 * 1000;
/**
* Maximum number of attempts to connect to the server for forward channel
* requests. Defaults to 2.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.forwardChannelMaxRetries_ = 2;
/**
* The timeout in milliseconds for a forward channel request. Defaults to 20
* seconds. Note that part of this timeout can be randomized.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.forwardChannelRequestTimeoutMs_ = 20 * 1000;
/**
* A throttle time in ms for readystatechange events for the backchannel.
* Useful for throttling when ready state is INTERACTIVE (partial data).
*
* This throttle is useful if the server sends large data chunks down the
* backchannel. It prevents examining XHR partial data on every
* readystate change event. This is useful because large chunks can
* trigger hundreds of readystatechange events, each of which takes ~5ms
* or so to handle, in turn making the UI unresponsive for a significant period.
*
* If set to zero no throttle is used.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.readyStateChangeThrottleMs_ = 0;
/**
* Whether cross origin requests are supported for the browser channel.
*
* See {@link goog.net.XhrIo#setWithCredentials}.
* @type {boolean}
* @private
*/
goog.net.BrowserChannel.prototype.supportsCrossDomainXhrs_ = false;
/**
* The latest protocol version that this class supports. We request this version
* from the server when opening the connection. Should match
* com.google.net.browserchannel.BrowserChannel.LATEST_CHANNEL_VERSION.
* @type {number}
*/
goog.net.BrowserChannel.LATEST_CHANNEL_VERSION = 8;
/**
* The channel version that we negotiated with the server for this session.
* Starts out as the version we request, and then is changed to the negotiated
* version after the initial open.
* @type {number}
* @private
*/
goog.net.BrowserChannel.prototype.channelVersion_ =
goog.net.BrowserChannel.LATEST_CHANNEL_VERSION;
/**
* Enum type for the browser channel state machine.
* @enum {number}
*/
goog.net.BrowserChannel.State = {
/** The channel is closed. */
CLOSED: 0,
/** The channel has been initialized but hasn't yet initiated a connection. */
INIT: 1,
/** The channel is in the process of opening a connection to the server. */
OPENING: 2,
/** The channel is open. */
OPENED: 3,
};
/**
* The timeout in milliseconds for a forward channel request.
* @type {number}
*/
goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;
/**
* Maximum number of attempts to connect to the server for back channel
* requests.
* @type {number}
*/
goog.net.BrowserChannel.BACK_CHANNEL_MAX_RETRIES = 3;
/**
* A number in MS of how long we guess the maxmium amount of time a round trip
* to the server should take. In the future this could be substituted with a
* real measurement of the RTT.
* @type {number}
*/
goog.net.BrowserChannel.RTT_ESTIMATE = 3 * 1000;
/**
* When retrying for an inactive channel, we will multiply the total delay by
* this number.
* @type {number}
*/
goog.net.BrowserChannel.INACTIVE_CHANNEL_RETRY_FACTOR = 2;
/**
* Enum type for identifying a BrowserChannel error.
* @enum {number}
*/
goog.net.BrowserChannel.Error = {
/** Value that indicates no error has occurred. */
OK: 0,
/** An error due to a request failing. */
REQUEST_FAILED: 2,
/** An error due to the user being logged out. */
LOGGED_OUT: 4,
/** An error due to server response which contains no data. */
NO_DATA: 5,
/** An error due to a server response indicating an unknown session id */
UNKNOWN_SESSION_ID: 6,
/** An error due to a server response requesting to stop the channel. */
STOP: 7,
/** A general network error. */
NETWORK: 8,
/** An error due to the channel being blocked by a network administrator. */
BLOCKED: 9,
/** An error due to bad data being returned from the server. */
BAD_DATA: 10,
/** An error due to a response that doesn't start with the magic cookie. */
BAD_RESPONSE: 11,
/** ActiveX is blocked by the machine's admin settings. */
ACTIVE_X_BLOCKED: 12,
};
/**
* Internal enum type for the two browser channel channel types.
* @enum {number}
* @private
*/
goog.net.BrowserChannel.ChannelType_ = {
FORWARD_CHANNEL: 1,
BACK_CHANNEL: 2,
};
/**
* The maximum number of maps that can be sent in one POST. Should match
* com.google.net.browserchannel.BrowserChannel.MAX_MAPS_PER_REQUEST.
* @type {number}
* @private
*/
goog.net.BrowserChannel.MAX_MAPS_PER_REQUEST_ = 1000;
/**
* Events fired by BrowserChannel and associated objects
* @const
*/
goog.net.BrowserChannel.Event = {};
/**
* Stat Event that fires when things of interest happen that may be useful for
* applications to know about for stats or debugging purposes. This event fires
* on the EventTarget returned by getStatEventTarget.
*/
goog.net.BrowserChannel.Event.STAT_EVENT = 'statevent';
/**
* An event that fires when POST requests complete successfully, indicating
* the size of the POST and the round trip time.
* This event fires on the EventTarget returned by getStatEventTarget.
*/
goog.net.BrowserChannel.Event.TIMING_EVENT = 'timingevent';
/** @const */
goog.net.BrowserChannel.StatEvent =
goog.net.browserchannelinternal.stats.StatEvent;
/**
* Event class for goog.net.BrowserChannel.Event.TIMING_EVENT
*
* @param {goog.events.EventTarget} target The stat event target for
the browser channel.
* @param {number} size The number of characters in the POST data.
* @param {number} rtt The total round trip time from POST to response in MS.
* @param {number} retries The number of times the POST had to be retried.
* @constructor
* @extends {goog.events.Event}
* @final
*/
goog.net.BrowserChannel.TimingEvent = function(target, size, rtt, retries) {
'use strict';
goog.events.Event.call(
this, goog.net.BrowserChannel.Event.TIMING_EVENT, target);
/**
* @type {number}
*/
this.size = size;
/**
* @type {number}
*/
this.rtt = rtt;
/**
* @type {number}
*/
this.retries = retries;
};
goog.inherits(goog.net.BrowserChannel.TimingEvent, goog.events.Event);
/**
* The type of event that occurs every time some information about how reachable
* the server is is discovered.
* @const {string}
*/
goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT = 'serverreachability';
/** @const */
goog.net.BrowserChannel.ServerReachability =
goog.net.browserchannelinternal.ServerReachability;
/**
* Event class for goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT.
*
* @param {goog.events.EventTarget} target The stat event target for
the browser channel.
* @param {goog.net.browserchannelinternal.ServerReachability} reachabilityType
The
* reachability event type.
* @constructor
* @extends {goog.events.Event}
* @final
*/
goog.net.BrowserChannel.ServerReachabilityEvent = function(
target, reachabilityType) {
'use strict';
goog.events.Event.call(
this, goog.net.BrowserChannel.Event.SERVER_REACHABILITY_EVENT, target);
/**
* @type {goog.net.browserchannelinternal.ServerReachability}
*/
this.reachabilityType = reachabilityType;
};
goog.inherits(
goog.net.BrowserChannel.ServerReachabilityEvent, goog.events.Event);
/**
* A guess at a cutoff at which to no longer assume the backchannel is dead
* when we are slow to receive data. Number in bytes.
*
* Assumption: The worst bandwidth we work on is 50 kilobits/sec
* 50kbits/sec * (1 byte / 8 bits) * 6 sec dead backchannel timeout
* @type {number}
*/
goog.net.BrowserChannel.OUTSTANDING_DATA_BACKCHANNEL_RETRY_CUTOFF = 37500;
/**
* Returns the browserchannel logger.
*
* @return {!goog.net.ChannelDebug} The channel debug object.
*/
goog.net.BrowserChannel.prototype.getChannelDebug = function() {
'use strict';
return this.channelDebug_;
};
/**
* Set the browserchannel logger.
* TODO(user): Add interface for channel loggers or remove this function.
*
* @param {goog.net.ChannelDebug} channelDebug The channel debug object.
*/
goog.net.BrowserChannel.prototype.setChannelDebug = function(channelDebug) {
'use strict';
if (channelDebug != null) {
this.channelDebug_ = channelDebug;
}
};
/**
* Alias for execution hooks. These are currently available as top-level
* aliases (see below), but users should prefer to use them via this alias
* instead.
* @const
*/
goog.net.BrowserChannel.Hooks = goog.net.browserchannelinternal.hooks;
/**
* Alias setStartThreadExecutionHook from the internal Hooks module, as it was
* previously available as a top-level export here.
* @const
*/
goog.net.BrowserChannel.setStartThreadExecutionHook =
goog.net.browserchannelinternal.hooks.setStartThreadExecutionHook;
/**
* Alias setEndThreadExecutionHook from the internal Hooks module, as it was
* previously available as a top-level export here.
* @const
*/
goog.net.BrowserChannel.setEndThreadExecutionHook =
goog.net.browserchannelinternal.hooks.setEndThreadExecutionHook;
/**
* Alias createChannelRequest from the internal Hooks module, as it was
* previously available as a top-level export here.
* @const
*/
goog.net.BrowserChannel.createChannelRequest =
goog.net.ChannelRequest.createChannelRequest;
/**
* Starts the channel. This initiates connections to the server.
*
* @param {string} testPath The path for the test connection.
* @param {string} channelPath The path for the channel connection.
* @param {Object=} opt_extraParams Extra parameter keys and values to add to
* the requests.
* @param {string=} opt_oldSessionId Session ID from a previous session.
* @param {number=} opt_oldArrayId The last array ID from a previous session.
*/
goog.net.BrowserChannel.prototype.connect = function(
testPath, channelPath, opt_extraParams, opt_oldSessionId, opt_oldArrayId) {
'use strict';
this.channelDebug_.debug('connect()');
goog.net.browserchannelinternal.stats.notifyStatEvent(
goog.net.browserchannelinternal.stats.Stat.CONNECT_ATTEMPT);
this.path_ = channelPath;
this.extraParams_ = opt_extraParams || {};
// Attach parameters about the previous session if reconnecting.
if (opt_oldSessionId && opt_oldArrayId !== undefined) {
this.extraParams_['OSID'] = opt_oldSessionId;
this.extraParams_['OAID'] = opt_oldArrayId;
}
if (this.asyncTest_) {
goog.net.browserchannelinternal.hooks.setTimeout(
goog.bind(this.connectTest_, this, testPath), 100);
this.connectChannel_();
} else {
this.connectTest_(testPath);
}
};
/**
* Disconnects and closes the channel.
* @suppress {strictMissingProperties} Added to tighten compiler checks
*/
goog.net.BrowserChannel.prototype.disconnect = function() {
'use strict';
this.channelDebug_.debug('disconnect()');
this.cancelRequests_();
if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
const rid = this.nextRid_++;
const uri = this.forwardChannelUri_.clone();
uri.setParameterValue('SID', this.sid_);
uri.setParameterValue('RID', rid);
uri.setParameterValue('TYPE', 'terminate');
// Add the reconnect parameters.
this.addAdditionalParams_(uri);
/** @suppress {strictMissingProperties} Added to tighten compiler checks */
const request = goog.net.ChannelRequest.createChannelRequest(
this, this.channelDebug_, this.sid_, rid);
request.sendUsingImgTag(uri);
}
this.onClose_();
};
/**
* Returns the session id of the channel. Only available after the
* channel has been opened.
* @return {string} Session ID.
* @suppress {strictMissingProperties} Added to tighten compiler checks
*/
goog.net.BrowserChannel.prototype.getSessionId = function() {
'use strict';
return this.sid_;
};
/**
* Starts the test channel to determine network conditions.
*
* @param {string} testPath The relative PATH for the test connection.
* @private
*/
goog.net.BrowserChannel.prototype.connectTest_ = function(testPath) {
'use strict';
this.channelDebug_.debug('connectTest_()');
if (!this.okToMakeRequest_()) {
return; // channel is cancelled
}
this.connectionTest_ =
new goog.net.BrowserTestChannel(this, this.channelDebug_);
this.connectionTest_.setExtraHeaders(this.extraHeaders_);
this.connectionTest_.setParser(this.parser_);
this.connectionTest_.connect(testPath);
};
/**
* Starts the regular channel which is run after the test channel is complete.
* @private
*/
goog.net.BrowserChannel.prototype.connectChannel_ = function() {
'use strict';
this.channelDebug_.debug('connectChannel_()');
this.ensureInState_(
goog.net.BrowserChannel.State.INIT, goog.net.BrowserChannel.State.CLOSED);
this.forwardChannelUri_ =
this.getForwardChannelUri(/** @type {string} */ (this.path_));
this.ensureForwardChannel_();
};
/**
* Cancels all outstanding requests.
* @private
*/
goog.net.BrowserChannel.prototype.cancelRequests_ = function() {
'use strict';
if (this.connectionTest_) {
this.connectionTest_.abort();
this.connectionTest_ = null;
}
if (this.backChannelRequest_) {
this.backChannelRequest_.cancel();
this.backChannelRequest_ = null;
}
if (this.backChannelTimerId_) {
goog.global.clearTimeout(this.backChannelTimerId_);
this.backChannelTimerId_ = null;
}
this.clearDeadBackchannelTimer_();
if (this.forwardChannelRequest_) {
this.forwardChannelRequest_.cancel();
this.forwardChannelRequest_ = null;
}
if (this.forwardChannelTimerId_) {
goog.global.clearTimeout(this.forwardChannelTimerId_);
this.forwardChannelTimerId_ = null;
}
};
/**
* Returns the extra HTTP headers to add to all the requests sent to the server.
*
* @return {Object} The HTTP headers, or null.
*/
goog.net.BrowserChannel.prototype.getExtraHeaders = function() {
'use strict';
return this.extraHeaders_;
};
/**
* Sets extra HTTP headers to add to all the requests sent to the server.
*
* @param {Object} extraHeaders The HTTP headers, or null.
*/
goog.net.BrowserChannel.prototype.setExtraHeaders = function(extraHeaders) {
'use strict';
this.extraHeaders_ = extraHeaders;
};
/**
* Sets the throttle for handling onreadystatechange events for the request.
*
* @param {number} throttle The throttle in ms. A value of zero indicates
* no throttle.
*/
goog.net.BrowserChannel.prototype.setReadyStateChangeThrottle = function(
throttle) {
'use strict';
this.readyStateChangeThrottleMs_ = throttle;
};
/**
* Sets whether cross origin requests are supported for the browser channel.
*
* Setting this allows the creation of requests to secondary domains and
* sends XHRs with the CORS withCredentials bit set to true.
*
* In order for cross-origin requests to work, the server will also need to set
* CORS response headers as per:
* https://developer.mozilla.org/en-US/docs/HTTP_access_control
*
* See {@link goog.net.XhrIo#setWithCredentials}.
* @param {boolean} supportCrossDomain Whether cross domain XHRs are supported.
*/
goog.net.BrowserChannel.prototype.setSupportsCrossDomainXhrs = function(
supportCrossDomain) {
'use strict';
this.supportsCrossDomainXhrs_ = supportCrossDomain;
};
/**
* Returns the handler used for channel callback events.
*
* @return {goog.net.BrowserChannel.Handler} The handler.
*/
goog.net.BrowserChannel.prototype.getHandler = function() {
'use strict';
return this.handler_;
};
/**
* Sets the handler used for channel callback events.
* @param {goog.net.BrowserChannel.Handler} handler The handler to set.
*/
goog.net.BrowserChannel.prototype.setHandler = function(handler) {
'use strict';
this.handler_ = handler;
};
/**
* Returns whether the channel allows the use of a subdomain. There may be
* cases where this isn't allowed.
* @return {boolean} Whether a host prefix is allowed.
*/
goog.net.BrowserChannel.prototype.getAllowHostPrefix = function() {
'use strict';
return this.allowHostPrefix_;
};