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).
15 * @implements {remoting.SignalStrategy}
17 remoting.WcsAdapter = function() {
18 /** @private {?function(remoting.SignalStrategy.State):void} */
19 this.onStateChangedCallback_ = null;
20 /** @private {?function(Element):void} */
21 this.onIncomingStanzaCallback_ = null;
23 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
27 this.error_ = remoting.Error.none();
31 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
33 remoting.WcsAdapter.prototype.setStateChangedCallback = function(
34 onStateChangedCallback) {
35 this.onStateChangedCallback_ = onStateChangedCallback;
39 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
42 remoting.WcsAdapter.prototype.setIncomingStanzaCallback =
43 function(onIncomingStanzaCallback) {
44 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback;
48 * @param {string} server
49 * @param {string} username
50 * @param {string} authToken
52 remoting.WcsAdapter.prototype.connect = function(server, username, authToken) {
53 console.assert(this.onStateChangedCallback_ != null,
54 'No state-change callback registered.');
56 remoting.wcsSandbox.setOnIq(this.onIncomingStanza_.bind(this));
57 remoting.wcsSandbox.connect(this.onWcsConnected_.bind(this),
58 this.onError_.bind(this));
61 /** @return {remoting.SignalStrategy.State} Current state */
62 remoting.WcsAdapter.prototype.getState = function() {
66 /** @return {!remoting.Error} Error when in FAILED state. */
67 remoting.WcsAdapter.prototype.getError = function() {
71 /** @return {string} Current JID when in CONNECTED state. */
72 remoting.WcsAdapter.prototype.getJid = function() {
76 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
77 remoting.WcsAdapter.prototype.getType = function() {
78 return remoting.SignalStrategy.Type.WCS;
81 remoting.WcsAdapter.prototype.dispose = function() {
82 this.setState_(remoting.SignalStrategy.State.CLOSED);
83 remoting.wcsSandbox.setOnIq(null);
86 /** @param {string} message */
87 remoting.WcsAdapter.prototype.sendMessage = function(message) {
88 // Extract the session id, so we can close the session later.
89 // HACK: Add 'x' prefix to the IDs of the outgoing messages to make sure that
90 // stanza IDs used by host and client do not match. This is necessary to
91 // workaround bug in the signaling endpoint used by chromoting.
92 // TODO(sergeyu): Remove this hack once the server-side bug is fixed.
93 var parser = new DOMParser();
94 var iqNode = parser.parseFromString(message, 'text/xml').firstChild;
95 var type = iqNode.getAttribute('type');
97 var id = iqNode.getAttribute('id');
98 iqNode.setAttribute('id', 'x' + id);
99 message = (new XMLSerializer()).serializeToString(iqNode);
103 remoting.wcsSandbox.sendIq(message);
106 /** @param {string} jid */
107 remoting.WcsAdapter.prototype.onWcsConnected_ = function(jid) {
109 this.setState_(remoting.SignalStrategy.State.CONNECTED);
112 /** @param {string} stanza */
113 remoting.WcsAdapter.prototype.onIncomingStanza_ = function(stanza) {
114 var parser = new DOMParser();
115 var parsed = parser.parseFromString(stanza, 'text/xml').firstChild;
117 // HACK: Remove 'x' prefix added to the id in sendMessage().
119 var type = parsed.getAttribute('type');
120 var id = parsed.getAttribute('id');
121 if (type != 'set' && id.charAt(0) == 'x') {
122 parsed.setAttribute('id', id.substr(1));
125 // Pass message as is when it is malformed.
128 if (this.onIncomingStanzaCallback_) {
129 this.onIncomingStanzaCallback_(parsed);
133 /** @param {!remoting.Error} error */
134 remoting.WcsAdapter.prototype.onError_ = function(error) {
136 this.setState_(remoting.SignalStrategy.State.FAILED);
140 * @param {remoting.SignalStrategy.State} newState
143 remoting.WcsAdapter.prototype.setState_ = function(newState) {
144 if (this.state_ != newState) {
145 this.state_ = newState;
146 this.onStateChangedCallback_(this.state_);