OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 bluetooth_internals.html, served from | 6 * Javascript for bluetooth_internals.html, served from |
7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
8 */ | 8 */ |
9 | 9 |
10 // Expose for testing. | 10 // Expose for testing. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 proxy.disconnect(); | 61 proxy.disconnect(); |
62 deviceAddressToProxy.delete(address); | 62 deviceAddressToProxy.delete(address); |
63 devices.updateConnectionStatus( | 63 devices.updateConnectionStatus( |
64 address, device_collection.ConnectionStatus.DISCONNECTED); | 64 address, device_collection.ConnectionStatus.DISCONNECTED); |
65 return; | 65 return; |
66 } | 66 } |
67 | 67 |
68 devices.updateConnectionStatus( | 68 devices.updateConnectionStatus( |
69 address, device_collection.ConnectionStatus.CONNECTING); | 69 address, device_collection.ConnectionStatus.CONNECTING); |
70 | 70 |
71 adapterBroker.connectToDevice(address).then(function(deviceProxy) { | 71 adapterBroker.connectToDevice(address) |
72 var deviceInfo = devices.getByAddress(address); | 72 .then(function(deviceProxy) { |
73 if (!deviceInfo) { | 73 var deviceInfo = devices.getByAddress(address); |
74 // Device no longer in list, so drop the connection. | 74 if (!deviceInfo) { |
75 deviceProxy.disconnect(); | 75 // Device no longer in list, so drop the connection. |
76 return; | 76 deviceProxy.disconnect(); |
77 } | 77 return; |
| 78 } |
78 | 79 |
79 deviceAddressToProxy.set(address, deviceProxy); | 80 deviceAddressToProxy.set(address, deviceProxy); |
80 devices.updateConnectionStatus( | 81 devices.updateConnectionStatus( |
81 address, device_collection.ConnectionStatus.CONNECTED); | 82 address, device_collection.ConnectionStatus.CONNECTED); |
82 Snackbar.show(deviceInfo.name_for_display + ': Connected', | 83 Snackbar.show( |
83 SnackbarType.SUCCESS); | 84 deviceInfo.name_for_display + ': Connected', |
| 85 SnackbarType.SUCCESS); |
84 | 86 |
85 // Fetch services asynchronously. | 87 // Fetch services asynchronously. |
86 return deviceProxy.getServices(); | 88 return deviceProxy.getServices(); |
87 }).then(function(response) { | 89 }) |
88 if (!response) return; | 90 .then(function(response) { |
| 91 if (!response) |
| 92 return; |
89 | 93 |
90 var deviceInfo = devices.getByAddress(address); | 94 var deviceInfo = devices.getByAddress(address); |
91 deviceInfo.services = response.services; | 95 deviceInfo.services = response.services; |
92 devices.addOrUpdate(deviceInfo); | 96 devices.addOrUpdate(deviceInfo); |
93 }).catch(function(error) { | 97 }) |
94 // If a connection error occurs while fetching the services, the proxy | 98 .catch(function(error) { |
95 // reference must be removed. | 99 // If a connection error occurs while fetching the services, the proxy |
96 var proxy = deviceAddressToProxy.get(address); | 100 // reference must be removed. |
97 if (proxy) { | 101 var proxy = deviceAddressToProxy.get(address); |
98 proxy.disconnect(); | 102 if (proxy) { |
99 deviceAddressToProxy.delete(address); | 103 proxy.disconnect(); |
100 } | 104 deviceAddressToProxy.delete(address); |
| 105 } |
101 | 106 |
102 devices.updateConnectionStatus( | 107 devices.updateConnectionStatus( |
103 address, device_collection.ConnectionStatus.DISCONNECTED); | 108 address, device_collection.ConnectionStatus.DISCONNECTED); |
104 | 109 |
105 var deviceInfo = devices.getByAddress(address); | 110 var deviceInfo = devices.getByAddress(address); |
106 Snackbar.show(deviceInfo.name_for_display + ': ' + error.message, | 111 Snackbar.show( |
107 SnackbarType.ERROR, 'Retry', function() { handleInspect(event); }); | 112 deviceInfo.name_for_display + ': ' + error.message, |
108 }); | 113 SnackbarType.ERROR, 'Retry', function() { |
| 114 handleInspect(event); |
| 115 }); |
| 116 }); |
109 } | 117 } |
110 | 118 |
111 function setupDeviceSystem(response) { | 119 function setupDeviceSystem(response) { |
112 // Hook up device collection events. | 120 // Hook up device collection events. |
113 adapterBroker.addEventListener('deviceadded', function(event) { | 121 adapterBroker.addEventListener('deviceadded', function(event) { |
114 devices.addOrUpdate(event.detail.deviceInfo); | 122 devices.addOrUpdate(event.detail.deviceInfo); |
115 }); | 123 }); |
116 adapterBroker.addEventListener('devicechanged', function(event) { | 124 adapterBroker.addEventListener('devicechanged', function(event) { |
117 devices.addOrUpdate(event.detail.deviceInfo); | 125 devices.addOrUpdate(event.detail.deviceInfo); |
118 }); | 126 }); |
119 adapterBroker.addEventListener('deviceremoved', function(event) { | 127 adapterBroker.addEventListener('deviceremoved', function(event) { |
120 devices.remove(event.detail.deviceInfo); | 128 devices.remove(event.detail.deviceInfo); |
121 }); | 129 }); |
122 | 130 |
123 response.devices.forEach(devices.addOrUpdate, devices /* this */); | 131 response.devices.forEach(devices.addOrUpdate, devices /* this */); |
124 | 132 |
125 devicesPage.setDevices(devices); | 133 devicesPage.setDevices(devices); |
126 devicesPage.pageDiv.addEventListener('inspectpressed', handleInspect); | 134 devicesPage.pageDiv.addEventListener('inspectpressed', handleInspect); |
127 } | 135 } |
128 | 136 |
129 function setupPages() { | 137 function setupPages() { |
130 sidebarObj = new window.sidebar.Sidebar($('sidebar')); | 138 sidebarObj = new window.sidebar.Sidebar($('sidebar')); |
131 $('menu-btn').addEventListener('click', function() { sidebarObj.open(); }); | 139 $('menu-btn').addEventListener('click', function() { |
| 140 sidebarObj.open(); |
| 141 }); |
132 PageManager.addObserver(sidebarObj); | 142 PageManager.addObserver(sidebarObj); |
133 PageManager.addObserver(new PageObserver()); | 143 PageManager.addObserver(new PageObserver()); |
134 | 144 |
135 devicesPage = new DevicesPage(); | 145 devicesPage = new DevicesPage(); |
136 PageManager.register(devicesPage); | 146 PageManager.register(devicesPage); |
137 | 147 |
138 // Set up hash-based navigation. | 148 // Set up hash-based navigation. |
139 window.addEventListener('hashchange', function() { | 149 window.addEventListener('hashchange', function() { |
140 PageManager.showPageByName(window.location.hash.substr(1)); | 150 PageManager.showPageByName(window.location.hash.substr(1)); |
141 }); | 151 }); |
142 | 152 |
143 if (!window.location.hash) { | 153 if (!window.location.hash) { |
144 PageManager.showPageByName(devicesPage.name); | 154 PageManager.showPageByName(devicesPage.name); |
145 return; | 155 return; |
146 } | 156 } |
147 | 157 |
148 PageManager.showPageByName(window.location.hash.substr(1)); | 158 PageManager.showPageByName(window.location.hash.substr(1)); |
149 } | 159 } |
150 | 160 |
151 function initializeViews() { | 161 function initializeViews() { |
152 setupPages(); | 162 setupPages(); |
153 | 163 |
154 adapter_broker.getAdapterBroker() | 164 adapter_broker.getAdapterBroker() |
155 .then(function(broker) { adapterBroker = broker; }) | 165 .then(function(broker) { |
156 .then(function() { return adapterBroker.getInfo(); }) | 166 adapterBroker = broker; |
157 .then(function(response) { console.log('adapter', response.info); }) | 167 }) |
158 .then(function() { return adapterBroker.getDevices(); }) | 168 .then(function() { |
159 .then(setupDeviceSystem) | 169 return adapterBroker.getInfo(); |
160 .catch(function(error) { | 170 }) |
161 Snackbar.show(error.message, SnackbarType.ERROR); | 171 .then(function(response) { |
162 console.error(error); | 172 console.log('adapter', response.info); |
163 }); | 173 }) |
| 174 .then(function() { |
| 175 return adapterBroker.getDevices(); |
| 176 }) |
| 177 .then(setupDeviceSystem) |
| 178 .catch(function(error) { |
| 179 Snackbar.show(error.message, SnackbarType.ERROR); |
| 180 console.error(error); |
| 181 }); |
164 } | 182 } |
165 | 183 |
166 return { | 184 return {initializeViews: initializeViews}; |
167 initializeViews: initializeViews | |
168 }; | |
169 }); | 185 }); |
170 | 186 |
171 document.addEventListener( | 187 document.addEventListener( |
172 'DOMContentLoaded', bluetooth_internals.initializeViews); | 188 'DOMContentLoaded', bluetooth_internals.initializeViews); |
OLD | NEW |