| 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 The API spec for the WebChannel messaging library. |
| 17 |
* |
| 18 |
* Similar to HTML5 WebSocket and Closure BrowserChannel, WebChannel |
| 19 |
* offers an abstraction for point-to-point socket-like communication between |
| 20 |
* a browser client and a remote origin. |
| 21 |
* |
| 22 |
* WebChannels are created via <code>WebChannel</code>. Multiple WebChannels |
| 23 |
* may be multiplexed over the same WebChannelTransport, which represents |
| 24 |
* the underlying physical connectivity over standard wire protocols |
| 25 |
* such as HTTP and SPDY. |
| 26 |
* |
| 27 |
* A WebChannels in turn represents a logical communication channel between |
| 28 |
* the client and server end point. A WebChannel remains open for |
| 29 |
* as long as the client or server end-point allows. |
| 30 |
* |
| 31 |
* Messages may be delivered in-order or out-of-order, reliably or unreliably |
| 32 |
* over the same WebChannel. Message delivery guarantees of a WebChannel is |
| 33 |
* to be specified by the application code; and the choice of the |
| 34 |
* underlying wire protocols is completely transparent to the API users. |
| 35 |
* |
| 36 |
* Client-to-client messaging via WebRTC based transport may also be support |
| 37 |
* via the same WebChannel API in future. |
| 38 |
* |
| 39 |
* Note that we have no immediate plan to move this API out of labs. While |
| 40 |
* the implementation is production ready, the API is subject to change |
| 41 |
* (addition only): |
| 42 |
* 1. Adopt new Web APIs (mainly whatwg streams) and goog.net.streams. |
| 43 |
* 2. New programming models for cloud (on the server-side) may require |
| 44 |
* new APIs to be defined. |
| 45 |
* 3. WebRTC DataChannel alignment |
| 46 |
* |
| 47 |
*/ |
| 48 |
|
| 49 |
goog.provide('goog.net.WebChannel'); |
| 50 |
|
| 51 |
goog.require('goog.events'); |
| 52 |
goog.require('goog.events.Event'); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* A WebChannel represents a logical bi-directional channel over which the |
| 58 |
* client communicates with a remote server that holds the other endpoint |
| 59 |
* of the channel. A WebChannel is always created in the context of a shared |
| 60 |
* {@link WebChannelTransport} instance. It is up to the underlying client-side |
| 61 |
* and server-side implementations to decide how or when multiplexing is |
| 62 |
* to be enabled. |
| 63 |
* |
| 64 |
* @interface |
| 65 |
* @extends {EventTarget} |
| 66 |
*/ |
| 67 |
goog.net.WebChannel = function() {}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* This interface defines a pluggable API to allow WebChannel runtime to support |
| 73 |
* customized algorithms in order to recover from transient failures such as |
| 74 |
* those failures caused by network or proxies (intermediaries). |
| 75 |
* |
| 76 |
* The algorithm may also choose to fail-fast, e.g. switch the client to some |
| 77 |
* offline mode. |
| 78 |
* |
| 79 |
* Extra measurements and logging could also be implemented in the custom |
| 80 |
* module, which has the full knowledge of all the state transitions |
| 81 |
* (due to failures). |
| 82 |
* |
| 83 |
* A default algorithm will be provided by the webchannel library itself. Custom |
| 84 |
* algorithms are expected to be tailored to specific client platforms or |
| 85 |
* networking environments, e.g. mobile, cellular network. |
| 86 |
* |
| 87 |
* @interface |
| 88 |
*/ |
| 89 |
goog.net.WebChannel.FailureRecovery = function() {}; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Configuration spec for newly created WebChannel instances. |
| 94 |
* |
| 95 |
* WebChannels are configured in the context of the containing |
| 96 |
* {@link WebChannelTransport}. The configuration parameters are specified |
| 97 |
* when a new instance of WebChannel is created via {@link WebChannelTransport}. |
| 98 |
* |
| 99 |
* messageHeaders: custom headers to be added to every message sent to the |
| 100 |
* server. This object is mutable, and custom headers may be changed, removed, |
| 101 |
* or added during the runtime after a channel has been opened. |
| 102 |
* |
| 103 |
* initMessageHeaders: similar to messageHeaders, but any custom headers will |
| 104 |
* be sent only once when the channel is opened. Typical usage is to send |
| 105 |
* an auth header to the server, which only checks the auth header at the time |
| 106 |
* when the channel is opened. |
| 107 |
* |
| 108 |
* messageContentType: sent as initMessageHeaders via X-WebChannel-Content-Type, |
| 109 |
* to inform the server the MIME type of WebChannel messages. |
| 110 |
* |
| 111 |
* messageUrlParams: custom url query parameters to be added to every message |
| 112 |
* sent to the server. This object is mutable, and custom parameters may be |
| 113 |
* changed, removed or added during the runtime after a channel has been opened. |
| 114 |
* |
| 115 |
* clientProtocolHeaderRequired: whether a special header should be added to |
| 116 |
* each message so that the server can dispatch webchannel messages without |
| 117 |
* knowing the URL path prefix. Defaults to false. |
| 118 |
* |
| 119 |
* concurrentRequestLimit: the maximum number of in-flight HTTP requests allowed |
| 120 |
* when SPDY is enabled. Currently we only detect SPDY in Chrome. |
| 121 |
* This parameter defaults to 10. When SPDY is not enabled, this parameter |
| 122 |
* will have no effect. |
| 123 |
* |
| 124 |
* supportsCrossDomainXhr: setting this to true to allow the use of sub-domains |
| 125 |
* (as configured by the server) to send XHRs with the CORS withCredentials |
| 126 |
* bit set to true. |
| 127 |
* |
| 128 |
* testUrl: the test URL for detecting connectivity during the initial |
| 129 |
* handshake. This parameter defaults to "/<channel_url>/test". |
| 130 |
* |
| 131 |
* sendRawJson: whether to bypass v8 encoding of client-sent messages. Will be |
| 132 |
* deprecated after v9 wire protocol is introduced. Only safe to set if the |
| 133 |
* server is known to support this feature. |
| 134 |
* |
| 135 |
* httpSessionIdParam: the URL parameter name that contains the session id ( |
| 136 |
* for sticky routing of HTTP requests). When this param is specified, a server |
| 137 |
* that supports this option will respond with an opaque session id as part of |
| 138 |
* the initial handshake (via the X-HTTP-Session-Id header); and all the |
| 139 |
* subsequent requests will contain the httpSessionIdParam. This option will |
| 140 |
* take precedence over any duplicated parameter specified with |
| 141 |
* messageUrlParams, whose value will be ignored. |
| 142 |
* |
| 143 |
* httpHeadersOverwriteParam: the URL parameter name to allow custom HTTP |
| 144 |
* headers to be overwritten as a URL param to bypass CORS preflight. |
| 145 |
* goog.net.rpc.HttpCors is used to encode the HTTP headers. |
| 146 |
* |
| 147 |
* backgroundChannelTest: whether to run the channel test (detecting networking |
| 148 |
* conditions) as a background process so the OPEN event will be fired sooner |
| 149 |
* to reduce the initial handshake delay. This option defaults to true. |
| 150 |
* |
| 151 |
* fastHandshake: experimental feature to speed up the initial handshake, e.g. |
| 152 |
* leveraging QUIC 0-RTT, in-band version negotiation. This option defaults |
| 153 |
* to false. To set this option to true, backgroundChannelTest needs be set |
| 154 |
* to true too. Note it is allowed to send messages before the Open event is |
| 155 |
* received after a channel has been connected. In order to enable 0-RTT, |
| 156 |
* messages may be encoded as part of URL and therefore there will be a size |
| 157 |
* limit for those immediate messages (e.g. 4KB). |
| 158 |
* |
| 159 |
* disableRedact: whether to disable logging redact. By default, redact is |
| 160 |
* enabled to remove any message payload or user-provided info |
| 161 |
* from closure logs. |
| 162 |
* |
| 163 |
* clientProfile: inform the server about the client profile to enable |
| 164 |
* customized configs that are optimized for certain clients or environments. |
| 165 |
* Currently this information is sent via X-WebChannel-Client-Profile header. |
| 166 |
* |
| 167 |
* backChannelFailureRecovery: provide a custom algorithm for managing |
| 168 |
* failure recovery for back channel (server-to-client messaging). We may |
| 169 |
* extend this support to forward channel (client-to-server messaging) or |
| 170 |
* initial handshake too in future. |
| 171 |
* |
| 172 |
* |
| 173 |
* @typedef {{ |
| 174 |
* messageHeaders: (!Object<string, string>|undefined), |
| 175 |
* initMessageHeaders: (!Object<string, string>|undefined), |
| 176 |
* messageContentType: (string|undefined), |
| 177 |
* messageUrlParams: (!Object<string, string>|undefined), |
| 178 |
* clientProtocolHeaderRequired: (boolean|undefined), |
| 179 |
* concurrentRequestLimit: (number|undefined), |
| 180 |
* supportsCrossDomainXhr: (boolean|undefined), |
| 181 |
* testUrl: (string|undefined), |
| 182 |
* sendRawJson: (boolean|undefined), |
| 183 |
* httpSessionIdParam: (string|undefined), |
| 184 |
* httpHeadersOverwriteParam: (string|undefined), |
| 185 |
* backgroundChannelTest: (boolean|undefined), |
| 186 |
* fastHandshake: (boolean|undefined), |
| 187 |
* disableRedact: (boolean|undefined), |
| 188 |
* clientProfile: (string|undefined), |
| 189 |
* backChannelFailureRecovery: |
| 190 |
* (!goog.net.WebChannel.FailureRecovery|undefined) |
| 191 |
* }} |
| 192 |
*/ |
| 193 |
goog.net.WebChannel.Options; |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Types that are allowed as message data. |
| 198 |
* |
| 199 |
* Note that JS objects (sent by the client) can only have string encoded |
| 200 |
* values due to the limitation of the current wire protocol. |
| 201 |
* |
| 202 |
* Unicode strings (sent by the server) may or may not need be escaped, as |
| 203 |
* decided by the server. |
| 204 |
* |
| 205 |
* @typedef {(!ArrayBuffer|!Blob|!Object<string, !Object|string>|!Array|string)} |
| 206 |
*/ |
| 207 |
goog.net.WebChannel.MessageData; |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Open the WebChannel against the URI specified in the constructor. |
| 212 |
*/ |
| 213 |
goog.net.WebChannel.prototype.open = goog.abstractMethod; |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* Close the WebChannel. |
| 218 |
* |
| 219 |
* This is a full close (shutdown) with no guarantee of FIFO delivery in respect |
| 220 |
* to any in-flight messages sent to the server. |
| 221 |
* |
| 222 |
* If you need such a guarantee, see the Half the halfClose() method. |
| 223 |
*/ |
| 224 |
goog.net.WebChannel.prototype.close = goog.abstractMethod; |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* Half-close the WebChannel. |
| 229 |
* |
| 230 |
* Half-close semantics: |
| 231 |
* 1. delivered as a regular message in FIFO programming order |
| 232 |
* 2. the server is expected to return a half-close too (with or without |
| 233 |
* application involved), which will trigger a full close (shutdown) |
| 234 |
* on the client side |
| 235 |
* 3. for now, the half-close event defined for server-initiated |
| 236 |
* half-close is not exposed to the client application |
| 237 |
* 4. a client-side half-close may be triggered internally when the client |
| 238 |
* receives a half-close from the server; and the client is expected to |
| 239 |
* do a full close after the half-close is acked and delivered |
| 240 |
* on the server-side. |
| 241 |
* 5. Full close is always a forced one. See the close() method. |
| 242 |
* |
| 243 |
* New messages sent after halfClose() will be dropped. |
| 244 |
*/ |
| 245 |
goog.net.WebChannel.prototype.halfClose = goog.abstractMethod; |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* Sends a message to the server that maintains the other end point of |
| 250 |
* the WebChannel. |
| 251 |
* |
| 252 |
* O-RTT behavior: |
| 253 |
* 1. messages sent before open() is called will always be delivered as |
| 254 |
* part of the handshake, i.e. with 0-RTT |
| 255 |
* 2. messages sent after open() is called but before the OPEN event |
| 256 |
* is received will be delivered as part of the handshake if |
| 257 |
* send() is called from the same execution context as open(). |
| 258 |
* 3. otherwise, those messages will be buffered till the handshake |
| 259 |
* is completed (which will fire the OPEN event). |
| 260 |
* |
| 261 |
* @param {!goog.net.WebChannel.MessageData} message The message to send. |
| 262 |
*/ |
| 263 |
goog.net.WebChannel.prototype.send = goog.abstractMethod; |
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* Common events fired by WebChannels. |
| 268 |
* @enum {string} |
| 269 |
*/ |
| 270 |
goog.net.WebChannel.EventType = { |
| 271 |
/** Dispatched when the channel is opened. */ |
| 272 |
OPEN: goog.events.getUniqueId('open'), |
| 273 |
|
| 274 |
/** Dispatched when the channel is closed. */ |
| 275 |
CLOSE: goog.events.getUniqueId('close'), |
| 276 |
|
| 277 |
/** |
| 278 |
* Dispatched when the channel is aborted due to errors. |
| 279 |
* |
| 280 |
* For backward compatibility reasons, a CLOSE event will also be |
| 281 |
* dispatched, following the ERROR event, which indicates that the channel |
| 282 |
* has been completely shutdown . |
| 283 |
*/ |
| 284 |
ERROR: goog.events.getUniqueId('error'), |
| 285 |
|
| 286 |
/** Dispatched when the channel has received a new message. */ |
| 287 |
MESSAGE: goog.events.getUniqueId('message') |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* The event interface for the MESSAGE event. |
| 294 |
* |
| 295 |
* @constructor |
| 296 |
* @extends {goog.events.Event} |
| 297 |
*/ |
| 298 |
goog.net.WebChannel.MessageEvent = function() { |
| 299 |
goog.net.WebChannel.MessageEvent.base( |
| 300 |
this, 'constructor', goog.net.WebChannel.EventType.MESSAGE); |
| 301 |
}; |
| 302 |
goog.inherits(goog.net.WebChannel.MessageEvent, goog.events.Event); |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* The content of the message received from the server. |
| 307 |
* |
| 308 |
* @type {!goog.net.WebChannel.MessageData} |
| 309 |
*/ |
| 310 |
goog.net.WebChannel.MessageEvent.prototype.data; |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* The metadata key when the MESSAGE event represents a metadata message. |
| 315 |
* |
| 316 |
* @type {string|undefined} |
| 317 |
*/ |
| 318 |
goog.net.WebChannel.MessageEvent.prototype.metadataKey; |
| 319 |
|
| 320 |
|
| 321 |
/** |
| 322 |
* WebChannel level error conditions. |
| 323 |
* |
| 324 |
* Summary of error debugging and reporting in WebChannel: |
| 325 |
* |
| 326 |
* Network Error |
| 327 |
* 1. By default the webchannel library will set the error status to |
| 328 |
* NETWORK_ERROR when a channel has to be aborted or closed. NETWORK_ERROR |
| 329 |
* may be recovered by the application by retrying and opening a new channel. |
| 330 |
* 2. There may be lost messages (not acked by the server) when a channel is |
| 331 |
* aborted. Currently we don't have a public API to retrieve messages that |
| 332 |
* are waiting to be acked on the client side. File a bug if you think it |
| 333 |
* is useful to expose such an API. |
| 334 |
* 3. Details of why a channel fails are available via closure debug logs, |
| 335 |
* and stats events (see webchannel/requeststats.js). Those are internal |
| 336 |
* stats and are subject to change. File a bug if you think it's useful to |
| 337 |
* version and expose such stats as part of the WebChannel API. |
| 338 |
* |
| 339 |
* Server Error |
| 340 |
* 1. SERVER_ERROR is intended to indicate a non-recoverable condition, e.g. |
| 341 |
* when auth fails. |
| 342 |
* 2. We don't currently generate any such errors, because most of the time |
| 343 |
* it's the responsibility of upper-layer frameworks or the application |
| 344 |
* itself to indicate to the client why a webchannel has been failed |
| 345 |
* by the server. |
| 346 |
* 3. When a channel is failed by the server explicitly, we still signal |
| 347 |
* NETWORK_ERROR to the client. Explicit server failure may happen when the |
| 348 |
* server does a fail-over, or becomes overloaded, or conducts a forced |
| 349 |
* shutdown etc. |
| 350 |
* 4. We use some heuristic to decide if the network (aka cloud) is down |
| 351 |
* v.s. the actual server is down. |
| 352 |
* |
| 353 |
* RuntimeProperties.getLastStatusCode is a useful state that we expose to |
| 354 |
* the client to indicate the HTTP response status code of the last HTTP |
| 355 |
* request initiated by the WebChannel client library, for debugging |
| 356 |
* purposes only. |
| 357 |
* |
| 358 |
* See WebChannel.Options.backChannelFailureRecovery and |
| 359 |
* WebChannel.FailureRecovery to install a custom failure-recovery algorithm. |
| 360 |
* |
| 361 |
* @enum {number} |
| 362 |
*/ |
| 363 |
goog.net.WebChannel.ErrorStatus = { |
| 364 |
/** No error has occurred. */ |
| 365 |
OK: 0, |
| 366 |
|
| 367 |
/** Communication to the server has failed. */ |
| 368 |
NETWORK_ERROR: 1, |
| 369 |
|
| 370 |
/** The server fails to accept or process the WebChannel. */ |
| 371 |
SERVER_ERROR: 2 |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
/** |
| 377 |
* The event interface for the ERROR event. |
| 378 |
* |
| 379 |
* @constructor |
| 380 |
* @extends {goog.events.Event} |
| 381 |
*/ |
| 382 |
goog.net.WebChannel.ErrorEvent = function() { |
| 383 |
goog.net.WebChannel.ErrorEvent.base( |
| 384 |
this, 'constructor', goog.net.WebChannel.EventType.ERROR); |
| 385 |
}; |
| 386 |
goog.inherits(goog.net.WebChannel.ErrorEvent, goog.events.Event); |
| 387 |
|
| 388 |
|
| 389 |
/** |
| 390 |
* The error status. |
| 391 |
* |
| 392 |
* @type {!goog.net.WebChannel.ErrorStatus} |
| 393 |
*/ |
| 394 |
goog.net.WebChannel.ErrorEvent.prototype.status; |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* @return {!goog.net.WebChannel.RuntimeProperties} The runtime properties |
| 399 |
* of the WebChannel instance. |
| 400 |
*/ |
| 401 |
goog.net.WebChannel.prototype.getRuntimeProperties = goog.abstractMethod; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
/** |
| 406 |
* The runtime properties of the WebChannel instance. |
| 407 |
* |
| 408 |
* This class is defined for debugging and monitoring purposes, as well as for |
| 409 |
* runtime functions that the application may choose to manage by itself. |
| 410 |
* |
| 411 |
* @interface |
| 412 |
*/ |
| 413 |
goog.net.WebChannel.RuntimeProperties = function() {}; |
| 414 |
|
| 415 |
|
| 416 |
/** |
| 417 |
* @return {number} The effective limit for the number of concurrent HTTP |
| 418 |
* requests that are allowed to be made for sending messages from the client |
| 419 |
* to the server. When SPDY is not enabled, this limit will be one. |
| 420 |
*/ |
| 421 |
goog.net.WebChannel.RuntimeProperties.prototype.getConcurrentRequestLimit = |
| 422 |
goog.abstractMethod; |
| 423 |
|
| 424 |
|
| 425 |
/** |
| 426 |
* For applications that need support multiple channels (e.g. from |
| 427 |
* different tabs) to the same origin, use this method to decide if SPDY is |
| 428 |
* enabled and therefore it is safe to open multiple channels. |
| 429 |
* |
| 430 |
* If SPDY is disabled, the application may choose to limit the number of active |
| 431 |
* channels to one or use other means such as sub-domains to work around |
| 432 |
* the browser connection limit. |
| 433 |
* |
| 434 |
* @return {boolean} Whether SPDY is enabled for the origin against which |
| 435 |
* the channel is created. |
| 436 |
*/ |
| 437 |
goog.net.WebChannel.RuntimeProperties.prototype.isSpdyEnabled = |
| 438 |
goog.abstractMethod; |
| 439 |
|
| 440 |
|
| 441 |
/** |
| 442 |
* @return {number} The number of requests (for sending messages to the server) |
| 443 |
* that are pending. If this number is approaching the value of |
| 444 |
* getConcurrentRequestLimit(), client-to-server message delivery may experience |
| 445 |
* a higher latency. |
| 446 |
*/ |
| 447 |
goog.net.WebChannel.RuntimeProperties.prototype.getPendingRequestCount = |
| 448 |
goog.abstractMethod; |
| 449 |
|
| 450 |
|
| 451 |
/** |
| 452 |
* For applications to query the current HTTP session id, sent by the server |
| 453 |
* during the initial handshake. |
| 454 |
* |
| 455 |
* @return {?string} the HTTP session id or null if no HTTP session is in use. |
| 456 |
*/ |
| 457 |
goog.net.WebChannel.RuntimeProperties.prototype.getHttpSessionId = |
| 458 |
goog.abstractMethod; |
| 459 |
|
| 460 |
|
| 461 |
/** |
| 462 |
* This method generates an in-band commit request to the server, which will |
| 463 |
* ack the commit request as soon as all messages sent prior to this commit |
| 464 |
* request have been committed by the application. |
| 465 |
* |
| 466 |
* Committing a message has a stronger semantics than delivering a message |
| 467 |
* to the application. Detail spec: |
| 468 |
* https://github.com/bidiweb/webchannel/blob/master/commit.md |
| 469 |
* |
| 470 |
* Timeout or cancellation is not supported and the application may have to |
| 471 |
* abort the channel if the commit-ack fails to arrive in time. |
| 472 |
* |
| 473 |
* @param {function()} callback The callback will be invoked once an |
| 474 |
* ack has been received for the current commit or any newly issued commit. |
| 475 |
*/ |
| 476 |
goog.net.WebChannel.RuntimeProperties.prototype.commit = goog.abstractMethod; |
| 477 |
|
| 478 |
|
| 479 |
/** |
| 480 |
* This method may be used by the application to recover from a peer failure |
| 481 |
* or to enable sender-initiated flow-control. |
| 482 |
* |
| 483 |
* Detail spec: https://github.com/bidiweb/webchannel/blob/master/commit.md |
| 484 |
* |
| 485 |
* @return {number} The total number of messages that have not received |
| 486 |
* commit-ack from the server; or if no commit has been issued, the number |
| 487 |
* of messages that have not been delivered to the server application. |
| 488 |
*/ |
| 489 |
goog.net.WebChannel.RuntimeProperties.prototype.getNonAckedMessageCount = |
| 490 |
goog.abstractMethod; |
| 491 |
|
| 492 |
|
| 493 |
/** |
| 494 |
* A low water-mark message count to notify the application when the |
| 495 |
* flow-control condition is cleared, that is, when the application is |
| 496 |
* able to send more messages. |
| 497 |
* |
| 498 |
* We expect the application to configure a high water-mark message count, |
| 499 |
* which is checked via getNonAckedMessageCount(). When the high water-mark |
| 500 |
* is exceeded, the application should install a callback via this method |
| 501 |
* to be notified when to start to send new messages. |
| 502 |
* |
| 503 |
* @param {number} count The low water-mark count. It is an error to pass |
| 504 |
* a non-positive value. |
| 505 |
* @param {!function()} callback The call back to notify the application |
| 506 |
* when NonAckedMessageCount is below the specified low water-mark count. |
| 507 |
* Any previously registered callback is cleared. This new callback will |
| 508 |
* be cleared once it has been fired, or when the channel is closed or aborted. |
| 509 |
*/ |
| 510 |
goog.net.WebChannel.RuntimeProperties.prototype.notifyNonAckedMessageCount = |
| 511 |
goog.abstractMethod; |
| 512 |
|
| 513 |
|
| 514 |
/** |
| 515 |
* This method registers a callback to handle the commit request sent |
| 516 |
* by the server. Commit protocol spec: |
| 517 |
* https://github.com/bidiweb/webchannel/blob/master/commit.md |
| 518 |
* |
| 519 |
* @param {function(!Object)} callback The callback will take an opaque |
| 520 |
* commitId which needs be passed back to the server when an ack-commit |
| 521 |
* response is generated by the client application, via ackCommit(). |
| 522 |
*/ |
| 523 |
goog.net.WebChannel.RuntimeProperties.prototype.onCommit = goog.abstractMethod; |
| 524 |
|
| 525 |
|
| 526 |
/** |
| 527 |
* This method is used by the application to generate an ack-commit response |
| 528 |
* for the given commitId. Commit protocol spec: |
| 529 |
* https://github.com/bidiweb/webchannel/blob/master/commit.md |
| 530 |
* |
| 531 |
* @param {!Object} commitId The commitId which denotes the commit request |
| 532 |
* from the server that needs be ack'ed. |
| 533 |
*/ |
| 534 |
goog.net.WebChannel.RuntimeProperties.prototype.ackCommit = goog.abstractMethod; |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* @return {number} The last HTTP status code received by the channel. |
| 539 |
*/ |
| 540 |
goog.net.WebChannel.RuntimeProperties.prototype.getLastStatusCode = |
| 541 |
goog.abstractMethod; |
| 542 |
|
| 543 |
|
| 544 |
/** |
| 545 |
* Enum to indicate the current recovery state. |
| 546 |
* |
| 547 |
* @enum {string} |
| 548 |
*/ |
| 549 |
goog.net.WebChannel.FailureRecovery.State = { |
| 550 |
/** Initial state. */ |
| 551 |
INIT: 'init', |
| 552 |
|
| 553 |
/** Once a failure has been detected. */ |
| 554 |
FAILED: 'failed', |
| 555 |
|
| 556 |
/** |
| 557 |
* Once a recovery operation has been issued, e.g. a new request to resume |
| 558 |
* communication. |
| 559 |
*/ |
| 560 |
RECOVERING: 'recovering', |
| 561 |
|
| 562 |
/** The channel has been closed. */ |
| 563 |
CLOSED: 'closed' |
| 564 |
}; |
| 565 |
|
| 566 |
|
| 567 |
/** |
| 568 |
* Enum to indicate different failure conditions as detected by the webchannel |
| 569 |
* runtime. |
| 570 |
* |
| 571 |
* This enum is to be used only between the runtime and FailureRecovery module, |
| 572 |
* and new states are expected to be introduced in future. |
| 573 |
* |
| 574 |
* @enum {string} |
| 575 |
*/ |
| 576 |
goog.net.WebChannel.FailureRecovery.FailureCondition = { |
| 577 |
/** |
| 578 |
* The HTTP response returned a non-successful http status code. |
| 579 |
*/ |
| 580 |
HTTP_ERROR: 'http_error', |
| 581 |
|
| 582 |
/** |
| 583 |
* The request was aborted. |
| 584 |
*/ |
| 585 |
ABORT: 'abort', |
| 586 |
|
| 587 |
/** |
| 588 |
* The request timed out. |
| 589 |
*/ |
| 590 |
TIMEOUT: 'timeout', |
| 591 |
|
| 592 |
/** |
| 593 |
* Exception was thrown while processing the request/response. |
| 594 |
*/ |
| 595 |
EXCEPTION: 'exception' |
| 596 |
}; |
| 597 |
|
| 598 |
|
| 599 |
/** |
| 600 |
* @return {!goog.net.WebChannel.FailureRecovery.State} the current state, |
| 601 |
* mainly for debugging use. |
| 602 |
*/ |
| 603 |
goog.net.WebChannel.FailureRecovery.prototype.getState = goog.abstractMethod; |
| 604 |
|
| 605 |
|
| 606 |
/** |
| 607 |
* This method is for WebChannel runtime to set the current failure condition |
| 608 |
* and to provide a callback for the algorithm to signal to the runtime |
| 609 |
* when it is time to issue a recovery operation, e.g. a new request to the |
| 610 |
* server. |
| 611 |
* |
| 612 |
* Supported transitions include: |
| 613 |
* INIT->FAILED |
| 614 |
* FAILED->FAILED (re-entry ok) |
| 615 |
* RECOVERY->FAILED. |
| 616 |
* |
| 617 |
* Ignored if state == CLOSED. |
| 618 |
* |
| 619 |
* Advanced implementations are expected to track all the state transitions |
| 620 |
* and their timestamps for monitoring purposes. |
| 621 |
* |
| 622 |
* @param {!goog.net.WebChannel.FailureRecovery.FailureCondition} failure The |
| 623 |
* new failure condition generated by the WebChannel runtime. |
| 624 |
* @param {!Function} operation The callback function to the WebChannel |
| 625 |
* runtime to issue a recovery operation, e.g. a new request. E.g. the default |
| 626 |
* recovery algorithm will issue timeout-based recovery operations. |
| 627 |
* Post-condition for the callback: state transition to RECOVERING. |
| 628 |
* |
| 629 |
* @return {!goog.net.WebChannel.FailureRecovery.State} The updated state |
| 630 |
* as decided by the failure recovery module. Upon a recoverable failure event, |
| 631 |
* the state is transitioned to RECOVERING; or the state is transitioned to |
| 632 |
* FAILED which indicates a fail-fast decision for the runtime to execute. |
| 633 |
*/ |
| 634 |
goog.net.WebChannel.FailureRecovery.prototype.setFailure = goog.abstractMethod; |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* The Webchannel runtime needs call this method when webchannel is closed or |
| 639 |
* aborted. |
| 640 |
* |
| 641 |
* Once the instance is closed, any access to the instance will be a no-op. |
| 642 |
*/ |
| 643 |
goog.net.WebChannel.FailureRecovery.prototype.close = goog.abstractMethod; |
| 644 |
|
| 645 |
|
| 646 |
/** |
| 647 |
* A request header to indicate to the server the messaging protocol |
| 648 |
* each HTTP message is speaking. |
| 649 |
* |
| 650 |
* @type {string} |
| 651 |
*/ |
| 652 |
goog.net.WebChannel.X_CLIENT_PROTOCOL = 'X-Client-Protocol'; |
| 653 |
|
| 654 |
|
| 655 |
/** |
| 656 |
* The value for x-client-protocol when the messaging protocol is WebChannel. |
| 657 |
* |
| 658 |
* @type {string} |
| 659 |
*/ |
| 660 |
goog.net.WebChannel.X_CLIENT_PROTOCOL_WEB_CHANNEL = 'webchannel'; |
| 661 |
|
| 662 |
|
| 663 |
/** |
| 664 |
* A response header for the server to signal the wire-protocol that |
| 665 |
* the browser establishes with the server (or proxy), e.g. "spdy" (aka http/2) |
| 666 |
* "quic". This information avoids the need to use private APIs to decide if |
| 667 |
* HTTP requests are multiplexed etc. |
| 668 |
* |
| 669 |
* @type {string} |
| 670 |
*/ |
| 671 |
goog.net.WebChannel.X_CLIENT_WIRE_PROTOCOL = 'X-Client-Wire-Protocol'; |
| 672 |
|
| 673 |
|
| 674 |
/** |
| 675 |
* A response header for the server to send back the HTTP session id as part of |
| 676 |
* the initial handshake. The value of the HTTP session id is opaque to the |
| 677 |
* WebChannel protocol. |
| 678 |
* |
| 679 |
* @type {string} |
| 680 |
*/ |
| 681 |
goog.net.WebChannel.X_HTTP_SESSION_ID = 'X-HTTP-Session-Id'; |
| 682 |
|
| 683 |
|
| 684 |
/** |
| 685 |
* A request header for specifying the content-type of WebChannel messages, |
| 686 |
* e.g. application-defined JSON encoding styles. Currently this header |
| 687 |
* is sent by the client via initMessageHeaders when the channel is opened. |
| 688 |
* |
| 689 |
* @type {string} |
| 690 |
*/ |
| 691 |
goog.net.WebChannel.X_WEBCHANNEL_CONTENT_TYPE = 'X-WebChannel-Content-Type'; |
| 692 |
|
| 693 |
|
| 694 |
/** |
| 695 |
* A request header for specifying the client profile in order to apply |
| 696 |
* customized config params on the server side, e.g. timeouts. |
| 697 |
* |
| 698 |
* @type {string} |
| 699 |
*/ |
| 700 |
goog.net.WebChannel.X_WEBCHANNEL_CLIENT_PROFILE = 'X-WebChannel-Client-Profile'; |