OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 /** |
| 6 * @fileoverview |
| 7 * |
| 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 |
| 10 * messaging. |
| 11 * |
| 12 * To download the host and wait for install: |
| 13 * var hostInstaller = new remoting.HostInstaller(); |
| 14 * hostInstaller.downloadAndWaitForInstall().then(function() { |
| 15 * // Install has completed. |
| 16 * }, function(){ |
| 17 * // Download has failed. |
| 18 * }) |
| 19 * |
| 20 * To stop listening to the install progress: |
| 21 * hostInstaller.cancel(); |
| 22 */ |
| 23 |
| 24 'use strict'; |
| 25 |
| 26 /** @suppress {duplicate} */ |
| 27 var remoting = remoting || {}; |
| 28 |
| 29 /** |
| 30 * @constructor |
| 31 */ |
| 32 remoting.HostInstaller = function() { |
| 33 /** |
| 34 * @type {Promise} |
| 35 * @private |
| 36 */ |
| 37 this.downloadAndWaitForInstallPromise_ = null; |
| 38 |
| 39 /** |
| 40 * @type {?number} |
| 41 * @private |
| 42 */ |
| 43 this.checkInstallIntervalId_ = null; |
| 44 }; |
| 45 |
| 46 /** |
| 47 * @return {Promise} The promise will resolve to a boolean value indicating |
| 48 * whether the host is installed or not. |
| 49 */ |
| 50 remoting.HostInstaller.prototype.isInstalled = function() { |
| 51 // Always do a fresh check as we don't get notified when the host is |
| 52 // uninstalled. |
| 53 |
| 54 /** @param {function(*=):void} resolve */ |
| 55 return new Promise(function(resolve) { |
| 56 // TODO(kelvinp): Use different native messaging ports for the Me2me host |
| 57 // vs It2MeHost. |
| 58 /** @type {chrome.runtime.Port} */ |
| 59 var port = |
| 60 chrome.runtime.connectNative('com.google.chrome.remote_assistance'); |
| 61 |
| 62 function onMessage() { |
| 63 port.onDisconnect.removeListener(onDisconnected); |
| 64 port.onMessage.removeListener(onMessage); |
| 65 port.disconnect(); |
| 66 resolve(true); |
| 67 } |
| 68 |
| 69 function onDisconnected() { |
| 70 port.onDisconnect.removeListener(onDisconnected); |
| 71 port.onMessage.removeListener(onMessage); |
| 72 resolve(false); |
| 73 } |
| 74 |
| 75 port.onDisconnect.addListener(onDisconnected); |
| 76 port.onMessage.addListener(onMessage); |
| 77 port.postMessage({type: 'hello'}); |
| 78 }); |
| 79 }; |
| 80 |
| 81 /** |
| 82 * @throws {Error} Throws if there is no matching host binary for the current |
| 83 * platform. |
| 84 * @private |
| 85 */ |
| 86 remoting.HostInstaller.prototype.download_ = function() { |
| 87 /** @type {Object.<string,string>} */ |
| 88 var hostDownloadUrls = { |
| 89 'Win32' : 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' + |
| 90 'chromeremotedesktophost.msi', |
| 91 'MacIntel' : 'https://dl.google.com/chrome-remote-desktop/' + |
| 92 'chromeremotedesktop.dmg', |
| 93 'Linux x86_64' : 'https://dl.google.com/linux/direct/' + |
| 94 'chrome-remote-desktop_current_amd64.deb', |
| 95 'Linux i386' : 'https://dl.google.com/linux/direct/' + |
| 96 'chrome-remote-desktop_current_i386.deb' |
| 97 }; |
| 98 |
| 99 var hostPackageUrl = hostDownloadUrls[navigator.platform]; |
| 100 if (hostPackageUrl === undefined) { |
| 101 throw new Error(remoting.Error.CANCELLED); |
| 102 } |
| 103 |
| 104 // Start downloading the package. |
| 105 if (remoting.isAppsV2) { |
| 106 // TODO(jamiewalch): Use chrome.downloads when it is available to |
| 107 // apps v2 (http://crbug.com/174046) |
| 108 window.open(hostPackageUrl); |
| 109 } else { |
| 110 window.location = hostPackageUrl; |
| 111 } |
| 112 }; |
| 113 |
| 114 /** @return {Promise} */ |
| 115 remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() { |
| 116 /** @type {remoting.HostInstaller} */ |
| 117 var that = this; |
| 118 /** |
| 119 * @type {number} |
| 120 * @const |
| 121 */ |
| 122 var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS = 1000; |
| 123 |
| 124 /** @param {boolean} installed */ |
| 125 return this.isInstalled().then(function(installed){ |
| 126 if (installed) { |
| 127 return Promise.resolve(true); |
| 128 } |
| 129 |
| 130 if (that.downloadAndWaitForInstallPromise_ !== null) { |
| 131 that.downloadAndWaitForInstallPromise_ = new Promise( |
| 132 /** @param {Function} resolve */ |
| 133 function(resolve){ |
| 134 that.download_(); |
| 135 that.checkInstallIntervalId_ = window.setInterval(function() { |
| 136 /** @param {boolean} installed */ |
| 137 that.isInstalled().then(function(installed) { |
| 138 if (installed) { |
| 139 that.cancel(); |
| 140 resolve(); |
| 141 } |
| 142 }); |
| 143 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS); |
| 144 }); |
| 145 } |
| 146 return that.downloadAndWaitForInstallPromise_; |
| 147 }); |
| 148 }; |
| 149 |
| 150 /** |
| 151 * Stops waiting for the host to be installed. |
| 152 * For example |
| 153 * var promise = hostInstaller.downloadAndWaitForInstall(); |
| 154 * hostInstaller.cancel(); // This will prevent |promise| from fulfilling. |
| 155 */ |
| 156 remoting.HostInstaller.prototype.cancel = function() { |
| 157 if (this.checkInstallIntervalId_ !== null) { |
| 158 window.clearInterval(this.checkInstallIntervalId_); |
| 159 this.checkInstallIntervalId_ = null; |
| 160 } |
| 161 this.downloadAndWaitForInstallPromise_ = null; |
| 162 }; |
OLD | NEW |