Commit 90d7824a authored by jamiewalch's avatar jamiewalch Committed by Commit bot

Create proper object types when loading host cache.

I suspect that host.options was originally a simple dictionary, and so the cast was safe. When it got upgraded to an object with methods on the prototype it broke, but because the window of opportunity for failure is small (you have to connect without a call to HostList.refresh happening in the interim), it was difficult to track down.

BUG=680302

Review-Url: https://codereview.chromium.org/2621403004
Cr-Commit-Position: refs/heads/master@{#443072}
parent 96557bfa
......@@ -52,6 +52,34 @@ remoting.Host = function(hostId) {
this.options = new remoting.HostOptions(hostId);
};
/**
* Create a typed Host instance from an untyped Object.
*
* @param {Object} object
* @return {remoting.Host}
*/
remoting.Host.fromObject = function(object) {
try {
var result = new remoting.Host(base.assertString(object.hostId));
result.hostName = base.assertString(object.hostName);
result.status = base.assertString(object.status);
result.jabberId = base.assertString(object.jabberId);
result.publicKey = base.assertString(object.publicKey);
result.hostVersion = base.assertString(object.hostVersion);
result.hostOs = base.assertNumber(object.hostOs);
result.hostOsVersion = base.assertString(object.hostOsVersion);
result.tokenUrlPatterns = base.assertArray(object.tokenUrlPatterns);
result.updatedTime = base.assertString(object.updatedTime);
result.hostOfflineReason = base.assertString(object.hostOfflineReason);
result.options = remoting.HostOptions.fromObject(
base.assertObject(object.options), base.assertString(object.hostId));
return result;
} catch (e) {
console.error(e);
return null;
}
};
/**
* Determine whether a host needs to be manually updated. This is the case if
* the host's major version number is more than 2 lower than that of the web-
......
......@@ -37,7 +37,34 @@ remoting.HostOptions = function (hostId) {
this.remapKeys = null;
};
/** @return {boolean} True if the remote desktop should be reduced in size to
/**
* Create a typed HostOptions instance from an untyped Object.
*
* @param {Object} object
* @param {string} hostId
* @return {remoting.HostOptions}
*/
remoting.HostOptions.fromObject = function(object, hostId) {
var result = new remoting.HostOptions(hostId);
try {
result.shrinkToFit = base.assertBoolean(object.shrinkToFit);
} catch (e) {}
try {
result.resizeToClient = base.assertBoolean(object.resizeToClient);
} catch (e) {}
try {
result.desktopScale = base.assertNumber(object.desktopScale);
} catch (e) {}
try {
pairingInfo = base.assertObject(object.pairingInfo);
} catch (e) {}
try {
remapKeys = base.assertObject(object.remapKeys);
} catch (e) {}
return result;
};
/** @return {boolean} True if the remote desktop should be reduced in size to
* fit a smaller client window; false if scroll-bars or bump-scrolling
* should be used instead.
*/
......
......@@ -109,8 +109,10 @@ remoting.HostList.prototype.load = function(onDone) {
var storeHostList = function(items) {
if (items[remoting.HostList.HOSTS_KEY]) {
var cached = base.jsonParseSafe(items[remoting.HostList.HOSTS_KEY]);
if (cached) {
that.hosts_ = /** @type {Array<remoting.Host>} */ (cached);
if (cached && cached instanceof Array) {
that.hosts_ = cached
.map((host) => remoting.Host.fromObject(host))
.filter((host) => Boolean(host));
} else {
console.error('Invalid value for ' + remoting.HostList.HOSTS_KEY);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment