OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 /** | |
6 * @fileoverview | |
7 * Connect set-up state machine for Me2Me and IT2Me | |
8 */ | |
9 | |
10 'use strict'; | |
11 | |
12 /** @suppress {duplicate} */ | |
13 var remoting = remoting || {}; | |
14 | |
15 /** | |
16 * @param {HTMLElement} clientContainer Container element for the client view. | |
17 * @param {Array<string>} requiredCapabilities Connector capabilities | |
18 * required by this application. | |
19 * @param {remoting.ClientSession.EventHandler} handler | |
20 * @constructor | |
21 * @implements {remoting.SessionConnector} | |
22 */ | |
23 remoting.SessionConnectorImpl = | |
24 function(clientContainer, requiredCapabilities, handler) { | |
25 /** @private {HTMLElement} */ | |
26 this.clientContainer_ = clientContainer; | |
27 | |
28 /** @private */ | |
29 this.onError_ = handler.onError.bind(handler); | |
30 | |
31 /** @private */ | |
32 this.handler_ = handler; | |
33 | |
34 /** @private {Array<string>} */ | |
35 this.requiredCapabilities_ = [ | |
36 remoting.ClientSession.Capability.SEND_INITIAL_RESOLUTION, | |
37 remoting.ClientSession.Capability.RATE_LIMIT_RESIZE_REQUESTS, | |
38 remoting.ClientSession.Capability.VIDEO_RECORDER | |
39 ]; | |
40 | |
41 // Append the app-specific capabilities. | |
42 this.requiredCapabilities_.push.apply(this.requiredCapabilities_, | |
43 requiredCapabilities); | |
44 | |
45 // Initialize/declare per-connection state. | |
46 this.closeSession_(); | |
47 }; | |
48 | |
49 /** | |
50 * Reset the per-connection state so that the object can be re-used for a | |
51 * second connection. Note the none of the shared WCS state is reset. | |
52 * @private | |
53 */ | |
54 remoting.SessionConnectorImpl.prototype.closeSession_ = function() { | |
55 // It's OK to initialize these member variables here because the | |
56 // constructor calls this method. | |
57 | |
58 base.dispose(this.eventHook_); | |
59 /** @private {base.Disposable} */ | |
60 this.eventHook_ = null; | |
61 | |
62 /** @private {remoting.Host} */ | |
63 this.host_ = null; | |
64 | |
65 /** @private {boolean} */ | |
66 this.logHostOfflineErrors_ = false; | |
67 | |
68 base.dispose(this.clientSession_); | |
69 /** @private {remoting.ClientSession} */ | |
70 this.clientSession_ = null; | |
71 | |
72 base.dispose(this.plugin_); | |
73 /** @private {remoting.ClientPlugin} */ | |
74 this.plugin_ = null; | |
75 | |
76 /** @private {remoting.CredentialsProvider} */ | |
77 this.credentialsProvider_ = null; | |
78 | |
79 base.dispose(this.signalStrategy_); | |
80 /** @private {remoting.SignalStrategy} */ | |
81 this.signalStrategy_ = null; | |
82 }; | |
83 | |
84 /** | |
85 * Initiates a connection. | |
86 * | |
87 * @param {remoting.Application.Mode} mode | |
88 * @param {remoting.Host} host the Host to connect to. | |
89 * @param {remoting.CredentialsProvider} credentialsProvider | |
90 * @param {boolean=} opt_suppressOfflineError | |
91 * @return {void} Nothing. | |
92 * @private | |
93 */ | |
94 remoting.SessionConnectorImpl.prototype.connect = | |
95 function(mode, host, credentialsProvider, opt_suppressOfflineError) { | |
96 // In some circumstances, the WCS <iframe> can get reloaded, which results | |
97 // in a new clientJid and a new callback. In this case, cancel any existing | |
98 // connect operation and remove the old client plugin before instantiating a | |
99 // new one. | |
100 this.closeSession_(); | |
101 remoting.app.setConnectionMode(mode); | |
102 this.host_ = host; | |
103 this.credentialsProvider_ = credentialsProvider; | |
104 this.logHostOfflineErrors_ = !Boolean(opt_suppressOfflineError); | |
105 this.connectSignaling_(); | |
106 }; | |
107 | |
108 /** | |
109 * @private | |
110 */ | |
111 remoting.SessionConnectorImpl.prototype.connectSignaling_ = function() { | |
112 base.dispose(this.signalStrategy_); | |
113 this.signalStrategy_ = null; | |
114 | |
115 /** @type {remoting.SessionConnectorImpl} */ | |
116 var that = this; | |
117 | |
118 /** @param {string} token */ | |
119 function connectSignalingWithToken(token) { | |
120 remoting.identity.getUserInfo().then( | |
121 connectSignalingWithTokenAndUserInfo.bind(null, token), | |
122 remoting.Error.handler(that.onError_)); | |
123 } | |
124 | |
125 /** | |
126 * Success callback for when the email and fullName have been retrieved | |
127 * for this user. | |
128 * Note that the full name will be null unless the webapp has requested | |
129 * and been granted the userinfo.profile permission. | |
130 * | |
131 * @param {string} token | |
132 * @param {{email: string, name: string}} userInfo | |
133 */ | |
134 function connectSignalingWithTokenAndUserInfo(token, userInfo) { | |
135 that.signalStrategy_.connect(remoting.settings.XMPP_SERVER, userInfo.email, | |
136 token); | |
137 } | |
138 | |
139 this.signalStrategy_ = remoting.SignalStrategy.create(); | |
140 this.signalStrategy_.setStateChangedCallback( | |
141 this.onSignalingState_.bind(this)); | |
142 | |
143 remoting.identity.getToken().then( | |
144 connectSignalingWithToken, | |
145 remoting.Error.handler(this.onError_)); | |
146 }; | |
147 | |
148 /** | |
149 * @private | |
150 * @param {remoting.SignalStrategy.State} state | |
151 */ | |
152 remoting.SessionConnectorImpl.prototype.onSignalingState_ = function(state) { | |
153 switch (state) { | |
154 case remoting.SignalStrategy.State.CONNECTED: | |
155 // Proceed only if the connection hasn't been canceled. | |
156 if (this.host_.jabberId) { | |
157 this.createSession_(); | |
158 } | |
159 break; | |
160 | |
161 case remoting.SignalStrategy.State.FAILED: | |
162 this.onError_(this.signalStrategy_.getError()); | |
163 break; | |
164 } | |
165 }; | |
166 | |
167 /** | |
168 * Creates ClientSession object. | |
169 */ | |
170 remoting.SessionConnectorImpl.prototype.createSession_ = function() { | |
171 var pluginContainer = this.clientContainer_.querySelector( | |
172 '.client-plugin-container'); | |
173 | |
174 this.plugin_ = remoting.ClientPlugin.factory.createPlugin( | |
175 pluginContainer, this.requiredCapabilities_); | |
176 | |
177 var that = this; | |
178 this.host_.options.load().then(function(){ | |
179 that.plugin_.initialize(that.onPluginInitialized_.bind(that)); | |
180 }); | |
181 }; | |
182 | |
183 /** | |
184 * @param {boolean} initialized | |
185 * @private | |
186 */ | |
187 remoting.SessionConnectorImpl.prototype.onPluginInitialized_ = function( | |
188 initialized) { | |
189 if (!initialized) { | |
190 console.error('ERROR: remoting plugin not loaded'); | |
191 this.pluginError_(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN)); | |
192 return; | |
193 } | |
194 | |
195 if (!this.plugin_.isSupportedVersion()) { | |
196 console.error('ERROR: bad plugin version'); | |
197 this.pluginError_(new remoting.Error( | |
198 remoting.Error.Tag.BAD_PLUGIN_VERSION)); | |
199 return; | |
200 } | |
201 | |
202 this.clientSession_ = new remoting.ClientSession( | |
203 this.plugin_, this.host_, this.signalStrategy_); | |
204 | |
205 this.clientSession_.logHostOfflineErrors(this.logHostOfflineErrors_); | |
206 this.eventHook_ = new base.EventHook( | |
207 this.clientSession_, 'stateChanged', this.onStateChange_.bind(this)); | |
208 this.plugin_.connect( | |
209 this.host_, this.signalStrategy_.getJid(), this.credentialsProvider_); | |
210 }; | |
211 | |
212 /** | |
213 * @param {!remoting.Error} error | |
214 * @private | |
215 */ | |
216 remoting.SessionConnectorImpl.prototype.pluginError_ = function(error) { | |
217 this.signalStrategy_.setIncomingStanzaCallback(null); | |
218 this.closeSession_(); | |
219 }; | |
220 | |
221 /** | |
222 * Handle a change in the state of the client session. | |
223 * | |
224 * @param {remoting.ClientSession.StateEvent=} event | |
225 * @return {void} Nothing. | |
226 * @private | |
227 */ | |
228 remoting.SessionConnectorImpl.prototype.onStateChange_ = function(event) { | |
229 switch (event.current) { | |
230 case remoting.ClientSession.State.CONNECTED: | |
231 var connectionInfo = new remoting.ConnectionInfo( | |
232 this.host_, this.credentialsProvider_, this.clientSession_, | |
233 this.plugin_); | |
234 this.handler_.onConnected(connectionInfo); | |
235 break; | |
236 | |
237 case remoting.ClientSession.State.CONNECTING: | |
238 remoting.identity.getEmail().then(function(/** string */ email) { | |
239 console.log('Connecting as ' + email); | |
240 }); | |
241 break; | |
242 | |
243 case remoting.ClientSession.State.AUTHENTICATED: | |
244 console.log('Connection authenticated'); | |
245 break; | |
246 | |
247 case remoting.ClientSession.State.INITIALIZING: | |
248 console.log('Initializing connection'); | |
249 break; | |
250 | |
251 case remoting.ClientSession.State.CLOSED: | |
252 this.handler_.onDisconnected(); | |
253 break; | |
254 | |
255 case remoting.ClientSession.State.FAILED: | |
256 var error = this.clientSession_.getError(); | |
257 console.error('Client plugin reported connection failed: ' + | |
258 error.toString()); | |
259 this.handler_.onConnectionFailed(error || remoting.Error.unexpected()); | |
260 break; | |
261 | |
262 default: | |
263 console.error('Unexpected client plugin state: ' + event.current); | |
264 // This should only happen if the web-app and client plugin get out of | |
265 // sync, and even then the version check should ensure compatibility. | |
266 this.onError_(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN)); | |
267 } | |
268 | |
269 if (this.clientSession_.isFinished()) { | |
270 this.closeSession_(); | |
271 } | |
272 }; | |
273 | |
274 /** | |
275 * @constructor | |
276 * @implements {remoting.SessionConnectorFactory} | |
277 */ | |
278 remoting.DefaultSessionConnectorFactory = function() {}; | |
279 | |
280 /** | |
281 * @param {HTMLElement} clientContainer Container element for the client view. | |
282 * @param {Array<string>} requiredCapabilities Connector capabilities | |
283 * required by this application. | |
284 * @param {remoting.ClientSession.EventHandler} handler | |
285 * @return {remoting.SessionConnector} | |
286 */ | |
287 remoting.DefaultSessionConnectorFactory.prototype.createConnector = | |
288 function(clientContainer, requiredCapabilities, handler) { | |
289 return new remoting.SessionConnectorImpl(clientContainer, | |
290 requiredCapabilities, handler); | |
291 }; | |
OLD | NEW |