1 // Copyright (c) 2012 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.
7 * The deserialized form of the chromoting host as returned by Apiary.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
20 * @param {!string} hostId
22 * TODO(kelvinp):Make fields private and expose them via getters.
25 remoting.Host = function(hostId) {
26 /** @const {string} */
31 * Either 'ONLINE' or 'OFFLINE'.
40 this.hostVersion = '';
41 /** @type {Array<string>} */
42 this.tokenUrlPatterns = [];
44 this.updatedTime = '';
46 this.hostOfflineReason = '';
48 this.loggingChannel = '';
49 /** @type {remoting.Host.Options} */
50 this.options = new remoting.Host.Options(hostId);
55 * @param {!string} hostId
58 remoting.Host.Options = function(hostId) {
59 /** @private @const */
60 this.hostId_ = hostId;
61 /** @type {boolean} */
62 this.shrinkToFit = true;
63 /** @type {boolean} */
64 this.resizeToClient = true;
65 /** @type {!Object} */
68 this.desktopScale = 1;
69 /** @type {remoting.PairingInfo} */
70 this.pairingInfo = {clientId: '', sharedSecret: ''};
73 remoting.Host.Options.prototype.save = function() {
74 // TODO(kelvinp): Migrate pairingInfo to use this class as well and get rid of
75 // remoting.HostSettings.
76 remoting.HostSettings.save(this.hostId_, this);
80 /** @return {Promise} A promise that resolves when the settings are loaded. */
81 remoting.Host.Options.prototype.load = function() {
83 return base.Promise.as(remoting.HostSettings.load, [this.hostId_]).then(
85 * @param {Object<string|boolean|number|!Object>} options
88 // Must be defaulted to true so that app-remoting can resize the host
90 // TODO(kelvinp): Uses a separate host options for app-remoting that
91 // hardcodes resizeToClient to true.
93 base.getBooleanAttr(options, 'resizeToClient', true);
94 that.shrinkToFit = base.getBooleanAttr(options, 'shrinkToFit', true);
95 that.desktopScale = base.getNumberAttr(options, 'desktopScale', 1);
97 /** @type {remoting.PairingInfo} */ (
98 base.getObjectAttr(options, 'pairingInfo', that.pairingInfo));
100 // Load the key remappings, allowing for either old or new formats.
101 var remappings = /** string|!Object */ (options['remapKeys']);
102 if (typeof(remappings) === 'string') {
103 remappings = remoting.Host.Options.convertRemapKeys(remappings);
104 } else if (typeof(remappings) !== 'object') {
107 that.remapKeys = /** @type {!Object} */ (base.deepCopy(remappings));
112 * Convert an old-style string key remapping into a new-style dictionary one.
114 * @param {string} remappings
115 * @return {!Object} The same remapping expressed as a dictionary.
117 remoting.Host.Options.convertRemapKeys = function(remappings) {
118 var remappingsArr = remappings.split(',');
120 for (var i = 0; i < remappingsArr.length; ++i) {
121 var keyCodes = remappingsArr[i].split('>');
122 if (keyCodes.length != 2) {
123 console.log('bad remapKey: ' + remappingsArr[i]);
126 var fromKey = parseInt(keyCodes[0], 0);
127 var toKey = parseInt(keyCodes[1], 0);
128 if (!fromKey || !toKey) {
129 console.log('bad remapKey code: ' + remappingsArr[i]);
132 result[fromKey] = toKey;
138 * Determine whether a host needs to be manually updated. This is the case if
139 * the host's major version number is more than 2 lower than that of the web-
140 * app (a difference of 2 is tolerated due to the different update mechanisms
141 * and to handle cases where we may skip releasing a version) and if the host is
142 * on-line (off-line hosts are not expected to auto-update).
144 * @param {remoting.Host} host The host information from the directory.
145 * @param {string|number} webappVersion The version number of the web-app, in
146 * either dotted-decimal notation notation, or directly represented by the
148 * @return {boolean} True if the host is on-line but out-of-date.
150 remoting.Host.needsUpdate = function(host, webappVersion) {
151 if (host.status != 'ONLINE') {
154 var hostMajorVersion = parseInt(host.hostVersion, 10);
155 if (isNaN(hostMajorVersion)) {
156 // Host versions 26 and higher include the version number in heartbeats,
157 // so if it's missing then the host is at most version 25.
158 hostMajorVersion = 25;
160 return (parseInt(webappVersion, 10) - hostMajorVersion) > 2;