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() {
39 this.downloadAndWaitForInstallPromise_ = null;
45 this.checkInstallIntervalId_ = null;
49 * @return {Promise} The promise will resolve to a boolean value indicating
50 * whether the host is installed or not.
52 remoting.HostInstaller.isInstalled = function() {
53 // Always do a fresh check as we don't get notified when the host is
56 /** @param {function(*=):void} resolve */
57 return new Promise(function(resolve) {
58 // TODO(kelvinp): Use different native messaging ports for the Me2me host
60 /** @type {chrome.runtime.Port} */
62 chrome.runtime.connectNative('com.google.chrome.remote_assistance');
64 function onMessage() {
65 port.onDisconnect.removeListener(onDisconnected);
66 port.onMessage.removeListener(onMessage);
71 function onDisconnected() {
72 port.onDisconnect.removeListener(onDisconnected);
73 port.onMessage.removeListener(onMessage);
77 port.onDisconnect.addListener(onDisconnected);
78 port.onMessage.addListener(onMessage);
79 port.postMessage({type: 'hello'});
83 /** @type {Object<string,string>} */
84 var HOST_DOWNLOAD_URLS = {
85 'Win32': 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
86 'chromeremotedesktophost.msi',
87 'Win64': 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
88 'chromeremotedesktophost.msi',
89 'MacIntel': 'https://dl.google.com/chrome-remote-desktop/' +
90 'chromeremotedesktop.dmg',
91 'Linux x86_64': 'https://dl.google.com/linux/direct/' +
92 'chrome-remote-desktop_current_amd64.deb',
93 'Linux i386': 'https://dl.google.com/linux/direct/' +
94 'chrome-remote-desktop_current_i386.deb',
95 'Linux i686': 'https://dl.google.com/linux/direct/' +
96 'chrome-remote-desktop_current_i386.deb'
100 * Returns true if the host is installable on the current platform.
103 remoting.HostInstaller.canInstall = function() {
104 return !!HOST_DOWNLOAD_URLS[navigator.platform];
108 * @throws {Error} Throws if there is no matching host binary for the current
111 remoting.HostInstaller.prototype.download = function() {
112 var hostPackageUrl = HOST_DOWNLOAD_URLS[navigator.platform];
113 if (hostPackageUrl === undefined) {
114 console.error("Tried to install host on " + navigator.platform);
115 throw new Error(remoting.Error.UNEXPECTED);
118 // Start downloading the package.
119 if (base.isAppsV2()) {
120 // TODO(jamiewalch): Use chrome.downloads when it is available to
121 // apps v2 (http://crbug.com/174046)
122 window.open(hostPackageUrl);
124 window.location = hostPackageUrl;
128 /** @return {Promise} */
129 remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() {
130 /** @type {remoting.HostInstaller} */
136 var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS = 1000;
138 return remoting.HostInstaller.isInstalled().then(
139 /** @param {boolean} installed */
142 return Promise.resolve(true);
145 if (that.downloadAndWaitForInstallPromise_ === null) {
146 that.downloadAndWaitForInstallPromise_ = new Promise(
147 /** @param {Function} resolve */
150 that.checkInstallIntervalId_ = window.setInterval(function() {
151 remoting.HostInstaller.isInstalled().then(
152 /** @param {boolean} installed */
153 function(installed) {
159 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS);
162 return that.downloadAndWaitForInstallPromise_;
167 * Stops waiting for the host to be installed.
169 * var promise = hostInstaller.downloadAndWaitForInstall();
170 * hostInstaller.cancel(); // This will prevent |promise| from fulfilling.
172 remoting.HostInstaller.prototype.cancel = function() {
173 if (this.checkInstallIntervalId_ !== null) {
174 window.clearInterval(this.checkInstallIntervalId_);
175 this.checkInstallIntervalId_ = null;
177 this.downloadAndWaitForInstallPromise_ = null;