summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/image_burner.js
blob: b3e5aa21c1b9265071c68e583a4e278161c1f853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var localStrings;
var browserBridge;

/**
 * Class that keeps track of current burn process state.
 * @param {Object} strings Localized state strings.
 * @constructor
 */
function State(strings) {
  this.setStrings(strings);
  this.changeState(State.StatesEnum.DEVICE_NONE);
}

/**
 * State Enum object.
 */
State.StatesEnum = {
  DEVICE_NONE: {
    cssState: 'device-detected-none',
  },
  DEVICE_USB: {
    cssState: 'device-detected-usb warning',
  },
  DEVICE_SD: {
    cssState: 'device-detected-sd warning',
  },
  DEVICE_MUL: {
    cssState: 'device-detected-mul warning',
  },
  ERROR_NO_NETWORK: {
    cssState: 'warning-no-conf',
  },
  ERROR_DEVICE_TOO_SMALL: {
    cssState: 'warning-no-conf',
  },
  PROGRESS_DOWNLOAD: {
    cssState: 'progress progress-canceble',
  },
  PROGRESS_UNZIP: {
    cssState: 'progress progress-canceble',
  },
  PROGRESS_BURN: {
    cssState: 'progress',
  },
  FAIL: {
    cssState: 'error',
  },
  SUCCESS: {
    cssState: 'success',
  },
};

State.prototype = {
  /**
   * Sets the state strings.
   * @param {Object} strings Localized state strings.
   */
  setStrings: function(strings) {
    State.StatesEnum.DEVICE_NONE.statusText =
        strings.getString('statusDevicesNone');
    State.StatesEnum.DEVICE_NONE.warningText =
        strings.getString('warningDevicesNone');
    State.StatesEnum.DEVICE_USB.statusText =
      strings.getString('statusDeviceUSB');
    State.StatesEnum.DEVICE_SD.statusText = strings.getString('statusDeviceSD');
    State.StatesEnum.DEVICE_MUL.statusText =
        strings.getString('statusDevicesMultiple');
    State.StatesEnum.ERROR_NO_NETWORK.statusText =
        strings.getString('statusNoConnection');
    State.StatesEnum.ERROR_NO_NETWORK.warningText =
        strings.getString('warningNoConnection');
    State.StatesEnum.ERROR_DEVICE_TOO_SMALL.statusText =
        strings.getString('statusNoSpace');
    State.StatesEnum.PROGRESS_DOWNLOAD.statusText =
        strings.getString('statusDownloading');
    State.StatesEnum.PROGRESS_UNZIP.statusText =
        strings.getString('statusUnzip');
    State.StatesEnum.PROGRESS_BURN.statusText = strings.getString('statusBurn');
    State.StatesEnum.FAIL.statusText = strings.getString('statusError');
    State.StatesEnum.SUCCESS.statusText = strings.getString('statusSuccess');
    State.StatesEnum.SUCCESS.warningText = strings.getString('warningSuccess');
  },

  /**
   * Changes the current state to new state.
   * @param {Object} newState Specifies the new state object.
   */
  changeState: function(newState) {
    if (newState == this.state)
      return;
    this.state = newState;

    $('main-content').className = this.state.cssState;

    $('status-text').textContent = this.state.statusText;

    if (newState.warningText)
      $('warning-text').textContent = this.state.warningText;

    if (this.isInitialState() && this.state != State.StatesEnum.DEVICE_NONE) {
      $('warning-button').textContent = localStrings.getString('confirmButton');
    } else if (this.state == State.StatesEnum.FAIL) {
      $('warning-button').textContent =
          localStrings.getString('retryButton');
    }
  },

  /**
   * Reset to initial state.
   * @param {Array} devices Array of device information.
   */
  gotoInitialState: function(devices) {
    if (devices.length == 0) {
      this.changeState(State.StatesEnum.DEVICE_NONE);
    } else if (devices.length == 1) {
      // If a device type is not specified for some reason, we should
      // default to display a USB device.
      var initialState = State.StatesEnum.DEVICE_USB;
      if (devices[0].type == 'sd')
        initialState = State.StatesEnum.DEVICE_SD;
      this.changeState(initialState);
    } else {
      this.changeState(State.StatesEnum.DEVICE_MUL);
    }
  },

  /**
   * Returns true if the device is in initial state.
   * @return {boolean} True if the device is in initial state else false.
   */
  isInitialState: function() {
    return this.state == State.StatesEnum.DEVICE_NONE ||
           this.state == State.StatesEnum.DEVICE_USB ||
           this.state == State.StatesEnum.DEVICE_SD ||
           this.state == State.StatesEnum.DEVICE_MUL;
  },

  /**
   * Returns true if device state matches the given state name.
   * @param {string} stateName Given state name.
   * @return {boolean} True if the device state matches the given state name.
   */
  equals: function(stateName) {
    return this.state == stateName;
  }
};

