1 // Copyright 2014 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.
8 * HostInstaller allows the caller to download the host binary and monitor the
9 * install progress of the host by pinging the host periodically via native
12 * To download the host and wait for install:
13 * var hostInstaller = new remoting.HostInstaller();
14 * hostInstaller.downloadAndWaitForInstall().then(function() {
15 * // Install has completed.
17 * // Download has failed.
20 * To stop listening to the install progress:
21 * hostInstaller.cancel();
26 /** @suppress {duplicate} */
27 var remoting
= remoting
|| {};
34 remoting
.HostInstaller = function() {
35 /** @private {Promise} */
36 this.downloadAndWaitForInstallPromise_
= null;
38 /** @private {?number} */
39 this.checkInstallIntervalId_
= null;
43 * @return {Promise} The promise will resolve to a boolean value indicating
44 * whether the host is installed or not.
46 remoting
.HostInstaller
.isInstalled = function() {
47 // Always do a fresh check as we don't get notified when the host is
51 /** @param {function(*=):void} resolve */
53 // TODO(kelvinp): Use different native messaging ports for the Me2me
57 chrome
.runtime
.connectNative('com.google.chrome.remote_assistance');
59 function onMessage() {
60 port
.onDisconnect
.removeListener(onDisconnected
);
61 port
.onMessage
.removeListener(onMessage
);
66 function onDisconnected() {
67 port
.onDisconnect
.removeListener(onDisconnected
);
68 port
.onMessage
.removeListener(onMessage
);
72 port
.onDisconnect
.addListener(onDisconnected
);
73 port
.onMessage
.addListener(onMessage
);
74 port
.postMessage({type
: 'hello'});
78 /** @type {Object<string>} */
79 var HOST_DOWNLOAD_URLS
= {
80 'Win32': 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
81 'chromeremotedesktophost.msi',
82 'Win64': 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
83 'chromeremotedesktophost.msi',
84 'MacIntel': 'https://dl.google.com/chrome-remote-desktop/' +
85 'chromeremotedesktop.dmg',
86 'Linux x86_64': 'https://dl.google.com/linux/direct/' +
87 'chrome-remote-desktop_current_amd64.deb',
88 'Linux i386': 'https://dl.google.com/linux/direct/' +
89 'chrome-remote-desktop_current_i386.deb',
90 'Linux i686': 'https://dl.google.com/linux/direct/' +
91 'chrome-remote-desktop_current_i386.deb'
95 * Returns true if the host is installable on the current platform.
98 remoting
.HostInstaller
.canInstall = function() {
99 return !!HOST_DOWNLOAD_URLS
[navigator
.platform
];
103 * @throws {Error} Throws if there is no matching host binary for the current
106 remoting
.HostInstaller
.prototype.download = function() {
107 var hostPackageUrl
= HOST_DOWNLOAD_URLS
[navigator
.platform
];
108 if (hostPackageUrl
=== undefined) {
109 console
.error("Tried to install host on " + navigator
.platform
);
110 throw new Error(remoting
.Error
.unexpected());
113 // Start downloading the package.
114 if (base
.isAppsV2()) {
115 // TODO(jamiewalch): Use chrome.downloads when it is available to
116 // apps v2 (http://crbug.com/174046)
117 window
.open(hostPackageUrl
);
119 window
.location
= hostPackageUrl
;
123 /** @return {Promise} */
124 remoting
.HostInstaller
.prototype.downloadAndWaitForInstall = function() {
125 /** @type {remoting.HostInstaller} */
131 var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS
= 1000;
133 return remoting
.HostInstaller
.isInstalled().then(
134 /** @param {boolean} installed */
137 return Promise
.resolve(true);
140 if (that
.downloadAndWaitForInstallPromise_
=== null) {
141 that
.downloadAndWaitForInstallPromise_
= new Promise(
142 /** @param {Function} resolve */
145 that
.checkInstallIntervalId_
= window
.setInterval(function() {
146 remoting
.HostInstaller
.isInstalled().then(
147 /** @param {boolean} installed */
148 function(installed
) {
154 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS
);
157 return that
.downloadAndWaitForInstallPromise_
;
162 * Stops waiting for the host to be installed.
164 * var promise = hostInstaller.downloadAndWaitForInstall();
165 * hostInstaller.cancel(); // This will prevent |promise| from fulfilling.
167 remoting
.HostInstaller
.prototype.cancel = function() {
168 if (this.checkInstallIntervalId_
!== null) {
169 window
.clearInterval(this.checkInstallIntervalId_
);
170 this.checkInstallIntervalId_
= null;
172 this.downloadAndWaitForInstallPromise_
= null;