Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / crd / js / host_installer.js
blob9324a0ffbe2481b7c3f86c25d232848911d221cf
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
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  */
24 'use strict';
26 /** @suppress {duplicate} */
27 var remoting = remoting || {};
29 (function() {
31 /**
32  * @constructor
33  */
34 remoting.HostInstaller = function() {
35   /**
36    * @type {Promise}
37    * @private
38    */
39   this.downloadAndWaitForInstallPromise_ = null;
41   /**
42    * @type {?number}
43    * @private
44    */
45   this.checkInstallIntervalId_ = null;
48 /**
49  * @return {Promise}  The promise will resolve to a boolean value indicating
50  *     whether the host is installed or not.
51  */
52 remoting.HostInstaller.isInstalled = function() {
53   // Always do a fresh check as we don't get notified when the host is
54   // uninstalled.
56   /** @param {function(*=):void} resolve */
57   return new Promise(function(resolve) {
58     // TODO(kelvinp): Use different native messaging ports for the Me2me host
59     // vs It2MeHost.
60     /** @type {chrome.runtime.Port} */
61     var port =
62         chrome.runtime.connectNative('com.google.chrome.remote_assistance');
64     function onMessage() {
65       port.onDisconnect.removeListener(onDisconnected);
66       port.onMessage.removeListener(onMessage);
67       port.disconnect();
68       resolve(true);
69     }
71     function onDisconnected() {
72       port.onDisconnect.removeListener(onDisconnected);
73       port.onMessage.removeListener(onMessage);
74       resolve(false);
75     }
77     port.onDisconnect.addListener(onDisconnected);
78     port.onMessage.addListener(onMessage);
79     port.postMessage({type: 'hello'});
80   });
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'
99 /**
100  * Returns true if the host is installable on the current platform.
101  * @returns {boolean}
102  */
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
109  *     platform.
110  */
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);
116   }
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);
123   } else {
124     window.location = hostPackageUrl;
125   }
128 /** @return {Promise} */
129 remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() {
130   /** @type {remoting.HostInstaller} */
131   var that = this;
132   /**
133    * @type {number}
134    * @const
135    */
136   var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS = 1000;
138   return remoting.HostInstaller.isInstalled().then(
139       /** @param {boolean} installed */
140       function(installed){
141         if (installed) {
142           return Promise.resolve(true);
143         }
145         if (that.downloadAndWaitForInstallPromise_ === null) {
146           that.downloadAndWaitForInstallPromise_ = new Promise(
147               /** @param {Function} resolve */
148               function(resolve){
149                 that.download();
150                 that.checkInstallIntervalId_ = window.setInterval(function() {
151                   remoting.HostInstaller.isInstalled().then(
152                       /** @param {boolean} installed */
153                       function(installed) {
154                         if (installed) {
155                           that.cancel();
156                           resolve();
157                         }
158                       });
159                 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS);
160               });
161         }
162         return that.downloadAndWaitForInstallPromise_;
163       });
167  * Stops waiting for the host to be installed.
168  * For example
169  *   var promise = hostInstaller.downloadAndWaitForInstall();
170  *   hostInstaller.cancel(); // This will prevent |promise| from fulfilling.
171  */
172 remoting.HostInstaller.prototype.cancel = function() {
173   if (this.checkInstallIntervalId_ !== null) {
174     window.clearInterval(this.checkInstallIntervalId_);
175     this.checkInstallIntervalId_ = null;
176   }
177   this.downloadAndWaitForInstallPromise_ = null;
180 })();