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