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 /** @type {?function(remoting.SignalStrategy.State):void} @private */
19 this.onStateChangedCallback_ = null;
20 /** @type {?function(Element):void} @private */
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 base.debug.assert(this.onStateChangedCallback_ != null);
55 remoting.wcsSandbox.setOnIq(this.onIncomingStanza_.bind(this));
56 remoting.wcsSandbox.connect(this.onWcsConnected_.bind(this),
57 this.onError_.bind(this));
60 /** @return {remoting.SignalStrategy.State} Current state */
61 remoting.WcsAdapter.prototype.getState = function() {
65 /** @return {remoting.Error} Error when in FAILED state. */
66 remoting.WcsAdapter.prototype.getError = function() {
70 /** @return {string} Current JID when in CONNECTED state. */
71 remoting.WcsAdapter.prototype.getJid = function() {
75 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
76 remoting.WcsAdapter.prototype.getType = function() {
77 return remoting.SignalStrategy.Type.WCS;
80 remoting.WcsAdapter.prototype.dispose = function() {
81 this.setState_(remoting.SignalStrategy.State.CLOSED);
82 remoting.wcsSandbox.setOnIq(null);
85 /** @param {string} message */
86 remoting.WcsAdapter.prototype.sendMessage = function(message) {
87 // Extract the session id, so we can close the session later.
88 // HACK: Add 'x' prefix to the IDs of the outgoing messages to make sure that
89 // stanza IDs used by host and client do not match. This is necessary to
90 // workaround bug in the signaling endpoint used by chromoting.
91 // TODO(sergeyu): Remove this hack once the server-side bug is fixed.
92 var parser = new DOMParser();
93 var iqNode = parser.parseFromString(message, 'text/xml').firstChild;
94 var type = iqNode.getAttribute('type');
96 var id = iqNode.getAttribute('id');
97 iqNode.setAttribute('id', 'x' + id);
98 message = (new XMLSerializer()).serializeToString(iqNode);
102 remoting.wcsSandbox.sendIq(message);
106 * @param {remoting.LogToServer} logToServer The LogToServer instance for the
109 remoting.WcsAdapter.prototype.sendConnectionSetupResults =
110 function(logToServer) {
113 /** @param {string} jid */
114 remoting.WcsAdapter.prototype.onWcsConnected_ = function(jid) {
116 this.setState_(remoting.SignalStrategy.State.CONNECTED);
119 /** @param {string} stanza */
120 remoting.WcsAdapter.prototype.onIncomingStanza_ = function(stanza) {
121 var parser = new DOMParser();
122 var parsed = parser.parseFromString(stanza, 'text/xml').firstChild;
124 // HACK: Remove 'x' prefix added to the id in sendMessage().
126 var type = parsed.getAttribute('type');
127 var id = parsed.getAttribute('id');
128 if (type != 'set' && id.charAt(0) == 'x') {
129 parsed.setAttribute('id', id.substr(1));
132 // Pass message as is when it is malformed.
135 if (this.onIncomingStanzaCallback_) {
136 this.onIncomingStanzaCallback_(parsed);
140 /** @param {remoting.Error} error */
141 remoting.WcsAdapter.prototype.onError_ = function(error) {
143 this.setState_(remoting.SignalStrategy.State.FAILED);
147 * @param {remoting.SignalStrategy.State} newState
150 remoting.WcsAdapter.prototype.setState_ = function(newState) {
151 if (this.state_ != newState) {
152 this.state_ = newState;
153 this.onStateChangedCallback_(this.state_);