1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 * WCS-based SignalStrategy implementation. Used instead of XMPPConnection
12 * when XMPP cannot be used (e.g. in V1 app).
14 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
15 * Callback to call on state change.
17 * @implements {remoting.SignalStrategy}
20 function(onStateChangedCallback) {
22 this.onStateChangedCallback_ = onStateChangedCallback;
23 /** @type {?function(Element):void} @private */
24 this.onIncomingStanzaCallback_ = null;
26 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
30 this.error_ = remoting.Error.NONE;
34 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
37 remoting.WcsAdapter.prototype.setIncomingStanzaCallback =
38 function(onIncomingStanzaCallback) {
39 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback;
42 remoting.WcsAdapter.prototype.connect = function(server, authToken) {
43 remoting.wcsSandbox.setOnIq(this.onIncomingStanza_.bind(this));
45 remoting.wcsSandbox.connect(this.onWcsConnected_.bind(this),
46 this.onError_.bind(this));
49 /** @return {remoting.SignalStrategy.State} Current state */
50 remoting.WcsAdapter.prototype.getState = function() {
54 /** @return {remoting.Error} Error when in FAILED state. */
55 remoting.WcsAdapter.prototype.getError = function() {
59 /** @return {string} Current JID when in CONNECTED state. */
60 remoting.WcsAdapter.prototype.getJid = function() {
64 remoting.WcsAdapter.prototype.dispose = function() {
65 this.setState_(remoting.SignalStrategy.State.CLOSED);
66 remoting.wcsSandbox.setOnIq(null);
69 /** @param {string} message */
70 remoting.WcsAdapter.prototype.sendMessage = function(message) {
71 // Extract the session id, so we can close the session later.
72 // HACK: Add 'x' prefix to the IDs of the outgoing messages to make sure that
73 // stanza IDs used by host and client do not match. This is necessary to
74 // workaround bug in the signaling endpoint used by chromoting.
75 // TODO(sergeyu): Remove this hack once the server-side bug is fixed.
76 var parser = new DOMParser();
77 var iqNode = parser.parseFromString(message, 'text/xml').firstChild;
78 var type = iqNode.getAttribute('type');
80 var id = iqNode.getAttribute('id');
81 iqNode.setAttribute('id', 'x' + id);
82 message = (new XMLSerializer()).serializeToString(iqNode);
86 remoting.wcsSandbox.sendIq(message);
89 /** @param {string} jid */
90 remoting.WcsAdapter.prototype.onWcsConnected_ = function(jid) {
92 this.setState_(remoting.SignalStrategy.State.CONNECTED);
95 /** @param {string} stanza */
96 remoting.WcsAdapter.prototype.onIncomingStanza_ = function(stanza) {
97 var parser = new DOMParser();
98 var parsed = parser.parseFromString(stanza, 'text/xml').firstChild;
100 // HACK: Remove 'x' prefix added to the id in sendMessage().
102 var type = parsed.getAttribute('type');
103 var id = parsed.getAttribute('id');
104 if (type != 'set' && id.charAt(0) == 'x') {
105 parsed.setAttribute('id', id.substr(1));
108 // Pass message as is when it is malformed.
111 if (this.onIncomingStanzaCallback_) {
112 this.onIncomingStanzaCallback_(parsed);
116 /** @param {remoting.Error} error */
117 remoting.WcsAdapter.prototype.onError_ = function(error) {
119 this.setState_(remoting.SignalStrategy.State.FAILED);
123 * @param {remoting.SignalStrategy.State} newState
126 remoting.WcsAdapter.prototype.setState_ = function(newState) {
127 if (this.state_ != newState) {
128 this.state_ = newState;
129 this.onStateChangedCallback_(this.state_);