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 DeviceCollection, served from | |
7 * chrome://bluetooth-internals/. | |
8 */ | |
9 | |
10 cr.define('device_collection', function() { | |
11 /* | |
12 * Collection of devices. Extends ArrayDataModel which provides a set of | |
13 * functions and events that notifies observers when the collection changes. | |
14 * @constructor | |
15 * @param {!Array<device_collection.Device>} array The starting collection of | |
16 * devices. | |
17 * @extends {cr.ui.ArrayDataModel} | |
18 */ | |
19 var DeviceCollection = function(array) { | |
20 cr.ui.ArrayDataModel.call(this, array); | |
21 }; | |
22 DeviceCollection.prototype = { | |
23 __proto__: cr.ui.ArrayDataModel.prototype, | |
24 | |
25 /** | |
26 * Finds the Device in the collection with the matching address. | |
27 * @param {string} address | |
28 */ | |
29 getByAddress: function(address) { | |
30 for (var i = 0; i < this.length; i++) { | |
31 var device = this.item(i); | |
32 if (address == device.info.address) | |
33 return device; | |
34 } | |
35 return null; | |
36 }, | |
37 | |
38 /** | |
39 * Adds or updates a Device with new DeviceInfo. | |
40 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | |
41 */ | |
42 addOrUpdate: function(deviceInfo) { | |
43 var oldDevice = this.getByAddress(deviceInfo.address); | |
44 if (oldDevice) { | |
45 // Update rssi if it's valid | |
46 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || | |
47 (oldDevice.info.rssi && oldDevice.info.rssi.value); | |
48 | |
49 oldDevice.info = deviceInfo; | |
50 oldDevice.info.rssi = { value: rssi }; | |
51 oldDevice.removed = false; | |
52 | |
53 this.updateIndex(this.indexOf(oldDevice)); | |
54 } else { | |
55 this.push(new Device(deviceInfo)); | |
56 } | |
57 }, | |
58 | |
59 /** | |
60 * Marks the Device as removed. | |
61 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo | |
62 */ | |
63 remove: function(deviceInfo) { | |
64 var device = this.getByAddress(deviceInfo.address); | |
65 assert(device, 'Device does not exist.'); | |
66 device.removed = true; | |
67 this.updateIndex(this.indexOf(device)); | |
68 } | |
69 }; | |
70 | |
71 /* | |
72 * Data model for a cached device. | |
73 * @constructor | |
74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info | |
75 */ | |
76 var Device = function(info) { | |
77 this.info = info; | |
78 this.removed = false; | |
Dan Beam
2016/11/09 18:00:32
are these used externally? can they be @private?
mbrunson
2016/11/10 01:02:13
Yes. These are used externally.
| |
79 }; | |
80 | |
81 return { | |
82 Device: Device, | |
83 DeviceCollection: DeviceCollection, | |
84 }; | |
85 }); | |
OLD | NEW |