OLD | NEW |
1 <script> | 1 <script> |
2 (function () { | 2 (function () { |
3 // Contains all notifications received since the page was loaded as a | 3 |
4 // map from each data type to the number of notifications received for | 4 // TODO(akalin): Use table.js. |
5 // it. | |
6 chrome.sync.notifications = {}; | |
7 | 5 |
8 function updateNotificationsEnabledInfo(notificationsEnabled) { | 6 function updateNotificationsEnabledInfo(notificationsEnabled) { |
9 var notificationsEnabledInfo = | 7 var notificationsEnabledInfo = |
10 document.getElementById('notificationsEnabledInfo'); | 8 document.getElementById('notificationsEnabledInfo'); |
11 jstProcess( | 9 jstProcess( |
12 new JsEvalContext({ 'notificationsEnabled': notificationsEnabled }), | 10 new JsEvalContext({ 'notificationsEnabled': notificationsEnabled }), |
13 notificationsEnabledInfo); | 11 notificationsEnabledInfo); |
14 } | 12 } |
15 | 13 |
16 function updateNotificationInfo(notification) { | 14 // Contains all notification data. The keys are sync types (as strings) and |
17 var notificationInfo = document.getElementById('notificationInfo'); | 15 // the value is a dictionary with: |
18 jstProcess( | 16 // |
19 new JsEvalContext({ | 17 // type: the sync type again (for convenience when using JsTemplate) |
20 'notificationInfo': JSON.stringify(notification, null, 2) | 18 // totalCount: Number of notifications received since browser start. |
21 }), | 19 // sessionCount: Number of notifications received this |
22 notificationInfo); | 20 // chrome://sync-internals session. |
| 21 // payload: The last received payload. |
| 22 // |
| 23 chrome.sync.notifications = {}; |
| 24 |
| 25 /** |
| 26 * Merges d1 and d2 (with d2 taking precedence) and returns the result. |
| 27 */ |
| 28 function mergeDictionaries(d1, d2) { |
| 29 var d = {}; |
| 30 for (var k in d1) { |
| 31 d[k] = d1[k]; |
| 32 } |
| 33 for (var k in d2) { |
| 34 d[k] = d2[k]; |
| 35 } |
| 36 return d; |
| 37 } |
| 38 |
| 39 /** |
| 40 * Merge notificationInfo into chrome.sync.notifications. |
| 41 */ |
| 42 function updateNotificationsFromNotificationInfo(notificationInfo) { |
| 43 for (var k in notificationInfo) { |
| 44 chrome.sync.notifications[k] = |
| 45 mergeDictionaries(chrome.sync.notifications[k] || {}, |
| 46 notificationInfo[k]); |
| 47 // notificationInfo[k] has values for the totalCount and payload keys, |
| 48 // so fill in the rest (if necessary). |
| 49 chrome.sync.notifications[k].type = k; |
| 50 chrome.sync.notifications[k].sessionCount = |
| 51 chrome.sync.notifications[k].sessionCount || 0; |
| 52 } |
| 53 } |
| 54 |
| 55 function incrementSessionNotificationCount(changedType) { |
| 56 chrome.sync.notifications[changedType].sessionCount = |
| 57 chrome.sync.notifications[changedType].sessionCount || 0; |
| 58 ++chrome.sync.notifications[changedType].sessionCount; |
| 59 } |
| 60 |
| 61 function updateNotificationInfoTable() { |
| 62 var notificationInfoTable = |
| 63 document.getElementById('notificationInfo'); |
| 64 var infos = []; |
| 65 for (var k in chrome.sync.notifications) { |
| 66 infos.push(chrome.sync.notifications[k]); |
| 67 } |
| 68 jstProcess(new JsEvalContext({ 'notifications': infos }), |
| 69 notificationInfoTable); |
| 70 } |
| 71 |
| 72 function updateNotificationInfo(notificationInfo) { |
| 73 updateNotificationsFromNotificationInfo(notificationInfo); |
| 74 updateNotificationInfoTable(); |
23 } | 75 } |
24 | 76 |
25 function onLoad() { | 77 function onLoad() { |
26 chrome.sync.getNotificationState(updateNotificationsEnabledInfo); | 78 chrome.sync.getNotificationState(updateNotificationsEnabledInfo); |
27 chrome.sync.getNotificationInfo(updateNotificationInfo); | 79 chrome.sync.getNotificationInfo(updateNotificationInfo); |
28 chrome.sync.onNotificationStateChange.addListener( | 80 chrome.sync.onNotificationStateChange.addListener( |
29 function(details) { updateNotificationsEnabledInfo(details.enabled); }); | 81 function(details) { updateNotificationsEnabledInfo(details.enabled); }); |
30 | 82 |
31 chrome.sync.onIncomingNotification.addListener(function(details) { | 83 chrome.sync.onIncomingNotification.addListener(function(details) { |
32 var changedTypes = details.changedTypes; | 84 var changedTypes = details.changedTypes; |
33 for (var i = 0; i < changedTypes.length; ++i) { | 85 for (var i = 0; i < changedTypes.length; ++i) { |
34 var changedType = changedTypes[i]; | 86 incrementSessionNotificationCount(changedTypes[i]); |
35 chrome.sync.notifications[changedType] = | |
36 chrome.sync.notifications[changedType] || 0; | |
37 ++chrome.sync.notifications[changedType]; | |
38 } | 87 } |
| 88 updateNotificationInfoTable(); |
39 | 89 |
40 var infos = []; | 90 // Also update total counts. |
41 for (var k in chrome.sync.notifications) { | |
42 var info = { | |
43 'modelType': k, | |
44 'notificationCount': chrome.sync.notifications[k] | |
45 }; | |
46 infos.push(info); | |
47 } | |
48 | |
49 var notificationsInfo = | |
50 document.getElementById('notificationsInfo'); | |
51 jstProcess(new JsEvalContext({ 'notifications': infos }), | |
52 notificationsInfo); | |
53 chrome.sync.getNotificationInfo(updateNotificationInfo); | 91 chrome.sync.getNotificationInfo(updateNotificationInfo); |
54 }); | 92 }); |
55 } | 93 } |
56 | 94 |
57 document.addEventListener("DOMContentLoaded", onLoad, false); | 95 document.addEventListener("DOMContentLoaded", onLoad, false); |
58 })(); | 96 })(); |
59 </script> | 97 </script> |
60 | 98 |
61 <style> | 99 <style> |
62 table#notificationsInfo tr:nth-child(odd) { | 100 table#notificationInfo tr:nth-child(odd) { |
63 background: #eff3ff; | 101 background: #eff3ff; |
64 } | 102 } |
65 </style> | 103 </style> |
66 | 104 |
67 <p id='notificationsEnabledInfo'> | 105 <p id='notificationsEnabledInfo'> |
68 Enabled: <span jscontent='notificationsEnabled'></span> | 106 Enabled: <span jscontent='notificationsEnabled'></span> |
69 </p> | 107 </p> |
70 <pre id='notificationInfo'><span jscontent='notificationInfo'></span></pre> | 108 <table id='notificationInfo'> |
71 <table id='notificationsInfo'> | 109 <tr> |
| 110 <th>Type</th> |
| 111 <th>Total count</th> |
| 112 <th>Session count</th> |
| 113 <th>Payload</th> |
| 114 </tr> |
72 <tr jsselect='notifications'> | 115 <tr jsselect='notifications'> |
73 <td jscontent='modelType'/> | 116 <td jscontent='type'/> |
74 <td jscontent='notificationCount'/> | 117 <td jscontent='totalCount'/> |
| 118 <td jscontent='sessionCount'/> |
| 119 <td jscontent='payload'/> |
75 </tr> | 120 </tr> |
76 </table> | 121 </table> |
OLD | NEW |