Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[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
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   /** @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.
45  */
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);
64         }
66         function onDisconnected() {
67           port.onDisconnect.removeListener(onDisconnected);
68           port.onMessage.removeListener(onMessage);
69           resolve(false);
70         }
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}
97  */
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.
105  */
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());
111   }
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;
120   }
123 /** @return {Promise} */
124 remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() {
125   /** @type {remoting.HostInstaller} */
126   var that = this;
127   /**
128    * @type {number}
129    * @const
130    */
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);
138         }
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();
152                         }
153                       });
154                 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS);
155               });
156         }
157         return that.downloadAndWaitForInstallPromise_;
158       });
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.
166  */
167 remoting.HostInstaller.prototype.cancel = function() {
168   if (this.checkInstallIntervalId_ !== null) {
169     window.clearInterval(this.checkInstallIntervalId_);
170     this.checkInstallIntervalId_ = null;
171   }
172   this.downloadAndWaitForInstallPromise_ = null;
175 })();