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
|| {};
32 remoting
.HostInstaller = function() {
37 this.downloadAndWaitForInstallPromise_
= null;
43 this.checkInstallIntervalId_
= null;
47 * @return {Promise} The promise will resolve to a boolean value indicating
48 * whether the host is installed or not.
50 remoting
.HostInstaller
.prototype.isInstalled = function() {
51 // Always do a fresh check as we don't get notified when the host is
54 /** @param {function(*=):void} resolve */
55 return new Promise(function(resolve
) {
56 // TODO(kelvinp): Use different native messaging ports for the Me2me host
58 /** @type {chrome.runtime.Port} */
60 chrome
.runtime
.connectNative('com.google.chrome.remote_assistance');
62 function onMessage() {
63 port
.onDisconnect
.removeListener(onDisconnected
);
64 port
.onMessage
.removeListener(onMessage
);
69 function onDisconnected() {
70 port
.onDisconnect
.removeListener(onDisconnected
);
71 port
.onMessage
.removeListener(onMessage
);
75 port
.onDisconnect
.addListener(onDisconnected
);
76 port
.onMessage
.addListener(onMessage
);
77 port
.postMessage({type
: 'hello'});
82 * @throws {Error} Throws if there is no matching host binary for the current
85 remoting
.HostInstaller
.prototype.download = function() {
86 /** @type {Object.<string,string>} */
87 var hostDownloadUrls
= {
88 'Win32' : 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
89 'chromeremotedesktophost.msi',
90 'Win64' : 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
91 'chromeremotedesktophost.msi',
92 'MacIntel' : 'https://dl.google.com/chrome-remote-desktop/' +
93 'chromeremotedesktop.dmg',
94 'Linux x86_64' : 'https://dl.google.com/linux/direct/' +
95 'chrome-remote-desktop_current_amd64.deb',
96 'Linux i386' : 'https://dl.google.com/linux/direct/' +
97 'chrome-remote-desktop_current_i386.deb'
100 var hostPackageUrl
= hostDownloadUrls
[navigator
.platform
];
101 if (hostPackageUrl
=== undefined) {
102 throw new Error(remoting
.Error
.CANCELLED
);
105 // Start downloading the package.
106 if (base
.isAppsV2()) {
107 // TODO(jamiewalch): Use chrome.downloads when it is available to
108 // apps v2 (http://crbug.com/174046)
109 window
.open(hostPackageUrl
);
111 window
.location
= hostPackageUrl
;
115 /** @return {Promise} */
116 remoting
.HostInstaller
.prototype.downloadAndWaitForInstall = function() {
117 /** @type {remoting.HostInstaller} */
123 var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS
= 1000;
125 /** @param {boolean} installed */
126 return this.isInstalled().then(function(installed
){
128 return Promise
.resolve(true);
131 if (that
.downloadAndWaitForInstallPromise_
!== null) {
132 that
.downloadAndWaitForInstallPromise_
= new Promise(
133 /** @param {Function} resolve */
136 that
.checkInstallIntervalId_
= window
.setInterval(function() {
137 /** @param {boolean} installed */
138 that
.isInstalled().then(function(installed
) {
144 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS
);
147 return that
.downloadAndWaitForInstallPromise_
;
152 * Stops waiting for the host to be installed.
154 * var promise = hostInstaller.downloadAndWaitForInstall();
155 * hostInstaller.cancel(); // This will prevent |promise| from fulfilling.
157 remoting
.HostInstaller
.prototype.cancel = function() {
158 if (this.checkInstallIntervalId_
!== null) {
159 window
.clearInterval(this.checkInstallIntervalId_
);
160 this.checkInstallIntervalId_
= null;
162 this.downloadAndWaitForInstallPromise_
= null;