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 // <starttls> handshake before starting TLS is not needed when connecting on
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);
110 this.socket_ = new remoting.TcpSocket();
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);
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 =
132 /** @return {remoting.SignalStrategy.State} Current state */
133 remoting.XmppConnection.prototype.getState = function() {
137 /** @return {!remoting.Error} Error when in FAILED state. */
138 remoting.XmppConnection.prototype.getError = function() {
142 /** @return {string} Current JID when in CONNECTED state. */
143 remoting.XmppConnection.prototype.getJid = function() {
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_);
155 this.setState_(remoting.SignalStrategy.State.CLOSED);
159 remoting.XmppConnection.prototype.onSocketConnected_ = function() {
160 // Check if connection was destroyed.
161 if (this.state_ != remoting.SignalStrategy.State.CONNECTING) {
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));
175 * @param {ArrayBuffer} data
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);
191 * @param {number} errorCode
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
203 remoting.XmppConnection.prototype.sendString_ = function(text) {
204 this.sendBuffer_(base.encodeUtf8(text));
208 * @param {ArrayBuffer} data
211 remoting.XmppConnection.prototype.sendBuffer_ = function(data) {
212 this.sendQueue_.push(data);
213 this.flushSendQueue_();
219 remoting.XmppConnection.prototype.flushSendQueue_ = function() {
220 if (this.sendPending_ || this.sendQueue_.length == 0) {
226 this.sendPending_ = true;
227 this.socket_.send(this.sendQueue_[0])
228 .then(function(/** number */ bytesSent) {
229 that.sendPending_ = false;
230 that.onSent_(bytesSent);
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);
240 * @param {number} bytesSent
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) {
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();
259 this.sendQueue_[0] = data.slice(data.byteLength - bytesSent);
262 this.flushSendQueue_();
268 remoting.XmppConnection.prototype.startTls_ = function() {
269 console.assert(!this.startTlsPending_, 'startTls already pending.');
273 this.startTlsPending_ = true;
274 this.socket_.startTls()
276 that.startTlsPending_ = false;
277 that.socket_.startReceiving(that.onReceive_.bind(that),
278 that.onReceiveError_.bind(that));
280 that.loginHandler_.onTlsStarted();
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);
290 * @param {string} jid
291 * @param {remoting.XmppStreamParser} streamParser
294 remoting.XmppConnection.prototype.onHandshakeDone_ =
295 function(jid, streamParser) {
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
307 remoting.XmppConnection.prototype.onIncomingStanza_ = function(stanza) {
308 if (this.onIncomingStanzaCallback_) {
309 this.onIncomingStanzaCallback_(stanza);
314 * @param {string} text
317 remoting.XmppConnection.prototype.onParserError_ = function(text) {
318 this.onError_(remoting.Error.unexpected(), text);
322 * @param {!remoting.Error} error
323 * @param {string} text
326 remoting.XmppConnection.prototype.onError_ = function(error, text) {
329 base.dispose(this.socket_);
331 this.setState_(remoting.SignalStrategy.State.FAILED);
335 * @param {remoting.SignalStrategy.State} newState
338 remoting.XmppConnection.prototype.setState_ = function(newState) {
339 if (this.state_ != newState) {
340 this.state_ = newState;
341 this.onStateChangedCallback_(this.state_);