Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 * API implementation that combines two other implementations. | |
| 8 */ | |
| 9 | |
| 10 /** @suppress {duplicate} */ | |
| 11 var remoting = remoting || {}; | |
| 12 | |
| 13 (function() { | |
| 14 | |
| 15 'use strict'; | |
| 16 | |
| 17 /** | |
| 18 * @constructor | |
| 19 * @param {!remoting.HostListApi} legacyImpl | |
| 20 * @param {!remoting.HostListApi} gcdImpl | |
| 21 * @implements {remoting.HostListApi} | |
| 22 */ | |
| 23 remoting.CombinedHostListApi = function(legacyImpl, gcdImpl) { | |
| 24 /** @const {!remoting.HostListApi} */ | |
| 25 this.legacyImpl_ = legacyImpl; | |
| 26 | |
| 27 /** @const {!remoting.HostListApi} */ | |
| 28 this.gcdImpl_ = gcdImpl; | |
| 29 | |
| 30 /** @type {!Set<string>} */ | |
| 31 this.gcdIds_ = new Set(); | |
| 32 | |
| 33 /** @type {!Set<string>} */ | |
| 34 this.legacyIds_ = new Set(); | |
| 35 }; | |
| 36 | |
| 37 /** @override */ | |
| 38 remoting.CombinedHostListApi.prototype.register = function( | |
| 39 hostName, publicKey, hostClientId) { | |
| 40 var that = this; | |
| 41 if (remoting.settings.PREFER_GCD) { | |
| 42 // First, register the new host with GCD, which will create a | |
| 43 // service account and generate a host ID. | |
| 44 return this.gcdImpl_.register(hostName, publicKey, hostClientId).then( | |
| 45 function(gcdRegResult) { | |
| 46 // After the GCD registration has been created, copy the | |
| 47 // registration to the legacy directory so that clients not yet | |
| 48 // upgraded to use GCD can see the new host. | |
| 49 // | |
| 50 // This is an ugly hack for multiple reasons: | |
| 51 // | |
| 52 // 1. It completely ignores |this.legacyImpl_|, complicating | |
| 53 // unit tests. | |
| 54 // | |
| 55 // 2. It relies on the fact that, when |hostClientId| is null, | |
| 56 // the legacy directory will "register" a host without | |
| 57 // creating a service account. This is an obsolete feature | |
| 58 // of the legacy directory that is being revived for a new | |
| 59 // purposes. | |
| 60 // | |
| 61 // 3. It assumes the device ID generated by GCD is usable as a | |
| 62 // host ID by the legacy directory. Fortunately both systems | |
| 63 // use UUIDs. | |
| 64 return remoting.LegacyHostListApi.registerWithHostId( | |
| 65 gcdRegResult.hostId, hostName, publicKey, null).then( | |
| 66 function() { | |
| 67 // On success, return the result from GCD, ignoring | |
| 68 // the result returned by the legacy directory. | |
| 69 that.gcdIds_.add(gcdRegResult.hostId); | |
| 70 that.legacyIds_.add(gcdRegResult.hostId); | |
| 71 return gcdRegResult; | |
| 72 }, | |
| 73 function(error) { | |
| 74 console.warn( | |
| 75 'Error copying host GCD host registration ' + | |
| 76 'to legacy directory: ' + error); | |
|
kelvinp
2015/06/29 23:53:45
Nit: alignment
John Williams
2015/07/17 23:29:33
Done.
| |
| 77 throw error; | |
| 78 } | |
| 79 ); | |
| 80 }); | |
| 81 } else { | |
| 82 // Just register the host in the legacy directory. GCD does not | |
| 83 // provide an API that can be used to copy the registration into | |
| 84 // GCD. | |
| 85 return this.legacyImpl_.register(hostName, publicKey, hostClientId).then( | |
| 86 function(regResult) { | |
| 87 that.legacyIds_.add(regResult.hostId); | |
| 88 return regResult; | |
| 89 }); | |
| 90 } | |
| 91 }; | |
| 92 | |
| 93 /** @override */ | |
| 94 remoting.CombinedHostListApi.prototype.get = function() { | |
| 95 // Fetch the host list from both directories and merge hosts that | |
| 96 // have the same ID. Normally the data retrieved from both | |
| 97 // directories should be the same, but in the case of a conflict, | |
| 98 // the value of |remoting.settings.PREFER_GCD| determines which | |
| 99 // directory's information is returned. | |
| 100 var that = this; | |
| 101 var legacyPromise = this.legacyImpl_.get(); | |
| 102 var gcdPromise = this.gcdImpl_.get(); | |
| 103 return legacyPromise.then(function(legacyHosts) { | |
| 104 return gcdPromise.then(function(gcdHosts) { | |
| 105 var hosts = []; | |
| 106 var newLegacyIds = new Set(); | |
| 107 var newGcdIds = new Set(); | |
| 108 function addLegacyHosts() { | |
| 109 legacyHosts.forEach(function(host) { | |
| 110 newLegacyIds.add(host.hostId); | |
| 111 if (!newGcdIds.has(host.hostId)) { | |
| 112 hosts.push(host); | |
| 113 } | |
| 114 }); | |
| 115 } | |
| 116 function addGcdHosts() { | |
| 117 gcdHosts.forEach(function(host) { | |
| 118 newGcdIds.add(host.hostId); | |
| 119 if (!newLegacyIds.has(host.hostId)) { | |
| 120 hosts.push(host); | |
| 121 } | |
| 122 }); | |
| 123 } | |
| 124 if (remoting.settings.PREFER_GCD) { | |
| 125 addGcdHosts(); | |
| 126 addLegacyHosts(); | |
| 127 } else { | |
| 128 addLegacyHosts(); | |
| 129 addGcdHosts(); | |
| 130 } | |
| 131 that.legacyIds_ = newLegacyIds; | |
| 132 that.gcdIds_ = newGcdIds; | |
| 133 return hosts; | |
| 134 }); | |
| 135 }); | |
| 136 }; | |
| 137 | |
| 138 /** @override */ | |
| 139 remoting.CombinedHostListApi.prototype.put = | |
| 140 function(hostId, hostName, hostPublicKey) { | |
| 141 var legacyPromise = Promise.resolve(); | |
| 142 if (this.legacyIds_.has(hostId)) { | |
| 143 legacyPromise = this.legacyImpl_.put(hostId, hostName, hostPublicKey); | |
| 144 } | |
| 145 var gcdPromise = Promise.resolve(); | |
| 146 if (this.gcdIds_.has(hostId)) { | |
| 147 gcdPromise = this.gcdImpl_.put(hostId, hostName, hostPublicKey); | |
| 148 } | |
| 149 return legacyPromise.then(function() { | |
|
kelvinp
2015/06/29 23:53:45
Does other matter? Can we do Promise.all instead?
John Williams
2015/07/17 23:29:33
Order doesn't matter in this version, bit I wrote
| |
| 150 return gcdPromise; | |
| 151 }); | |
| 152 }; | |
| 153 | |
| 154 /** @override */ | |
| 155 remoting.CombinedHostListApi.prototype.remove = function(hostId) { | |
| 156 var legacyPromise = Promise.resolve(); | |
| 157 if (this.legacyIds_.has(hostId)) { | |
| 158 legacyPromise = this.legacyImpl_.remove(hostId); | |
| 159 } | |
| 160 var gcdPromise = Promise.resolve(); | |
| 161 if (this.gcdIds_.has(hostId)) { | |
| 162 gcdPromise = this.gcdImpl_.remove(hostId); | |
| 163 } | |
| 164 return legacyPromise.then(function() { | |
| 165 return gcdPromise; | |
| 166 }); | |
| 167 }; | |
| 168 | |
| 169 /** @override */ | |
| 170 remoting.CombinedHostListApi.prototype.getSupportHost = function(supportId) { | |
| 171 return this.legacyImpl_.getSupportHost(supportId); | |
| 172 }; | |
| 173 | |
| 174 /** @override */ | |
| 175 remoting.CombinedHostListApi.prototype.getHostIdFromConfig = function(config) { | |
| 176 var gcdId = base.getStringAttr(config, 'gcd_device_id', ''); | |
| 177 var legacyId = base.getStringAttr(config, 'host_id', ''); | |
| 178 if (gcdId && (!legacyId || remoting.settings.PREFER_GCD)) { | |
| 179 return gcdId; | |
| 180 } | |
| 181 else { | |
| 182 base.debug.assert(Boolean(legacyId)); | |
| 183 return legacyId; | |
| 184 } | |
| 185 }; | |
| 186 | |
| 187 })(); | |
| OLD | NEW |