Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / remoting / webapp / crd / js / host_installer.js
blob6386fe73b5e49b7980f163465117759982c0757e
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.
5 /**
6 * @fileoverview
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.
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 * })
20 * To stop listening to the install progress:
21 * hostInstaller.cancel();
24 'use strict';
26 /** @suppress {duplicate} */
27 var remoting = remoting || {};
29 (function() {
31 /**
32 * @constructor
34 remoting.HostInstaller = function() {
35 /** @private {Promise} */
36 this.downloadAndWaitForInstallPromise_ = null;
38 /** @private {?number} */
39 this.checkInstallIntervalId_ = null;
42 /**
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
48 // uninstalled.
50 return new Promise(
51 /** @param {function(*=):void} resolve */
52 function(resolve) {
53 // TODO(kelvinp): Use different native messaging ports for the Me2me
54 // host vs It2MeHost.
55 /** @type {Port} */
56 var port =
57 chrome.runtime.connectNative('com.google.chrome.remote_assistance');
59 function onMessage() {
60 port.onDisconnect.removeListener(onDisconnected);
61 port.onMessage.removeListener(onMessage);
62 port.disconnect();
63 resolve(true);
66 function onDisconnected() {
67 port.onDisconnect.removeListener(onDisconnected);
68 port.onMessage.removeListener(onMessage);
69 resolve(false);
72 port.onDisconnect.addListener(onDisconnected);
73 port.onMessage.addListener(onMessage);
74 port.postMessage({type: 'hello'});
75 });
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'
94 /**
95 * Returns true if the host is installable on the current platform.
96 * @returns {boolean}
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
104 * platform.
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);
118 } else {
119 window.location = hostPackageUrl;
123 /** @return {Promise} */
124 remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() {
125 /** @type {remoting.HostInstaller} */
126 var that = this;
128 * @type {number}
129 * @const
131 var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS = 1000;
133 return remoting.HostInstaller.isInstalled().then(
134 /** @param {boolean} installed */
135 function(installed){
136 if (installed) {
137 return Promise.resolve(true);
140 if (that.downloadAndWaitForInstallPromise_ === null) {
141 that.downloadAndWaitForInstallPromise_ = new Promise(
142 /** @param {Function} resolve */
143 function(resolve){
144 that.download();
145 that.checkInstallIntervalId_ = window.setInterval(function() {
146 remoting.HostInstaller.isInstalled().then(
147 /** @param {boolean} installed */
148 function(installed) {
149 if (installed) {
150 that.cancel();
151 resolve();
154 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS);
157 return that.downloadAndWaitForInstallPromise_;
162 * Stops waiting for the host to be installed.
163 * For example
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;
175 })();