Chromium Code Reviews| 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); | |
| 256 if (config && | 254 if (config && |
| 257 typeof config['host_id'] == 'string' && | 255 typeof config['host_id'] == 'string' && |
| 258 typeof config['xmpp_login'] == 'string') { | 256 typeof config['xmpp_login'] == 'string') { |
| 259 return config; | 257 return true; |
| 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 } | 258 } |
| 270 return null; | 259 return false; |
|
Jamie
2013/05/22 00:54:21
You can just replace "if" with "return" and simpli
Lambros
2013/05/23 00:22:48
Done, using the !! trick :)
| |
| 271 } | 260 } |
| 272 | 261 |
| 273 /** | 262 /** |
| 274 * @param {string} newPin The new PIN to set | 263 * @param {string} newPin The new PIN to set |
| 275 * @param {function(remoting.HostController.AsyncResult):void} callback | 264 * @param {function(remoting.HostController.AsyncResult):void} callback |
| 276 * Callback to be called when finished. | 265 * Callback to be called when finished. |
| 277 * @return {void} Nothing. | 266 * @return {void} Nothing. |
| 278 */ | 267 */ |
| 279 remoting.HostController.prototype.updatePin = function(newPin, callback) { | 268 remoting.HostController.prototype.updatePin = function(newPin, callback) { |
| 280 /** @type {remoting.HostController} */ | 269 /** @type {remoting.HostController} */ |
| 281 var that = this; | 270 var that = this; |
| 282 | 271 |
| 283 /** @param {string} configStr */ | 272 /** @param {Object} config */ |
| 284 function onConfig(configStr) { | 273 function onConfig(config) { |
| 285 var config = parseHostConfig_(configStr); | 274 if (!isHostConfigValid_(config)) { |
| 286 if (!config) { | |
| 287 callback(remoting.HostController.AsyncResult.FAILED); | 275 callback(remoting.HostController.AsyncResult.FAILED); |
| 288 return; | 276 return; |
| 289 } | 277 } |
| 278 /** @type {string} */ | |
| 290 var hostId = config['host_id']; | 279 var hostId = config['host_id']; |
| 291 that.hostDispatcher_.getPinHash(hostId, newPin, updateDaemonConfigWithHash); | 280 that.hostDispatcher_.getPinHash(hostId, newPin, updateDaemonConfigWithHash); |
| 292 } | 281 } |
| 293 | 282 |
| 294 /** @param {string} pinHash */ | 283 /** @param {string} pinHash */ |
| 295 function updateDaemonConfigWithHash(pinHash) { | 284 function updateDaemonConfigWithHash(pinHash) { |
| 296 var newConfig = JSON.stringify({ | 285 var newConfig = { |
| 297 host_secret_hash: pinHash | 286 host_secret_hash: pinHash |
| 298 }); | 287 }; |
| 299 that.hostDispatcher_.updateDaemonConfig(newConfig, callback); | 288 that.hostDispatcher_.updateDaemonConfig(newConfig, callback); |
| 300 } | 289 } |
| 301 | 290 |
| 302 // TODO(sergeyu): When crbug.com/121518 is fixed: replace this call | 291 // TODO(sergeyu): When crbug.com/121518 is fixed: replace this call |
| 303 // with an upriveleged version if that is necessary. | 292 // with an unprivileged version if that is necessary. |
| 304 this.hostDispatcher_.getDaemonConfig(onConfig); | 293 this.hostDispatcher_.getDaemonConfig(onConfig); |
| 305 }; | 294 }; |
| 306 | 295 |
| 307 /** | 296 /** |
| 308 * Get the state of the local host. | 297 * Get the state of the local host. |
| 309 * | 298 * |
| 310 * @param {function(remoting.HostController.State):void} onDone | 299 * @param {function(remoting.HostController.State):void} onDone |
| 311 * Completion callback. | 300 * Completion callback. |
| 312 */ | 301 */ |
| 313 remoting.HostController.prototype.getLocalHostState = function(onDone) { | 302 remoting.HostController.prototype.getLocalHostState = function(onDone) { |
| 314 this.hostDispatcher_.getDaemonState(onDone); | 303 this.hostDispatcher_.getDaemonState(onDone); |
| 315 }; | 304 }; |
| 316 | 305 |
| 317 /** | 306 /** |
| 318 * Get the id of the local host, or null if it is not registered. | 307 * Get the id of the local host, or null if it is not registered. |
| 319 * | 308 * |
| 320 * @param {function(string?):void} onDone Completion callback. | 309 * @param {function(string?):void} onDone Completion callback. |
| 321 */ | 310 */ |
| 322 remoting.HostController.prototype.getLocalHostId = function(onDone) { | 311 remoting.HostController.prototype.getLocalHostId = function(onDone) { |
| 323 /** @type {remoting.HostController} */ | 312 /** @type {remoting.HostController} */ |
| 324 var that = this; | 313 var that = this; |
| 325 /** @param {string} configStr */ | 314 /** @param {Object} config */ |
| 326 function onConfig(configStr) { | 315 function onConfig(config) { |
| 327 var config = parseHostConfig_(configStr); | |
| 328 var hostId = null; | 316 var hostId = null; |
| 329 if (config) { | 317 if (isHostConfigValid_(config)) { |
| 330 hostId = config['host_id']; | 318 hostId = /** @type {string} */ config['host_id']; |
| 331 } | 319 } |
| 332 onDone(hostId); | 320 onDone(hostId); |
| 333 }; | 321 }; |
| 334 try { | 322 try { |
| 335 this.hostDispatcher_.getDaemonConfig(onConfig); | 323 this.hostDispatcher_.getDaemonConfig(onConfig); |
| 336 } catch (err) { | 324 } catch (err) { |
| 337 onDone(null); | 325 onDone(null); |
| 338 } | 326 } |
| 339 }; | 327 }; |
| 340 | 328 |
| 341 /** @type {remoting.HostController} */ | 329 /** @type {remoting.HostController} */ |
| 342 remoting.hostController = null; | 330 remoting.hostController = null; |
| OLD | NEW |