Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * REST API for host-list management. | 7 * REST API for host-list management. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /** @suppress {duplicate} */ | 10 /** @suppress {duplicate} */ |
| 11 var remoting = remoting || {}; | 11 var remoting = remoting || {}; |
| 12 | 12 |
| 13 (function() { | 13 (function() { |
| 14 | 14 |
| 15 'use strict'; | 15 'use strict'; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * @constructor | 18 * @constructor |
| 19 * @implements {remoting.HostListApi} | 19 * @implements {remoting.HostListApi} |
| 20 */ | 20 */ |
| 21 remoting.HostListApiGcdImpl = function() { | 21 remoting.HostListApiGcdImpl = function() { |
| 22 this.gcd_ = new remoting.gcd.Client({ | 22 this.gcd_ = new remoting.gcd.Client({ |
| 23 apiKey: remoting.settings.GOOGLE_API_KEY | 23 apiKey: remoting.settings.GOOGLE_API_KEY |
| 24 }); | 24 }); |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 /** @override */ | 27 /** @override */ |
| 28 remoting.HostListApiGcdImpl.prototype.register = function( | 28 remoting.HostListApiGcdImpl.prototype.register = function( |
| 29 newHostId, hostName, publicKey, hostClientId) { | 29 hostName, publicKey, hostClientId) { |
| 30 var self = this; | 30 var self = this; |
| 31 var deviceDraft = { | 31 var deviceDraft = { |
| 32 channel: { | 32 channel: { |
| 33 supportedType: 'xmpp' | 33 supportedType: 'xmpp' |
| 34 }, | 34 }, |
| 35 deviceKind: 'vendor', | 35 deviceKind: 'vendor', |
| 36 name: newHostId, | 36 name: hostName, |
| 37 displayName: hostName, | |
| 38 state: { | 37 state: { |
| 39 'publicKey': publicKey | 38 'base': { |
|
Jamie
2015/06/08 21:44:34
Elsewhere in this file there are no quotes around
John Williams
2015/06/09 19:46:30
Done.
| |
| 40 } | 39 'firmwareVersion': 'none', |
| 40 'localDiscoveryEnabled': false, | |
| 41 'localAnonymousAccessMaxRole': 'none', | |
| 42 'localPairingEnabled': false, | |
| 43 '_publicKey': publicKey | |
|
Jamie
2015/06/08 21:44:34
What's the significance of the leading underscore?
John Williams
2015/06/09 19:46:30
Done.
| |
| 44 } | |
| 45 }, | |
| 46 'tags': [MAGIC_TAG] | |
| 41 }; | 47 }; |
| 42 | 48 |
| 43 return /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ ( | 49 return /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ ( |
| 44 this.gcd_.insertRegistrationTicket(). | 50 this.gcd_.insertRegistrationTicket(). |
| 45 then(function(ticket) { | 51 then(function(ticket) { |
| 46 return self.gcd_.patchRegistrationTicket( | 52 return self.gcd_.patchRegistrationTicket( |
| 47 ticket.id, deviceDraft, hostClientId); | 53 ticket.id, deviceDraft, hostClientId); |
| 48 }). | 54 }). |
| 49 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { | 55 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { |
| 50 return self.gcd_.finalizeRegistrationTicket(ticket.id); | 56 return self.gcd_.finalizeRegistrationTicket(ticket.id); |
| 51 }). | 57 }). |
| 52 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { | 58 then(function(/**remoting.gcd.RegistrationTicket*/ ticket) { |
| 53 return { | 59 return { |
| 54 authCode: ticket.robotAccountAuthorizationCode, | 60 authCode: ticket.robotAccountAuthorizationCode, |
| 55 email: ticket.robotAccountEmail, | 61 email: ticket.robotAccountEmail, |
| 56 gcdId: ticket.deviceId | 62 hostId: ticket.deviceId, |
| 63 isLegacy: false | |
| 57 }; | 64 }; |
| 58 }). | 65 }). |
| 59 catch(function(error) { | 66 catch(function(error) { |
| 60 console.error('Error registering device with GCD: ' + error); | 67 console.error('Error registering device with GCD: ' + error); |
| 61 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | 68 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); |
| 62 })); | 69 })); |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 /** @override */ | 72 /** @override */ |
| 66 remoting.HostListApiGcdImpl.prototype.get = function() { | 73 remoting.HostListApiGcdImpl.prototype.get = function() { |
| 67 return this.gcd_.listDevices(). | 74 return this.gcd_.listDevices(). |
| 68 then(function(devices) { | 75 then(function(devices) { |
| 69 var hosts = []; | 76 var hosts = []; |
| 70 devices.forEach(function(device) { | 77 devices.forEach(function(device) { |
| 71 try { | 78 try { |
| 72 hosts.push(deviceToHost(device)); | 79 if (isChromotingHost(device)) { |
| 80 hosts.push(deviceToHost(device)); | |
| 81 } | |
| 73 } catch (/** @type {*} */ error) { | 82 } catch (/** @type {*} */ error) { |
| 74 console.warn('Invalid device spec:', error); | 83 console.warn('Invalid device spec:', error); |
| 75 } | 84 } |
| 76 }); | 85 }); |
| 77 return hosts; | 86 return hosts; |
| 78 }); | 87 }); |
| 79 }; | 88 }; |
| 80 | 89 |
| 81 /** @override */ | 90 /** @override */ |
| 82 remoting.HostListApiGcdImpl.prototype.put = | 91 remoting.HostListApiGcdImpl.prototype.put = |
| 83 function(hostId, hostName, hostPublicKey) { | 92 function(hostId, hostName, hostPublicKey) { |
| 84 // TODO(jrw) | 93 return this.gcd_.patchDevice(hostId, { |
| 85 throw new Error('Not implemented'); | 94 'name': hostName |
| 95 }).then(function(device) { | |
| 96 if (device.name != hostName) { | |
| 97 console.error('error updating host name'); | |
| 98 throw remoting.Error.unexpected(); | |
| 99 } | |
| 100 if (!device.state || device.state['_publicKey'] != hostPublicKey) { | |
| 101 // TODO(jrw): Is there any reason to believe this would ever be | |
| 102 // happen? | |
| 103 console.error('unexpected host public key'); | |
| 104 throw remoting.Error.unexpected(); | |
| 105 } | |
| 106 // Don't return anything. | |
| 107 }); | |
| 86 }; | 108 }; |
| 87 | 109 |
| 88 /** @override */ | 110 /** @override */ |
| 89 remoting.HostListApiGcdImpl.prototype.remove = function(hostId) { | 111 remoting.HostListApiGcdImpl.prototype.remove = function(hostId) { |
| 90 var that = this; | 112 return this.gcd_.deleteDevice(hostId).then(function(deleted) { |
| 91 return this.gcd_.listDevices(hostId).then(function(devices) { | 113 if (!deleted) { |
| 92 var gcdId = null; | 114 console.error('error deleting host from GCD'); |
| 93 for (var i = 0; i < devices.length; i++) { | 115 throw remoting.Error.unexpected(); |
| 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 } | 116 } |
| 101 if (gcdId == null) { | 117 // Don't return anything. |
| 102 return false; | |
| 103 } else { | |
| 104 return that.gcd_.deleteDevice(gcdId); | |
| 105 } | |
| 106 }); | 118 }); |
| 107 }; | 119 }; |
| 108 | 120 |
| 109 /** @override */ | 121 /** @override */ |
| 110 remoting.HostListApiGcdImpl.prototype.getSupportHost = function(supportId) { | 122 remoting.HostListApiGcdImpl.prototype.getSupportHost = function(supportId) { |
| 111 console.error('getSupportHost not supported by HostListApiGclImpl'); | 123 console.error('getSupportHost not supported by HostListApiGclImpl'); |
| 112 return Promise.reject(remoting.Error.unexpected()); | 124 return Promise.reject(remoting.Error.unexpected()); |
| 113 }; | 125 }; |
| 114 | 126 |
| 115 /** | 127 /** |
| 128 * Tag for distinguishing Chromoting hosts from other devices stores | |
|
Jamie
2015/06/08 21:44:34
s/stores/stored/
John Williams
2015/06/09 19:46:30
Done.
| |
| 129 * in GCD. | |
|
Jamie
2015/06/08 21:44:34
Blank line between description and annotations, pl
John Williams
2015/06/09 19:46:30
Done.
| |
| 130 * @const | |
| 131 */ | |
| 132 var MAGIC_TAG = '1ce4542c-dd87-4320-ba19-ac173f98c04e'; | |
|
Jamie
2015/06/08 21:44:34
Please give this a more descriptive name.
John Williams
2015/06/09 19:46:30
Done.
| |
| 133 | |
| 134 /** | |
| 135 * Check whether a GCD device entry is a Chromoting host. | |
| 136 * @param {remoting.gcd.Device} device | |
| 137 * @return {boolean} | |
| 138 */ | |
| 139 function isChromotingHost(device) { | |
| 140 return device.tags != null && device.tags.indexOf(MAGIC_TAG) != -1; | |
| 141 } | |
| 142 | |
| 143 /** | |
| 116 * Converts a GCD device description to a Host object. | 144 * Converts a GCD device description to a Host object. |
| 117 * @param {!Object} device | 145 * @param {!Object} device |
| 118 * @return {!remoting.Host} | 146 * @return {!remoting.Host} |
| 119 */ | 147 */ |
| 120 function deviceToHost(device) { | 148 function deviceToHost(device) { |
| 121 var statusMap = { | 149 var statusMap = { |
| 122 'online': 'ONLINE', | 150 'online': 'ONLINE', |
| 123 'offline': 'OFFLINE' | 151 'offline': 'OFFLINE' |
| 124 }; | 152 }; |
| 125 var hostId = base.getStringAttr(device, 'name'); | 153 var hostId = base.getStringAttr(device, 'id'); |
| 126 var host = new remoting.Host(hostId); | 154 var host = new remoting.Host(hostId); |
| 127 host.hostName = base.getStringAttr(device, 'displayName'); | 155 host.hostName = base.getStringAttr(device, 'name'); |
| 128 host.status = base.getStringAttr( | 156 host.status = base.getStringAttr( |
| 129 statusMap, base.getStringAttr(device, 'connectionStatus')); | 157 statusMap, base.getStringAttr(device, 'connectionStatus')); |
| 130 var state = base.getObjectAttr(device, 'state', {}); | 158 var state = base.getObjectAttr(device, 'state', {}); |
| 131 host.publicKey = base.getStringAttr(state, 'publicKey'); | 159 var baseState = base.getObjectAttr(state, 'base', {}); |
| 132 host.jabberId = base.getStringAttr(state, 'jabberId', ''); | 160 host.publicKey = base.getStringAttr(baseState, '_publicKey'); |
| 133 host.hostVersion = base.getStringAttr(state, 'hostVersion', ''); | 161 host.jabberId = base.getStringAttr(baseState, '_jabberId', ''); |
| 162 host.hostVersion = base.getStringAttr(baseState, '_hostVersion', ''); | |
| 134 var creationTimeMs = base.getNumberAttr(device, 'creationTimeMs', 0); | 163 var creationTimeMs = base.getNumberAttr(device, 'creationTimeMs', 0); |
| 135 if (creationTimeMs) { | 164 if (creationTimeMs) { |
| 136 host.createdTime = new Date(creationTimeMs).toISOString(); | 165 host.createdTime = new Date(creationTimeMs).toISOString(); |
| 137 } | 166 } |
| 138 var lastUpdateTimeMs = base.getNumberAttr(device, 'lastUpdateTimeMs', 0); | 167 var lastUpdateTimeMs = base.getNumberAttr(device, 'lastUpdateTimeMs', 0); |
| 139 if (lastUpdateTimeMs) { | 168 if (lastUpdateTimeMs) { |
| 140 host.updatedTime = new Date(lastUpdateTimeMs).toISOString(); | 169 host.updatedTime = new Date(lastUpdateTimeMs).toISOString(); |
| 141 } | 170 } |
| 142 return host; | 171 return host; |
| 143 }; | 172 }; |
| 144 | 173 |
| 145 })(); | 174 })(); |
| OLD | NEW |