OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
6 * @fileoverview A class for managing all enumerated gnubby devices. | 6 * @fileoverview A class for managing all enumerated gnubby devices. |
7 */ | 7 */ |
8 'use strict'; | 8 'use strict'; |
9 | 9 |
10 /** | 10 /** |
(...skipping 12 matching lines...) Expand all Loading... |
23 * device: number | 23 * device: number |
24 * }} | 24 * }} |
25 */ | 25 */ |
26 var GnubbyDeviceId; | 26 var GnubbyDeviceId; |
27 | 27 |
28 /** | 28 /** |
29 * Ways in which gnubby devices are enumerated. | 29 * Ways in which gnubby devices are enumerated. |
30 * @const | 30 * @const |
31 * @enum {number} | 31 * @enum {number} |
32 */ | 32 */ |
33 var GnubbyEnumerationTypes = { | 33 var GnubbyEnumerationTypes = {ANY: 0, VID_PID: 1, FIDO_U2F: 2}; |
34 ANY: 0, | |
35 VID_PID: 1, | |
36 FIDO_U2F: 2 | |
37 }; | |
38 | 34 |
39 /** | 35 /** |
40 * @typedef {{ | 36 * @typedef {{ |
41 * isSharedAccess: boolean, | 37 * isSharedAccess: boolean, |
42 * enumerate: function(function(Array), GnubbyEnumerationTypes=), | 38 * enumerate: function(function(Array), GnubbyEnumerationTypes=), |
43 * deviceToDeviceId: function(*): GnubbyDeviceId, | 39 * deviceToDeviceId: function(*): GnubbyDeviceId, |
44 * open: function(Gnubbies, number, *, function(number, GnubbyDevice=)), | 40 * open: function(Gnubbies, number, *, function(number, GnubbyDevice=)), |
45 * cancelOpen: (undefined|function(Gnubbies, number, *)) | 41 * cancelOpen: (undefined|function(Gnubbies, number, *)) |
46 * }} | 42 * }} |
47 */ | 43 */ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 this.namespaces_.push(namespace); | 76 this.namespaces_.push(namespace); |
81 } | 77 } |
82 this.impl_[namespace] = impl; | 78 this.impl_[namespace] = impl; |
83 }; | 79 }; |
84 | 80 |
85 /** | 81 /** |
86 * @param {GnubbyDeviceId} id The device id. | 82 * @param {GnubbyDeviceId} id The device id. |
87 * @return {boolean} Whether the device is a shared access device. | 83 * @return {boolean} Whether the device is a shared access device. |
88 */ | 84 */ |
89 Gnubbies.prototype.isSharedAccess = function(id) { | 85 Gnubbies.prototype.isSharedAccess = function(id) { |
90 if (!this.impl_.hasOwnProperty(id.namespace)) return false; | 86 if (!this.impl_.hasOwnProperty(id.namespace)) |
| 87 return false; |
91 return this.impl_[id.namespace].isSharedAccess; | 88 return this.impl_[id.namespace].isSharedAccess; |
92 }; | 89 }; |
93 | 90 |
94 /** | 91 /** |
95 * @param {GnubbyDeviceId} which The device to remove. | 92 * @param {GnubbyDeviceId} which The device to remove. |
96 */ | 93 */ |
97 Gnubbies.prototype.removeOpenDevice = function(which) { | 94 Gnubbies.prototype.removeOpenDevice = function(which) { |
98 if (this.openDevs_[which.namespace] && | 95 if (this.openDevs_[which.namespace] && |
99 this.openDevs_[which.namespace].hasOwnProperty(which.device)) { | 96 this.openDevs_[which.namespace].hasOwnProperty(which.device)) { |
100 delete this.openDevs_[which.namespace][which.device]; | 97 delete this.openDevs_[which.namespace][which.device]; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 Gnubbies.SYS_TIMER_ = new WindowTimer(); | 237 Gnubbies.SYS_TIMER_ = new WindowTimer(); |
241 | 238 |
242 /** | 239 /** |
243 * @param {number|undefined} opt_timeoutMillis Timeout in milliseconds | 240 * @param {number|undefined} opt_timeoutMillis Timeout in milliseconds |
244 */ | 241 */ |
245 Gnubbies.prototype.resetInactivityTimer = function(opt_timeoutMillis) { | 242 Gnubbies.prototype.resetInactivityTimer = function(opt_timeoutMillis) { |
246 var millis = opt_timeoutMillis ? | 243 var millis = opt_timeoutMillis ? |
247 opt_timeoutMillis + Gnubbies.INACTIVITY_TIMEOUT_MARGIN_MILLIS : | 244 opt_timeoutMillis + Gnubbies.INACTIVITY_TIMEOUT_MARGIN_MILLIS : |
248 Gnubbies.INACTIVITY_TIMEOUT_MARGIN_MILLIS; | 245 Gnubbies.INACTIVITY_TIMEOUT_MARGIN_MILLIS; |
249 if (!this.inactivityTimer) { | 246 if (!this.inactivityTimer) { |
250 this.inactivityTimer = | 247 this.inactivityTimer = new CountdownTimer( |
251 new CountdownTimer( | 248 Gnubbies.SYS_TIMER_, millis, this.inactivityTimeout_.bind(this)); |
252 Gnubbies.SYS_TIMER_, millis, this.inactivityTimeout_.bind(this)); | |
253 } else if (millis > this.inactivityTimer.millisecondsUntilExpired()) { | 249 } else if (millis > this.inactivityTimer.millisecondsUntilExpired()) { |
254 this.inactivityTimer.clearTimeout(); | 250 this.inactivityTimer.clearTimeout(); |
255 this.inactivityTimer.setTimeout(millis, this.inactivityTimeout_.bind(this)); | 251 this.inactivityTimer.setTimeout(millis, this.inactivityTimeout_.bind(this)); |
256 } | 252 } |
257 }; | 253 }; |
258 | 254 |
259 /** | 255 /** |
260 * Called when the inactivity timeout expires. | 256 * Called when the inactivity timeout expires. |
261 * @private | 257 * @private |
262 */ | 258 */ |
263 Gnubbies.prototype.inactivityTimeout_ = function() { | 259 Gnubbies.prototype.inactivityTimeout_ = function() { |
264 this.inactivityTimer = undefined; | 260 this.inactivityTimer = undefined; |
265 for (var namespace in this.openDevs_) { | 261 for (var namespace in this.openDevs_) { |
266 for (var dev in this.openDevs_[namespace]) { | 262 for (var dev in this.openDevs_[namespace]) { |
267 var deviceId = Number(dev); | 263 var deviceId = Number(dev); |
268 console.warn(namespace + ' device ' + deviceId + | 264 console.warn( |
| 265 namespace + ' device ' + deviceId + |
269 ' still open after inactivity, closing'); | 266 ' still open after inactivity, closing'); |
270 this.openDevs_[namespace][deviceId].destroy(); | 267 this.openDevs_[namespace][deviceId].destroy(); |
271 } | 268 } |
272 } | 269 } |
273 }; | 270 }; |
274 | 271 |
275 /** | 272 /** |
276 * Opens and adds a new client of the specified device. | 273 * Opens and adds a new client of the specified device. |
277 * @param {GnubbyDeviceId} which Which device to open. | 274 * @param {GnubbyDeviceId} which Which device to open. |
278 * @param {*} who Client of the device. | 275 * @param {*} who Client of the device. |
279 * @param {function(number, GnubbyDevice=)} cb Called back with the result of | 276 * @param {function(number, GnubbyDevice=)} cb Called back with the result of |
280 * opening the device. | 277 * opening the device. |
281 */ | 278 */ |
282 Gnubbies.prototype.addClient = function(which, who, cb) { | 279 Gnubbies.prototype.addClient = function(which, who, cb) { |
283 this.resetInactivityTimer(); | 280 this.resetInactivityTimer(); |
284 | 281 |
285 var self = this; | 282 var self = this; |
286 | 283 |
287 function opened(gnubby, who, cb) { | 284 function opened(gnubby, who, cb) { |
288 if (gnubby.closing) { | 285 if (gnubby.closing) { |
289 // Device is closing or already closed. | 286 // Device is closing or already closed. |
290 self.removeClient(gnubby, who); | 287 self.removeClient(gnubby, who); |
291 if (cb) { cb(-GnubbyDevice.NODEVICE); } | 288 if (cb) { |
| 289 cb(-GnubbyDevice.NODEVICE); |
| 290 } |
292 } else { | 291 } else { |
293 gnubby.registerClient(who); | 292 gnubby.registerClient(who); |
294 if (cb) { cb(-GnubbyDevice.OK, gnubby); } | 293 if (cb) { |
| 294 cb(-GnubbyDevice.OK, gnubby); |
| 295 } |
295 } | 296 } |
296 } | 297 } |
297 | 298 |
298 function notifyOpenResult(rc) { | 299 function notifyOpenResult(rc) { |
299 if (self.pendingOpens_[which.namespace]) { | 300 if (self.pendingOpens_[which.namespace]) { |
300 while (self.pendingOpens_[which.namespace][which.device].length != 0) { | 301 while (self.pendingOpens_[which.namespace][which.device].length != 0) { |
301 var client = self.pendingOpens_[which.namespace][which.device].shift(); | 302 var client = self.pendingOpens_[which.namespace][which.device].shift(); |
302 client.cb(rc); | 303 client.cb(rc); |
303 } | 304 } |
304 delete self.pendingOpens_[which.namespace][which.device]; | 305 delete self.pendingOpens_[which.namespace][which.device]; |
305 } | 306 } |
306 } | 307 } |
307 | 308 |
308 var dev = null; | 309 var dev = null; |
309 var deviceToDeviceId = this.getDeviceToDeviceId_(which.namespace); | 310 var deviceToDeviceId = this.getDeviceToDeviceId_(which.namespace); |
310 if (this.devs_[which.namespace]) { | 311 if (this.devs_[which.namespace]) { |
311 for (var i = 0; i < this.devs_[which.namespace].length; i++) { | 312 for (var i = 0; i < this.devs_[which.namespace].length; i++) { |
312 var device = this.devs_[which.namespace][i]; | 313 var device = this.devs_[which.namespace][i]; |
313 if (deviceToDeviceId(device).device == which.device) { | 314 if (deviceToDeviceId(device).device == which.device) { |
314 dev = device; | 315 dev = device; |
315 break; | 316 break; |
316 } | 317 } |
317 } | 318 } |
318 } | 319 } |
319 if (!dev) { | 320 if (!dev) { |
320 // Index out of bounds. Device does not exist in current enumeration. | 321 // Index out of bounds. Device does not exist in current enumeration. |
321 this.removeClient(null, who); | 322 this.removeClient(null, who); |
322 if (cb) { cb(-GnubbyDevice.NODEVICE); } | 323 if (cb) { |
| 324 cb(-GnubbyDevice.NODEVICE); |
| 325 } |
323 return; | 326 return; |
324 } | 327 } |
325 | 328 |
326 function openCb(rc, opt_gnubby) { | 329 function openCb(rc, opt_gnubby) { |
327 if (rc) { | 330 if (rc) { |
328 notifyOpenResult(rc); | 331 notifyOpenResult(rc); |
329 return; | 332 return; |
330 } | 333 } |
331 if (!opt_gnubby) { | 334 if (!opt_gnubby) { |
332 notifyOpenResult(-GnubbyDevice.NODEVICE); | 335 notifyOpenResult(-GnubbyDevice.NODEVICE); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 if (whichDev && dev != whichDev) { | 416 if (whichDev && dev != whichDev) { |
414 console.warn('Gnubby attached to more than one device!?'); | 417 console.warn('Gnubby attached to more than one device!?'); |
415 } | 418 } |
416 if (!dev.deregisterClient(who)) { | 419 if (!dev.deregisterClient(who)) { |
417 dev.destroy(); | 420 dev.destroy(); |
418 } | 421 } |
419 } | 422 } |
420 } | 423 } |
421 } | 424 } |
422 }; | 425 }; |
OLD | NEW |