1// Copyright 2014 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
5cr.define('gcmInternals', function() {
6  'use strict';
7
8  var isRecording = false;
9
10  /**
11   * If the info dictionary has property prop, then set the text content of
12   * element to the value of this property. Otherwise clear the content.
13   * @param {!Object} info A dictionary of device infos to be displayed.
14   * @param {string} prop Name of the property.
15   * @param {string} element The id of a HTML element.
16   */
17  function setIfExists(info, prop, element) {
18    if (info[prop] !== undefined) {
19      $(element).textContent = info[prop];
20    } else {
21      $(element).textContent = '';
22    }
23  }
24
25  /**
26   * Display device informations.
27   * @param {!Object} info A dictionary of device infos to be displayed.
28   */
29  function displayDeviceInfo(info) {
30    setIfExists(info, 'androidId', 'android-id');
31    setIfExists(info, 'profileServiceCreated', 'profile-service-created');
32    setIfExists(info, 'gcmEnabled', 'gcm-enabled');
33    setIfExists(info, 'signedInUserName', 'signed-in-username');
34    setIfExists(info, 'gcmClientCreated', 'gcm-client-created');
35    setIfExists(info, 'gcmClientState', 'gcm-client-state');
36    setIfExists(info, 'connectionClientCreated', 'connection-client-created');
37    setIfExists(info, 'connectionState', 'connection-state');
38    setIfExists(info, 'registeredAppIds', 'registered-app-ids');
39    setIfExists(info, 'sendQueueSize', 'send-queue-size');
40    setIfExists(info, 'resendQueueSize', 'resend-queue-size');
41  }
42
43  /**
44   * Remove all the child nodes of the element.
45   * @param {HTMLElement} element A HTML element.
46   */
47  function removeAllChildNodes(element) {
48    element.textContent = '';
49  }
50
51  /**
52   * For each item in line, add a row to the table. Each item is actually a list
53   * of sub-items; each of which will have a corresponding cell created in that
54   * row, and the sub-item will be displayed in the cell.
55   * @param {HTMLElement} table A HTML tbody element.
56   * @param {!Object} list A list of list of item.
57   */
58  function addRows(table, list) {
59    for (var i = 0; i < list.length; ++i) {
60      var row = document.createElement('tr');
61
62      // The first element is always a timestamp.
63      var cell = document.createElement('td');
64      var d = new Date(list[i][0]);
65      cell.textContent = d;
66      row.appendChild(cell);
67
68      for (var j = 1; j < list[i].length; ++j) {
69        var cell = document.createElement('td');
70        cell.textContent = list[i][j];
71        row.appendChild(cell);
72      }
73      table.appendChild(row);
74    }
75  }
76
77  /**
78   * Refresh all displayed information.
79   */
80  function refreshAll() {
81    chrome.send('getGcmInternalsInfo', [false]);
82  }
83
84  /**
85   * Toggle the isRecording variable and send it to browser.
86   */
87  function setRecording() {
88    isRecording = !isRecording;
89    chrome.send('setGcmInternalsRecording', [isRecording]);
90  }
91
92  /**
93   * Clear all the activity logs.
94   */
95  function clearLogs() {
96    chrome.send('getGcmInternalsInfo', [true]);
97  }
98
99  function initialize() {
100    $('recording').disabled = true;
101    $('refresh').onclick = refreshAll;
102    $('recording').onclick = setRecording;
103    $('clear-logs').onclick = clearLogs;
104    chrome.send('getGcmInternalsInfo', [false]);
105  }
106
107  /**
108   * Refresh the log html table by clearing it first. If data is not empty, then
109   * it will be used to populate the table.
110   * @param {string} id ID of the log html table.
111   * @param {!Object} data A list of list of data items.
112   */
113  function refreshLogTable(id, data) {
114    removeAllChildNodes($(id));
115    if (data !== undefined) {
116      addRows($(id), data);
117    }
118  }
119
120  /**
121   * Callback function accepting a dictionary of info items to be displayed.
122   * @param {!Object} infos A dictionary of info items to be displayed.
123   */
124  function setGcmInternalsInfo(infos) {
125    isRecording = infos.isRecording;
126    if (isRecording)
127      $('recording').textContent = 'Stop Recording';
128    else
129      $('recording').textContent = 'Start Recording';
130    $('recording').disabled = false;
131    if (infos.deviceInfo !== undefined) {
132      displayDeviceInfo(infos.deviceInfo);
133    }
134
135    refreshLogTable('checkin-info', infos.checkinInfo);
136    refreshLogTable('connection-info', infos.connectionInfo);
137    refreshLogTable('registration-info', infos.registrationInfo);
138    refreshLogTable('receive-info', infos.receiveInfo);
139    refreshLogTable('send-info', infos.sendInfo);
140  }
141
142  // Return an object with all of the exports.
143  return {
144    initialize: initialize,
145    setGcmInternalsInfo: setGcmInternalsInfo,
146  };
147});
148
149document.addEventListener('DOMContentLoaded', gcmInternals.initialize);
150