/**
 * Class that keeps track of available devices.
 * @constructor
 */
function DeviceSelection() {
  this.selectedDevice = undefined;
  this.devices = [];
}

DeviceSelection.prototype = {
  /**
   * Shows the currently selected device.
   */
  showDeviceSelection: function() {
    if (this.devices.length == 0) {
      this.selectedDevice = undefined;
    } else {
      this.selectDevice(this.devices[0].devicePath);
    }
  },

  /**
   * Handles device selected event.
   * @param {string} label Device label.
   * @param {string} filePath File path.
   * @param {string} devicePath Selected device path.
   */
  onDeviceSelected: function(label, filePath, devicePath) {
    $('warning-button').onclick =
        browserBridge.sendBurnImageMessage.bind(browserBridge, filePath,
            devicePath);

    this.selectedDevice = devicePath;

    $('warning-text').textContent =
        localStrings.getStringF('warningDevices', label);
  },

  /**
   * Selects the specified device based on the specified path.
   * @param {string} path Device path.
   */
  selectDevice: function(path) {
    var element = $('radio-' + path);
    element.checked = true;
    element.onclick.apply(element);
  },

  /**
   * Creates a new device element.
   * @param {Object} device Specifies new device information.
   * @return {HTMLLIElement} New device element.
   */
  createNewDeviceElement: function(device) {
    var element = document.createElement('li');
    var radioButton = document.createElement('input');
    radioButton.type = 'radio';
    radioButton.name = 'device';
    radioButton.value = device.label;
    radioButton.id = 'radio-' + device.devicePath;
    radioButton.className = 'float-start';
    var deviceLabelText = document.createElement('p');
    deviceLabelText.textContent = device.label;
    deviceLabelText.className = 'select-option float-start';
    var newLine = document.createElement('div');
    newLine.className = 'new-line';
    element.appendChild(radioButton);
    element.appendChild(deviceLabelText);
    element.appendChild(newLine);
    element.id = 'select-' + device.devicePath;
    element.className = 'selection-element';
    radioButton.onclick = this.onDeviceSelected.bind(this,
        device.label, device.filePath, device.devicePath);
    return element;
  },

  /**
   * Updates the list of selected devices.
   * @param {Array} devices List of devices.
   */
  devicesUpdated: function(newDevices) {
    this.devices = newDevices;
    var selectListDOM = $('device-selection');
    selectListDOM.innerHTML = '';
    if (this.devices.length > 0) {
      for (var i = 0; i < this.devices.length; i++) {
        var element = this.createNewDeviceElement(this.devices[i]);
        selectListDOM.appendChild(element);
      }
      this.selectDevice(this.devices[0].devicePath);
    } else {
      this.selectedDevice = undefined;
    }
  },

  /**
   * Handles device added event.
   * @param {Object} device Device information.
   * @param {boolean} allowSelect True to update the selected device info.
   */
  deviceAdded: function(device, allowSelect) {
    this.devices.push(device);
    var selectListDOM = $('device-selection');
    selectListDOM.appendChild(this.createNewDeviceElement(device));
    if (allowSelect && this.devices.length == 1)
      this.selectDevice(device.devicePath);
  },

  /**
   * Handles device removed event.
   * @param {string} devicePath Device path to be removed.
   * @param {boolean} allowSelect True to update the selected device info.
   */
  deviceRemoved: function(devicePath, allowSelect) {
    device = this.findDevice(devicePath);
    if (!device)
      return;
    this.devices.splice(this.devices.indexOf(device), 1);

    // Remove device selection element from DOM.
    var deviceSelectElement = $('select-' + devicePath);
    deviceSelectElement.parentNode.removeChild(deviceSelectElement);

    // Update selected device element.
    if (allowSelect) {
      if (this.devices.length > 0) {
        if (this.selectedDevice == devicePath)
          this.selectDevice(this.devices[0].devicePath);
      } else {
        this.selectedDevice = undefined;
      }
    }
  },

  /**
   * Finds device with given device path property.
   * @param {string} devicePath Device path of device to find.
   * @return {Object} Matching device information or undefined if not found.
   */
  findDevice: function(devicePath) {
    for (var i = 0; i < this.devices.length; ++i) {
      if (this.devices[i].devicePath == devicePath) {
        return this.devices[i];
      }
    }
    return undefined;
  }
};

/**
 * Class that handles communication with chrome.
 * @constructor
 */
function BrowserBridge() {
  this.currentState = new State(localStrings);
  this.deviceSelection = new DeviceSelection();
  // We will use these often so it makes sence making them class members to
  // avoid frequent document.getElementById calls.
  this.progressElement = $('progress-div');
  this.progressText = $('progress-text');
  this.progressTimeLeftText = $('pending-time');
}

