Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / crd / js / xmpp_connection.js
blobff9f4111fd03b92837208104f09b7289e791026b
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.
13 * TODO(sergeyu): Chrome provides two APIs for TCP sockets: chrome.socket and
14 * chrome.sockets.tcp . chrome.socket is deprecated but it's still used here
15 * because TLS support in chrome.sockets.tcp is currently broken, see
16 * crbug.com/403076 .
18 * @constructor
19 * @implements {remoting.SignalStrategy}
21 remoting.XmppConnection = function() {
22 /** @private */
23 this.server_ = '';
24 /** @private */
25 this.port_ = 0;
26 /** @type {?function(remoting.SignalStrategy.State):void} @private */
27 this.onStateChangedCallback_ = null;
28 /** @type {?function(Element):void} @private */
29 this.onIncomingStanzaCallback_ = null;
30 /** @private */
31 this.socketId_ = -1;
32 /** @private */
33 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
34 /** @private */
35 this.readPending_ = false;
36 /** @private */
37 this.sendPending_ = false;
38 /** @private */
39 this.startTlsPending_ = false;
40 /** @type {Array<ArrayBuffer>} @private */
41 this.sendQueue_ = [];
42 /** @type {remoting.XmppLoginHandler} @private*/
43 this.loginHandler_ = null;
44 /** @type {remoting.XmppStreamParser} @private*/
45 this.streamParser_ = null;
46 /** @private */
47 this.jid_ = '';
48 /** @private */
49 this.error_ = remoting.Error.NONE;
52 /**
53 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
55 remoting.XmppConnection.prototype.setStateChangedCallback = function(
56 onStateChangedCallback) {
57 this.onStateChangedCallback_ = onStateChangedCallback;
60 /**
61 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
62 * incoming messages.
64 remoting.XmppConnection.prototype.setIncomingStanzaCallback =
65 function(onIncomingStanzaCallback) {
66 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback;
69 /**
70 * @param {string} server
71 * @param {string} username
72 * @param {string} authToken
74 remoting.XmppConnection.prototype.connect =
75 function(server, username, authToken) {
76 base.debug.assert(this.state_ == remoting.SignalStrategy.State.NOT_CONNECTED);
77 base.debug.assert(this.onStateChangedCallback_ != null);
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 chrome.socket.create("tcp", {}, this.onSocketCreated_.bind(this));
108 this.setState_(remoting.SignalStrategy.State.CONNECTING);
111 /** @param {string} message */
112 remoting.XmppConnection.prototype.sendMessage = function(message) {
113 base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED);
114 this.sendString_(message);
118 * @param {remoting.LogToServer} logToServer The LogToServer instance for the
119 * connection.
121 remoting.XmppConnection.prototype.sendConnectionSetupResults =
122 function(logToServer) {
125 /** @return {remoting.SignalStrategy.State} Current state */
126 remoting.XmppConnection.prototype.getState = function() {
127 return this.state_;
130 /** @return {remoting.Error} Error when in FAILED state. */
131 remoting.XmppConnection.prototype.getError = function() {
132 return this.error_;
135 /** @return {string} Current JID when in CONNECTED state. */
136 remoting.XmppConnection.prototype.getJid = function() {
137 return this.jid_;
140 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
141 remoting.XmppConnection.prototype.getType = function() {
142 return remoting.SignalStrategy.Type.XMPP;
145 remoting.XmppConnection.prototype.dispose = function() {
146 this.closeSocket_();
147 this.setState_(remoting.SignalStrategy.State.CLOSED);
151 * @param {chrome.socket.CreateInfo} createInfo
152 * @private
154 remoting.XmppConnection.prototype.onSocketCreated_ = function(createInfo) {
155 // Check if connection was destroyed.
156 if (this.state_ != remoting.SignalStrategy.State.CONNECTING) {
157 chrome.socket.destroy(createInfo.socketId);
158 return;
161 this.socketId_ = createInfo.socketId;
163 chrome.socket.connect(this.socketId_,
164 this.server_,
165 this.port_,
166 this.onSocketConnected_.bind(this));
170 * @param {number} result
171 * @private
173 remoting.XmppConnection.prototype.onSocketConnected_ = function(result) {
174 // Check if connection was destroyed.
175 if (this.state_ != remoting.SignalStrategy.State.CONNECTING) {
176 return;
179 if (result != 0) {
180 this.onError_(remoting.Error.NETWORK_FAILURE,
181 'Failed to connect to ' + this.server_ + ': ' + result);
182 return;
185 this.setState_(remoting.SignalStrategy.State.HANDSHAKE);
186 this.loginHandler_.start();
188 if (!this.startTlsPending_) {
189 this.tryRead_();
194 * @private
196 remoting.XmppConnection.prototype.tryRead_ = function() {
197 base.debug.assert(!this.readPending_);
198 base.debug.assert(this.state_ == remoting.SignalStrategy.State.HANDSHAKE ||
199 this.state_ == remoting.SignalStrategy.State.CONNECTED);
200 base.debug.assert(!this.startTlsPending_);
202 this.readPending_ = true;
203 chrome.socket.read(this.socketId_, this.onRead_.bind(this));
207 * @param {chrome.socket.ReadInfo} readInfo
208 * @private
210 remoting.XmppConnection.prototype.onRead_ = function(readInfo) {
211 base.debug.assert(this.readPending_);
212 this.readPending_ = false;
214 // Check if the socket was closed while reading.
215 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE &&
216 this.state_ != remoting.SignalStrategy.State.CONNECTED) {
217 return;
220 if (readInfo.resultCode < 0) {
221 this.onError_(remoting.Error.NETWORK_FAILURE,
222 'Failed to receive from XMPP socket: ' + readInfo.resultCode);
223 return;
226 if (this.state_ == remoting.SignalStrategy.State.HANDSHAKE) {
227 this.loginHandler_.onDataReceived(readInfo.data);
228 } else if (this.state_ == remoting.SignalStrategy.State.CONNECTED) {
229 this.streamParser_.appendData(readInfo.data);
232 if (!this.startTlsPending_ &&
233 this.state_ != remoting.SignalStrategy.State.CLOSED) {
234 this.tryRead_();
239 * @param {string} text
240 * @private
242 remoting.XmppConnection.prototype.sendString_ = function(text) {
243 this.sendBuffer_(base.encodeUtf8(text));
247 * @param {ArrayBuffer} data
248 * @private
250 remoting.XmppConnection.prototype.sendBuffer_ = function(data) {
251 this.sendQueue_.push(data);
252 this.flushSendQueue_();
256 * @private
258 remoting.XmppConnection.prototype.flushSendQueue_ = function() {
259 if (this.sendPending_ || this.sendQueue_.length == 0) {
260 return;
263 var data = this.sendQueue_[0]
264 this.sendPending_ = true;
265 chrome.socket.write(this.socketId_, data, this.onWrite_.bind(this));
269 * @param {chrome.socket.WriteInfo} writeInfo
270 * @private
272 remoting.XmppConnection.prototype.onWrite_ = function(writeInfo) {
273 base.debug.assert(this.sendPending_);
274 this.sendPending_ = false;
276 // Ignore write() result if the socket was closed.
277 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE &&
278 this.state_ != remoting.SignalStrategy.State.CONNECTED) {
279 return;
282 if (writeInfo.bytesWritten < 0) {
283 this.onError_(remoting.Error.NETWORK_FAILURE,
284 'TCP write failed with error ' + writeInfo.bytesWritten);
285 return;
288 base.debug.assert(this.sendQueue_.length > 0);
290 var data = this.sendQueue_[0]
291 base.debug.assert(writeInfo.bytesWritten <= data.byteLength);
292 if (writeInfo.bytesWritten == data.byteLength) {
293 this.sendQueue_.shift();
294 } else {
295 this.sendQueue_[0] = data.slice(data.byteLength - writeInfo.bytesWritten);
298 this.flushSendQueue_();
302 * @private
304 remoting.XmppConnection.prototype.startTls_ = function() {
305 base.debug.assert(!this.readPending_);
306 base.debug.assert(!this.startTlsPending_);
308 this.startTlsPending_ = true;
309 chrome.socket.secure(
310 this.socketId_, {}, this.onTlsStarted_.bind(this));
314 * @param {number} resultCode
315 * @private
317 remoting.XmppConnection.prototype.onTlsStarted_ = function(resultCode) {
318 base.debug.assert(this.startTlsPending_);
319 this.startTlsPending_ = false;
321 if (resultCode < 0) {
322 this.onError_(remoting.Error.NETWORK_FAILURE,
323 'Failed to start TLS: ' + resultCode);
324 return;
327 this.tryRead_();
328 this.loginHandler_.onTlsStarted();
332 * @param {string} jid
333 * @param {remoting.XmppStreamParser} streamParser
334 * @private
336 remoting.XmppConnection.prototype.onHandshakeDone_ =
337 function(jid, streamParser) {
338 this.jid_ = jid;
339 this.streamParser_ = streamParser;
340 this.streamParser_.setCallbacks(this.onIncomingStanza_.bind(this),
341 this.onParserError_.bind(this));
342 this.setState_(remoting.SignalStrategy.State.CONNECTED);
346 * @param {Element} stanza
347 * @private
349 remoting.XmppConnection.prototype.onIncomingStanza_ = function(stanza) {
350 if (this.onIncomingStanzaCallback_) {
351 this.onIncomingStanzaCallback_(stanza);
356 * @param {string} text
357 * @private
359 remoting.XmppConnection.prototype.onParserError_ = function(text) {
360 this.onError_(remoting.Error.UNEXPECTED, text);
364 * @param {remoting.Error} error
365 * @param {string} text
366 * @private
368 remoting.XmppConnection.prototype.onError_ = function(error, text) {
369 console.error(text);
370 this.error_ = error;
371 this.closeSocket_();
372 this.setState_(remoting.SignalStrategy.State.FAILED);
376 * @private
378 remoting.XmppConnection.prototype.closeSocket_ = function() {
379 if (this.socketId_ != -1) {
380 chrome.socket.destroy(this.socketId_);
381 this.socketId_ = -1;
386 * @param {remoting.SignalStrategy.State} newState
387 * @private
389 remoting.XmppConnection.prototype.setState_ = function(newState) {
390 if (this.state_ != newState) {
391 this.state_ = newState;
392 this.onStateChangedCallback_(this.state_);