| 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 * open: function(Gnubbies, number, *, function(number, GnubbyDevice=)) | 23 * open: function(Gnubbies, number, *, function(number, GnubbyDevice=)) |
| 24 * }} | 24 * }} |
| 25 */ | 25 */ |
| 26 var GnubbyNamespaceImpl; | 26 var GnubbyNamespaceImpl; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Manager of opened devices. | 29 * Manager of opened devices. |
| 30 * @constructor | 30 * @constructor |
| 31 */ | 31 */ |
| 32 function Gnubbies() { | 32 function Gnubbies() { |
| 33 /** @private {Object.<string, Array>} */ | 33 /** @private {Object<string, Array>} */ |
| 34 this.devs_ = {}; | 34 this.devs_ = {}; |
| 35 this.pendingEnumerate = []; // clients awaiting an enumerate | 35 this.pendingEnumerate = []; // clients awaiting an enumerate |
| 36 /** | 36 /** |
| 37 * The distinct namespaces registered in this Gnubbies instance, in order of | 37 * The distinct namespaces registered in this Gnubbies instance, in order of |
| 38 * registration. | 38 * registration. |
| 39 * @private {Array.<string>} | 39 * @private {Array<string>} |
| 40 */ | 40 */ |
| 41 this.namespaces_ = []; | 41 this.namespaces_ = []; |
| 42 /** @private {Object.<string, GnubbyNamespaceImpl>} */ | 42 /** @private {Object<string, GnubbyNamespaceImpl>} */ |
| 43 this.impl_ = {}; | 43 this.impl_ = {}; |
| 44 /** @private {Object.<string, Object.<number, !GnubbyDevice>>} */ | 44 /** @private {Object<string, Object<number, !GnubbyDevice>>} */ |
| 45 this.openDevs_ = {}; | 45 this.openDevs_ = {}; |
| 46 /** @private {Object.<string, Object.<number, *>>} */ | 46 /** @private {Object<string, Object<number, *>>} */ |
| 47 this.pendingOpens_ = {}; // clients awaiting an open | 47 this.pendingOpens_ = {}; // clients awaiting an open |
| 48 } | 48 } |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Registers a new gnubby namespace, i.e. an implementation of the | 51 * Registers a new gnubby namespace, i.e. an implementation of the |
| 52 * enumerate/open functions for all devices within a namespace. | 52 * enumerate/open functions for all devices within a namespace. |
| 53 * @param {string} namespace The namespace of the numerator, e.g. 'usb'. | 53 * @param {string} namespace The namespace of the numerator, e.g. 'usb'. |
| 54 * @param {GnubbyNamespaceImpl} impl The implementation. | 54 * @param {GnubbyNamespaceImpl} impl The implementation. |
| 55 */ | 55 */ |
| 56 Gnubbies.prototype.registerNamespace = function(namespace, impl) { | 56 Gnubbies.prototype.registerNamespace = function(namespace, impl) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 for (var dev in this.openDevs_[namespace]) { | 90 for (var dev in this.openDevs_[namespace]) { |
| 91 var deviceId = Number(dev); | 91 var deviceId = Number(dev); |
| 92 this.openDevs_[namespace][deviceId].destroy(); | 92 this.openDevs_[namespace][deviceId].destroy(); |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 this.devs_ = {}; | 95 this.devs_ = {}; |
| 96 this.openDevs_ = {}; | 96 this.openDevs_ = {}; |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 /** | 99 /** |
| 100 * @param {function(number, Array.<GnubbyDeviceId>)} cb Called back with the | 100 * @param {function(number, Array<GnubbyDeviceId>)} cb Called back with the |
| 101 * result of enumerating. | 101 * result of enumerating. |
| 102 */ | 102 */ |
| 103 Gnubbies.prototype.enumerate = function(cb) { | 103 Gnubbies.prototype.enumerate = function(cb) { |
| 104 if (!cb) { | 104 if (!cb) { |
| 105 cb = function(rc, indexes) { | 105 cb = function(rc, indexes) { |
| 106 var msg = 'defaultEnumerateCallback(' + rc; | 106 var msg = 'defaultEnumerateCallback(' + rc; |
| 107 if (indexes) { | 107 if (indexes) { |
| 108 msg += ', ['; | 108 msg += ', ['; |
| 109 for (var i = 0; i < indexes.length; i++) { | 109 for (var i = 0; i < indexes.length; i++) { |
| 110 msg += JSON.stringify(indexes[i]); | 110 msg += JSON.stringify(indexes[i]); |
| 111 } | 111 } |
| 112 msg += ']'; | 112 msg += ']'; |
| 113 } | 113 } |
| 114 msg += ')'; | 114 msg += ')'; |
| 115 console.log(UTIL_fmt(msg)); | 115 console.log(UTIL_fmt(msg)); |
| 116 }; | 116 }; |
| 117 } | 117 } |
| 118 | 118 |
| 119 if (!this.namespaces_.length) { | 119 if (!this.namespaces_.length) { |
| 120 cb(-GnubbyDevice.OK, []); | 120 cb(-GnubbyDevice.OK, []); |
| 121 return; | 121 return; |
| 122 } | 122 } |
| 123 | 123 |
| 124 var namespacesEnumerated = 0; | 124 var namespacesEnumerated = 0; |
| 125 var self = this; | 125 var self = this; |
| 126 | 126 |
| 127 /** | 127 /** |
| 128 * @param {string} namespace The namespace that was enumerated. | 128 * @param {string} namespace The namespace that was enumerated. |
| 129 * @param {Array.<GnubbyDeviceId>} existingDeviceIds Previously enumerated | 129 * @param {Array<GnubbyDeviceId>} existingDeviceIds Previously enumerated |
| 130 * device IDs (from other namespaces), if any. | 130 * device IDs (from other namespaces), if any. |
| 131 * @param {Array} devs The devices in the namespace. | 131 * @param {Array} devs The devices in the namespace. |
| 132 */ | 132 */ |
| 133 function enumerated(namespace, existingDeviceIds, devs) { | 133 function enumerated(namespace, existingDeviceIds, devs) { |
| 134 namespacesEnumerated++; | 134 namespacesEnumerated++; |
| 135 var lastNamespace = (namespacesEnumerated == self.namespaces_.length); | 135 var lastNamespace = (namespacesEnumerated == self.namespaces_.length); |
| 136 | 136 |
| 137 if (chrome.runtime.lastError) { | 137 if (chrome.runtime.lastError) { |
| 138 console.warn(UTIL_fmt('lastError: ' + chrome.runtime.lastError)); | 138 console.warn(UTIL_fmt('lastError: ' + chrome.runtime.lastError)); |
| 139 console.log(chrome.runtime.lastError); | 139 console.log(chrome.runtime.lastError); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 if (whichDev && dev != whichDev) { | 342 if (whichDev && dev != whichDev) { |
| 343 console.warn('Gnubby attached to more than one device!?'); | 343 console.warn('Gnubby attached to more than one device!?'); |
| 344 } | 344 } |
| 345 if (!dev.deregisterClient(who)) { | 345 if (!dev.deregisterClient(who)) { |
| 346 dev.destroy(); | 346 dev.destroy(); |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 }; | 351 }; |
| OLD | NEW |