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 * Amount of time to wait for GCD results after legacy registry has | |
| 19 * returned. | |
| 20 */ | |
| 21 var GCD_TIMEOUT_MS = 1000; | |
| 22 | |
| 23 /** | |
| 24 * @constructor | |
| 25 * @param {!remoting.HostListApi} legacyImpl | |
| 26 * @param {!remoting.HostListApi} gcdImpl | |
| 27 * @implements {remoting.HostListApi} | |
| 28 */ | |
| 29 remoting.CombinedHostListApi = function(legacyImpl, gcdImpl) { | |
| 30 /** @const {!remoting.HostListApi} */ | |
| 31 this.legacyImpl_ = legacyImpl; | |
| 32 | |
| 33 /** @const {!remoting.HostListApi} */ | |
| 34 this.gcdImpl_ = gcdImpl; | |
| 35 | |
| 36 /** | |
| 37 * List of host IDs most recently retrieved from |legacyImpl_|. | |
| 38 * @type {!Set<string>} | |
| 39 */ | |
| 40 this.legacyIds_ = new Set(); | |
| 41 | |
| 42 /** | |
| 43 * List of host IDs most recently retrieved |gcdImpl_|. | |
| 44 * @type {!Set<string>} | |
| 45 */ | |
| 46 this.gcdIds_ = new Set(); | |
| 47 }; | |
| 48 | |
| 49 /** @override */ | |
| 50 remoting.CombinedHostListApi.prototype.register = function( | |
| 51 hostName, publicKey, hostClientId) { | |
| 52 var that = this; | |
| 53 // First, register the new host with GCD, which will create a | |
| 54 // service account and generate a host ID. | |
| 55 return this.gcdImpl_.register(hostName, publicKey, hostClientId).then( | |
| 56 function(gcdRegResult) { | |
| 57 // After the GCD registration has been created, copy the | |
| 58 // registration to the legacy directory so that clients not yet | |
| 59 // upgraded to use GCD can see the new host. | |
| 60 // | |
| 61 // This is an ugly hack for multiple reasons: | |
| 62 // | |
| 63 // 1. It completely ignores |this.legacyImpl_|, complicating | |
| 64 // unit tests. | |
| 65 // | |
| 66 // 2. It relies on the fact that, when |hostClientId| is null, | |
| 67 // the legacy directory will "register" a host without | |
| 68 // creating a service account. This is an obsolete feature | |
| 69 // of the legacy directory that is being revived for a new | |
| 70 // purpose. | |
| 71 // | |
| 72 // 3. It assumes the device ID generated by GCD is usable as a | |
| 73 // host ID by the legacy directory. Fortunately both systems | |
| 74 // use UUIDs. | |
| 75 return remoting.LegacyHostListApi.registerWithHostId( | |
| 76 gcdRegResult.hostId, hostName, publicKey, null).then( | |
| 77 function() { | |
| 78 // On success, return the result from GCD, ignoring | |
| 79 // the result returned by the legacy directory. | |
| 80 that.gcdIds_.add(gcdRegResult.hostId); | |
| 81 that.legacyIds_.add(gcdRegResult.hostId); | |
| 82 return gcdRegResult; | |
| 83 }, | |
| 84 function(error) { | |
| 85 console.warn( | |
| 86 'Error copying host GCD host registration ' + | |
| 87 'to legacy directory: ' + error); | |
| 88 throw error; | |
| 89 } | |
| 90 ); | |
| 91 }); | |
| 92 }; | |
| 93 | |
| 94 /** @override */ | |
| 95 remoting.CombinedHostListApi.prototype.get = function() { | |
| 96 // Fetch the host list from both directories and merge hosts that | |
| 97 // have the same ID. | |
| 98 var that = this; | |
| 99 var legacyPromise = this.legacyImpl_.get(); | |
| 100 var gcdPromise = this.gcdImpl_.get(); | |
| 101 return legacyPromise.then(function(legacyHosts) { | |
| 102 // If GCD is too slow, just ignore it and return results from the | |
| 103 // legacy directory. | |
| 104 /** @type {!Promise<!Array<!remoting.Host>>} */ | |
| 105 var timeoutPromise = base.Promise.withTimeout( | |
| 106 gcdPromise, GCD_TIMEOUT_MS, []); | |
| 107 return timeoutPromise.then(function(gcdHosts) { | |
| 108 // Combine host information from both directories. In the case | |
| 109 // of conflicting information, prefer information from whichever | |
| 110 // directory claims to have newer information. | |
| 111 | |
| 112 var hosts = []; | |
| 113 /** @type {!Map<string,!remoting.Host>} */ | |
| 114 var gcdHostMap = new Map(); | |
| 115 /** @type {!Map<string,!remoting.Host>} */ | |
| 116 var legacyHostMap = new Map(); | |
| 117 gcdHosts.forEach(function(host) { | |
| 118 gcdHostMap.set(host.hostId, host); | |
| 119 }); | |
| 120 legacyHosts.forEach(function(host) { | |
| 121 legacyHostMap.set(host.hostId, host); | |
| 122 }); | |
| 123 gcdHosts.forEach(function(host) { | |
| 124 var legacyHost = legacyHostMap.get(host.hostId); | |
| 125 if (!legacyHost || host.updatedTime >= legacyHost.updatedTime) { | |
| 126 hosts.push(host); | |
|
kelvinp
2015/07/20 18:00:51
Nit:
After we ensure that either there is no lega
John Williams
2015/07/20 21:43:10
I rewrote this method for clarity; PTAL.
| |
| 127 } | |
| 128 }); | |
| 129 legacyHosts.forEach(function(host) { | |
| 130 var gcdHost = gcdHostMap.get(host.hostId); | |
| 131 if (!gcdHost || host.updatedTime > gcdHost.updatedTime) { | |
| 132 hosts.push(host); | |
| 133 } | |
| 134 }); | |
| 135 that.legacyIds_ = base.keySet(legacyHostMap); | |
| 136 that.gcdIds_ = base.keySet(gcdHostMap); | |
| 137 return hosts; | |
| 138 }); | |
| 139 }); | |
| 140 }; | |
| 141 | |
| 142 /** @override */ | |
| 143 remoting.CombinedHostListApi.prototype.put = | |
| 144 function(hostId, hostName, hostPublicKey) { | |
| 145 var legacyPromise = Promise.resolve(); | |
| 146 if (this.legacyIds_.has(hostId)) { | |
| 147 legacyPromise = this.legacyImpl_.put(hostId, hostName, hostPublicKey); | |
| 148 } | |
| 149 var gcdPromise = Promise.resolve(); | |
| 150 if (this.gcdIds_.has(hostId)) { | |
| 151 gcdPromise = this.gcdImpl_.put(hostId, hostName, hostPublicKey); | |
| 152 } | |
| 153 return legacyPromise.then(function() { | |
| 154 // If GCD is too slow, just ignore it and return result from the | |
| 155 // legacy directory. | |
| 156 return base.Promise.withTimeout( | |
| 157 gcdPromise, GCD_TIMEOUT_MS); | |
| 158 }); | |
| 159 }; | |
| 160 | |
| 161 /** @override */ | |
| 162 remoting.CombinedHostListApi.prototype.remove = function(hostId) { | |
| 163 var legacyPromise = Promise.resolve(); | |
| 164 if (this.legacyIds_.has(hostId)) { | |
| 165 legacyPromise = this.legacyImpl_.remove(hostId); | |
| 166 } | |
| 167 var gcdPromise = Promise.resolve(); | |
| 168 if (this.gcdIds_.has(hostId)) { | |
| 169 gcdPromise = this.gcdImpl_.remove(hostId); | |
| 170 } | |
| 171 return legacyPromise.then(function() { | |
| 172 // If GCD is too slow, just ignore it and return result from the | |
| 173 // legacy directory. | |
| 174 return base.Promise.withTimeout( | |
| 175 gcdPromise, GCD_TIMEOUT_MS); | |
| 176 }); | |
| 177 }; | |
| 178 | |
| 179 /** @override */ | |
| 180 remoting.CombinedHostListApi.prototype.getSupportHost = function(supportId) { | |
| 181 return this.legacyImpl_.getSupportHost(supportId); | |
| 182 }; | |
| 183 | |
| 184 })(); | |
| OLD | NEW |