| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * REST API for host-list management. | |
| 8 */ | |
| 9 | |
| 10 /** @suppress {duplicate} */ | |
| 11 var remoting = remoting || {}; | |
| 12 | |
| 13 (function() { | |
| 14 | |
| 15 'use strict'; | |
| 16 | |
| 17 /** | |
| 18 * @constructor | |
| 19 * @implements {remoting.HostListApi} | |
| 20 */ | |
| 21 remoting.HostListApiGcdImpl = function() { | |
| 22 this.gcd_ = new remoting.gcd.Client({ | |
| 23 apiKey: remoting.settings.GOOGLE_API_KEY | |
| 24 }); | |
| 25 }; | |
| 26 | |
| 27 /** @override */ | |
| 28 remoting.HostListApiGcdImpl.prototype.register = function( | |
| 29 newHostId, hostName, publicKey, hostClientId) { | |
| 30 var self = this; | |
| 31 var deviceDraft = { | |
| 32 channel: { | |
| 33 supportedType: 'xmpp' | |
| 34 }, | |
| 35 deviceKind: 'vendor', | |
| 36 name: newHostId, | |
| 37 displayName: hostName, | |
| 38 state: { | |
| 39 'publicKey': publicKey | |
| 40 } | |
| 41 }; | |
| 42 | |
| 43 return /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ ( | |
| 44 this.gcd_.insertRegistrationTicket(). | |
| 45 then(function(ticket) { | |
| 46 return self.gcd_.patchRegistrationTicket( | |
| 47 ticket.id, deviceDraft, hostClientId); | |
| 48 }). | |
| 49 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { | |
| 50 return self.gcd_.finalizeRegistrationTicket(ticket.id); | |
| 51 }). | |
| 52 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { | |
| 53 return { | |
| 54 authCode: ticket.robotAccountAuthorizationCode, | |
| 55 email: ticket.robotAccountEmail, | |
| 56 gcdId: ticket.deviceId | |
| 57 }; | |
| 58 }). | |
| 59 catch(function(error) { | |
| 60 console.error('Error registering device with GCD: ' + error); | |
| 61 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | |
| 62 })); | |
| 63 }; | |
| 64 | |
| 65 /** @override */ | |
| 66 remoting.HostListApiGcdImpl.prototype.get = function() { | |
| 67 return this.gcd_.listDevices(). | |
| 68 then(function(devices) { | |
| 69 var hosts = []; | |
| 70 devices.forEach(function(device) { | |
| 71 try { | |
| 72 hosts.push(deviceToHost(device)); | |
| 73 } catch (/** @type {*} */ error) { | |
| 74 console.warn('Invalid device spec:', error); | |
| 75 } | |
| 76 }); | |
| 77 return hosts; | |
| 78 }); | |
| 79 }; | |
| 80 | |
| 81 /** @override */ | |
| 82 remoting.HostListApiGcdImpl.prototype.put = | |
| 83 function(hostId, hostName, hostPublicKey) { | |
| 84 // TODO(jrw) | |
| 85 throw new Error('Not implemented'); | |
| 86 }; | |
| 87 | |
| 88 /** @override */ | |
| 89 remoting.HostListApiGcdImpl.prototype.remove = function(hostId) { | |
| 90 var that = this; | |
| 91 return this.gcd_.listDevices(hostId).then(function(devices) { | |
| 92 var gcdId = null; | |
| 93 for (var i = 0; i < devices.length; i++) { | |
| 94 var device = devices[i]; | |
| 95 // The "name" field in GCD holds what Chromoting considers to be | |
| 96 // the host ID. | |
| 97 if (device.name == hostId) { | |
| 98 gcdId = device.id; | |
| 99 } | |
| 100 } | |
| 101 if (gcdId == null) { | |
| 102 return false; | |
| 103 } else { | |
| 104 return that.gcd_.deleteDevice(gcdId); | |
| 105 } | |
| 106 }); | |
| 107 }; | |
| 108 | |
| 109 /** @override */ | |
| 110 remoting.HostListApiGcdImpl.prototype.getSupportHost = function(supportId) { | |
| 111 console.error('getSupportHost not supported by HostListApiGclImpl'); | |
| 112 return Promise.reject(remoting.Error.unexpected()); | |
| 113 }; | |
| 114 | |
| 115 /** | |
| 116 * Converts a GCD device description to a Host object. | |
| 117 * @param {!Object} device | |
| 118 * @return {!remoting.Host} | |
| 119 */ | |
| 120 function deviceToHost(device) { | |
| 121 var statusMap = { | |
| 122 'online': 'ONLINE', | |
| 123 'offline': 'OFFLINE' | |
| 124 }; | |
| 125 var hostId = base.getStringAttr(device, 'name'); | |
| 126 var host = new remoting.Host(hostId); | |
| 127 host.hostName = base.getStringAttr(device, 'displayName'); | |
| 128 host.status = base.getStringAttr( | |
| 129 statusMap, base.getStringAttr(device, 'connectionStatus')); | |
| 130 var state = base.getObjectAttr(device, 'state', {}); | |
| 131 host.publicKey = base.getStringAttr(state, 'publicKey'); | |
| 132 host.jabberId = base.getStringAttr(state, 'jabberId', ''); | |
| 133 host.hostVersion = base.getStringAttr(state, 'hostVersion', ''); | |
| 134 var creationTimeMs = base.getNumberAttr(device, 'creationTimeMs', 0); | |
| 135 if (creationTimeMs) { | |
| 136 host.createdTime = new Date(creationTimeMs).toISOString(); | |
| 137 } | |
| 138 var lastUpdateTimeMs = base.getNumberAttr(device, 'lastUpdateTimeMs', 0); | |
| 139 if (lastUpdateTimeMs) { | |
| 140 host.updatedTime = new Date(lastUpdateTimeMs).toISOString(); | |
| 141 } | |
| 142 return host; | |
| 143 }; | |
| 144 | |
| 145 })(); | |
| OLD | NEW |