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 * A connection to an XMPP server.
14 * @implements {remoting.SignalStrategy}
16 remoting.XmppConnection = function() {
21 /** @private {?function(remoting.SignalStrategy.State):void} */
22 this.onStateChangedCallback_ = null;
23 /** @private {?function(Element):void} */
24 this.onIncomingStanzaCallback_ = null;
25 /** @type {?remoting.TcpSocket} @private */
28 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
30 this.sendPending_ = false;
32 this.startTlsPending_ = false;
33 /** @private {Array<!ArrayBuffer>} */
35 /** @private {remoting.XmppLoginHandler} */
36 this.loginHandler_ = null;
37 /** @private {remoting.XmppStreamParser} */
38 this.streamParser_ = null;
42 this.error_ = remoting.Error.none();
46 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
48 remoting.XmppConnection.prototype.setStateChangedCallback = function(
49 onStateChangedCallback) {
50 this.onStateChangedCallback_ = onStateChangedCallback;
53 remoting.XmppConnection.prototype.setSocketForTests = function(
54 /** remoting.TcpSocket */ socket) {
55 this.socket_ = socket;
59 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
62 remoting.XmppConnection.prototype.setIncomingStanzaCallback =
63 function(onIncomingStanzaCallback) {
64 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback;
68 * @param {string} server
69 * @param {string} username
70 * @param {string} authToken
72 remoting.XmppConnection.prototype.connect =
73 function(server, username, authToken) {
74 console.assert(this.state_ == remoting.SignalStrategy.State.NOT_CONNECTED,
75 'connect() called in state ' + this.state_ + '.');
76 console.assert(this.onStateChangedCallback_ != null,
77 'No state-change callback registered.');
79 this.error_ = remoting.Error.none();
80 var hostnameAndPort = server.split(':', 2);
81 this.server_ = hostnameAndPort[0];
83 (hostnameAndPort.length == 2) ? parseInt(hostnameAndPort[1], 10) : 5222;
85 // The server name is passed as to attribute in the <stream>. When connecting
86 // to talk.google.com it affects the certificate the server will use for TLS:
87 // talk.google.com uses gmail certificate when specified server is gmail.com
88 // or googlemail.com and google.com cert otherwise. In the same time it
89 // doesn't accept talk.google.com as target server. Here we use google.com
90 // server name when authenticating to talk.google.com. This ensures that the
91 // server will use google.com cert which will be accepted by the TLS
92 // implementation in Chrome (TLS API doesn't allow specifying domain other
93 // than the one that was passed to connect()).
94 var xmppServer = this.server_;
95 if (xmppServer == 'talk.google.com')
96 xmppServer = 'google.com';
98 var tlsMode = remoting.TlsMode.WITH_HANDSHAKE;
99 if (this.port_ === 443) {
100 // <starttls> handshake before starting TLS is not needed when connecting on
102 tlsMode = remoting.TlsMode.WITHOUT_HANDSHAKE;
103 } else if (remoting.settings.XMPP_SERVER_USE_TLS === false) {
104 tlsMode = remoting.TlsMode.NO_TLS;
107 /** @type {remoting.XmppLoginHandler} */
108 this.loginHandler_ = new remoting.XmppLoginHandler(
109 xmppServer, username, authToken, tlsMode,
110 this.sendString_.bind(this), this.startTls_.bind(this),
111 this.onHandshakeDone_.bind(this), this.onError_.bind(this));
112 this.setState_(remoting.SignalStrategy.State.CONNECTING);
115 this.socket_ = new remoting.TcpSocket();
118 this.socket_.connect(this.server_, this.port_)
119 .then(this.onSocketConnected_.bind(this))
120 .catch(function(error) {
121 that.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
122 'Failed to connect to ' + that.server_ + ': ' + error);
126 /** @param {string} message */
127 remoting.XmppConnection.prototype.sendMessage = function(message) {
128 console.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED,
129 'sendMessage() called in state ' + this.state_ + '.');
130 this.sendString_(message);
133 /** @return {remoting.SignalStrategy.State} Current state */
134 remoting.XmppConnection.prototype.getState = function() {
138 /** @return {!remoting.Error} Error when in FAILED state. */
139 remoting.XmppConnection.prototype.getError = function() {
143 /** @return {string} Current JID when in CONNECTED state. */
144 remoting.XmppConnection.prototype.getJid = function() {
148 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
149 remoting.XmppConnection.prototype.getType = function() {
150 return remoting.SignalStrategy.Type.XMPP;
153 remoting.XmppConnection.prototype.dispose = function() {
154 base.dispose(this.socket_);
156 this.setState_(remoting.SignalStrategy.State.CLOSED);
160 remoting.XmppConnection.prototype.onSocketConnected_ = function() {
161 // Check if connection was destroyed.
162 if (this.state_ != remoting.SignalStrategy.State.CONNECTING) {
166 this.setState_(remoting.SignalStrategy.State.HANDSHAKE);
167 this.loginHandler_.start();
169 if (!this.startTlsPending_) {
170 this.socket_.startReceiving(this.onReceive_.bind(this),
171 this.onReceiveError_.bind(this));
176 * @param {ArrayBuffer} data
179 remoting.XmppConnection.prototype.onReceive_ = function(data) {
180 console.assert(this.state_ == remoting.SignalStrategy.State.HANDSHAKE ||
181 this.state_ == remoting.SignalStrategy.State.CONNECTED,
182 'onReceive_() called in state ' + this.state_ + '.');
184 if (this.state_ == remoting.SignalStrategy.State.HANDSHAKE) {
185 this.loginHandler_.onDataReceived(data);
186 } else if (this.state_ == remoting.SignalStrategy.State.CONNECTED) {
187 this.streamParser_.appendData(data);
192 * @param {number} errorCode
195 remoting.XmppConnection.prototype.onReceiveError_ = function(errorCode) {
196 this.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
197 'Failed to receive from XMPP socket: ' + errorCode);
201 * @param {string} text
204 remoting.XmppConnection.prototype.sendString_ = function(text) {
205 this.sendBuffer_(base.encodeUtf8(text));
209 * @param {ArrayBuffer} data
212 remoting.XmppConnection.prototype.sendBuffer_ = function(data) {
213 this.sendQueue_.push(data);
214 this.flushSendQueue_();
220 remoting.XmppConnection.prototype.flushSendQueue_ = function() {
221 if (this.sendPending_ || this.sendQueue_.length == 0) {
227 this.sendPending_ = true;
228 this.socket_.send(this.sendQueue_[0])
229 .then(function(/** number */ bytesSent) {
230 that.sendPending_ = false;
231 that.onSent_(bytesSent);
233 .catch(function(/** number */ error) {
234 that.sendPending_ = false;
235 that.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
236 'TCP write failed with error ' + error);
241 * @param {number} bytesSent
244 remoting.XmppConnection.prototype.onSent_ = function(bytesSent) {
245 // Ignore send() result if the socket was closed.
246 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE &&
247 this.state_ != remoting.SignalStrategy.State.CONNECTED) {
251 console.assert(this.sendQueue_.length > 0,
252 'Bad queue length: ' + this.sendQueue_.length + '.');
254 var data = this.sendQueue_[0];
255 console.assert(bytesSent <= data.byteLength,
256 'Bad |bytesSent|: ' + bytesSent + '.');
257 if (bytesSent == data.byteLength) {
258 this.sendQueue_.shift();
260 this.sendQueue_[0] = data.slice(data.byteLength - bytesSent);
263 this.flushSendQueue_();
269 remoting.XmppConnection.prototype.startTls_ = function() {
270 console.assert(!this.startTlsPending_, 'startTls already pending.');
274 this.startTlsPending_ = true;
275 this.socket_.startTls()
277 that.startTlsPending_ = false;
278 that.socket_.startReceiving(that.onReceive_.bind(that),
279 that.onReceiveError_.bind(that));
281 that.loginHandler_.onTlsStarted();
283 .catch(function(/** number */ error) {
284 that.startTlsPending_ = false;
285 that.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
286 'Failed to start TLS: ' + error);
291 * @param {string} jid
292 * @param {remoting.XmppStreamParser} streamParser
295 remoting.XmppConnection.prototype.onHandshakeDone_ =
296 function(jid, streamParser) {
298 this.streamParser_ = streamParser;
299 this.streamParser_.setCallbacks(this.onIncomingStanza_.bind(this),
300 this.onParserError_.bind(this));
301 this.setState_(remoting.SignalStrategy.State.CONNECTED);
305 * @param {Element} stanza
308 remoting.XmppConnection.prototype.onIncomingStanza_ = function(stanza) {
309 if (this.onIncomingStanzaCallback_) {
310 this.onIncomingStanzaCallback_(stanza);
315 * @param {string} text
318 remoting.XmppConnection.prototype.onParserError_ = function(text) {
319 this.onError_(remoting.Error.unexpected(), text);
323 * @param {!remoting.Error} error
324 * @param {string} text
327 remoting.XmppConnection.prototype.onError_ = function(error, text) {
330 base.dispose(this.socket_);
332 this.setState_(remoting.SignalStrategy.State.FAILED);
336 * @param {remoting.SignalStrategy.State} newState
339 remoting.XmppConnection.prototype.setState_ = function(newState) {
340 if (this.state_ != newState) {
341 this.state_ = newState;
342 this.onStateChangedCallback_(this.state_);