| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 var localStrings; | |
| 6 var browserBridge; | |
| 7 | |
| 8 // Class that keeps track of current burn process state. | |
| 9 function State(strings) { | |
| 10 this.setStrings(strings); | |
| 11 this.changeState(State.StatesEnum.DEVICE_NONE); | |
| 12 } | |
| 13 | |
| 14 State.StatesEnum = { | |
| 15 DEVICE_NONE : { | |
| 16 cssState : "device-detected-none", | |
| 17 }, | |
| 18 DEVICE_USB : { | |
| 19 cssState : "device-detected-usb warning", | |
| 20 }, | |
| 21 DEVICE_SD : { | |
| 22 cssState : "device-detected-sd warning", | |
| 23 }, | |
| 24 DEVICE_MUL : { | |
| 25 cssState: "device-detected-mul warning", | |
| 26 }, | |
| 27 ERROR_NO_NETWORK : { | |
| 28 cssState : "warning-no-conf", | |
| 29 }, | |
| 30 ERROR_DEVICE_TOO_SMALL : { | |
| 31 cssState : "warning-no-conf", | |
| 32 }, | |
| 33 PROGRESS_DOWNLOAD : { | |
| 34 cssState : "progress progress-canceble", | |
| 35 }, | |
| 36 PROGRESS_UNZIP : { | |
| 37 cssState : "progress progress-canceble", | |
| 38 }, | |
| 39 PROGRESS_BURN : { | |
| 40 cssState : "progress", | |
| 41 }, | |
| 42 FAIL : { | |
| 43 cssState : "error", | |
| 44 }, | |
| 45 SUCCESS : { | |
| 46 cssState : "success", | |
| 47 }, | |
| 48 }; | |
| 49 | |
| 50 State.prototype = { | |
| 51 setStrings: function(strings) { | |
| 52 State.StatesEnum.DEVICE_NONE.statusText = | |
| 53 strings.getString('statusDevicesNone'); | |
| 54 State.StatesEnum.DEVICE_NONE.warningText = | |
| 55 strings.getString('warningDevicesNone'); | |
| 56 State.StatesEnum.DEVICE_USB.statusText = | |
| 57 strings.getString('statusDeviceUSB'); | |
| 58 State.StatesEnum.DEVICE_SD.statusText = strings.getString('statusDeviceSD'); | |
| 59 State.StatesEnum.DEVICE_MUL.statusText = | |
| 60 strings.getString('statusDevicesMultiple'); | |
| 61 State.StatesEnum.ERROR_NO_NETWORK.statusText = | |
| 62 strings.getString('statusNoConnection'); | |
| 63 State.StatesEnum.ERROR_NO_NETWORK.warningText = | |
| 64 strings.getString('warningNoConnection'); | |
| 65 State.StatesEnum.ERROR_DEVICE_TOO_SMALL.statusText = | |
| 66 strings.getString('statusNoSpace'); | |
| 67 State.StatesEnum.PROGRESS_DOWNLOAD.statusText = | |
| 68 strings.getString('statusDownloading'); | |
| 69 State.StatesEnum.PROGRESS_UNZIP.statusText = | |
| 70 strings.getString('statusUnzip'); | |
| 71 State.StatesEnum.PROGRESS_BURN.statusText = strings.getString('statusBurn'); | |
| 72 State.StatesEnum.FAIL.statusText = strings.getString('statusError'); | |
| 73 State.StatesEnum.SUCCESS.statusText = strings.getString('statusSuccess'); | |
| 74 State.StatesEnum.SUCCESS.warningText = strings.getString('warningSuccess'); | |
| 75 }, | |
| 76 | |
| 77 changeState: function(newState) { | |
| 78 if (newState == this.state) | |
| 79 return; | |
| 80 this.state = newState; | |
| 81 | |
| 82 $('main-content').className = this.state.cssState; | |
| 83 | |
| 84 $('status-text').textContent = this.state.statusText; | |
| 85 | |
| 86 if (newState.warningText) { | |
| 87 $('warning-text').textContent = this.state.warningText; | |
| 88 } | |
| 89 | |
| 90 if (this.isInitialState() && this.state != State.StatesEnum.DEVICE_NONE) { | |
| 91 $('warning-button').textContent = localStrings.getString('confirmButton'); | |
| 92 } else if (this.state == State.StatesEnum.FAIL) { | |
| 93 $('warning-button').textContent = | |
| 94 localStrings.getString('retryButton'); | |
| 95 } | |
| 96 }, | |
| 97 | |
| 98 gotoInitialState: function(deviceCount) { | |
| 99 if (deviceCount == 0) { | |
| 100 this.changeState(State.StatesEnum.DEVICE_NONE); | |
| 101 } else if (deviceCount == 1) { | |
| 102 this.changeState(State.StatesEnum.DEVICE_USB); | |
| 103 } else { | |
| 104 this.changeState(State.StatesEnum.DEVICE_MUL); | |
| 105 } | |
| 106 }, | |
| 107 | |
| 108 isInitialState: function() { | |
| 109 return (this.state == State.StatesEnum.DEVICE_NONE || | |
| 110 this.state == State.StatesEnum.DEVICE_USB || | |
| 111 this.state == State.StatesEnum.DEVICE_SD || | |
| 112 this.state == State.StatesEnum.DEVICE_MUL); | |
| 113 }, | |
| 114 | |
| 115 equals: function(stateName) { | |
| 116 return this.state == stateName; | |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 // Class that keeps track of available devices. | |
| 121 function DeviceSelection() { | |
| 122 this.deviceCount = 0; | |
| 123 }; | |
| 124 | |
| 125 DeviceSelection.prototype = { | |
| 126 clearSelectList: function(list) { | |
| 127 list.innerHtml = ''; | |
| 128 }, | |
| 129 | |
| 130 showDeviceSelection: function() { | |
| 131 if (this.deviceCount == 0) { | |
| 132 this.selectedDevice = undefined; | |
| 133 } else { | |
| 134 var devices = document.getElementsByClassName('selection-element'); | |
| 135 this.selectDevice(devices[0].devicePath); | |
| 136 } | |
| 137 return this.deviceCount; | |
| 138 }, | |
| 139 | |
| 140 onDeviceSelected: function(label, filePath, devicePath) { | |
| 141 $('warning-button').onclick = | |
| 142 browserBridge.sendBurnImageMessage.bind(browserBridge, filePath, | |
| 143 devicePath); | |
| 144 | |
| 145 this.selectedDevice = devicePath; | |
| 146 | |
| 147 $('warning-text').textContent = | |
| 148 localStrings.getStringF('warningDevices', label); | |
| 149 }, | |
| 150 | |
| 151 selectDevice: function(path) { | |
| 152 var element = $('radio ' + path); | |
| 153 element.checked = true; | |
| 154 element.onclick.apply(element); | |
| 155 }, | |
| 156 | |
| 157 createNewDeviceElement: function(device) { | |
| 158 var element = document.createElement('li'); | |
| 159 var radioButton = document.createElement('input'); | |
| 160 radioButton.type = 'radio'; | |
| 161 radioButton.name = 'device'; | |
| 162 radioButton.value = device.label; | |
| 163 radioButton.id = 'radio ' + device.devicePath; | |
| 164 radioButton.className = 'float-start'; | |
| 165 var deviceLabelText = document.createElement('p'); | |
| 166 deviceLabelText.textContent = device.label; | |
| 167 deviceLabelText.className = 'select-option float-start'; | |
| 168 var newLine = document.createElement('div'); | |
| 169 newLine.className = 'new-line'; | |
| 170 element.appendChild(radioButton); | |
| 171 element.appendChild(deviceLabelText); | |
| 172 element.appendChild(newLine); | |
| 173 element.id = 'select ' + device.devicePath; | |
| 174 element.devicePath = device.devicePath; | |
| 175 element.className = 'selection-element'; | |
| 176 radioButton.onclick = this.onDeviceSelected.bind(this, | |
| 177 device.label, device.filePath, device.devicePath); | |
| 178 return element; | |
| 179 }, | |
| 180 | |
| 181 getDevicesCallback: function(devices) { | |
| 182 var selectListDOM = $('device-selection'); | |
| 183 this.clearSelectList(selectListDOM); | |
| 184 this.deviceCount = devices.length; | |
| 185 if (devices.length > 0) { | |
| 186 for (var i = 0; i < devices.length; i++) { | |
| 187 var element = this.createNewDeviceElement(devices[i]); | |
| 188 selectListDOM.appendChild(element); | |
| 189 } | |
| 190 this.selectDevice(devices[0].devicePath); | |
| 191 } else { | |
| 192 this.selectedDevice = undefined; | |
| 193 } | |
| 194 return this.deviceCount; | |
| 195 }, | |
| 196 | |
| 197 deviceAdded: function(device, allowSelect) { | |
| 198 var selectListDOM = $('device-selection'); | |
| 199 selectListDOM.appendChild(this.createNewDeviceElement(device)); | |
| 200 this.deviceCount++; | |
| 201 if (allowSelect && this.deviceCount == 1) | |
| 202 this.selectDevice(device.devicePath); | |
| 203 return this.deviceCount; | |
| 204 }, | |
| 205 | |
| 206 deviceRemoved: function(devicePath, allowSelect) { | |
| 207 var deviceSelectElement = $('select ' + devicePath); | |
| 208 deviceSelectElement.parentNode.removeChild(deviceSelectElement); | |
| 209 this.deviceCount--; | |
| 210 var devices = document.getElementsByClassName('selection-element'); | |
| 211 | |
| 212 if (allowSelect) { | |
| 213 if (devices.length > 0) { | |
| 214 if (this.selectedDevice == devicePath) | |
| 215 this.selectDevice(devices[0].devicePath); | |
| 216 } else { | |
| 217 this.selectedDevice = undefined; | |
| 218 } | |
| 219 } | |
| 220 return this.deviceCount; | |
| 221 } | |
| 222 }; | |
| 223 | |
| 224 // Class that handles communication with chrome. | |
| 225 function BrowserBridge() { | |
| 226 this.currentState = new State(localStrings); | |
| 227 this.devices = new DeviceSelection(); | |
| 228 // We will use these often so it makes sence making them class members to | |
| 229 // avoid frequent document.getElementById calls. | |
| 230 this.progressElement = $('progress-div'); | |
| 231 this.progressText = $('progress-text'); | |
| 232 this.progressTimeLeftText = $('pending-time'); | |
| 233 }; | |
| 234 | |
| 235 BrowserBridge.prototype = { | |
| 236 sendCancelMessage: function() { | |
| 237 chrome.send("cancelBurnImage"); | |
| 238 }, | |
| 239 | |
| 240 sendGetDevicesMessage: function() { | |
| 241 chrome.send("getDevices"); | |
| 242 }, | |
| 243 | |
| 244 sendWebuiInitializedMessage: function() { | |
| 245 chrome.send("webuiInitialized"); | |
| 246 }, | |
| 247 | |
| 248 sendBurnImageMessage: function(filePath, devicePath) { | |
| 249 chrome.send('burnImage', [devicePath, filePath]); | |
| 250 }, | |
| 251 | |
| 252 reportSuccess: function() { | |
| 253 this.currentState.changeState(State.StatesEnum.SUCCESS); | |
| 254 }, | |
| 255 | |
| 256 reportFail: function(errorMessage) { | |
| 257 this.currentState.changeState(State.StatesEnum.FAIL); | |
| 258 $('warning-text').textContent = errorMessage; | |
| 259 $('warning-button').onclick = this.onBurnRetry.bind(this); | |
| 260 }, | |
| 261 | |
| 262 deviceAdded: function(device) { | |
| 263 var inInitialState = this.currentState.isInitialState(); | |
| 264 var deviceCount = this.devices.deviceAdded(device, inInitialState); | |
| 265 if (inInitialState) | |
| 266 this.currentState.gotoInitialState(deviceCount); | |
| 267 }, | |
| 268 | |
| 269 deviceRemoved: function(device) { | |
| 270 var inInitialState = this.currentState.isInitialState(); | |
| 271 var deviceCount = this.devices.deviceRemoved(device, inInitialState); | |
| 272 if (inInitialState) | |
| 273 this.currentState.gotoInitialState(deviceCount); | |
| 274 }, | |
| 275 | |
| 276 getDevicesCallback: function(devices) { | |
| 277 var deviceCount = this.devices.getDevicesCallback(devices); | |
| 278 this.currentState.gotoInitialState(deviceCount); | |
| 279 this.sendWebuiInitializedMessage(); | |
| 280 }, | |
| 281 | |
| 282 updateProgress: function(update_signal) { | |
| 283 if (update_signal.progressType == 'download' && | |
| 284 !this.currentState.equals(State.StatesEnum.PROGRESS_DOWNLOAD)) { | |
| 285 this.currentState.changeState(State.StatesEnum.PROGRESS_DOWNLOAD); | |
| 286 } else if (update_signal.progressType == 'unzip' && | |
| 287 !this.currentState.equals(State.StatesEnum.PROGRESS_UNZIP)) { | |
| 288 this.currentState.changeState(State.StatesEnum.PROGRESS_UNZIP); | |
| 289 } else if (update_signal.progressType == 'burn' && | |
| 290 !this.currentState.equals(State.StatesEnum.PROGRESS_BURN)) { | |
| 291 this.currentState.changeState(State.StatesEnum.PROGRESS_BURN); | |
| 292 } | |
| 293 | |
| 294 if (!(update_signal.amountTotal > 0)) { | |
| 295 this.progressElement.removeAttribute('value'); | |
| 296 } else { | |
| 297 this.progressElement.value = update_signal.amountFinished; | |
| 298 this.progressElement.max = update_signal.amountTotal; | |
| 299 } | |
| 300 | |
| 301 this.progressText.textContent = update_signal.progressText; | |
| 302 this.progressTimeLeftText.textContent = update_signal.timeLeftText; | |
| 303 }, | |
| 304 | |
| 305 reportNoNetwork: function() { | |
| 306 this.currentState.changeState(State.StatesEnum.ERROR_NO_NETWORK); | |
| 307 }, | |
| 308 | |
| 309 reportNetworkDetected: function() { | |
| 310 if (this.currentState.equals(State.StatesEnum.ERROR_NO_NETWORK)) { | |
| 311 var deviceCount = this.devices.showDeviceSelection(); | |
| 312 this.currentState.gotoInitialState(deviceCount); | |
| 313 } | |
| 314 }, | |
| 315 | |
| 316 reportDeviceTooSmall: function(device_size) { | |
| 317 this.currentState.changeState(State.StatesEnum.ERROR_DEVICE_TOO_SMALL); | |
| 318 $('warning-text').textContent = | |
| 319 localStrings.getStringF('warningNoSpace', device_size); | |
| 320 }, | |
| 321 | |
| 322 // Processes click on "Retry" button in FAIL state. | |
| 323 onBurnRetry: function () { | |
| 324 var deviceCount = this.devices.showDeviceSelection(); | |
| 325 this.currentState.gotoInitialState(deviceCount); | |
| 326 } | |
| 327 }; | |
| 328 | |
| 329 document.addEventListener('DOMContentLoaded', function() { | |
| 330 localStrings = new LocalStrings(); | |
| 331 browserBridge = new BrowserBridge(); | |
| 332 | |
| 333 jstProcess(new JsEvalContext(templateData), $("more-info-link")); | |
| 334 | |
| 335 $('cancel-button').onclick = | |
| 336 browserBridge.sendCancelMessage.bind(browserBridge); | |
| 337 browserBridge.sendGetDevicesMessage(); | |
| 338 }); | |
| OLD | NEW |