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

Side by Side Diff: remoting/webapp/crd/js/host_controller.js

Issue 1003433002: Updated remoting.xhr API to use promises. Removed access to the native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@spy-promise
Patch Set: Created 5 years, 9 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
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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 /** 151 /**
152 * @param {string} hostName 152 * @param {string} hostName
153 * @param {string} publicKey 153 * @param {string} publicKey
154 * @param {remoting.HostController.AsyncResult} result 154 * @param {remoting.HostController.AsyncResult} result
155 */ 155 */
156 function onStarted(hostName, publicKey, result) { 156 function onStarted(hostName, publicKey, result) {
157 if (result == remoting.HostController.AsyncResult.OK) { 157 if (result == remoting.HostController.AsyncResult.OK) {
158 remoting.hostList.onLocalHostStarted(hostName, newHostId, publicKey); 158 remoting.hostList.onLocalHostStarted(hostName, newHostId, publicKey);
159 onDone(); 159 onDone();
160 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { 160 } else if (result == remoting.HostController.AsyncResult.CANCELLED) {
161 onStartError(remoting.Error.CANCELLED); 161 onStartError(new remoting.Error(remoting.Error.Tag.CANCELLED));
162 } else { 162 } else {
163 onStartError(remoting.Error.UNEXPECTED); 163 onStartError(remoting.Error.unexpected());
164 } 164 }
165 } 165 }
166 166
167 /** 167 /**
168 * @param {string} hostName 168 * @param {string} hostName
169 * @param {string} publicKey 169 * @param {string} publicKey
170 * @param {string} privateKey 170 * @param {string} privateKey
171 * @param {?string} xmppLogin 171 * @param {?string} xmppLogin
172 * @param {?string} refreshToken 172 * @param {?string} refreshToken
173 * @param {?string} clientBaseJid 173 * @param {?string} clientBaseJid
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 that.getClientBaseJid_( 227 that.getClientBaseJid_(
228 onClientBaseJid.bind( 228 onClientBaseJid.bind(
229 null, hostName, publicKey, privateKey, email, refreshToken), 229 null, hostName, publicKey, privateKey, email, refreshToken),
230 onStartError); 230 onStartError);
231 } 231 }
232 232
233 /** 233 /**
234 * @param {string} hostName 234 * @param {string} hostName
235 * @param {string} publicKey 235 * @param {string} publicKey
236 * @param {string} privateKey 236 * @param {string} privateKey
237 * @param {XMLHttpRequest} xhr 237 * @param {!remoting.Xhr.Response} response
238 */ 238 */
239 function onRegistered( 239 function onRegistered(
240 hostName, publicKey, privateKey, xhr) { 240 hostName, publicKey, privateKey, response) {
241 var success = (xhr.status == 200); 241 var success = (response.status == 200);
242 242
243 if (success) { 243 if (success) {
244 var result = base.jsonParseSafe(xhr.responseText); 244 var result = base.jsonParseSafe(response.getText());
245 if ('data' in result && 'authorizationCode' in result['data']) { 245 if ('data' in result && 'authorizationCode' in result['data']) {
246 that.hostDaemonFacade_.getCredentialsFromAuthCode( 246 that.hostDaemonFacade_.getCredentialsFromAuthCode(
247 result['data']['authorizationCode'], 247 result['data']['authorizationCode'],
248 onServiceAccountCredentials.bind( 248 onServiceAccountCredentials.bind(
249 null, hostName, publicKey, privateKey), 249 null, hostName, publicKey, privateKey),
250 onError); 250 onError);
251 } else { 251 } else {
252 // No authorization code returned, use regular user credential flow. 252 // No authorization code returned, use regular user credential flow.
253 remoting.identity.getEmail().then( 253 remoting.identity.getEmail().then(
254 function(/** string */ email) { 254 function(/** string */ email) {
255 that.hostDaemonFacade_.getPinHash( 255 that.hostDaemonFacade_.getPinHash(
256 newHostId, hostPin, startHostWithHash.bind( 256 newHostId, hostPin, startHostWithHash.bind(
257 null, hostName, publicKey, privateKey, 257 null, hostName, publicKey, privateKey,
258 email, remoting.oauth2.getRefreshToken(), email), 258 email, remoting.oauth2.getRefreshToken(), email),
259 onError); 259 onError);
260 }); 260 });
261 } 261 }
262 } else { 262 } else {
263 console.log('Failed to register the host. Status: ' + xhr.status + 263 console.log('Failed to register the host. Status: ' + response.status +
264 ' response: ' + xhr.responseText); 264 ' response: ' + response.getText());
265 onError(remoting.Error.REGISTRATION_FAILED); 265 onError(new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED));
266 } 266 }
267 } 267 }
268 268
269 /** 269 /**
270 * @param {string} hostName 270 * @param {string} hostName
271 * @param {string} privateKey 271 * @param {string} privateKey
272 * @param {string} publicKey 272 * @param {string} publicKey
273 * @param {?string} hostClientId 273 * @param {?string} hostClientId
274 * @param {string} oauthToken 274 * @param {string} oauthToken
275 */ 275 */
276 function doRegisterHost( 276 function doRegisterHost(
277 hostName, privateKey, publicKey, hostClientId, oauthToken) { 277 hostName, privateKey, publicKey, hostClientId, oauthToken) {
278 var newHostDetails = { data: { 278 var newHostDetails = { data: {
279 hostId: newHostId, 279 hostId: newHostId,
280 hostName: hostName, 280 hostName: hostName,
281 publicKey: publicKey 281 publicKey: publicKey
282 } }; 282 } };
283 283
284 remoting.xhr.start({ 284 new remoting.Xhr({
285 method: 'POST', 285 method: 'POST',
286 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', 286 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
287 urlParams: { 287 urlParams: {
288 hostClientId: hostClientId 288 hostClientId: hostClientId
289 }, 289 },
290 onDone: onRegistered.bind(null, hostName, publicKey, privateKey),
291 jsonContent: newHostDetails, 290 jsonContent: newHostDetails,
292 oauthToken: oauthToken 291 oauthToken: oauthToken
293 }); 292 }).start().then(onRegistered.bind(null, hostName, publicKey, privateKey));
294 } 293 }
295 294
296 /** 295 /**
297 * @param {string} hostName 296 * @param {string} hostName
298 * @param {string} privateKey 297 * @param {string} privateKey
299 * @param {string} publicKey 298 * @param {string} publicKey
300 * @param {string} hostClientId 299 * @param {string} hostClientId
301 */ 300 */
302 function onHostClientId( 301 function onHostClientId(
303 hostName, privateKey, publicKey, hostClientId) { 302 hostName, privateKey, publicKey, hostClientId) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 return; 366 return;
368 } 367 }
369 onDone(); 368 onDone();
370 } 369 }
371 370
372 /** @param {remoting.HostController.AsyncResult} result */ 371 /** @param {remoting.HostController.AsyncResult} result */
373 function onStopped(result) { 372 function onStopped(result) {
374 if (result == remoting.HostController.AsyncResult.OK) { 373 if (result == remoting.HostController.AsyncResult.OK) {
375 that.getLocalHostId(unregisterHost); 374 that.getLocalHostId(unregisterHost);
376 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { 375 } else if (result == remoting.HostController.AsyncResult.CANCELLED) {
377 onError(remoting.Error.CANCELLED); 376 onError(new remoting.Error(remoting.Error.Tag.CANCELLED));
378 } else { 377 } else {
379 onError(remoting.Error.UNEXPECTED); 378 onError(remoting.Error.unexpected());
380 } 379 }
381 } 380 }
382 381
383 this.hostDaemonFacade_.stopDaemon(onStopped, onError); 382 this.hostDaemonFacade_.stopDaemon(onStopped, onError);
384 }; 383 };
385 384
386 /** 385 /**
387 * Check the host configuration is valid (non-null, and contains both host_id 386 * Check the host configuration is valid (non-null, and contains both host_id
388 * and xmpp_login keys). 387 * and xmpp_login keys).
389 * @param {Object} config The host configuration. 388 * @param {Object} config The host configuration.
(...skipping 14 matching lines...) Expand all
404 remoting.HostController.prototype.updatePin = function(newPin, onDone, 403 remoting.HostController.prototype.updatePin = function(newPin, onDone,
405 onError) { 404 onError) {
406 /** @type {remoting.HostController} */ 405 /** @type {remoting.HostController} */
407 var that = this; 406 var that = this;
408 407
409 /** @param {remoting.HostController.AsyncResult} result */ 408 /** @param {remoting.HostController.AsyncResult} result */
410 function onConfigUpdated(result) { 409 function onConfigUpdated(result) {
411 if (result == remoting.HostController.AsyncResult.OK) { 410 if (result == remoting.HostController.AsyncResult.OK) {
412 onDone(); 411 onDone();
413 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { 412 } else if (result == remoting.HostController.AsyncResult.CANCELLED) {
414 onError(remoting.Error.CANCELLED); 413 onError(new remoting.Error(remoting.Error.Tag.CANCELLED));
415 } else { 414 } else {
416 onError(remoting.Error.UNEXPECTED); 415 onError(remoting.Error.unexpected());
417 } 416 }
418 } 417 }
419 418
420 /** @param {string} pinHash */ 419 /** @param {string} pinHash */
421 function updateDaemonConfigWithHash(pinHash) { 420 function updateDaemonConfigWithHash(pinHash) {
422 var newConfig = { 421 var newConfig = {
423 host_secret_hash: pinHash 422 host_secret_hash: pinHash
424 }; 423 };
425 that.hostDaemonFacade_.updateDaemonConfig(newConfig, onConfigUpdated, 424 that.hostDaemonFacade_.updateDaemonConfig(newConfig, onConfigUpdated,
426 onError); 425 onError);
427 } 426 }
428 427
429 /** @param {Object} config */ 428 /** @param {Object} config */
430 function onConfig(config) { 429 function onConfig(config) {
431 if (!isHostConfigValid_(config)) { 430 if (!isHostConfigValid_(config)) {
432 onError(remoting.Error.UNEXPECTED); 431 onError(remoting.Error.unexpected());
433 return; 432 return;
434 } 433 }
435 /** @type {string} */ 434 /** @type {string} */
436 var hostId = config['host_id']; 435 var hostId = config['host_id'];
437 that.hostDaemonFacade_.getPinHash( 436 that.hostDaemonFacade_.getPinHash(
438 hostId, newPin, updateDaemonConfigWithHash, onError); 437 hostId, newPin, updateDaemonConfigWithHash, onError);
439 } 438 }
440 439
441 // TODO(sergeyu): When crbug.com/121518 is fixed: replace this call 440 // TODO(sergeyu): When crbug.com/121518 is fixed: replace this call
442 // with an unprivileged version if that is necessary. 441 // with an unprivileged version if that is necessary.
443 this.hostDaemonFacade_.getDaemonConfig(onConfig, onError); 442 this.hostDaemonFacade_.getDaemonConfig(onConfig, onError);
444 }; 443 };
445 444
446 /** 445 /**
447 * Get the state of the local host. 446 * Get the state of the local host.
448 * 447 *
449 * @param {function(remoting.HostController.State):void} onDone Completion 448 * @param {function(remoting.HostController.State):void} onDone Completion
450 * callback. 449 * callback.
451 */ 450 */
452 remoting.HostController.prototype.getLocalHostState = function(onDone) { 451 remoting.HostController.prototype.getLocalHostState = function(onDone) {
453 /** @param {!remoting.Error} error */ 452 /** @param {!remoting.Error} error */
454 function onError(error) { 453 function onError(error) {
455 onDone((error.tag == remoting.Error.Tag.MISSING_PLUGIN) ? 454 onDone((error.hasTag(remoting.Error.Tag.MISSING_PLUGIN)) ?
456 remoting.HostController.State.NOT_INSTALLED : 455 remoting.HostController.State.NOT_INSTALLED :
457 remoting.HostController.State.UNKNOWN); 456 remoting.HostController.State.UNKNOWN);
458 } 457 }
459 this.hostDaemonFacade_.getDaemonState(onDone, onError); 458 this.hostDaemonFacade_.getDaemonState(onDone, onError);
460 }; 459 };
461 460
462 /** 461 /**
463 * Get the id of the local host, or null if it is not registered. 462 * Get the id of the local host, or null if it is not registered.
464 * 463 *
465 * @param {function(string?):void} onDone Completion callback. 464 * @param {function(string?):void} onDone Completion callback.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 function connectSignalingWithTokenAndEmail(token, email) { 567 function connectSignalingWithTokenAndEmail(token, email) {
569 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); 568 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token);
570 } 569 }
571 570
572 remoting.identity.getToken().then( 571 remoting.identity.getToken().then(
573 connectSignalingWithToken, remoting.Error.handler(onError)); 572 connectSignalingWithToken, remoting.Error.handler(onError));
574 }; 573 };
575 574
576 /** @type {remoting.HostController} */ 575 /** @type {remoting.HostController} */
577 remoting.hostController = null; 576 remoting.hostController = null;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698