BrowserBridge.prototype = {
  sendCancelMessage: function() {
    chrome.send('cancelBurnImage');
  },

  sendGetDevicesMessage: function() {
    chrome.send('getDevices');
  },

  sendWebuiInitializedMessage: function() {
    chrome.send('webuiInitialized');
  },

  /**
   * Sends the burn image message to c++ code.
   * @param {string} filePath Specifies the file path.
   * @param {string} devicePath Specifies the device path.
   */
  sendBurnImageMessage: function(filePath, devicePath) {
    chrome.send('burnImage', [devicePath, filePath]);
  },

  reportSuccess: function() {
    this.currentState.changeState(State.StatesEnum.SUCCESS);
  },

  /**
   * Update the device state to report a failure and display an error message to
   * the user.
   * @param {string} errorMessage Specifies the warning text message.
   */
  reportFail: function(errorMessage) {
    this.currentState.changeState(State.StatesEnum.FAIL);
    $('warning-text').textContent = errorMessage;
    $('warning-button').onclick = this.onBurnRetry.bind(this);
  },

  /**
   * Handles device added event.
   * @param {Object} device Device information.
   */
  deviceAdded: function(device) {
    var inInitialState = this.currentState.isInitialState();
    this.deviceSelection.deviceAdded(device, inInitialState);
    if (inInitialState)
      this.currentState.gotoInitialState(this.deviceSelection.devices);
  },

  /**
   * Handles device removed event.
   * @param {string} devicePath Device path to be removed.
   */
  deviceRemoved: function(devicePath) {
    var inInitialState = this.currentState.isInitialState();
    this.deviceSelection.deviceRemoved(devicePath, inInitialState);
    if (inInitialState)
      this.currentState.gotoInitialState(this.deviceSelection.devices);
  },

  /**
   * Gets device callbacks and update the current state.
   * @param {Array} devices List of devices.
   */
  getDevicesCallback: function(devices) {
    this.deviceSelection.devicesUpdated(devices);
    this.currentState.gotoInitialState(this.deviceSelection.devices);
    this.sendWebuiInitializedMessage();
  },

  /**
   *  Updates the progress information based on the signal received.
   *  @param {Object} updateSignal Specifies the signal information.
   */
  updateProgress: function(updateSignal) {
    if (updateSignal.progressType == 'download' &&
        !this.currentState.equals(State.StatesEnum.PROGRESS_DOWNLOAD)) {
      this.currentState.changeState(State.StatesEnum.PROGRESS_DOWNLOAD);
    } else if (updateSignal.progressType == 'unzip' &&
        !this.currentState.equals(State.StatesEnum.PROGRESS_UNZIP)) {
      this.currentState.changeState(State.StatesEnum.PROGRESS_UNZIP);
    } else if (updateSignal.progressType == 'burn' &&
        !this.currentState.equals(State.StatesEnum.PROGRESS_BURN)) {
      this.currentState.changeState(State.StatesEnum.PROGRESS_BURN);
    }

    if (!(updateSignal.amountTotal > 0)) {
      this.progressElement.removeAttribute('value');
    } else {
      this.progressElement.value = updateSignal.amountFinished;
      this.progressElement.max = updateSignal.amountTotal;
    }

    this.progressText.textContent = updateSignal.progressText;
    this.progressTimeLeftText.textContent = updateSignal.timeLeftText;
  },

  reportNoNetwork: function() {
    this.currentState.changeState(State.StatesEnum.ERROR_NO_NETWORK);
  },

  reportNetworkDetected: function() {
    if (this.currentState.equals(State.StatesEnum.ERROR_NO_NETWORK)) {
      this.deviceSelection.showDeviceSelection();
      this.currentState.gotoInitialState(this.deviceSelection.devices);
    }
  },

  /**
   *  Updates the current state to report device too small error.
   *  @param {number} deviceSize Received device size.
   */
  reportDeviceTooSmall: function(deviceSize) {
    this.currentState.changeState(State.StatesEnum.ERROR_DEVICE_TOO_SMALL);
    $('warning-text').textContent =
        localStrings.getStringF('warningNoSpace', deviceSize);
  },

  /**
   * Processes click on 'Retry' button in FAIL state.
   */
  onBurnRetry: function() {
    this.deviceSelection.showDeviceSelection();
    this.currentState.gotoInitialState(this.deviceSelection.devices);
  }
};

document.addEventListener('DOMContentLoaded', function() {
  localStrings = new LocalStrings();
  browserBridge = new BrowserBridge();

  jstProcess(new JsEvalContext(templateData), $('more-info-link'));

  $('cancel-button').onclick =
      browserBridge.sendCancelMessage.bind(browserBridge);
  browserBridge.sendGetDevicesMessage();
});