OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** @constructor */ | 10 /** @constructor */ |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 131 |
132 /** | 132 /** |
133 * @param {string} hostName | 133 * @param {string} hostName |
134 * @param {string} publicKey | 134 * @param {string} publicKey |
135 * @param {string} privateKey | 135 * @param {string} privateKey |
136 * @param {XMLHttpRequest} xhr | 136 * @param {XMLHttpRequest} xhr |
137 * @param {string} hostSecretHash | 137 * @param {string} hostSecretHash |
138 */ | 138 */ |
139 function startHostWithHash(hostName, publicKey, privateKey, xhr, | 139 function startHostWithHash(hostName, publicKey, privateKey, xhr, |
140 hostSecretHash) { | 140 hostSecretHash) { |
141 var hostConfig = JSON.stringify({ | 141 var hostConfig = { |
142 xmpp_login: remoting.identity.getCachedEmail(), | 142 xmpp_login: remoting.identity.getCachedEmail(), |
143 oauth_refresh_token: remoting.oauth2.exportRefreshToken(), | 143 oauth_refresh_token: remoting.oauth2.exportRefreshToken(), |
144 host_id: newHostId, | 144 host_id: newHostId, |
145 host_name: hostName, | 145 host_name: hostName, |
146 host_secret_hash: hostSecretHash, | 146 host_secret_hash: hostSecretHash, |
147 private_key: privateKey | 147 private_key: privateKey |
148 }); | 148 }; |
149 /** @param {remoting.HostController.AsyncResult} result */ | 149 /** @param {remoting.HostController.AsyncResult} result */ |
150 var onStartDaemon = function(result) { | 150 var onStartDaemon = function(result) { |
151 onStarted(callback, result, hostName, publicKey); | 151 onStarted(callback, result, hostName, publicKey); |
152 }; | 152 }; |
153 that.hostDispatcher_.startDaemon(hostConfig, consent, onStartDaemon); | 153 that.hostDispatcher_.startDaemon(hostConfig, consent, onStartDaemon); |
154 } | 154 } |
155 | 155 |
156 /** | 156 /** |
157 * @param {string} hostName | 157 * @param {string} hostName |
158 * @param {string} privateKey | 158 * @param {string} privateKey |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 callback(result); | 238 callback(result); |
239 return; | 239 return; |
240 } | 240 } |
241 that.getLocalHostId(unregisterHost.bind(null, result)); | 241 that.getLocalHostId(unregisterHost.bind(null, result)); |
242 }; | 242 }; |
243 | 243 |
244 this.hostDispatcher_.stopDaemon(onStopped); | 244 this.hostDispatcher_.stopDaemon(onStopped); |
245 }; | 245 }; |
246 | 246 |
247 /** | 247 /** |
248 * Parse a stringified host configuration and return it as a dictionary if it | 248 * Check the host configuration is valid (non-null, and contains both host_id |
249 * is well-formed and contains both host_id and xmpp_login keys. null is | 249 * and xmpp_login keys). |
250 * returned if either key is missing, or if the configuration is corrupt. | 250 * @param {Object} config The host configuration. |
251 * @param {string} configStr The host configuration, JSON encoded to a string. | 251 * @return {boolean} True if it is valid. |
252 * @return {Object.<string,string>|null} The host configuration. | |
253 */ | 252 */ |
254 function parseHostConfig_(configStr) { | 253 function isHostConfigValid_(config) { |
255 var config = /** @type {Object.<string,string>} */ jsonParseSafe(configStr); | 254 return !!config && typeof config['host_id'] == 'string' && |
256 if (config && | 255 typeof config['xmpp_login'] == 'string'; |
257 typeof config['host_id'] == 'string' && | |
258 typeof config['xmpp_login'] == 'string') { | |
259 return config; | |
260 } else { | |
261 // {} means that host is not configured; '' means that the config file could | |
262 // not be read. | |
263 // TODO(jamiewalch): '' is expected if the host isn't installed, but should | |
264 // be reported as an error otherwise. Fix this once we have an event-based | |
265 // daemon state mechanism. | |
266 if (configStr != '{}' && configStr != '') { | |
267 console.error('Invalid getDaemonConfig response.'); | |
268 } | |
269 } | |
270 return null; | |
271 } | 256 } |
272 | 257 |
273 /** | 258 /** |
274 * @param {string} newPin The new PIN to set | 259 * @param {string} newPin The new PIN to set |
275 * @param {function(remoting.HostController.AsyncResult):void} callback | 260 * @param {function(remoting.HostController.AsyncResult):void} callback |
276 * Callback to be called when finished. | 261 * Callback to be called when finished. |
277 * @return {void} Nothing. | 262 * @return {void} Nothing. |
278 */ | 263 */ |
279 remoting.HostController.prototype.updatePin = function(newPin, callback) { | 264 remoting.HostController.prototype.updatePin = function(newPin, callback) { |
280 /** @type {remoting.HostController} */ | 265 /** @type {remoting.HostController} */ |
281 var that = this; | 266 var that = this; |
282 | 267 |
283 /** @param {string} configStr */ | 268 /** @param {Object} config */ |
284 function onConfig(configStr) { | 269 function onConfig(config) { |
285 var config = parseHostConfig_(configStr); | 270 if (!isHostConfigValid_(config)) { |
286 if (!config) { | |
287 callback(remoting.HostController.AsyncResult.FAILED); | 271 callback(remoting.HostController.AsyncResult.FAILED); |
288 return; | 272 return; |
289 } | 273 } |
| 274 /** @type {string} */ |
290 var hostId = config['host_id']; | 275 var hostId = config['host_id']; |
291 that.hostDispatcher_.getPinHash(hostId, newPin, updateDaemonConfigWithHash); | 276 that.hostDispatcher_.getPinHash(hostId, newPin, updateDaemonConfigWithHash); |
292 } | 277 } |
293 | 278 |
294 /** @param {string} pinHash */ | 279 /** @param {string} pinHash */ |
295 function updateDaemonConfigWithHash(pinHash) { | 280 function updateDaemonConfigWithHash(pinHash) { |
296 var newConfig = JSON.stringify({ | 281 var newConfig = { |
297 host_secret_hash: pinHash | 282 host_secret_hash: pinHash |
298 }); | 283 }; |
299 that.hostDispatcher_.updateDaemonConfig(newConfig, callback); | 284 that.hostDispatcher_.updateDaemonConfig(newConfig, callback); |
300 } | 285 } |
301 | 286 |
302 // TODO(sergeyu): When crbug.com/121518 is fixed: replace this call | 287 // TODO(sergeyu): When crbug.com/121518 is fixed: replace this call |
303 // with an upriveleged version if that is necessary. | 288 // with an unprivileged version if that is necessary. |
304 this.hostDispatcher_.getDaemonConfig(onConfig); | 289 this.hostDispatcher_.getDaemonConfig(onConfig); |
305 }; | 290 }; |
306 | 291 |
307 /** | 292 /** |
308 * Get the state of the local host. | 293 * Get the state of the local host. |
309 * | 294 * |
310 * @param {function(remoting.HostController.State):void} onDone | 295 * @param {function(remoting.HostController.State):void} onDone |
311 * Completion callback. | 296 * Completion callback. |
312 */ | 297 */ |
313 remoting.HostController.prototype.getLocalHostState = function(onDone) { | 298 remoting.HostController.prototype.getLocalHostState = function(onDone) { |
314 this.hostDispatcher_.getDaemonState(onDone); | 299 this.hostDispatcher_.getDaemonState(onDone); |
315 }; | 300 }; |
316 | 301 |
317 /** | 302 /** |
318 * Get the id of the local host, or null if it is not registered. | 303 * Get the id of the local host, or null if it is not registered. |
319 * | 304 * |
320 * @param {function(string?):void} onDone Completion callback. | 305 * @param {function(string?):void} onDone Completion callback. |
321 */ | 306 */ |
322 remoting.HostController.prototype.getLocalHostId = function(onDone) { | 307 remoting.HostController.prototype.getLocalHostId = function(onDone) { |
323 /** @type {remoting.HostController} */ | 308 /** @type {remoting.HostController} */ |
324 var that = this; | 309 var that = this; |
325 /** @param {string} configStr */ | 310 /** @param {Object} config */ |
326 function onConfig(configStr) { | 311 function onConfig(config) { |
327 var config = parseHostConfig_(configStr); | |
328 var hostId = null; | 312 var hostId = null; |
329 if (config) { | 313 if (isHostConfigValid_(config)) { |
330 hostId = config['host_id']; | 314 hostId = /** @type {string} */ config['host_id']; |
331 } | 315 } |
332 onDone(hostId); | 316 onDone(hostId); |
333 }; | 317 }; |
334 try { | 318 try { |
335 this.hostDispatcher_.getDaemonConfig(onConfig); | 319 this.hostDispatcher_.getDaemonConfig(onConfig); |
336 } catch (err) { | 320 } catch (err) { |
337 onDone(null); | 321 onDone(null); |
338 } | 322 } |
339 }; | 323 }; |
340 | 324 |
341 /** @type {remoting.HostController} */ | 325 /** @type {remoting.HostController} */ |
342 remoting.hostController = null; | 326 remoting.hostController = null; |
OLD | NEW |