OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 CharacteristicList and CharacteristicListItem, served from |
| 7 * chrome://bluetooth-internals/. |
| 8 */ |
| 9 |
| 10 cr.define('characteristic_list', function() { |
| 11 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; |
| 12 /** @const */ var ExpandableList = expandable_list.ExpandableList; |
| 13 /** @const */ var ExpandableListItem = expandable_list.ExpandableListItem; |
| 14 /** @const */ var Snackbar = snackbar.Snackbar; |
| 15 /** @const */ var SnackbarType = snackbar.SnackbarType; |
| 16 |
| 17 /** Property names for the CharacteristicInfo fieldset */ |
| 18 var INFO_PROPERTY_NAMES = { |
| 19 id: 'ID', |
| 20 'uuid.uuid': 'UUID', |
| 21 }; |
| 22 |
| 23 /** Property names for the Properties fieldset. */ |
| 24 var PROPERTIES_PROPERTY_NAMES = { |
| 25 broadcast: 'Broadcast', |
| 26 read: 'Read', |
| 27 write_without_response: 'Write Without Response', |
| 28 write: 'Write', |
| 29 notify: 'Notify', |
| 30 indicate: 'Indicate', |
| 31 authenticated_signed_writes: 'Authenticated Signed Writes', |
| 32 extended_properties: 'Extended Properties', |
| 33 reliable_write: 'Reliable Write', |
| 34 writable_auxiliaries: 'Writable Auxiliaries', |
| 35 read_encrypted: 'Read Encrypted', |
| 36 write_encrypted: 'Write Encrypted', |
| 37 read_encrypted_authenticated: 'Read Encrypted Authenticated', |
| 38 write_encrypted_authenticated: 'Write Encrypted Authenticated', |
| 39 }; |
| 40 |
| 41 /** |
| 42 * A list item that displays the properties of a CharacteristicInfo object. |
| 43 * Two fieldsets are created within the element: one for the primitive |
| 44 * properties, 'id' and 'uuid', and one for the 'properties' bitfield in the |
| 45 * CharacteristicInfo object. |
| 46 * @constructor |
| 47 * @param {!interfaces.BluetoothDevice.CharacteristicInfo} characteristicInfo |
| 48 */ |
| 49 function CharacteristicListItem(characteristicInfo) { |
| 50 var listItem = new ExpandableListItem(); |
| 51 listItem.__proto__ = CharacteristicListItem.prototype; |
| 52 |
| 53 listItem.info = characteristicInfo; |
| 54 listItem.decorate(); |
| 55 |
| 56 return listItem; |
| 57 } |
| 58 |
| 59 CharacteristicListItem.prototype = { |
| 60 __proto__: ExpandableListItem.prototype, |
| 61 |
| 62 /** |
| 63 * Decorates the element as a characteristic list item. Creates and caches |
| 64 * two fieldsets for displaying property values. |
| 65 * @override |
| 66 */ |
| 67 decorate: function() { |
| 68 this.classList.add('characteristic-list-item'); |
| 69 |
| 70 /** @private {!object_fieldset.ObjectFieldSet} */ |
| 71 this.characteristicFieldSet_ = object_fieldset.ObjectFieldSet(); |
| 72 this.characteristicFieldSet_.setPropertyDisplayNames(INFO_PROPERTY_NAMES); |
| 73 this.characteristicFieldSet_.setObject({ |
| 74 id: this.info.id, |
| 75 'uuid.uuid': this.info.uuid.uuid, |
| 76 }); |
| 77 |
| 78 /** @private {!object_fieldset.ObjectFieldSet} */ |
| 79 this.propertiesFieldSet_ = new object_fieldset.ObjectFieldSet(); |
| 80 this.propertiesFieldSet_.setPropertyDisplayNames( |
| 81 PROPERTIES_PROPERTY_NAMES); |
| 82 var Property = interfaces.BluetoothDevice.Property; |
| 83 this.propertiesFieldSet_.setObject({ |
| 84 broadcast: (this.info.properties & Property.BROADCAST) > 0, |
| 85 read: (this.info.properties & Property.READ) > 0, |
| 86 write_without_response: (this.info.properties & |
| 87 Property.WRITE_WITHOUT_RESPONSE) > 0, |
| 88 write: (this.info.properties & Property.WRITE) > 0, |
| 89 notify: (this.info.properties & Property.NOTIFY) > 0, |
| 90 indicate: (this.info.properties & Property.INDICATE) > 0, |
| 91 authenticated_signed_writes: (this.info.properties & |
| 92 Property.AUTHENTICATED_SIGNED_WRITES) > 0, |
| 93 extended_properties: (this.info.properties & |
| 94 Property.EXTENDED_PROPERTIES) > 0, |
| 95 reliable_write: (this.info.properties & Property.RELIABLE_WRITE) > 0, |
| 96 writable_auxiliaries: (this.info.properties & |
| 97 Property.WRITABLE_AUXILIARIES) > 0, |
| 98 read_encrypted: (this.info.properties & Property.READ_ENCRYPTED) > 0, |
| 99 write_encrypted: (this.info.properties & Property.WRITE_ENCRYPTED) > 0, |
| 100 read_encrypted_authenticated: (this.info.properties & |
| 101 Property.READ_ENCRYPTED_AUTHENTICATED) > 0, |
| 102 write_encrypted_authenticated: (this.info.properties & |
| 103 Property.WRITE_ENCRYPTED_AUTHENTICATED) > 0, |
| 104 }); |
| 105 |
| 106 // Create content for display in brief content container. |
| 107 var characteristicHeaderText = document.createElement('div'); |
| 108 characteristicHeaderText.textContent = 'Characteristic:'; |
| 109 |
| 110 var characteristicHeaderValue = document.createElement('div'); |
| 111 characteristicHeaderValue.textContent = this.info.uuid.uuid; |
| 112 |
| 113 var characteristicHeader = document.createElement('div'); |
| 114 characteristicHeader.appendChild(characteristicHeaderText); |
| 115 characteristicHeader.appendChild(characteristicHeaderValue); |
| 116 this.briefContent_.appendChild(characteristicHeader); |
| 117 |
| 118 // Create content for display in expanded content container. |
| 119 var characteristicInfoHeader = document.createElement('h4'); |
| 120 characteristicInfoHeader.textContent = 'Characteristic Info'; |
| 121 |
| 122 var characteristicDiv = document.createElement('div'); |
| 123 characteristicDiv.classList.add('flex'); |
| 124 characteristicDiv.appendChild(this.characteristicFieldSet_); |
| 125 |
| 126 var propertiesHeader = document.createElement('h4'); |
| 127 propertiesHeader.textContent = 'Properties'; |
| 128 |
| 129 var propertiesDiv = document.createElement('div'); |
| 130 propertiesDiv.classList.add('flex'); |
| 131 propertiesDiv.appendChild(this.propertiesFieldSet_); |
| 132 |
| 133 var infoDiv = document.createElement('div'); |
| 134 infoDiv.classList.add('info-container'); |
| 135 infoDiv.appendChild(characteristicInfoHeader); |
| 136 infoDiv.appendChild(characteristicDiv); |
| 137 infoDiv.appendChild(propertiesHeader); |
| 138 infoDiv.appendChild(propertiesDiv); |
| 139 |
| 140 this.expandedContent_.appendChild(infoDiv); |
| 141 }, |
| 142 }; |
| 143 |
| 144 /** |
| 145 * A list that displays CharacteristicListItems. |
| 146 * @constructor |
| 147 */ |
| 148 var CharacteristicList = cr.ui.define('list'); |
| 149 |
| 150 CharacteristicList.prototype = { |
| 151 __proto__: ExpandableList.prototype, |
| 152 |
| 153 /** @override */ |
| 154 decorate: function() { |
| 155 ExpandableList.prototype.decorate.call(this); |
| 156 |
| 157 /** @private {boolean} */ |
| 158 this.characteristicsRequested_ = false; |
| 159 |
| 160 this.classList.add('characteristic-list'); |
| 161 this.setEmptyMessage('No Characteristics Found'); |
| 162 }, |
| 163 |
| 164 /** @override */ |
| 165 createItem: function(data) { |
| 166 return new CharacteristicListItem(data); |
| 167 }, |
| 168 |
| 169 /** |
| 170 * Loads the characteristic list with an array of CharacteristicInfo from |
| 171 * the device with |deviceAddress| and service with |serviceId|. If no |
| 172 * active connection to the device exists, one is created. |
| 173 * @param {string} deviceAddress |
| 174 * @param {string} serviceId |
| 175 */ |
| 176 load: function(deviceAddress, serviceId) { |
| 177 if (!this.characteristicsRequested_ && this.isLoading()) { |
| 178 this.characteristicsRequested_ = true; |
| 179 |
| 180 device_broker.connectToDevice(deviceAddress).then(function(device) { |
| 181 return device.getCharacteristics(serviceId); |
| 182 }.bind(this)).then(function(response) { |
| 183 this.setData(new ArrayDataModel(response.characteristics)); |
| 184 this.setLoading(false); |
| 185 this.characteristicsRequested_ = false; |
| 186 }.bind(this)).catch(function(error) { |
| 187 this.characteristicsRequested_ = false; |
| 188 Snackbar.show( |
| 189 deviceAddress + ': ' + error.message, SnackbarType.ERROR, |
| 190 'Retry', function() { |
| 191 this.load(deviceAddress, serviceId); |
| 192 }.bind(this)); |
| 193 }.bind(this)); |
| 194 } |
| 195 }, |
| 196 }; |
| 197 |
| 198 return { |
| 199 CharacteristicList: CharacteristicList, |
| 200 CharacteristicListItem: CharacteristicListItem, |
| 201 }; |
| 202 }); |
OLD | NEW |