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 = '';
49 * TODO(kelvinp): Remove this once we have migrated away from XMPP based
50 * logging (crbug.com/523423).
54 this.loggingChannel = '';
55 /** @type {remoting.Host.Options} */
56 this.options = new remoting.Host.Options(hostId);
61 * @param {!string} hostId
64 remoting.Host.Options = function(hostId) {
65 /** @private @const */
66 this.hostId_ = hostId;
67 /** @type {boolean} */
68 this.shrinkToFit = true;
69 /** @type {boolean} */
70 this.resizeToClient = true;
71 /** @type {!Object} */
74 this.desktopScale = 1;
75 /** @type {remoting.PairingInfo} */
76 this.pairingInfo = {clientId: '', sharedSecret: ''};
79 remoting.Host.Options.prototype.save = function() {
80 // TODO(kelvinp): Migrate pairingInfo to use this class as well and get rid of
81 // remoting.HostSettings.
82 remoting.HostSettings.save(this.hostId_, this);
86 /** @return {Promise} A promise that resolves when the settings are loaded. */
87 remoting.Host.Options.prototype.load = function() {
89 return base.Promise.as(remoting.HostSettings.load, [this.hostId_]).then(
91 * @param {Object<string|boolean|number|!Object>} options
94 // Must be defaulted to true so that app-remoting can resize the host
96 // TODO(kelvinp): Uses a separate host options for app-remoting that
97 // hardcodes resizeToClient to true.
99 base.getBooleanAttr(options, 'resizeToClient', true);
100 that.shrinkToFit = base.getBooleanAttr(options, 'shrinkToFit', true);
101 that.desktopScale = base.getNumberAttr(options, 'desktopScale', 1);
103 /** @type {remoting.PairingInfo} */ (
104 base.getObjectAttr(options, 'pairingInfo', that.pairingInfo));
106 // Load the key remappings, allowing for either old or new formats.
107 var remappings = /** string|!Object */ (options['remapKeys']);
108 if (typeof(remappings) === 'string') {
109 remappings = remoting.Host.Options.convertRemapKeys(remappings);
110 } else if (typeof(remappings) !== 'object') {
113 that.remapKeys = /** @type {!Object} */ (base.deepCopy(remappings));
118 * Convert an old-style string key remapping into a new-style dictionary one.
120 * @param {string} remappings
121 * @return {!Object} The same remapping expressed as a dictionary.
123 remoting.Host.Options.convertRemapKeys = function(remappings) {
124 var remappingsArr = remappings.split(',');
126 for (var i = 0; i < remappingsArr.length; ++i) {
127 var keyCodes = remappingsArr[i].split('>');
128 if (keyCodes.length != 2) {
129 console.log('bad remapKey: ' + remappingsArr[i]);
132 var fromKey = parseInt(keyCodes[0], 0);
133 var toKey = parseInt(keyCodes[1], 0);
134 if (!fromKey || !toKey) {
135 console.log('bad remapKey code: ' + remappingsArr[i]);
138 result[fromKey] = toKey;
144 * Determine whether a host needs to be manually updated. This is the case if
145 * the host's major version number is more than 2 lower than that of the web-
146 * app (a difference of 2 is tolerated due to the different update mechanisms
147 * and to handle cases where we may skip releasing a version) and if the host is
148 * on-line (off-line hosts are not expected to auto-update).
150 * @param {remoting.Host} host The host information from the directory.
151 * @param {string|number} webappVersion The version number of the web-app, in
152 * either dotted-decimal notation notation, or directly represented by the
154 * @return {boolean} True if the host is on-line but out-of-date.
156 remoting.Host.needsUpdate = function(host, webappVersion) {
157 if (host.status != 'ONLINE') {
160 var hostMajorVersion = parseInt(host.hostVersion, 10);
161 if (isNaN(hostMajorVersion)) {
162 // Host versions 26 and higher include the version number in heartbeats,
163 // so if it's missing then the host is at most version 25.
164 hostMajorVersion = 25;
166 return (parseInt(webappVersion, 10) - hostMajorVersion) > 2;