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

Unified Diff: chrome/browser/resources/bluetooth_internals/device_collection.js

Issue 2446823002: bluetooth: Componentize device list in chrome://bluetooth-internals. (Closed)
Patch Set: Formatting changes, comments 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 side-by-side diff with in-line comments
Download patch
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..73ddb710618ac8cf1cbe95fa6ae5662ac8b2f26a
--- /dev/null
+++ b/chrome/browser/resources/bluetooth_internals/device_collection.js
@@ -0,0 +1,84 @@
+// 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);
ortuno 2016/11/03 22:14:15 nit: I would just do this.push(new Device(deviceIn
mbrunson 2016/11/03 23:59:40 Done.
+ }
+ },
+
+ /**
+ * 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.');
ortuno 2016/11/03 22:14:15 nit: Why is this on a different line?
mbrunson 2016/11/03 23:59:40 Should be on same line. Done.
+ 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,
ortuno 2016/11/03 22:14:15 qq: What are you returning here? Shouldn't this be
mbrunson 2016/11/03 23:59:40 Must have hit a key by accident or something. Shou
+ };
+});

Powered by Google App Engine
This is Rietveld 408576698