summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/chromeos/nfc_debug.js
blob: 02ccf5179771ae7cd80623fa0ff2edfc18ab5229 (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
// Copyright 2014 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.

cr.define('nfcDebug', function() {
  'use strict';

  function NfcDebugUI() {
    this.adapterData_ = {};
    this.peerData_ = {};
    this.tagData_ = {};
  }

  NfcDebugUI.prototype = {
    setAdapterData: function(data) {
      this.adapterData_ = data;
    },

    setPeerData: function(data) {
      this.peerData_ = data;
    },

    setTagData: function(data) {
      this.tagData_ = data;
    },

    /**
     * Powers the NFC adapter ON or OFF.
     */
    toggleAdapterPower: function() {
      chrome.send('setAdapterPower', [!this.adapterData_.powered]);
    },

    /**
     * Tells the NFC adapter to start or stop polling.
     */
    toggleAdapterPolling: function() {
      chrome.send('setAdapterPolling', [!this.adapterData_.polling]);
    },

    /**
     * Notifies the UI that the user made an NDEF type selection and the
     * appropriate form should be displayed.
     */
    recordTypeChanged: function() {
      this.updateRecordFormContents();
    },

    /**
     * Creates a table element and populates it for each record contained
     * in the given list of records and adds them as a child of the given
     * DOMElement. This method will replace the contents of the given element
     * with the tables.
     *
     * @param {DOMElement} div The container that the records should be rendered
     *                         to.
     * @param {Array} records List of NDEF record data.
     */
    renderRecords: function(div, records) {
      div.textContent = '';
      if (records.length == 0) {
        return;
      }
      var self = this;
      records.forEach(function(record) {
        var recordDiv = document.createElement('div');
        recordDiv.setAttribute('class', 'record-div');
        for (var key in record) {
          if (!record.hasOwnProperty(key))
            continue;

          var rowDiv = document.createElement('div');
          rowDiv.setAttribute('class', 'record-key-value-div');

          var keyElement, valueElement;
          if (key == 'titles') {
            keyElement = document.createElement('div');
            keyElement.setAttribute('class', 'record-key-div');
            keyElement.appendChild(document.createTextNode(key));
            valueElement = document.createElement('div');
            valueElement.setAttribute('class', 'record-value-div');
            self.renderRecords(valueElement, record[key]);
          } else {
            keyElement = document.createElement('span');
            keyElement.setAttribute('class', 'record-key-span');
            keyElement.appendChild(document.createTextNode(key));
            valueElement = document.createElement('span');
            valueElement.setAttribute('class', 'record-value-span');
            valueElement.appendChild(document.createTextNode(record[key]));
          }
          rowDiv.appendChild(keyElement);
          rowDiv.appendChild(valueElement);
          recordDiv.appendChild(rowDiv);
        }
        div.appendChild(recordDiv);
        if (records[records.length - 1] !== record)
          div.appendChild(document.createElement('hr'));
      });
    },

    /**
     * Updates which record type form is displayed based on the currently
     * selected type.
     */
    updateRecordFormContents: function() {
      var recordTypeMenu = $('record-type-menu');
      var selectedType =
          recordTypeMenu.options[recordTypeMenu.selectedIndex].value;
      this.updateRecordFormContentsFromType(selectedType);
    },

    /**
     * Updates which record type form is displayed based on the passed in
     * type string.
     *
     * @param {string} type The record type.
     */
    updateRecordFormContentsFromType: function(type) {
      $('text-form').hidden = (type != 'text');
      $('uri-form').hidden = (type != 'uri');
      $('smart-poster-form').hidden = (type != 'smart-poster');
    },

    /**
     * Tries to push or write the record to the remote tag or device based on
     * the contents of the record form fields.
     */
    submitRecordForm: function() {
      var recordTypeMenu = $('record-type-menu');
      var selectedType =
          recordTypeMenu.options[recordTypeMenu.selectedIndex].value;
      var recordData = {};
      if (selectedType == 'text') {
        recordData.type = 'TEXT';
        if ($('text-form-text').value)
          recordData.text = $('text-form-text').value;
        if ($('text-form-encoding').value)
          recordData.encoding = $('text-form-encoding').value;
        if ($('text-form-language-code').value)
          recordData.languageCode = $('text-form-language-code').value;
      } else if (selectedType == 'uri') {
        recordData.type = 'URI';
        if ($('uri-form-uri').value)
          recordData.uri = $('uri-form-uri').value;
        if ($('uri-form-mime-type').value)
          recordData.mimeType = $('uri-form-mime-type').value;
        if ($('uri-form-target-size').value) {
          var targetSize = $('uri-form-target-size').value;
          targetSize = parseFloat(targetSize);
          recordData.targetSize = isNaN(targetSize) ? 0.0 : targetSize;
        }
      } else if (selectedType == 'smart-poster') {
        recordData.type = 'SMART_POSTER';
        if ($('smart-poster-form-uri').value)
          recordData.uri = $('smart-poster-form-uri').value;
        if ($('smart-poster-form-mime-type').value)
          recordData.mimeType = $('smart-poster-form-mime-type').value;
        if ($('smart-poster-form-target-size').value) {
          var targetSize = $('smart-poster-form-target-size').value;
          targetSize = parseFloat(targetSize);
          recordData.targetSize = isNaN(targetSize) ? 0.0 : targetSize;
        }
        var title = {};
        if ($('smart-poster-form-title-text').value)
          title.text = $('smart-poster-form-title-text').value;
        if ($('smart-poster-form-title-encoding').value)
          title.encoding = $('smart-poster-form-title-encoding').value;
        if ($('smart-poster-form-title-language-code').value)
          title.languageCode =
              $('smart-poster-form-title-language-code').value;
        if (Object.keys(title).length != 0)
          recordData.titles = [title];
      }
      chrome.send('submitRecordForm', [recordData]);
    },

    /**
     * Given a dictionary |data|, builds a table where each row contains the
     * a key and its value. The resulting table is then added as the sole child
     * of |div|. |data| contains information about an adapter, tag, or peer and
     * this method creates a table for display, thus the value of some keys
     * will be processed.
     *
     * @param {DOMElement} div The container that the table should be rendered
     *                         to.
     * @param {dictionary} data Data to generate the table from.
     */
    createTableFromData: function(div, data) {
      div.textContent = '';
      var table = document.createElement('table');
      table.classList.add('parameters-table');
      for (var key in data) {
        var row = document.createElement('tr');
        var col = document.createElement('td');
        col.textContent = key;
        row.appendChild(col);

        col = document.createElement('td');
        var value = data[key];
        if (key == 'records')
          value = value.length;
        else if (key == 'supportedTechnologies')
          value = value.join(', ');
        col.textContent = value;
        row.appendChild(col);
        table.appendChild(row);
      }
      div.appendChild(table);
    },
  };

  cr.addSingletonGetter(NfcDebugUI);

  /**
   * Initializes the page after the content has loaded.
   */
  NfcDebugUI.initialize = function() {
    $('nfc-adapter-info').hidden = true;
    $('adapter-toggles').hidden = true;
    $('nfc-adapter-info').classList.add('transition-out');
    $('ndef-record-form').classList.add('transition-out');
    $('nfc-peer-info').classList.add('transition-out');
    $('nfc-tag-info').classList.add('transition-out');
    $('power-toggle').onclick = function() {
      NfcDebugUI.getInstance().toggleAdapterPower();
    };
    $('poll-toggle').onclick = function() {
      NfcDebugUI.getInstance().toggleAdapterPolling();
    };
    $('record-type-menu').onchange = function() {
      NfcDebugUI.getInstance().recordTypeChanged();
    };
    $('record-form-submit-button').onclick = function() {
      NfcDebugUI.getInstance().submitRecordForm();
    };
    $('record-form-submit-button').hidden = true;
    NfcDebugUI.getInstance().updateRecordFormContents();
    chrome.send('initialize');
  };

  /**
   * Updates the UI based on the NFC availability on the current platform.
   *
   * @param {bool} available If true, NFC is supported on the current platform.
   */
  NfcDebugUI.onNfcAvailabilityDetermined = function(available) {
    $('nfc-not-supported').hidden = available;
  };

  /**
   * Notifies the UI that information about the NFC adapter has been received.
   *
   * @param {dictionary} data Properties of the NFC adapter.
   */
  NfcDebugUI.onNfcAdapterInfoChanged = function(data) {
    NfcDebugUI.getInstance().setAdapterData(data);

    $('nfc-adapter-info').hidden = false;
    NfcDebugUI.getInstance().createTableFromData($('adapter-parameters'), data);

    $('nfc-adapter-info').classList.toggle('transition-out', !data.present);
    $('nfc-adapter-info').classList.toggle('transition-in', data.present);
    $('ndef-record-form').classList.toggle('transition-out', !data.present);
    $('ndef-record-form').classList.toggle('transition-in', data.present);

    $('adapter-toggles').hidden = !data.present;
    $('ndef-record-form').hidden = !data.present;

    $('power-toggle').textContent = loadTimeData.getString(
        data.powered ? 'adapterPowerOffText' : 'adapterPowerOnText');
    $('poll-toggle').textContent = loadTimeData.getString(
        data.polling ? 'adapterStopPollText' : 'adapterStartPollText');
  };

  /**
   * Notifies the UI that information about an NFC peer has been received.
   *
   * @param {dictionary} data Properties of the NFC peer device.
   */
  NfcDebugUI.onNfcPeerDeviceInfoChanged = function(data) {
    NfcDebugUI.getInstance().setPeerData(data);

    if (Object.keys(data).length == 0) {
      $('nfc-peer-info').classList.add('transition-out');
      $('nfc-peer-info').classList.remove('transition-in');
      $('record-form-submit-button').hidden = true;
      return;
    }

    $('nfc-peer-info').classList.remove('transition-out');
    $('nfc-peer-info').classList.add('transition-in');

    NfcDebugUI.getInstance().createTableFromData($('peer-parameters'), data);

    $('record-form-submit-button').hidden = false;
    $('record-form-submit-button').textContent =
        loadTimeData.getString('ndefFormPushButtonText');

    if (data.records.length == 0) {
      $('peer-records-entry').hidden = true;
      return;
    }

    $('peer-records-entry').hidden = false;
    NfcDebugUI.getInstance().renderRecords($('peer-records-container'),
                                           data.records);
  };

  /**
   * Notifies the UI that information about an NFC tag has been received.
   *
   * @param {dictionary} data Properties of the NFC tag.
   */
  NfcDebugUI.onNfcTagInfoChanged = function(data) {
    NfcDebugUI.getInstance().setTagData(data);

    if (Object.keys(data).length == 0) {
      $('nfc-tag-info').classList.add('transition-out');
      $('nfc-tag-info').classList.remove('transition-in');
      $('record-form-submit-button').hidden = true;
      return;
    }

    $('nfc-tag-info').classList.remove('transition-out');
    $('nfc-tag-info').classList.add('transition-in');

    NfcDebugUI.getInstance().createTableFromData($('tag-parameters'), data);

    $('record-form-submit-button').hidden = false;
    $('record-form-submit-button').textContent =
        loadTimeData.getString('ndefFormWriteButtonText');

    if (data.records.length == 0) {
      $('tag-records-entry').hidden = true;
      return;
    }

    $('tag-records-entry').hidden = false;
    NfcDebugUI.getInstance().renderRecords($('tag-records-container'),
                                           data.records);
  };

  /**
   * Notifies the UI that a call to "setAdapterPower" failed. Displays an
   * alert.
   */
  NfcDebugUI.onSetAdapterPowerFailed = function() {
    alert(loadTimeData.getString('errorFailedToSetPowerText'));
  };

  /**
   * Notifies the UI that a call to "setAdapterPolling" failed. Displays an
   * alert.
   */
  NfcDebugUI.onSetAdapterPollingFailed = function() {
    alert(loadTimeData.getString('errorFailedToSetPollingText'));
  };

  /**
   * Notifies the UI that an error occurred while submitting an NDEF record
   * form.
   * @param {string} errorMessage An error message, describing the failure.
   */
  NfcDebugUI.onSubmitRecordFormFailed = function(errorMessage) {
    alert(loadTimeData.getString('errorFailedToSubmitPrefixText') +
          ' ' + errorMessage);
  };

  // Export
  return {
    NfcDebugUI: NfcDebugUI
  };
});

document.addEventListener('DOMContentLoaded', nfcDebug.NfcDebugUI.initialize);