OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** @suppress {duplicate} */ | 7 /** @suppress {duplicate} */ |
8 var remoting = remoting || {}; | 8 var remoting = remoting || {}; |
9 | 9 |
10 /** @constructor */ | 10 /** @constructor */ |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 var keyPairPromise = this.hostDaemonFacade_.generateKeyPair(); | 158 var keyPairPromise = this.hostDaemonFacade_.generateKeyPair(); |
159 var hostClientIdPromise = hasOauthPromise.then(function(hasOauth) { | 159 var hostClientIdPromise = hasOauthPromise.then(function(hasOauth) { |
160 if (hasOauth) { | 160 if (hasOauth) { |
161 return that.hostDaemonFacade_.getHostClientId(); | 161 return that.hostDaemonFacade_.getHostClientId(); |
162 } else { | 162 } else { |
163 return null; | 163 return null; |
164 } | 164 } |
165 }); | 165 }); |
166 var newHostId = base.generateUuid(); | 166 var newHostId = base.generateUuid(); |
167 var pinHashPromise = this.hostDaemonFacade_.getPinHash(newHostId, hostPin); | 167 var pinHashPromise = this.hostDaemonFacade_.getPinHash(newHostId, hostPin); |
| 168 var hostOwnerPromise = this.getClientBaseJid_(); |
168 | 169 |
169 /** @type {boolean} */ | 170 /** @type {boolean} */ |
170 var hostRegistered = false; | 171 var hostRegistered = false; |
171 | 172 |
172 // Register the host and extract an optional auth code from the host | 173 // Register the host and extract an auth code from the host response |
173 // response. The absence of an auth code is represented by an empty | 174 // and, optionally an email address for the robot account. |
174 // string. | 175 /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ |
175 /** @type {!Promise<string>} */ | 176 var regResultPromise = Promise.all([ |
176 var authCodePromise = Promise.all([ | |
177 hostClientIdPromise, | 177 hostClientIdPromise, |
178 hostNamePromise, | 178 hostNamePromise, |
179 keyPairPromise | 179 keyPairPromise |
180 ]).then(function(/** Array */ a) { | 180 ]).then(function(/** Array */ a) { |
181 var hostClientId = /** @type {string} */ (a[0]); | 181 var hostClientId = /** @type {string} */ (a[0]); |
182 var hostName = /** @type {string} */ (a[1]); | 182 var hostName = /** @type {string} */ (a[1]); |
183 var keyPair = /** @type {remoting.KeyPair} */ (a[2]); | 183 var keyPair = /** @type {remoting.KeyPair} */ (a[2]); |
184 | 184 |
185 return remoting.HostListApi.getInstance().register( | 185 return remoting.HostListApi.getInstance().register( |
186 newHostId, hostName, keyPair.publicKey, hostClientId); | 186 newHostId, hostName, keyPair.publicKey, hostClientId); |
187 }).then(function(/** string */ result) { | 187 }).then(function(/** remoting.HostListApi.RegisterResult */ result) { |
188 hostRegistered = true; | 188 hostRegistered = true; |
189 return result; | 189 return result; |
190 }); | 190 }); |
191 | 191 |
192 // Get XMPP creditials. | 192 // Get XMPP creditials. |
193 var xmppCredsPromise = authCodePromise.then(function(authCode) { | 193 var xmppCredsPromise = regResultPromise.then(function(regResult) { |
194 if (authCode) { | 194 base.debug.assert(regResult.authCode != ''); |
| 195 if (regResult.email) { |
| 196 // Use auth code and email supplied by GCD. |
| 197 return that.hostDaemonFacade_.getRefreshTokenFromAuthCode( |
| 198 regResult.authCode).then(function(token) { |
| 199 return { |
| 200 userEmail: regResult.email, |
| 201 refreshToken: token |
| 202 }; |
| 203 }); |
| 204 } else { |
195 // Use auth code supplied by Chromoting registry. | 205 // Use auth code supplied by Chromoting registry. |
196 return that.hostDaemonFacade_.getCredentialsFromAuthCode(authCode); | 206 return that.hostDaemonFacade_.getCredentialsFromAuthCode( |
197 } else { | 207 regResult.authCode); |
198 // No authorization code returned, use regular Chrome | |
199 // identity credential flow. | |
200 return remoting.identity.getEmail().then(function(/** string */ email) { | |
201 return { | |
202 userEmail: email, | |
203 refreshToken: remoting.oauth2.getRefreshToken() | |
204 }; | |
205 }); | |
206 } | 208 } |
207 }); | 209 }); |
208 | 210 |
209 // Get as JID to use as the host owner. | 211 // Build the host configuration. |
210 var hostOwnerPromise = authCodePromise.then(function(authCode) { | |
211 if (authCode) { | |
212 return that.getClientBaseJid_(); | |
213 } else { | |
214 return remoting.identity.getEmail(); | |
215 } | |
216 }); | |
217 | |
218 // Build an initial host configuration. | |
219 /** @type {!Promise<!Object>} */ | 212 /** @type {!Promise<!Object>} */ |
220 var hostConfigNoOwnerPromise = Promise.all([ | 213 var hostConfigPromise = Promise.all([ |
221 hostNamePromise, | 214 hostNamePromise, |
222 pinHashPromise, | 215 pinHashPromise, |
223 xmppCredsPromise, | 216 xmppCredsPromise, |
224 keyPairPromise | 217 keyPairPromise, |
| 218 hostOwnerPromise, |
| 219 remoting.identity.getEmail() |
225 ]).then(function(/** Array */ a) { | 220 ]).then(function(/** Array */ a) { |
226 var hostName = /** @type {string} */ (a[0]); | 221 var hostName = /** @type {string} */ (a[0]); |
227 var hostSecretHash = /** @type {string} */ (a[1]); | 222 var hostSecretHash = /** @type {string} */ (a[1]); |
228 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[2]); | 223 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[2]); |
229 var keyPair = /** @type {remoting.KeyPair} */ (a[3]); | 224 var keyPair = /** @type {remoting.KeyPair} */ (a[3]); |
230 return { | 225 var hostOwner = /** @type {string} */ (a[4]); |
| 226 var hostOwnerEmail = /** @type {string} */ (a[5]); |
| 227 var hostConfig = { |
231 xmpp_login: xmppCreds.userEmail, | 228 xmpp_login: xmppCreds.userEmail, |
232 oauth_refresh_token: xmppCreds.refreshToken, | 229 oauth_refresh_token: xmppCreds.refreshToken, |
233 host_id: newHostId, | 230 host_id: newHostId, |
234 host_name: hostName, | 231 host_name: hostName, |
235 host_secret_hash: hostSecretHash, | 232 host_secret_hash: hostSecretHash, |
236 private_key: keyPair.privateKey | 233 private_key: keyPair.privateKey, |
| 234 host_owner: hostOwner |
237 }; | 235 }; |
238 }); | 236 if (hostOwnerEmail != hostOwner) { |
239 | 237 hostConfig['host_owner_email'] = hostOwnerEmail; |
240 // Add host_owner and host_owner_email fields to the host config if | |
241 // necessary. This promise resolves to the same value as | |
242 // hostConfigNoOwnerPromise, with not until the extra fields are | |
243 // either added or determined to be redundant. | |
244 /** @type {!Promise<!Object>} */ | |
245 var hostConfigWithOwnerPromise = Promise.all([ | |
246 hostConfigNoOwnerPromise, | |
247 hostOwnerPromise, | |
248 remoting.identity.getEmail(), | |
249 xmppCredsPromise | |
250 ]).then(function(/** Array */ a) { | |
251 var hostConfig = /** @type {!Object} */ (a[0]); | |
252 var hostOwner = /** @type {string} */ (a[1]); | |
253 var hostOwnerEmail = /** @type {string} */ (a[2]); | |
254 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[3]); | |
255 if (hostOwner != xmppCreds.userEmail) { | |
256 hostConfig['host_owner'] = hostOwner; | |
257 if (hostOwnerEmail != hostOwner) { | |
258 hostConfig['host_owner_email'] = hostOwnerEmail; | |
259 } | |
260 } | 238 } |
261 return hostConfig; | 239 return hostConfig; |
262 }); | 240 }); |
263 | 241 |
264 // Start the daemon. | 242 // Start the daemon. |
265 /** @type {!Promise<remoting.HostController.AsyncResult>} */ | 243 /** @type {!Promise<remoting.HostController.AsyncResult>} */ |
266 var startDaemonResultPromise = | 244 var startDaemonResultPromise = |
267 hostConfigWithOwnerPromise.then(function(hostConfig) { | 245 hostConfigPromise.then(function(hostConfig) { |
268 return that.hostDaemonFacade_.startDaemon(hostConfig, consent); | 246 return that.hostDaemonFacade_.startDaemon(hostConfig, consent); |
269 }); | 247 }); |
270 | 248 |
271 // Update the UI or report an error. | 249 // Update the UI or report an error. |
272 return startDaemonResultPromise.then(function(result) { | 250 return startDaemonResultPromise.then(function(result) { |
273 if (result == remoting.HostController.AsyncResult.OK) { | 251 if (result == remoting.HostController.AsyncResult.OK) { |
274 return hostNamePromise.then(function(hostName) { | 252 return hostNamePromise.then(function(hostName) { |
275 return keyPairPromise.then(function(keyPair) { | 253 return keyPairPromise.then(function(keyPair) { |
276 remoting.hostList.onLocalHostStarted( | 254 remoting.hostList.onLocalHostStarted( |
277 hostName, newHostId, keyPair.publicKey); | 255 hostName, newHostId, keyPair.publicKey); |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 emailPromise.then(function(/** string */ email) { | 485 emailPromise.then(function(/** string */ email) { |
508 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); | 486 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); |
509 }); | 487 }); |
510 }); | 488 }); |
511 | 489 |
512 return result; | 490 return result; |
513 }; | 491 }; |
514 | 492 |
515 /** @type {remoting.HostController} */ | 493 /** @type {remoting.HostController} */ |
516 remoting.hostController = null; | 494 remoting.hostController = null; |
OLD | NEW |