OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Javascript for Device-related features, served from |
| 7 * chrome://bluetooth-internals/. |
| 8 */ |
| 9 |
| 10 cr.define('device_collection', function() { |
| 11 /* |
| 12 * Collection of devices. |
| 13 * @constructor |
| 14 * @param {!Array} array the starting collection of devices. |
| 15 * @extends {cr.ui.ArrayDataModel} |
| 16 */ |
| 17 var DeviceCollection = function(array) { |
| 18 cr.ui.ArrayDataModel.call(this, array); |
| 19 }; |
| 20 DeviceCollection.prototype = { |
| 21 __proto__: cr.ui.ArrayDataModel.prototype, |
| 22 /** |
| 23 * Finds the Device in the collection with the matching address. |
| 24 * @param {!string} address |
| 25 */ |
| 26 getByAddress: function(address) { |
| 27 for (var i = 0; i < this.length; i++) { |
| 28 var device = this.item(i); |
| 29 if (address == device.info.address) |
| 30 return device; |
| 31 } |
| 32 return null; |
| 33 }, |
| 34 /** |
| 35 * Adds or updates a Device with new DeviceInfo. |
| 36 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 37 */ |
| 38 addOrUpdate: function(deviceInfo) { |
| 39 var oldDevice = this.getByAddress(deviceInfo.address); |
| 40 if (oldDevice) { |
| 41 // Update rssi if it's valid |
| 42 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
| 43 (oldDevice.info.rssi && oldDevice.info.rssi.value); |
| 44 |
| 45 oldDevice.info = deviceInfo; |
| 46 oldDevice.info.rssi = { value: rssi }; |
| 47 oldDevice.removed = false; |
| 48 |
| 49 this.updateIndex(this.indexOf(oldDevice)); |
| 50 } else { |
| 51 var device = new Device(deviceInfo); |
| 52 this.push(device); |
| 53 } |
| 54 }, |
| 55 remove: function(deviceInfo) { |
| 56 var device = this.getByAddress(deviceInfo.address); |
| 57 assert(device, |
| 58 'Device does not exist.'); |
| 59 device.removed = true; |
| 60 } |
| 61 }; |
| 62 |
| 63 /* |
| 64 * Data model for a cached device. |
| 65 * @constructor |
| 66 * @param {!interfaces.BluetoothDevice.DeviceInfo} info |
| 67 */ |
| 68 var Device = function(info) { |
| 69 this.info = info; |
| 70 this.removed = false; |
| 71 }; |
| 72 |
| 73 return { |
| 74 DeviceCollection, DeviceCollection, |
| 75 }; |
| 76 }); |
OLD | NEW |