-
Notifications
You must be signed in to change notification settings - Fork 8
/
host_installer.js
180 lines (160 loc) · 5.34 KB
/
host_installer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
*
* HostInstaller allows the caller to download the host binary and monitor the
* install progress of the host by pinging the host periodically via native
* messaging.
*
* To download the host and wait for install:
* var hostInstaller = new remoting.HostInstaller();
* hostInstaller.downloadAndWaitForInstall().then(function() {
* // Install has completed.
* }, function(){
* // Download has failed.
* })
*
* To stop listening to the install progress:
* hostInstaller.cancel();
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
(function() {
/**
* @constructor
*/
remoting.HostInstaller = function() {
/**
* @type {Promise}
* @private
*/
this.downloadAndWaitForInstallPromise_ = null;
/**
* @type {?number}
* @private
*/
this.checkInstallIntervalId_ = null;
};
/**
* @return {Promise} The promise will resolve to a boolean value indicating
* whether the host is installed or not.
*/
remoting.HostInstaller.isInstalled = function() {
// Always do a fresh check as we don't get notified when the host is
// uninstalled.
/** @param {function(*=):void} resolve */
return new Promise(function(resolve) {
// TODO(kelvinp): Use different native messaging ports for the Me2me host
// vs It2MeHost.
/** @type {chrome.runtime.Port} */
var port =
chrome.runtime.connectNative('com.google.chrome.remote_assistance');
function onMessage() {
port.onDisconnect.removeListener(onDisconnected);
port.onMessage.removeListener(onMessage);
port.disconnect();
resolve(true);
}
function onDisconnected() {
port.onDisconnect.removeListener(onDisconnected);
port.onMessage.removeListener(onMessage);
resolve(false);
}
port.onDisconnect.addListener(onDisconnected);
port.onMessage.addListener(onMessage);
port.postMessage({type: 'hello'});
});
};
/** @type {Object<string,string>} */
var HOST_DOWNLOAD_URLS = {
'Win32': 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
'chromeremotedesktophost.msi',
'Win64': 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' +
'chromeremotedesktophost.msi',
'MacIntel': 'https://dl.google.com/chrome-remote-desktop/' +
'chromeremotedesktop.dmg',
'Linux x86_64': 'https://dl.google.com/linux/direct/' +
'chrome-remote-desktop_current_amd64.deb',
'Linux i386': 'https://dl.google.com/linux/direct/' +
'chrome-remote-desktop_current_i386.deb',
'Linux i686': 'https://dl.google.com/linux/direct/' +
'chrome-remote-desktop_current_i386.deb'
};
/**
* Returns true if the host is installable on the current platform.
* @returns {boolean}
*/
remoting.HostInstaller.canInstall = function() {
return !!HOST_DOWNLOAD_URLS[navigator.platform];
};
/**
* @throws {Error} Throws if there is no matching host binary for the current
* platform.
*/
remoting.HostInstaller.prototype.download = function() {
var hostPackageUrl = HOST_DOWNLOAD_URLS[navigator.platform];
if (hostPackageUrl === undefined) {
console.error("Tried to install host on " + navigator.platform);
throw new Error(remoting.Error.UNEXPECTED);
}
// Start downloading the package.
if (base.isAppsV2()) {
// TODO(jamiewalch): Use chrome.downloads when it is available to
// apps v2 (http://crbug.com/174046)
window.open(hostPackageUrl);
} else {
window.location = hostPackageUrl;
}
};
/** @return {Promise} */
remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() {
/** @type {remoting.HostInstaller} */
var that = this;
/**
* @type {number}
* @const
*/
var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS = 1000;
return remoting.HostInstaller.isInstalled().then(
/** @param {boolean} installed */
function(installed){
if (installed) {
return Promise.resolve(true);
}
if (that.downloadAndWaitForInstallPromise_ === null) {
that.downloadAndWaitForInstallPromise_ = new Promise(
/** @param {Function} resolve */
function(resolve){
that.download();
that.checkInstallIntervalId_ = window.setInterval(function() {
remoting.HostInstaller.isInstalled().then(
/** @param {boolean} installed */
function(installed) {
if (installed) {
that.cancel();
resolve();
}
});
}, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS);
});
}
return that.downloadAndWaitForInstallPromise_;
});
};
/**
* Stops waiting for the host to be installed.
* For example
* var promise = hostInstaller.downloadAndWaitForInstall();
* hostInstaller.cancel(); // This will prevent |promise| from fulfilling.
*/
remoting.HostInstaller.prototype.cancel = function() {
if (this.checkInstallIntervalId_ !== null) {
window.clearInterval(this.checkInstallIntervalId_);
this.checkInstallIntervalId_ = null;
}
this.downloadAndWaitForInstallPromise_ = null;
};
})();