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

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

Issue 2404623002: bluetooth: Add DeviceChanged logging in Device service. (Closed)
Patch Set: Split JS into functions Created 4 years, 2 months 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
« no previous file with comments | « no previous file | device/bluetooth/device.h » ('j') | device/bluetooth/public/interfaces/device.mojom » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
diff --git a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
index b9af7fb9c83e1c876d8d6555e259545d87b986da..654b85c201a3b21f39e825780264823db81854b0 100644
--- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
+++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js
@@ -7,27 +7,56 @@
* chrome://bluetooth-internals/.
*/
-/**
- * The implementation of AdapterClient in
- * device/bluetooth/public/interfaces/adapter.mojom.
- */
-var AdapterClient = function() {};
-AdapterClient.prototype = {
- /**
- * Prints added device to console.
- * @param {!bluetoothDevice.DeviceInfo} device
- */
- deviceAdded: function(device) { console.log('Device added', device); },
+(function() {
+ var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
+
+ // Dictionary for address->client mapping
scheib 2016/10/14 07:14:51 Close with .
mbrunson 2016/10/14 18:25:25 Done.
+ var gattClients = {};
+
+ // Dictionary for address->device mapping
+ var devices = {};
/**
- * Prints removed device to console.
- * @param {!bluetoothDevice.DeviceInfo} device
- */
- deviceRemoved: function(device) { console.log('Device removed', device); }
-};
+ * The implementation of AdapterClient in
+ * device/bluetooth/public/interfaces/adapter.mojom.
+ */
+ var AdapterClient = function() {};
+ AdapterClient.prototype = {
+ /**
+ * Prints added device to console.
+ * @param {!bluetoothDevice.DeviceInfo} device
+ */
+ deviceAdded: function(device) {
+ console.log('Device added', device);
+ setDeviceClient(device);
+ },
+
+ /**
+ * Prints removed device to console.
+ * @param {!bluetoothDevice.DeviceInfo} device
+ */
+ deviceRemoved: function(device) {
+ console.log('Device removed', device);
+ delete gattClients[device.address];
+ delete devices[device.address];
+ }
+ };
-(function() {
- var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection;
+ /**
+ * The implementation of GattClient in
+ * device/bluetooth/public/interfaces/device.mojom.
+ */
+ var GattClient = function() {};
+ GattClient.prototype = {
+ /**
+ * Prints received advertising packet to console.
+ * @param {!bluetoothDevice.AdvertisingPacket} advertisingPacket the
+ * advertising packet received from the device.
+ */
+ deviceChanged: function(advertisingPacket) {
+ console.log(new Date(advertisingPacket.timestamp), advertisingPacket);
+ }
+ };
/**
* TODO(crbug.com/652361): Move to shared location.
@@ -64,6 +93,9 @@ AdapterClient.prototype = {
AdapterClient.prototype.__proto__ =
bluetoothAdapter.AdapterClient.stubClass.prototype;
+ GattClient.prototype.__proto__ =
+ bluetoothDevice.GattClient.stubClass.prototype;
+
var adapterFactory = connection.bindHandleToProxy(
frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name),
bluetoothAdapter.AdapterFactory);
@@ -86,25 +118,62 @@ AdapterClient.prototype = {
}
/**
- * Prints device info from the device service.
+ * Logs the latest info from the device. Creates a message pipe to the device
+ * if none exists and caches it.
* @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to
* get the information.
* @return {!Promise} resolves if device service is retrieved, rejects
* otherwise.
*/
function logDevice(deviceInfo) {
+ return getDevice(deviceInfo).then(function(device) {
+ return device.getInfo();
+ }).then(function(response) {
+ console.log(response.info.name_for_display, response.info);
+ });
+ }
+
+ /**
+ * Creates a logging client to the device with the given |deviceInfo|.
+ * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to
+ * get the information.
+ * @return {!Promise} resolves if device service is retrieved, rejects
+ * otherwise.
+ */
+ function setDeviceClient(deviceInfo) {
+ return getDevice(deviceInfo).then(function(device) {
+ var gattClient = new GattClient();
+ device.setClient(connection.bindStubDerivedImpl(gattClient));
+ gattClients[deviceInfo.address] = gattClient;
+ return Promise.resolve();
+ });
+ }
+
+ /**
+ * Gets a message pipe for the device. If none exists, a message pipe is
+ * created and cached.
+ * @param {!bluetoothDevice.DeviceInfo} deviceInfo the device for which to
+ * get the information.
+ * @return {!Promise<bluetoothDevice.Device>} resolves with Device if
+ * message pipe was created successfully, reject otherwise.
+ */
+ function getDevice(deviceInfo) {
+ if (devices[deviceInfo.address]) {
+ return Promise.resolve(devices[deviceInfo.address]);
+ }
+
return adapter.getDevice(deviceInfo.address).then(function(response) {
- var deviceHandle = response.device;
- if (!deviceHandle) {
- throw new Error(deviceInfo.name_for_display + ' cannot be found.');
- }
+ var deviceHandle = response.device;
+ if (!deviceHandle) {
+ throw new Error(deviceInfo.name_for_display + ' cannot be found.');
+ }
- var device = connection.bindHandleToProxy(
+ var device = connection.bindHandleToProxy(
deviceHandle, bluetoothDevice.Device);
- return device.getInfo();
- }).then(function(response) {
- console.log(deviceInfo.name_for_display, response.info);
- });
+
+ devices[deviceInfo.address] = device;
+ return device;
+ });
}
document.addEventListener('DOMContentLoaded', function() {
@@ -113,10 +182,11 @@ AdapterClient.prototype = {
.then(function(response) { console.log('adapter', response.info); })
.then(function() { return adapter.getDevices(); })
.then(function(response) {
- var devices = response.devices;
- console.log('devices', devices.length);
+ var deviceInfos = response.devices;
+ console.log('devices', deviceInfos.length);
- return Promise.all(devices.map(logDevice));
+ return Promise.all([...deviceInfos.map(logDevice),
+ ...deviceInfos.map(setDeviceClient)]);
})
.catch(function(error) { console.error(error); });
});
« no previous file with comments | « no previous file | device/bluetooth/device.h » ('j') | device/bluetooth/public/interfaces/device.mojom » ('J')

Powered by Google App Engine
This is Rietveld 408576698