- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / sync_internals / notifications.js
1 // Copyright (c) 2012 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 (function() {
6
7 // TODO(akalin): Use table.js.
8
9 function updateNotificationStateInfo(notificationState) {
10   var notificationStateInfo = $('notificationStateInfo');
11   jstProcess(
12       new JsEvalContext({ 'notificationState': notificationState }),
13       notificationStateInfo);
14 }
15
16 // Contains all notification data.  The keys are sync types (as strings) and
17 // the value is a dictionary with:
18 //
19 //   type: the sync type again (for convenience when using JsTemplate)
20 //   totalCount: Number of notifications received since browser start.
21 //   sessionCount: Number of notifications received this
22 //                 chrome://sync-internals session.
23 //   payload: The last received payload.
24 //
25 chrome.sync.notifications = {};
26
27 /**
28  * Merges d1 and d2 (with d2 taking precedence) and returns the result.
29  */
30 function mergeDictionaries(d1, d2) {
31   var d = {};
32   for (var k in d1) {
33     d[k] = d1[k];
34   }
35   for (var k in d2) {
36     d[k] = d2[k];
37   }
38   return d;
39 }
40
41 /**
42  * Merge notificationInfo into chrome.sync.notifications.
43  */
44 function updateNotificationsFromNotificationInfo(notificationInfo) {
45   for (var k in notificationInfo) {
46     chrome.sync.notifications[k] =
47         mergeDictionaries(chrome.sync.notifications[k] || {},
48                           notificationInfo[k]);
49     // notificationInfo[k] has values for the totalCount and payload keys,
50     // so fill in the rest (if necessary).
51     chrome.sync.notifications[k].type = k;
52     chrome.sync.notifications[k].sessionCount =
53         chrome.sync.notifications[k].sessionCount || 0;
54   }
55 }
56
57 function incrementSessionNotificationCount(changedType) {
58   chrome.sync.notifications[changedType].sessionCount =
59       chrome.sync.notifications[changedType].sessionCount || 0;
60   ++chrome.sync.notifications[changedType].sessionCount;
61 }
62
63 function updateNotificationInfoTable() {
64   var notificationInfoTable = $('notificationInfo');
65   var infos = [];
66   for (var k in chrome.sync.notifications) {
67     infos.push(chrome.sync.notifications[k]);
68   }
69   jstProcess(new JsEvalContext({ 'notifications': infos }),
70              notificationInfoTable);
71 }
72
73 function updateNotificationInfo(notificationInfo) {
74   updateNotificationsFromNotificationInfo(notificationInfo);
75   updateNotificationInfoTable();
76 }
77
78 function onLoad() {
79   chrome.sync.getNotificationState(updateNotificationStateInfo);
80   chrome.sync.getNotificationInfo(updateNotificationInfo);
81   chrome.sync.onNotificationStateChange.addListener(
82       function(details) { updateNotificationStateInfo(details.state); });
83
84   chrome.sync.onIncomingNotification.addListener(function(details) {
85     var changedTypes = details.changedTypes;
86     for (var i = 0; i < changedTypes.length; ++i) {
87       incrementSessionNotificationCount(changedTypes[i]);
88     }
89     updateNotificationInfoTable();
90
91     // Also update total counts.
92     chrome.sync.getNotificationInfo(updateNotificationInfo);
93   });
94 }
95
96 document.addEventListener('DOMContentLoaded', onLoad, false);
97 })();