OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Javascript for DeviceDetailsPage which displays all of the details of a | 6 * Javascript for DeviceDetailsPage which displays all of the details of a |
7 * device. The page is generated and managed dynamically in bluetooth_internals. | 7 * device. The page is generated and managed dynamically in bluetooth_internals. |
8 * served from chrome://bluetooth-internals/. | 8 * served from chrome://bluetooth-internals/. |
9 */ | 9 */ |
10 | 10 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 /** @private {!service_list.ServiceList} */ | 52 /** @private {!service_list.ServiceList} */ |
53 this.serviceList_ = new service_list.ServiceList(); | 53 this.serviceList_ = new service_list.ServiceList(); |
54 | 54 |
55 /** @private {!device_collection.ConnectionStatus} */ | 55 /** @private {!device_collection.ConnectionStatus} */ |
56 this.status_ = device_collection.ConnectionStatus.DISCONNECTED; | 56 this.status_ = device_collection.ConnectionStatus.DISCONNECTED; |
57 | 57 |
58 /** @private {?HTMLElement} */ | 58 /** @private {?HTMLElement} */ |
59 this.connectBtn_ = null; | 59 this.connectBtn_ = null; |
60 | 60 |
61 this.pageDiv.appendChild( | 61 this.pageDiv.appendChild(document.importNode( |
62 document.importNode($('device-details-template').content, | 62 $('device-details-template').content, true /* deep */)); |
63 true /* deep */)); | |
64 | 63 |
65 this.pageDiv.querySelector('.device-details').appendChild( | 64 this.pageDiv.querySelector('.device-details') |
66 this.deviceFieldSet_); | 65 .appendChild(this.deviceFieldSet_); |
67 this.pageDiv.querySelector('.services').appendChild(this.serviceList_); | 66 this.pageDiv.querySelector('.services').appendChild(this.serviceList_); |
68 | 67 |
69 this.pageDiv.querySelector('.forget').addEventListener( | 68 this.pageDiv.querySelector('.forget').addEventListener('click', function() { |
70 'click', function() { | 69 this.disconnect(); |
71 this.disconnect(); | 70 this.pageDiv.dispatchEvent(new CustomEvent('forgetpressed', { |
72 this.pageDiv.dispatchEvent(new CustomEvent('forgetpressed', { | 71 detail: { |
73 detail: { | 72 address: this.deviceInfo.address, |
74 address: this.deviceInfo.address, | 73 }, |
75 }, | 74 })); |
76 })); | 75 }.bind(this)); |
77 }.bind(this)); | |
78 | 76 |
79 this.connectBtn_ = this.pageDiv.querySelector('.disconnect'); | 77 this.connectBtn_ = this.pageDiv.querySelector('.disconnect'); |
80 this.connectBtn_.addEventListener('click', function() { | 78 this.connectBtn_.addEventListener('click', function() { |
81 this.devicePtr_ !== null ? this.disconnect() : this.connect(); | 79 this.devicePtr_ !== null ? this.disconnect() : this.connect(); |
82 }.bind(this)); | 80 }.bind(this)); |
83 | 81 |
84 this.redraw(); | 82 this.redraw(); |
85 } | 83 } |
86 | 84 |
87 DeviceDetailsPage.prototype = { | 85 DeviceDetailsPage.prototype = { |
88 __proto__: Page.prototype, | 86 __proto__: Page.prototype, |
89 | 87 |
90 /** Creates a connection to the Bluetooth device. */ | 88 /** Creates a connection to the Bluetooth device. */ |
91 connect: function() { | 89 connect: function() { |
92 if (this.status_ !== device_collection.ConnectionStatus.DISCONNECTED) | 90 if (this.status_ !== device_collection.ConnectionStatus.DISCONNECTED) |
93 return; | 91 return; |
94 | 92 |
95 this.updateConnectionStatus_( | 93 this.updateConnectionStatus_( |
96 device_collection.ConnectionStatus.CONNECTING); | 94 device_collection.ConnectionStatus.CONNECTING); |
97 | 95 |
98 device_broker.connectToDevice(this.deviceInfo.address).then( | 96 device_broker.connectToDevice(this.deviceInfo.address) |
99 function(devicePtr) { | 97 .then(function(devicePtr) { |
100 this.devicePtr_ = devicePtr; | 98 this.devicePtr_ = devicePtr; |
101 | 99 |
102 this.updateConnectionStatus_( | 100 this.updateConnectionStatus_( |
103 device_collection.ConnectionStatus.CONNECTED); | 101 device_collection.ConnectionStatus.CONNECTED); |
104 | 102 |
105 // Fetch services asynchronously. | 103 // Fetch services asynchronously. |
106 return this.devicePtr_.getServices(); | 104 return this.devicePtr_.getServices(); |
107 }.bind(this)).then(function(response) { | 105 }.bind(this)) |
| 106 .then(function(response) { |
108 this.deviceInfo.services = response.services; | 107 this.deviceInfo.services = response.services; |
109 this.serviceList_.load(this.deviceInfo.address); | 108 this.serviceList_.load(this.deviceInfo.address); |
110 this.redraw(); | 109 this.redraw(); |
111 this.fireDeviceInfoChanged_(); | 110 this.fireDeviceInfoChanged_(); |
112 }.bind(this)).catch(function(error) { | 111 }.bind(this)) |
| 112 .catch(function(error) { |
113 // If a connection error occurs while fetching the services, the | 113 // If a connection error occurs while fetching the services, the |
114 // devicePtr reference must be removed. | 114 // devicePtr reference must be removed. |
115 if (this.devicePtr_) { | 115 if (this.devicePtr_) { |
116 this.devicePtr_.disconnect(); | 116 this.devicePtr_.disconnect(); |
117 this.devicePtr_ = null; | 117 this.devicePtr_ = null; |
118 } | 118 } |
119 | 119 |
120 Snackbar.show( | 120 Snackbar.show( |
121 this.deviceInfo.name_for_display + ': ' + error.message, | 121 this.deviceInfo.name_for_display + ': ' + error.message, |
122 SnackbarType.ERROR, 'Retry', this.connect.bind(this)); | 122 SnackbarType.ERROR, 'Retry', this.connect.bind(this)); |
123 | 123 |
124 this.updateConnectionStatus_( | 124 this.updateConnectionStatus_( |
125 device_collection.ConnectionStatus.DISCONNECTED); | 125 device_collection.ConnectionStatus.DISCONNECTED); |
126 }.bind(this)); | 126 }.bind(this)); |
127 }, | 127 }, |
128 | 128 |
129 /** Disconnects the page from the Bluetooth device. */ | 129 /** Disconnects the page from the Bluetooth device. */ |
130 disconnect: function() { | 130 disconnect: function() { |
131 if (!this.devicePtr_) return; | 131 if (!this.devicePtr_) |
| 132 return; |
132 | 133 |
133 this.devicePtr_.disconnect(); | 134 this.devicePtr_.disconnect(); |
134 this.devicePtr_ = null; | 135 this.devicePtr_ = null; |
135 this.updateConnectionStatus_( | 136 this.updateConnectionStatus_( |
136 device_collection.ConnectionStatus.DISCONNECTED); | 137 device_collection.ConnectionStatus.DISCONNECTED); |
137 }, | 138 }, |
138 | 139 |
139 /** Redraws the contents of the page with the current |deviceInfo|. */ | 140 /** Redraws the contents of the page with the current |deviceInfo|. */ |
140 redraw: function() { | 141 redraw: function() { |
141 var isConnected = this.deviceInfo.is_gatt_connected; | 142 var isConnected = this.deviceInfo.is_gatt_connected; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 status: status, | 224 status: status, |
224 } | 225 } |
225 })); | 226 })); |
226 }, | 227 }, |
227 }; | 228 }; |
228 | 229 |
229 return { | 230 return { |
230 DeviceDetailsPage: DeviceDetailsPage, | 231 DeviceDetailsPage: DeviceDetailsPage, |
231 }; | 232 }; |
232 }); | 233 }); |
OLD | NEW |