Remove base.debug.assert.
[chromium-blink-merge.git] / remoting / webapp / base / js / xmpp_connection.js
blob398acdc9a2cae1812768ac6a3447879dd89c9b62
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /**
11  * A connection to an XMPP server.
12  *
13  * @constructor
14  * @implements {remoting.SignalStrategy}
15  */
16 remoting.XmppConnection = function() {
17   /** @private */
18   this.server_ = '';
19   /** @private */
20   this.port_ = 0;
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 */
26   this.socket_ = null;
27   /** @private */
28   this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
29   /** @private */
30   this.sendPending_ = false;
31   /** @private */
32   this.startTlsPending_ = false;
33   /** @private {Array<!ArrayBuffer>} */
34   this.sendQueue_ = [];
35   /** @private {remoting.XmppLoginHandler} */
36   this.loginHandler_ = null;
37   /** @private {remoting.XmppStreamParser} */
38   this.streamParser_ = null;
39   /** @private */
40   this.jid_ = '';
41   /** @private */
42   this.error_ = remoting.Error.none();
45 /**
46  * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
47  */
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;
58 /**
59  * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
60  *     incoming messages.
61  */
62 remoting.XmppConnection.prototype.setIncomingStanzaCallback =
63     function(onIncomingStanzaCallback) {
64   this.onIncomingStanzaCallback_ = onIncomingStanzaCallback;
67 /**
68  * @param {string} server
69  * @param {string} username
70  * @param {string} authToken
71  */
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];
82   this.port_ =
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   // <starttls> handshake before starting TLS is not needed when connecting on
99   // the HTTPS port.
100   var needHandshakeBeforeTls = this.port_ != 443;
102   /** @type {remoting.XmppLoginHandler} */
103   this.loginHandler_ = new remoting.XmppLoginHandler(
104       xmppServer, username, authToken, needHandshakeBeforeTls,
105       this.sendString_.bind(this), this.startTls_.bind(this),
106       this.onHandshakeDone_.bind(this), this.onError_.bind(this));
107   this.setState_(remoting.SignalStrategy.State.CONNECTING);
109   if (!this.socket_) {
110     this.socket_ = new remoting.TcpSocket();
111   }
112   var that = this;
113   this.socket_.connect(this.server_, this.port_)
114       .then(this.onSocketConnected_.bind(this))
115       .catch(function(error) {
116         that.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
117                       'Failed to connect to ' + that.server_ + ': ' + error);
118       });
121 /** @param {string} message */
122 remoting.XmppConnection.prototype.sendMessage = function(message) {
123   console.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED,
124                 'sendMessage() called in state ' + this.state_ + '.');
125   this.sendString_(message);
128 remoting.XmppConnection.prototype.sendConnectionSetupResults =
129     function(logger) {
132 /** @return {remoting.SignalStrategy.State} Current state */
133 remoting.XmppConnection.prototype.getState = function() {
134   return this.state_;
137 /** @return {!remoting.Error} Error when in FAILED state. */
138 remoting.XmppConnection.prototype.getError = function() {
139   return this.error_;
142 /** @return {string} Current JID when in CONNECTED state. */
143 remoting.XmppConnection.prototype.getJid = function() {
144   return this.jid_;
147 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
148 remoting.XmppConnection.prototype.getType = function() {
149   return remoting.SignalStrategy.Type.XMPP;
152 remoting.XmppConnection.prototype.dispose = function() {
153   base.dispose(this.socket_);
154   this.socket_ = null;
155   this.setState_(remoting.SignalStrategy.State.CLOSED);
158 /** @private */
159 remoting.XmppConnection.prototype.onSocketConnected_ = function() {
160   // Check if connection was destroyed.
161   if (this.state_ != remoting.SignalStrategy.State.CONNECTING) {
162     return;
163   }
165   this.setState_(remoting.SignalStrategy.State.HANDSHAKE);
166   this.loginHandler_.start();
168   if (!this.startTlsPending_) {
169     this.socket_.startReceiving(this.onReceive_.bind(this),
170                                 this.onReceiveError_.bind(this));
171   }
175  * @param {ArrayBuffer} data
176  * @private
177  */
178 remoting.XmppConnection.prototype.onReceive_ = function(data) {
179   console.assert(this.state_ == remoting.SignalStrategy.State.HANDSHAKE ||
180                  this.state_ == remoting.SignalStrategy.State.CONNECTED,
181                 'onReceive_() called in state ' + this.state_ + '.');
183   if (this.state_ == remoting.SignalStrategy.State.HANDSHAKE) {
184     this.loginHandler_.onDataReceived(data);
185   } else if (this.state_ == remoting.SignalStrategy.State.CONNECTED) {
186     this.streamParser_.appendData(data);
187   }
191  * @param {number} errorCode
192  * @private
193  */
194 remoting.XmppConnection.prototype.onReceiveError_ = function(errorCode) {
195   this.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
196                 'Failed to receive from XMPP socket: ' + errorCode);
200  * @param {string} text
201  * @private
202  */
203 remoting.XmppConnection.prototype.sendString_ = function(text) {
204   this.sendBuffer_(base.encodeUtf8(text));
208  * @param {ArrayBuffer} data
209  * @private
210  */
211 remoting.XmppConnection.prototype.sendBuffer_ = function(data) {
212   this.sendQueue_.push(data);
213   this.flushSendQueue_();
217  * @private
218  */
219 remoting.XmppConnection.prototype.flushSendQueue_ = function() {
220   if (this.sendPending_ || this.sendQueue_.length == 0) {
221     return;
222   }
224   var that = this;
226   this.sendPending_ = true;
227   this.socket_.send(this.sendQueue_[0])
228       .then(function(/** number */ bytesSent) {
229         that.sendPending_ = false;
230         that.onSent_(bytesSent);
231       })
232       .catch(function(/** number */ error) {
233         that.sendPending_ = false;
234         that.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
235                       'TCP write failed with error ' + error);
236       });
240  * @param {number} bytesSent
241  * @private
242  */
243 remoting.XmppConnection.prototype.onSent_ = function(bytesSent) {
244   // Ignore send() result if the socket was closed.
245   if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE &&
246       this.state_ != remoting.SignalStrategy.State.CONNECTED) {
247     return;
248   }
250   console.assert(this.sendQueue_.length > 0,
251                  'Bad queue length: ' + this.sendQueue_.length + '.');
253   var data = this.sendQueue_[0];
254   console.assert(bytesSent <= data.byteLength,
255                  'Bad |bytesSent|: ' + bytesSent + '.');
256   if (bytesSent == data.byteLength) {
257     this.sendQueue_.shift();
258   } else {
259     this.sendQueue_[0] = data.slice(data.byteLength - bytesSent);
260   }
262   this.flushSendQueue_();
266  * @private
267  */
268 remoting.XmppConnection.prototype.startTls_ = function() {
269   console.assert(!this.startTlsPending_, 'startTls already pending.');
271   var that = this;
273   this.startTlsPending_ = true;
274   this.socket_.startTls()
275       .then(function() {
276         that.startTlsPending_ = false;
277         that.socket_.startReceiving(that.onReceive_.bind(that),
278                                     that.onReceiveError_.bind(that));
280         that.loginHandler_.onTlsStarted();
281       })
282       .catch(function(/** number */ error) {
283         that.startTlsPending_ = false;
284         that.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE),
285                       'Failed to start TLS: ' + error);
286       });
290  * @param {string} jid
291  * @param {remoting.XmppStreamParser} streamParser
292  * @private
293  */
294 remoting.XmppConnection.prototype.onHandshakeDone_ =
295     function(jid, streamParser) {
296   this.jid_ = jid;
297   this.streamParser_ = streamParser;
298   this.streamParser_.setCallbacks(this.onIncomingStanza_.bind(this),
299                                   this.onParserError_.bind(this));
300   this.setState_(remoting.SignalStrategy.State.CONNECTED);
304  * @param {Element} stanza
305  * @private
306  */
307 remoting.XmppConnection.prototype.onIncomingStanza_ = function(stanza) {
308   if (this.onIncomingStanzaCallback_) {
309     this.onIncomingStanzaCallback_(stanza);
310   }
314  * @param {string} text
315  * @private
316  */
317 remoting.XmppConnection.prototype.onParserError_ = function(text) {
318   this.onError_(remoting.Error.unexpected(), text);
322  * @param {!remoting.Error} error
323  * @param {string} text
324  * @private
325  */
326 remoting.XmppConnection.prototype.onError_ = function(error, text) {
327   console.error(text);
328   this.error_ = error;
329   base.dispose(this.socket_);
330   this.socket_ = null;
331   this.setState_(remoting.SignalStrategy.State.FAILED);
335  * @param {remoting.SignalStrategy.State} newState
336  * @private
337  */
338 remoting.XmppConnection.prototype.setState_ = function(newState) {
339   if (this.state_ != newState) {
340     this.state_ = newState;
341     this.onStateChangedCallback_(this.state_);
342   }