Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/device_collection.js

Issue 2446823002: bluetooth: Componentize device list in chrome://bluetooth-internals. (Closed)
Patch Set: Fix issues, add comments, move Adapter code to AdapterBroker Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 /**
24 * Finds the Device in the collection with the matching address.
25 * @param {!string} address
26 */
27 getByAddress: function(address) {
28 for (var i = 0; i < this.length; i++) {
29 var device = this.item(i);
30 if (address == device.info.address)
31 return device;
32 }
33 return null;
34 },
35
36 /**
37 * Adds or updates a Device with new DeviceInfo.
38 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
39 */
40 addOrUpdate: function(deviceInfo) {
41 var oldDevice = this.getByAddress(deviceInfo.address);
42 if (oldDevice) {
43 // Update rssi if it's valid
44 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
45 (oldDevice.info.rssi && oldDevice.info.rssi.value);
46
47 oldDevice.info = deviceInfo;
48 oldDevice.info.rssi = { value: rssi };
49 oldDevice.removed = false;
50
51 this.updateIndex(this.indexOf(oldDevice));
52 } else {
53 this.push(new Device(deviceInfo));
54 }
55 },
56
57 /**
58 * Marks the Device as removed.
59 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
60 */
61 remove: function(deviceInfo) {
62 var device = this.getByAddress(deviceInfo.address);
63 assert(device, 'Device does not exist.');
64 device.removed = true;
65 this.updateIndex(this.indexOf(device));
66 }
67 };
68
69 /*
70 * Data model for a cached device.
71 * @constructor
72 * @param {!interfaces.BluetoothDevice.DeviceInfo} info
73 */
74 var Device = function(info) {
75 this.info = info;
76 this.removed = false;
77 };
78
79 return {
80 DeviceCollection: DeviceCollection,
81 };
82 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698