| 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..96e31bada1385a478dbe8f91654fdfa0828cffa4
|
| --- /dev/null
|
| +++ b/chrome/browser/resources/bluetooth_internals/device_collection.js
|
| @@ -0,0 +1,82 @@
|
| +// 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 {
|
| + this.push(new Device(deviceInfo));
|
| + }
|
| + },
|
| +
|
| + /**
|
| + * Marks the Device as removed.
|
| + * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
|
| + */
|
| + remove: function(deviceInfo) {
|
| + var device = this.getByAddress(deviceInfo.address);
|
| + assert(device, 'Device does not exist.');
|
| + device.removed = true;
|
| + this.updateIndex(this.indexOf(device));
|
| + }
|
| + };
|
| +
|
| + /*
|
| + * 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,
|
| + };
|
| +});
|
|
|