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

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

Issue 1217643002: Added HostListApl implementation to connect to legacy directory and GCD. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gcd-e2e
Patch Set: rewrote code for combining host lists Created 5 years, 5 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
(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 act as if it had returned an empty
103 // result set.
104 /** @type {!Promise<!Array<!remoting.Host>>} */
105 var timeoutPromise = base.Promise.withTimeout(
106 gcdPromise, GCD_TIMEOUT_MS, []);
107
108 // Combine host information from both directories. In the case of
109 // conflicting information, prefer information from whichever
110 // directory claims to have newer information.
111 return timeoutPromise.then(function(gcdHosts) {
112 // First, for each directory, build a mapping from host ID to
113 // host data, and collect a set of host IDs present in the
114 // directory.
115 /** @type {!Map<string,!remoting.Host>} */
116 var gcdHostMap = new Map();
kelvinp 2015/07/20 22:46:18 Would the following be simpler? var hostMap = new
John Williams 2015/07/21 04:54:55 That's not quite right, but I incorporated somethi
117 /** @type {!Map<string,!remoting.Host>} */
118 var legacyHostMap = new Map();
119 that.gcdIds_ = new Set();
120 that.legacyIds_ = new Set();
121 gcdHosts.forEach(function(host) {
122 that.gcdIds_.add(host.hostId);
123 gcdHostMap.set(host.hostId, host);
124 });
125 legacyHosts.forEach(function(host) {
126 that.legacyIds_.add(host.hostId);
127 legacyHostMap.set(host.hostId, host);
128 });
129
130 // Trim the host maps such that each host exists in only a
131 // single map. We iterate over |that.gcdIds_| instead of
132 // |gcdHosts| to avoid any potential issues with modifying a map
133 // while iterating over it.
134 that.gcdIds_.forEach(function(hostId) {
135 var gcdHost = gcdHostMap.get(hostId);
136 console.assert(gcdHost);
137 var legacyHost = legacyHostMap.get(hostId);
138 if (legacyHost) {
139 // The host is in both maps.
140 if (gcdHost.updatedTime >= legacyHost.updatedTime) {
141 legacyHostMap.delete(hostId);
142 } else {
143 gcdHostMap.delete(hostId);
144 }
145 }
146 });
147
148 // Build a combined list of hosts, preserving the original
149 // ordering as much as possible.
150 var hosts = [];
151 gcdHosts.forEach(function(host) {
152 if (gcdHostMap.has(host.hostId)) {
153 hosts.push(host);
154 }
155 });
156 legacyHosts.forEach(function(host) {
157 if (legacyHostMap.has(host.hostId)) {
158 hosts.push(host);
159 }
160 });
161
162 return hosts;
163 });
164 });
165 };
166
167 /** @override */
168 remoting.CombinedHostListApi.prototype.put =
169 function(hostId, hostName, hostPublicKey) {
170 var legacyPromise = Promise.resolve();
171 if (this.legacyIds_.has(hostId)) {
172 legacyPromise = this.legacyImpl_.put(hostId, hostName, hostPublicKey);
173 }
174 var gcdPromise = Promise.resolve();
175 if (this.gcdIds_.has(hostId)) {
176 gcdPromise = this.gcdImpl_.put(hostId, hostName, hostPublicKey);
177 }
178 return legacyPromise.then(function() {
179 // If GCD is too slow, just ignore it and return result from the
180 // legacy directory.
181 return base.Promise.withTimeout(
182 gcdPromise, GCD_TIMEOUT_MS);
183 });
184 };
185
186 /** @override */
187 remoting.CombinedHostListApi.prototype.remove = function(hostId) {
188 var legacyPromise = Promise.resolve();
189 if (this.legacyIds_.has(hostId)) {
190 legacyPromise = this.legacyImpl_.remove(hostId);
191 }
192 var gcdPromise = Promise.resolve();
193 if (this.gcdIds_.has(hostId)) {
194 gcdPromise = this.gcdImpl_.remove(hostId);
195 }
196 return legacyPromise.then(function() {
197 // If GCD is too slow, just ignore it and return result from the
198 // legacy directory.
199 return base.Promise.withTimeout(
200 gcdPromise, GCD_TIMEOUT_MS);
201 });
202 };
203
204 /** @override */
205 remoting.CombinedHostListApi.prototype.getSupportHost = function(supportId) {
206 return this.legacyImpl_.getSupportHost(supportId);
207 };
208
209 })();
OLDNEW
« no previous file with comments | « remoting/webapp/base/js/host.js ('k') | remoting/webapp/crd/js/combined_host_list_api_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698