Index: chrome/browser/resources/bluetooth_internals/device_collection.js |
diff --git a/chrome/browser/resources/bluetooth_internals/device_collection.js b/chrome/browser/resources/bluetooth_internals/device_collection.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ab62448c12433f64117e8a262ef5bf030596ab47 |
--- /dev/null |
+++ b/chrome/browser/resources/bluetooth_internals/device_collection.js |
@@ -0,0 +1,76 @@ |
+// Copyright 2016 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. |
+ |
+/** |
+ * Javascript for Device-related features, served from |
+ * chrome://bluetooth-internals/. |
+ */ |
+ |
+cr.define('device_collection', function() { |
+ /* |
+ * Collection of devices. |
+ * @constructor |
+ * @param {!Array} array the starting collection of devices. |
+ * @extends {cr.ui.ArrayDataModel} |
+ */ |
+ var DeviceCollection = function(array) { |
+ cr.ui.ArrayDataModel.call(this, array); |
+ }; |
+ DeviceCollection.prototype = { |
+ __proto__: cr.ui.ArrayDataModel.prototype, |
+ /** |
+ * Finds the Device in the collection with the matching address. |
+ * @param {!string} address |
+ */ |
+ getByAddress: function(address) { |
+ for (var i = 0; i < this.length; i++) { |
+ var device = this.item(i); |
+ if (address == device.info.address) |
+ return device; |
+ } |
+ return null; |
+ }, |
+ /** |
+ * Adds or updates a Device with new DeviceInfo. |
+ * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
+ */ |
+ addOrUpdate: function(deviceInfo) { |
+ var oldDevice = this.getByAddress(deviceInfo.address); |
+ if (oldDevice) { |
+ // Update rssi if it's valid |
+ var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
+ (oldDevice.info.rssi && oldDevice.info.rssi.value); |
+ |
+ oldDevice.info = deviceInfo; |
+ oldDevice.info.rssi = { value: rssi }; |
+ oldDevice.removed = false; |
+ |
+ this.updateIndex(this.indexOf(oldDevice)); |
+ } else { |
+ var device = new Device(deviceInfo); |
+ this.push(device); |
+ } |
+ }, |
+ remove: function(deviceInfo) { |
+ var device = this.getByAddress(deviceInfo.address); |
+ assert(device, |
+ 'Device does not exist.'); |
+ device.removed = true; |
+ } |
+ }; |
+ |
+ /* |
+ * Data model for a cached device. |
+ * @constructor |
+ * @param {!interfaces.BluetoothDevice.DeviceInfo} info |
+ */ |
+ var Device = function(info) { |
+ this.info = info; |
+ this.removed = false; |
+ }; |
+ |
+ return { |
+ DeviceCollection, DeviceCollection, |
+ }; |
+}); |