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|null} | |
Jamie
2014/08/13 02:50:39
I think this is equivalent to ?number
kelvinp
2014/08/14 00:09:34
Done.
| |
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) { | |
Jamie
2014/08/13 02:50:39
Why resolve(bool) instead of resolve/reject? I thi
kelvinp
2014/08/14 00:09:34
I think as a pattern, reject should be used for ha
| |
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 when the there is not matching host binary for the | |
Jamie
2014/08/13 02:50:39
s/when/if/
s/the//
s/not/no/
kelvinp
2014/08/14 00:09:34
Done.
| |
83 * current platform. | |
Jamie
2014/08/13 02:50:39
@private
kelvinp
2014/08/14 00:09:33
Done.
| |
84 */ | |
85 remoting.HostInstaller.prototype.download_ = function() { | |
86 /** @type {Object.<string,string>} */ | |
87 var hostDownloadUrls = { | |
88 'Win32' : 'http://dl.google.com/dl/edgedl/chrome-remote-desktop/' + | |
89 'chromeremotedesktophost.msi', | |
90 'MacIntel' : 'https://dl.google.com/chrome-remote-desktop/' + | |
91 'chromeremotedesktop.dmg', | |
92 'Linux x86_64' : 'https://dl.google.com/linux/direct/' + | |
93 'chrome-remote-desktop_current_amd64.deb', | |
94 'Linux i386' : 'https://dl.google.com/linux/direct/' + | |
95 'chrome-remote-desktop_current_i386.deb' | |
96 }; | |
97 | |
98 var hostPackageUrl = hostDownloadUrls[navigator.platform]; | |
99 if (hostPackageUrl === undefined) { | |
100 throw new Error(remoting.Error.CANCELLED); | |
101 } | |
102 | |
103 // Start downloading the package. | |
104 if (remoting.isAppsV2) { | |
105 // TODO(jamiewalch): Use chrome.downloads when it is available to | |
106 // apps v2 (http://crbug.com/174046) | |
107 window.open(hostPackageUrl); | |
108 } else { | |
109 window.location = hostPackageUrl; | |
110 } | |
111 }; | |
112 | |
113 /** @return {Promise} */ | |
114 remoting.HostInstaller.prototype.downloadAndWaitForInstall = function() { | |
115 /** @type {remoting.HostInstaller} */ | |
116 var that = this; | |
117 /** | |
118 * @type {number} | |
119 * @const | |
120 */ | |
121 var CHECK_INSTALL_INTERVAL_IN_MILLISECONDS = 1000; | |
122 | |
123 /** @param {boolean} installed */ | |
124 return this.isInstalled().then(function(installed){ | |
125 if (installed) { | |
126 return Promise.resolve(true); | |
127 } | |
128 | |
129 if (that.downloadAndWaitForInstallPromise_ !== null) { | |
Jamie
2014/08/13 02:50:38
Why is it important to save this promise rather th
kelvinp
2014/08/14 00:09:33
The downloadAndWaitForInstallPromise_ is quite exp
| |
130 that.downloadAndWaitForInstallPromise_ = new Promise( | |
131 /** @param {Function} resolve */ | |
132 function(resolve){ | |
133 that.download_(); | |
134 that.checkInstallIntervalId_ = window.setInterval(function() { | |
135 /** @param {boolean} installed */ | |
136 that.isInstalled().then(function(installed) { | |
137 if (installed) { | |
138 that.cancel(); | |
139 resolve(); | |
140 } | |
141 }); | |
142 }, CHECK_INSTALL_INTERVAL_IN_MILLISECONDS); | |
143 }); | |
144 } | |
145 return that.downloadAndWaitForInstallPromise_; | |
146 }); | |
147 }; | |
148 | |
149 /** | |
150 * Stops waiting for the host to be installed. | |
151 * For example | |
152 * var promise = hostInstaller.downloadAndWaitForInstall(); | |
153 * hostInstaller.cancel(); // This will prevent |promise| from fulfilling. | |
154 */ | |
155 remoting.HostInstaller.prototype.cancel = function() { | |
156 if (this.checkInstallIntervalId_ !== null) { | |
157 window.clearInterval(this.checkInstallIntervalId_); | |
158 this.checkInstallIntervalId_ = null; | |
159 } | |
160 this.downloadAndWaitForInstallPromise_ = null; | |
161 }; | |
OLD | NEW |