Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: remoting/webapp/wcs_adapter.js

Issue 530213004: Use XMPP in V2 webapp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 'use strict';
6
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
9
10 /**
11 * WCS-based SignalStrategy implementation. Used instead of XMPPConnection
12 * when XMPP cannot be used (e.g. in V1 app).
13 *
14 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
15 * Callback to call on state change.
16 * @constructor
17 * @implements {remoting.SignalStrategy}
18 */
19 remoting.WcsAdapter =
20 function(onStateChangedCallback) {
21 /** @private */
22 this.onStateChangedCallback_ = onStateChangedCallback;
23 /** @type {?function(Element):void} @private */
24 this.onIncomingStanzaCallback_ = null;
25 /** @private */
26 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
27 /** @private */
28 this.jid_ = '';
29 /** @private */
30 this.error_ = remoting.Error.NONE;
31 }
32
33 /**
34 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
35 * incoming messages.
36 */
37 remoting.WcsAdapter.prototype.setIncomingStanzaCallback =
38 function(onIncomingStanzaCallback) {
39 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback;
40 }
41
42 remoting.WcsAdapter.prototype.connect = function(server, authToken) {
43 remoting.wcsSandbox.setOnIq(this.onIncomingStanza_.bind(this));
44
45 remoting.wcsSandbox.connect(this.onWcsConnected_.bind(this),
46 this.onError_.bind(this));
47 }
48
49 /** @return {remoting.SignalStrategy.State} Current state */
50 remoting.WcsAdapter.prototype.getState = function() {
51 return this.state_;
52 }
53
54 /** @return {remoting.Error} Error when in FAILED state. */
55 remoting.WcsAdapter.prototype.getError = function() {
56 return this.error_;
57 }
58
59 /** @return {string} Current JID when in CONNECTED state. */
60 remoting.WcsAdapter.prototype.getJid = function() {
61 return this.jid_;
62 }
63
64 remoting.WcsAdapter.prototype.dispose = function() {
65 this.setState_(remoting.SignalStrategy.State.CLOSED);
66 remoting.wcsSandbox.setOnIq(null);
67 }
68
69 /** @param {string} message */
70 remoting.WcsAdapter.prototype.sendMessage = function(message) {
71 // Extract the session id, so we can close the session later.
72 // HACK: Add 'x' prefix to the IDs of the outgoing messages to make sure that
73 // stanza IDs used by host and client do not match. This is necessary to
74 // workaround bug in the signaling endpoint used by chromoting.
75 // TODO(sergeyu): Remove this hack once the server-side bug is fixed.
76 var parser = new DOMParser();
77 var iqNode = parser.parseFromString(message, 'text/xml').firstChild;
78 var type = iqNode.getAttribute('type');
79 if (type == 'set') {
80 var id = iqNode.getAttribute('id');
81 iqNode.setAttribute('id', 'x' + id);
82 message = (new XMLSerializer()).serializeToString(iqNode);
83 }
84
85 // Send the stanza.
86 remoting.wcsSandbox.sendIq(message);
87 }
88
89 /** @param {string} jid */
90 remoting.WcsAdapter.prototype.onWcsConnected_ = function(jid) {
91 this.jid_ = jid;
92 this.setState_(remoting.SignalStrategy.State.CONNECTED);
93 }
94
95 /** @param {string} stanza */
96 remoting.WcsAdapter.prototype.onIncomingStanza_ = function(stanza) {
97 var parser = new DOMParser();
98 var parsed = parser.parseFromString(stanza, 'text/xml').firstChild;
99
100 // HACK: Remove 'x' prefix added to the id in sendMessage().
101 try {
102 var type = parsed.getAttribute('type');
103 var id = parsed.getAttribute('id');
104 if (type != 'set' && id.charAt(0) == 'x') {
105 parsed.setAttribute('id', id.substr(1));
106 }
107 } catch (err) {
108 // Pass message as is when it is malformed.
109 }
110
111 if (this.onIncomingStanzaCallback_) {
112 this.onIncomingStanzaCallback_(parsed);
113 }
114 }
115
116 /** @param {remoting.Error} error */
117 remoting.WcsAdapter.prototype.onError_ = function(error) {
118 this.error_ = error;
119 this.setState_(remoting.SignalStrategy.State.FAILED);
120 }
121
122 /**
123 * @param {remoting.SignalStrategy.State} newState
124 * @private
125 */
126 remoting.WcsAdapter.prototype.setState_ = function(newState) {
127 if (this.state_ != newState) {
128 this.state_ = newState;
129 this.onStateChangedCallback_(this.state_);
130 }
131 };
OLDNEW
« no previous file with comments | « remoting/webapp/unittests/xmpp_connection_unittest.js ('k') | remoting/webapp/xmpp_connection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698