OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** @suppress {duplicate} */ | 7 /** @suppress {duplicate} */ |
8 var remoting = remoting || {}; | 8 var remoting = remoting || {}; |
9 | 9 |
10 /** | 10 /** |
(...skipping 28 matching lines...) Expand all Loading... |
39 this.startTlsPending_ = false; | 39 this.startTlsPending_ = false; |
40 /** @private {Array<ArrayBuffer>} */ | 40 /** @private {Array<ArrayBuffer>} */ |
41 this.sendQueue_ = []; | 41 this.sendQueue_ = []; |
42 /** @private {remoting.XmppLoginHandler} */ | 42 /** @private {remoting.XmppLoginHandler} */ |
43 this.loginHandler_ = null; | 43 this.loginHandler_ = null; |
44 /** @private {remoting.XmppStreamParser} */ | 44 /** @private {remoting.XmppStreamParser} */ |
45 this.streamParser_ = null; | 45 this.streamParser_ = null; |
46 /** @private */ | 46 /** @private */ |
47 this.jid_ = ''; | 47 this.jid_ = ''; |
48 /** @private */ | 48 /** @private */ |
49 this.error_ = remoting.Error.NONE; | 49 this.error_ = remoting.Error.none(); |
50 }; | 50 }; |
51 | 51 |
52 /** | 52 /** |
53 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback | 53 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback |
54 */ | 54 */ |
55 remoting.XmppConnection.prototype.setStateChangedCallback = function( | 55 remoting.XmppConnection.prototype.setStateChangedCallback = function( |
56 onStateChangedCallback) { | 56 onStateChangedCallback) { |
57 this.onStateChangedCallback_ = onStateChangedCallback; | 57 this.onStateChangedCallback_ = onStateChangedCallback; |
58 }; | 58 }; |
59 | 59 |
60 /** | 60 /** |
61 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on | 61 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on |
62 * incoming messages. | 62 * incoming messages. |
63 */ | 63 */ |
64 remoting.XmppConnection.prototype.setIncomingStanzaCallback = | 64 remoting.XmppConnection.prototype.setIncomingStanzaCallback = |
65 function(onIncomingStanzaCallback) { | 65 function(onIncomingStanzaCallback) { |
66 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback; | 66 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback; |
67 }; | 67 }; |
68 | 68 |
69 /** | 69 /** |
70 * @param {string} server | 70 * @param {string} server |
71 * @param {string} username | 71 * @param {string} username |
72 * @param {string} authToken | 72 * @param {string} authToken |
73 */ | 73 */ |
74 remoting.XmppConnection.prototype.connect = | 74 remoting.XmppConnection.prototype.connect = |
75 function(server, username, authToken) { | 75 function(server, username, authToken) { |
76 base.debug.assert(this.state_ == remoting.SignalStrategy.State.NOT_CONNECTED); | 76 base.debug.assert(this.state_ == remoting.SignalStrategy.State.NOT_CONNECTED); |
77 base.debug.assert(this.onStateChangedCallback_ != null); | 77 base.debug.assert(this.onStateChangedCallback_ != null); |
78 | 78 |
79 this.error_ = remoting.Error.NONE; | 79 this.error_ = remoting.Error.none(); |
80 var hostnameAndPort = server.split(':', 2); | 80 var hostnameAndPort = server.split(':', 2); |
81 this.server_ = hostnameAndPort[0]; | 81 this.server_ = hostnameAndPort[0]; |
82 this.port_ = | 82 this.port_ = |
83 (hostnameAndPort.length == 2) ? parseInt(hostnameAndPort[1], 10) : 5222; | 83 (hostnameAndPort.length == 2) ? parseInt(hostnameAndPort[1], 10) : 5222; |
84 | 84 |
85 // The server name is passed as to attribute in the <stream>. When connecting | 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: | 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 | 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 | 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 | 89 // doesn't accept talk.google.com as target server. Here we use google.com |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 * @param {number} result | 170 * @param {number} result |
171 * @private | 171 * @private |
172 */ | 172 */ |
173 remoting.XmppConnection.prototype.onSocketConnected_ = function(result) { | 173 remoting.XmppConnection.prototype.onSocketConnected_ = function(result) { |
174 // Check if connection was destroyed. | 174 // Check if connection was destroyed. |
175 if (this.state_ != remoting.SignalStrategy.State.CONNECTING) { | 175 if (this.state_ != remoting.SignalStrategy.State.CONNECTING) { |
176 return; | 176 return; |
177 } | 177 } |
178 | 178 |
179 if (result != 0) { | 179 if (result != 0) { |
180 this.onError_(remoting.Error.NETWORK_FAILURE, | 180 this.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE), |
181 'Failed to connect to ' + this.server_ + ': ' + result); | 181 'Failed to connect to ' + this.server_ + ': ' + result); |
182 return; | 182 return; |
183 } | 183 } |
184 | 184 |
185 this.setState_(remoting.SignalStrategy.State.HANDSHAKE); | 185 this.setState_(remoting.SignalStrategy.State.HANDSHAKE); |
186 this.loginHandler_.start(); | 186 this.loginHandler_.start(); |
187 | 187 |
188 if (!this.startTlsPending_) { | 188 if (!this.startTlsPending_) { |
189 this.tryRead_(); | 189 this.tryRead_(); |
190 } | 190 } |
(...skipping 20 matching lines...) Expand all Loading... |
211 base.debug.assert(this.readPending_); | 211 base.debug.assert(this.readPending_); |
212 this.readPending_ = false; | 212 this.readPending_ = false; |
213 | 213 |
214 // Check if the socket was closed while reading. | 214 // Check if the socket was closed while reading. |
215 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE && | 215 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE && |
216 this.state_ != remoting.SignalStrategy.State.CONNECTED) { | 216 this.state_ != remoting.SignalStrategy.State.CONNECTED) { |
217 return; | 217 return; |
218 } | 218 } |
219 | 219 |
220 if (readInfo.resultCode < 0) { | 220 if (readInfo.resultCode < 0) { |
221 this.onError_(remoting.Error.NETWORK_FAILURE, | 221 this.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE), |
222 'Failed to receive from XMPP socket: ' + readInfo.resultCode); | 222 'Failed to receive from XMPP socket: ' + readInfo.resultCode); |
223 return; | 223 return; |
224 } | 224 } |
225 | 225 |
226 if (this.state_ == remoting.SignalStrategy.State.HANDSHAKE) { | 226 if (this.state_ == remoting.SignalStrategy.State.HANDSHAKE) { |
227 this.loginHandler_.onDataReceived(readInfo.data); | 227 this.loginHandler_.onDataReceived(readInfo.data); |
228 } else if (this.state_ == remoting.SignalStrategy.State.CONNECTED) { | 228 } else if (this.state_ == remoting.SignalStrategy.State.CONNECTED) { |
229 this.streamParser_.appendData(readInfo.data); | 229 this.streamParser_.appendData(readInfo.data); |
230 } | 230 } |
231 | 231 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 base.debug.assert(this.sendPending_); | 273 base.debug.assert(this.sendPending_); |
274 this.sendPending_ = false; | 274 this.sendPending_ = false; |
275 | 275 |
276 // Ignore write() result if the socket was closed. | 276 // Ignore write() result if the socket was closed. |
277 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE && | 277 if (this.state_ != remoting.SignalStrategy.State.HANDSHAKE && |
278 this.state_ != remoting.SignalStrategy.State.CONNECTED) { | 278 this.state_ != remoting.SignalStrategy.State.CONNECTED) { |
279 return; | 279 return; |
280 } | 280 } |
281 | 281 |
282 if (writeInfo.bytesWritten < 0) { | 282 if (writeInfo.bytesWritten < 0) { |
283 this.onError_(remoting.Error.NETWORK_FAILURE, | 283 this.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE), |
284 'TCP write failed with error ' + writeInfo.bytesWritten); | 284 'TCP write failed with error ' + writeInfo.bytesWritten); |
285 return; | 285 return; |
286 } | 286 } |
287 | 287 |
288 base.debug.assert(this.sendQueue_.length > 0); | 288 base.debug.assert(this.sendQueue_.length > 0); |
289 | 289 |
290 var data = this.sendQueue_[0] | 290 var data = this.sendQueue_[0] |
291 base.debug.assert(writeInfo.bytesWritten <= data.byteLength); | 291 base.debug.assert(writeInfo.bytesWritten <= data.byteLength); |
292 if (writeInfo.bytesWritten == data.byteLength) { | 292 if (writeInfo.bytesWritten == data.byteLength) { |
293 this.sendQueue_.shift(); | 293 this.sendQueue_.shift(); |
(...skipping 18 matching lines...) Expand all Loading... |
312 | 312 |
313 /** | 313 /** |
314 * @param {number} resultCode | 314 * @param {number} resultCode |
315 * @private | 315 * @private |
316 */ | 316 */ |
317 remoting.XmppConnection.prototype.onTlsStarted_ = function(resultCode) { | 317 remoting.XmppConnection.prototype.onTlsStarted_ = function(resultCode) { |
318 base.debug.assert(this.startTlsPending_); | 318 base.debug.assert(this.startTlsPending_); |
319 this.startTlsPending_ = false; | 319 this.startTlsPending_ = false; |
320 | 320 |
321 if (resultCode < 0) { | 321 if (resultCode < 0) { |
322 this.onError_(remoting.Error.NETWORK_FAILURE, | 322 this.onError_(new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE), |
323 'Failed to start TLS: ' + resultCode); | 323 'Failed to start TLS: ' + resultCode); |
324 return; | 324 return; |
325 } | 325 } |
326 | 326 |
327 this.tryRead_(); | 327 this.tryRead_(); |
328 this.loginHandler_.onTlsStarted(); | 328 this.loginHandler_.onTlsStarted(); |
329 }; | 329 }; |
330 | 330 |
331 /** | 331 /** |
332 * @param {string} jid | 332 * @param {string} jid |
(...skipping 17 matching lines...) Expand all Loading... |
350 if (this.onIncomingStanzaCallback_) { | 350 if (this.onIncomingStanzaCallback_) { |
351 this.onIncomingStanzaCallback_(stanza); | 351 this.onIncomingStanzaCallback_(stanza); |
352 } | 352 } |
353 }; | 353 }; |
354 | 354 |
355 /** | 355 /** |
356 * @param {string} text | 356 * @param {string} text |
357 * @private | 357 * @private |
358 */ | 358 */ |
359 remoting.XmppConnection.prototype.onParserError_ = function(text) { | 359 remoting.XmppConnection.prototype.onParserError_ = function(text) { |
360 this.onError_(remoting.Error.UNEXPECTED, text); | 360 this.onError_(remoting.Error.unexpected(), text); |
361 } | 361 } |
362 | 362 |
363 /** | 363 /** |
364 * @param {!remoting.Error} error | 364 * @param {!remoting.Error} error |
365 * @param {string} text | 365 * @param {string} text |
366 * @private | 366 * @private |
367 */ | 367 */ |
368 remoting.XmppConnection.prototype.onError_ = function(error, text) { | 368 remoting.XmppConnection.prototype.onError_ = function(error, text) { |
369 console.error(text); | 369 console.error(text); |
370 this.error_ = error; | 370 this.error_ = error; |
(...skipping 14 matching lines...) Expand all Loading... |
385 /** | 385 /** |
386 * @param {remoting.SignalStrategy.State} newState | 386 * @param {remoting.SignalStrategy.State} newState |
387 * @private | 387 * @private |
388 */ | 388 */ |
389 remoting.XmppConnection.prototype.setState_ = function(newState) { | 389 remoting.XmppConnection.prototype.setState_ = function(newState) { |
390 if (this.state_ != newState) { | 390 if (this.state_ != newState) { |
391 this.state_ = newState; | 391 this.state_ = newState; |
392 this.onStateChangedCallback_(this.state_); | 392 this.onStateChangedCallback_(this.state_); |
393 } | 393 } |
394 }; | 394 }; |
OLD | NEW |