Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / base / js / xmpp_connection.js
blob269053ac71d69c5b2d9a1100974419619d871640
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   var tlsMode = remoting.TlsMode.WITH_HANDSHAKE;
99   if (this.port_ === 443) {
100     // <starttls> handshake before starting TLS is not needed when connecting on
101     // the HTTPS port.
102     tlsMode = remoting.TlsMode.WITHOUT_HANDSHAKE;
103   } else if (remoting.settings.XMPP_SERVER_USE_TLS === false) {
104     tlsMode = remoting.TlsMode.NO_TLS;
105   }
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);
114   if (!this.socket_) {
115     this.socket_ = new remoting.TcpSocket();
116   }
117   var that = this;
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);
123       });
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() {
135   return this.state_;
138 /** @return {!remoting.Error} Error when in FAILED state. */
139 remoting.XmppConnection.prototype.getError = function() {
140   return this.error_;
143 /** @return {string} Current JID when in CONNECTED state. */
144 remoting.XmppConnection.prototype.getJid = function() {
145   return this.jid_;
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_);
155   this.socket_ = null;
156   this.setState_(remoting.SignalStrategy.State.CLOSED);
159 /** @private */
160 remoting.XmppConnection.prototype.onSocketConnected_ = function() {
161   // Check if connection was destroyed.
162   if (this.state_ != remoting.SignalStrategy.State.CONNECTING) {
163     return;
164   }
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));
172   }
176  * @param {ArrayBuffer} data
177  * @private
178  */
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);
188   }
192  * @param {number} errorCode
193  * @private
194  */
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
202  * @private
203  */
204 remoting.XmppConnection.prototype.sendString_ = function(text) {
205   this.sendBuffer_(base.encodeUtf8(text));
209  * @param {ArrayBuffer} data
210  * @private
211  */
212 remoting.XmppConnection.prototype.sendBuffer_ = function(data) {
213   this.sendQueue_.push(data);
214   this.flushSendQueue_();
218  * @private
219  */
220 remoting.XmppConnection.prototype.flushSendQueue_ = function() {
221   if (this.sendPending_ || this.sendQueue_.length == 0) {
222     return;
223   }
225   var that = this;
227   this.sendPending_ = true;
228   this.socket_.send(this.sendQueue_[0])
229       .then(function(/** number */ bytesSent) {
230         that.sendPending_ = false;
231         that.onSent_(bytesSent);
232       })
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);
237       });
241  * @param {number} bytesSent
242  * @private
243  */
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) {
248     return;
249   }
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();
259   } else {
260     this.sendQueue_[0] = data.slice(data.byteLength - bytesSent);
261   }
263   this.flushSendQueue_();
267  * @private
268  */
269 remoting.XmppConnection.prototype.startTls_ = function() {
270   console.assert(!this.startTlsPending_, 'startTls already pending.');
272   var that = this;
274   this.startTlsPending_ = true;
275   this.socket_.startTls()
276       .then(function() {
277         that.startTlsPending_ = false;
278         that.socket_.startReceiving(that.onReceive_.bind(that),
279                                     that.onReceiveError_.bind(that));
281         that.loginHandler_.onTlsStarted();
282       })
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);
287       });
291  * @param {string} jid
292  * @param {remoting.XmppStreamParser} streamParser
293  * @private
294  */
295 remoting.XmppConnection.prototype.onHandshakeDone_ =
296     function(jid, streamParser) {
297   this.jid_ = jid;
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
306  * @private
307  */
308 remoting.XmppConnection.prototype.onIncomingStanza_ = function(stanza) {
309   if (this.onIncomingStanzaCallback_) {
310     this.onIncomingStanzaCallback_(stanza);
311   }
315  * @param {string} text
316  * @private
317  */
318 remoting.XmppConnection.prototype.onParserError_ = function(text) {
319   this.onError_(remoting.Error.unexpected(), text);
323  * @param {!remoting.Error} error
324  * @param {string} text
325  * @private
326  */
327 remoting.XmppConnection.prototype.onError_ = function(error, text) {
328   console.error(text);
329   this.error_ = error;
330   base.dispose(this.socket_);
331   this.socket_ = null;
332   this.setState_(remoting.SignalStrategy.State.FAILED);
336  * @param {remoting.SignalStrategy.State} newState
337  * @private
338  */
339 remoting.XmppConnection.prototype.setState_ = function(newState) {
340   if (this.state_ != newState) {
341     this.state_ = newState;
342     this.onStateChangedCallback_(this.state_);
343   }