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 /** |
11 * WCS-based SignalStrategy implementation. Used instead of XMPPConnection | 11 * WCS-based SignalStrategy implementation. Used instead of XMPPConnection |
12 * when XMPP cannot be used (e.g. in V1 app). | 12 * when XMPP cannot be used (e.g. in V1 app). |
13 * | 13 * |
14 * @constructor | 14 * @constructor |
15 * @implements {remoting.SignalStrategy} | 15 * @implements {remoting.SignalStrategy} |
16 */ | 16 */ |
17 remoting.WcsAdapter = function() { | 17 remoting.WcsAdapter = function() { |
18 /** @type {?function(remoting.SignalStrategy.State):void} @private */ | 18 /** @type {?function(remoting.SignalStrategy.State):void} @private */ |
19 this.onStateChangedCallback_ = null; | 19 this.onStateChangedCallback_ = null; |
20 /** @type {?function(Element):void} @private */ | 20 /** @type {?function(Element):void} @private */ |
21 this.onIncomingStanzaCallback_ = null; | 21 this.onIncomingStanzaCallback_ = null; |
22 /** @private */ | 22 /** @private */ |
23 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED; | 23 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED; |
24 /** @private */ | 24 /** @private */ |
25 this.jid_ = ''; | 25 this.jid_ = ''; |
26 /** @private */ | 26 /** @private */ |
27 this.error_ = remoting.Error.NONE; | 27 this.error_ = remoting.Error.NONE; |
28 } | 28 }; |
29 | 29 |
30 /** | 30 /** |
31 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback | 31 * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback |
32 */ | 32 */ |
33 remoting.WcsAdapter.prototype.setStateChangedCallback = function( | 33 remoting.WcsAdapter.prototype.setStateChangedCallback = function( |
34 onStateChangedCallback) { | 34 onStateChangedCallback) { |
35 this.onStateChangedCallback_ = onStateChangedCallback; | 35 this.onStateChangedCallback_ = onStateChangedCallback; |
36 }; | 36 }; |
37 | 37 |
38 /** | 38 /** |
39 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on | 39 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on |
40 * incoming messages. | 40 * incoming messages. |
41 */ | 41 */ |
42 remoting.WcsAdapter.prototype.setIncomingStanzaCallback = | 42 remoting.WcsAdapter.prototype.setIncomingStanzaCallback = |
43 function(onIncomingStanzaCallback) { | 43 function(onIncomingStanzaCallback) { |
44 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback; | 44 this.onIncomingStanzaCallback_ = onIncomingStanzaCallback; |
45 } | 45 }; |
46 | 46 |
47 /** | 47 /** |
48 * @param {string} server | 48 * @param {string} server |
49 * @param {string} username | 49 * @param {string} username |
50 * @param {string} authToken | 50 * @param {string} authToken |
51 */ | 51 */ |
52 remoting.WcsAdapter.prototype.connect = function(server, username, authToken) { | 52 remoting.WcsAdapter.prototype.connect = function(server, username, authToken) { |
53 base.debug.assert(this.onStateChangedCallback_ != null); | 53 base.debug.assert(this.onStateChangedCallback_ != null); |
54 | 54 |
55 remoting.wcsSandbox.setOnIq(this.onIncomingStanza_.bind(this)); | 55 remoting.wcsSandbox.setOnIq(this.onIncomingStanza_.bind(this)); |
56 remoting.wcsSandbox.connect(this.onWcsConnected_.bind(this), | 56 remoting.wcsSandbox.connect(this.onWcsConnected_.bind(this), |
57 this.onError_.bind(this)); | 57 this.onError_.bind(this)); |
58 } | 58 }; |
59 | 59 |
60 /** @return {remoting.SignalStrategy.State} Current state */ | 60 /** @return {remoting.SignalStrategy.State} Current state */ |
61 remoting.WcsAdapter.prototype.getState = function() { | 61 remoting.WcsAdapter.prototype.getState = function() { |
62 return this.state_; | 62 return this.state_; |
63 } | 63 }; |
64 | 64 |
65 /** @return {remoting.Error} Error when in FAILED state. */ | 65 /** @return {remoting.Error} Error when in FAILED state. */ |
66 remoting.WcsAdapter.prototype.getError = function() { | 66 remoting.WcsAdapter.prototype.getError = function() { |
67 return this.error_; | 67 return this.error_; |
68 } | 68 }; |
69 | 69 |
70 /** @return {string} Current JID when in CONNECTED state. */ | 70 /** @return {string} Current JID when in CONNECTED state. */ |
71 remoting.WcsAdapter.prototype.getJid = function() { | 71 remoting.WcsAdapter.prototype.getJid = function() { |
72 return this.jid_; | 72 return this.jid_; |
73 } | 73 }; |
| 74 |
| 75 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */ |
| 76 remoting.WcsAdapter.prototype.getType = function() { |
| 77 return remoting.SignalStrategy.Type.WCS; |
| 78 }; |
74 | 79 |
75 remoting.WcsAdapter.prototype.dispose = function() { | 80 remoting.WcsAdapter.prototype.dispose = function() { |
76 this.setState_(remoting.SignalStrategy.State.CLOSED); | 81 this.setState_(remoting.SignalStrategy.State.CLOSED); |
77 remoting.wcsSandbox.setOnIq(null); | 82 remoting.wcsSandbox.setOnIq(null); |
78 } | 83 }; |
79 | 84 |
80 /** @param {string} message */ | 85 /** @param {string} message */ |
81 remoting.WcsAdapter.prototype.sendMessage = function(message) { | 86 remoting.WcsAdapter.prototype.sendMessage = function(message) { |
82 // Extract the session id, so we can close the session later. | 87 // Extract the session id, so we can close the session later. |
83 // HACK: Add 'x' prefix to the IDs of the outgoing messages to make sure that | 88 // HACK: Add 'x' prefix to the IDs of the outgoing messages to make sure that |
84 // stanza IDs used by host and client do not match. This is necessary to | 89 // stanza IDs used by host and client do not match. This is necessary to |
85 // workaround bug in the signaling endpoint used by chromoting. | 90 // workaround bug in the signaling endpoint used by chromoting. |
86 // TODO(sergeyu): Remove this hack once the server-side bug is fixed. | 91 // TODO(sergeyu): Remove this hack once the server-side bug is fixed. |
87 var parser = new DOMParser(); | 92 var parser = new DOMParser(); |
88 var iqNode = parser.parseFromString(message, 'text/xml').firstChild; | 93 var iqNode = parser.parseFromString(message, 'text/xml').firstChild; |
89 var type = iqNode.getAttribute('type'); | 94 var type = iqNode.getAttribute('type'); |
90 if (type == 'set') { | 95 if (type == 'set') { |
91 var id = iqNode.getAttribute('id'); | 96 var id = iqNode.getAttribute('id'); |
92 iqNode.setAttribute('id', 'x' + id); | 97 iqNode.setAttribute('id', 'x' + id); |
93 message = (new XMLSerializer()).serializeToString(iqNode); | 98 message = (new XMLSerializer()).serializeToString(iqNode); |
94 } | 99 } |
95 | 100 |
96 // Send the stanza. | 101 // Send the stanza. |
97 remoting.wcsSandbox.sendIq(message); | 102 remoting.wcsSandbox.sendIq(message); |
98 } | 103 }; |
| 104 |
| 105 /** |
| 106 * @param {remoting.LogToServer} logToServer The LogToServer instance for the |
| 107 * connection. |
| 108 */ |
| 109 remoting.WcsAdapter.prototype.sendConnectionSetupResults = |
| 110 function(logToServer) { |
| 111 }; |
99 | 112 |
100 /** @param {string} jid */ | 113 /** @param {string} jid */ |
101 remoting.WcsAdapter.prototype.onWcsConnected_ = function(jid) { | 114 remoting.WcsAdapter.prototype.onWcsConnected_ = function(jid) { |
102 this.jid_ = jid; | 115 this.jid_ = jid; |
103 this.setState_(remoting.SignalStrategy.State.CONNECTED); | 116 this.setState_(remoting.SignalStrategy.State.CONNECTED); |
104 } | 117 }; |
105 | 118 |
106 /** @param {string} stanza */ | 119 /** @param {string} stanza */ |
107 remoting.WcsAdapter.prototype.onIncomingStanza_ = function(stanza) { | 120 remoting.WcsAdapter.prototype.onIncomingStanza_ = function(stanza) { |
108 var parser = new DOMParser(); | 121 var parser = new DOMParser(); |
109 var parsed = parser.parseFromString(stanza, 'text/xml').firstChild; | 122 var parsed = parser.parseFromString(stanza, 'text/xml').firstChild; |
110 | 123 |
111 // HACK: Remove 'x' prefix added to the id in sendMessage(). | 124 // HACK: Remove 'x' prefix added to the id in sendMessage(). |
112 try { | 125 try { |
113 var type = parsed.getAttribute('type'); | 126 var type = parsed.getAttribute('type'); |
114 var id = parsed.getAttribute('id'); | 127 var id = parsed.getAttribute('id'); |
115 if (type != 'set' && id.charAt(0) == 'x') { | 128 if (type != 'set' && id.charAt(0) == 'x') { |
116 parsed.setAttribute('id', id.substr(1)); | 129 parsed.setAttribute('id', id.substr(1)); |
117 } | 130 } |
118 } catch (err) { | 131 } catch (err) { |
119 // Pass message as is when it is malformed. | 132 // Pass message as is when it is malformed. |
120 } | 133 } |
121 | 134 |
122 if (this.onIncomingStanzaCallback_) { | 135 if (this.onIncomingStanzaCallback_) { |
123 this.onIncomingStanzaCallback_(parsed); | 136 this.onIncomingStanzaCallback_(parsed); |
124 } | 137 } |
125 } | 138 }; |
126 | 139 |
127 /** @param {remoting.Error} error */ | 140 /** @param {remoting.Error} error */ |
128 remoting.WcsAdapter.prototype.onError_ = function(error) { | 141 remoting.WcsAdapter.prototype.onError_ = function(error) { |
129 this.error_ = error; | 142 this.error_ = error; |
130 this.setState_(remoting.SignalStrategy.State.FAILED); | 143 this.setState_(remoting.SignalStrategy.State.FAILED); |
131 } | 144 }; |
132 | 145 |
133 /** | 146 /** |
134 * @param {remoting.SignalStrategy.State} newState | 147 * @param {remoting.SignalStrategy.State} newState |
135 * @private | 148 * @private |
136 */ | 149 */ |
137 remoting.WcsAdapter.prototype.setState_ = function(newState) { | 150 remoting.WcsAdapter.prototype.setState_ = function(newState) { |
138 if (this.state_ != newState) { | 151 if (this.state_ != newState) { |
139 this.state_ = newState; | 152 this.state_ = newState; |
140 this.onStateChangedCallback_(this.state_); | 153 this.onStateChangedCallback_(this.state_); |
141 } | 154 } |
142 }; | 155 }; |
OLD | NEW |