Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/browser/resources/google_now/cards.js

Issue 24924002: Switching getting/dismissing cards to new protocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: robliao comments. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 var MS_IN_SECOND = 1000; 7 /**
8 * Show/hide trigger in a card.
9 *
10 * @typedef {{
11 * showTime: number=,
12 * hideTime: number=
13 * }}
14 */
15 var Trigger;
16
17 /**
18 * Data to build a dismissal request for a card from a specific group.
19 *
20 * @typedef {{
21 * notificationId: string,
22 * parameters: Object
23 * }}
24 */
25 var DismissalData;
26
27 /**
28 * Card merged from potentially multiple groups.
29 *
30 * @typedef {{
31 * trigger: Trigger,
32 * version: number,
33 * timestamp: number,
34 * notification: Object,
35 * actionUrls: Object,
36 * groupRank: number,
37 * dismissals: Array.<DismissalData>
38 * }}
39 */
40 var MergedCard;
41
42 /**
43 * Set of parameters for creating card notification.
44 *
45 * @typedef {{
46 * notification: Object,
47 * hideTime: number=,
48 * version: number,
49 * previousVersion: number=
50 * }}
51 */
52 var CardCreateInfo;
8 53
9 /** 54 /**
10 * Builds an object to manage notification card set. 55 * Builds an object to manage notification card set.
11 * @return {Object} Card set interface. 56 * @return {Object} Card set interface.
12 */ 57 */
13 function buildCardSet() { 58 function buildCardSet() {
14 var cardShowPrefix = 'card-show-'; 59 var cardShowPrefix = 'card-show-';
15 var cardHidePrefix = 'card-hide-'; 60 var cardHidePrefix = 'card-hide-';
16 61
17 /** 62 /**
18 * Schedules hiding a notification. 63 * Schedules hiding a notification.
19 * @param {string} cardId Card ID. 64 * @param {string} cardId Card ID.
20 * @param {number=} opt_timeHide If specified, epoch time to hide the card. If 65 * @param {number=} opt_timeHide If specified, epoch time to hide the card. If
21 * undefined, the card will be kept shown at least until next update. 66 * undefined, the card will be kept shown at least until next update.
22 */ 67 */
23 function scheduleHiding(cardId, opt_timeHide) { 68 function scheduleHiding(cardId, opt_timeHide) {
24 if (opt_timeHide === undefined) 69 if (opt_timeHide === undefined)
25 return; 70 return;
26 71
27 var alarmName = cardHidePrefix + cardId; 72 var alarmName = cardHidePrefix + cardId;
28 var alarmInfo = {when: opt_timeHide}; 73 var alarmInfo = {when: opt_timeHide};
29 chrome.alarms.create(alarmName, alarmInfo); 74 chrome.alarms.create(alarmName, alarmInfo);
30 } 75 }
31 76
32 /** 77 /**
33 * Shows a notification. 78 * Shows a notification.
34 * @param {string} cardId Card ID. 79 * @param {string} cardId Card ID.
35 * @param {Object} cardCreateInfo Google Now card represented as a set of 80 * @param {CardCreateInfo} cardCreateInfo Google Now card represented as a set
36 * parameters for showing a Chrome notification. 81 * of parameters for showing a Chrome notification.
37 */ 82 */
38 function showNotification(cardId, cardCreateInfo) { 83 function showNotification(cardId, cardCreateInfo) {
39 console.log('cardManager.showNotification ' + cardId + ' ' + 84 console.log('cardManager.showNotification ' + cardId + ' ' +
40 JSON.stringify(cardCreateInfo)); 85 JSON.stringify(cardCreateInfo));
41 86
42 if (cardCreateInfo.previousVersion !== cardCreateInfo.version) { 87 if (cardCreateInfo.previousVersion !== cardCreateInfo.version) {
43 // Delete a notification with the specified id if it already exists, and 88 // Delete a notification with the specified id if it already exists, and
44 // then create a notification. 89 // then create a notification.
45 instrumented.notifications.create( 90 instrumented.notifications.create(
46 cardId, 91 cardId,
47 cardCreateInfo.notification, 92 cardCreateInfo.notification,
48 function(newNotificationId) { 93 function(newNotificationId) {
49 if (!newNotificationId || chrome.runtime.lastError) { 94 if (!newNotificationId || chrome.runtime.lastError) {
50 var errorMessage = chrome.runtime.lastError && 95 var errorMessage = chrome.runtime.lastError &&
51 chrome.runtime.lastError.message; 96 chrome.runtime.lastError.message;
52 console.error('notifications.create: ID=' + newNotificationId + 97 console.error('notifications.create: ID=' + newNotificationId +
53 ', ERROR=' + errorMessage); 98 ', ERROR=' + errorMessage);
54 return; 99 return;
55 } 100 }
56 101
57 scheduleHiding(cardId, cardCreateInfo.timeHide); 102 scheduleHiding(cardId, cardCreateInfo.hideTime);
58 }); 103 });
59 } else { 104 } else {
60 // Update existing notification. 105 // Update existing notification.
61 instrumented.notifications.update( 106 instrumented.notifications.update(
62 cardId, 107 cardId,
63 cardCreateInfo.notification, 108 cardCreateInfo.notification,
64 function(wasUpdated) { 109 function(wasUpdated) {
65 if (!wasUpdated || chrome.runtime.lastError) { 110 if (!wasUpdated || chrome.runtime.lastError) {
66 var errorMessage = chrome.runtime.lastError && 111 var errorMessage = chrome.runtime.lastError &&
67 chrome.runtime.lastError.message; 112 chrome.runtime.lastError.message;
68 console.error('notifications.update: UPDATED=' + wasUpdated + 113 console.error('notifications.update: UPDATED=' + wasUpdated +
69 ', ERROR=' + errorMessage); 114 ', ERROR=' + errorMessage);
70 return; 115 return;
71 } 116 }
72 117
73 scheduleHiding(cardId, cardCreateInfo.timeHide); 118 scheduleHiding(cardId, cardCreateInfo.hideTime);
74 }); 119 });
75 } 120 }
76 } 121 }
77 122
78 /** 123 /**
79 * Updates/creates a card notification with new data. 124 * Updates/creates a card notification with new data.
80 * @param {Object} card Google Now from the server. 125 * @param {string} cardId Card ID.
126 * @param {MergedCard} card Google Now from the server.
rgustafson 2013/09/30 21:26:09 Google Now card?
vadimt 2013/09/30 22:10:39 Done.
81 * @param {number=} previousVersion The version of the shown card with 127 * @param {number=} previousVersion The version of the shown card with
82 * this id, if it exists, undefined otherwise. 128 * this id, if it exists, undefined otherwise.
83 * @return {Object} Notification data entry for this card. 129 * @return {Object} Notification data entry for this card.
84 */ 130 */
85 function update(card, previousVersion) { 131 function update(cardId, card, previousVersion) {
86 console.log('cardManager.update ' + JSON.stringify(card) + ' ' + 132 console.log('cardManager.update ' + JSON.stringify(card) + ' ' +
87 previousVersion); 133 previousVersion);
88 134
89 if (typeof card.version != 'number') {
90 console.log('cardCreateInfo.version is not a number');
91 // Fix card version.
92 card.version = previousVersion || 0;
93 }
94
95 // TODO(vadimt): Don't clear alarms etc that don't exist. Or make sure doing 135 // TODO(vadimt): Don't clear alarms etc that don't exist. Or make sure doing
96 // this doesn't output an error to console. 136 // this doesn't output an error to console.
97 chrome.alarms.clear(cardHidePrefix + card.notificationId); 137 chrome.alarms.clear(cardHidePrefix + cardId);
98 138
99 var timeHide = card.trigger && card.trigger.hideTimeSec !== undefined ?
100 Date.now() + card.trigger.hideTimeSec * MS_IN_SECOND : undefined;
101 var cardCreateInfo = { 139 var cardCreateInfo = {
102 notification: card.notification, 140 notification: card.notification,
103 timeHide: timeHide, 141 hideTime: card.trigger.hideTime,
104 version: card.version, 142 version: card.version,
105 previousVersion: previousVersion 143 previousVersion: previousVersion
106 }; 144 };
107 145
108 var cardShowAlarmName = cardShowPrefix + card.notificationId; 146 var cardShowAlarmName = cardShowPrefix + cardId;
109 if (card.trigger && card.trigger.showTimeSec) { 147 if (card.trigger.showTime && card.trigger.showTime > Date.now()) {
110 // Card needs to be shown later. 148 // Card needs to be shown later.
111 console.log('cardManager.register: postponed'); 149 console.log('cardManager.update: postponed');
112 var alarmInfo = { 150 var alarmInfo = {
113 when: Date.now() + card.trigger.showTimeSec * MS_IN_SECOND 151 when: card.trigger.showTime
114 }; 152 };
115 chrome.alarms.create(cardShowAlarmName, alarmInfo); 153 chrome.alarms.create(cardShowAlarmName, alarmInfo);
116 } else { 154 } else {
117 // Card needs to be shown immediately. 155 // Card needs to be shown immediately.
118 console.log('cardManager.register: immediate'); 156 console.log('cardManager.update: immediate');
119 chrome.alarms.clear(cardShowAlarmName); 157 chrome.alarms.clear(cardShowAlarmName);
120 showNotification(card.notificationId, cardCreateInfo); 158 showNotification(cardId, cardCreateInfo);
121 } 159 }
122 160
123 return { 161 return {
124 actionUrls: card.actionUrls, 162 actionUrls: card.actionUrls,
125 cardCreateInfo: cardCreateInfo, 163 cardCreateInfo: cardCreateInfo,
126 dismissalParameters: card.dismissal 164 dismissals: card.dismissals
127 }; 165 };
128 } 166 }
129 167
130 /** 168 /**
131 * Removes a card notification. 169 * Removes a card notification.
132 * @param {string} cardId Card ID. 170 * @param {string} cardId Card ID.
133 */ 171 */
134 function clear(cardId) { 172 function clear(cardId) {
135 console.log('cardManager.unregister ' + cardId); 173 console.log('cardManager.clear ' + cardId);
136 174
137 chrome.notifications.clear(cardId, function() {}); 175 chrome.notifications.clear(cardId, function() {});
138 chrome.alarms.clear(cardShowPrefix + cardId); 176 chrome.alarms.clear(cardShowPrefix + cardId);
139 chrome.alarms.clear(cardHidePrefix + cardId); 177 chrome.alarms.clear(cardHidePrefix + cardId);
140 } 178 }
141 179
142 instrumented.alarms.onAlarm.addListener(function(alarm) { 180 instrumented.alarms.onAlarm.addListener(function(alarm) {
143 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); 181 console.log('cardManager.onAlarm ' + JSON.stringify(alarm));
144 182
145 if (alarm.name.indexOf(cardShowPrefix) == 0) { 183 if (alarm.name.indexOf(cardShowPrefix) == 0) {
146 // Alarm to show the card. 184 // Alarm to show the card.
147 var cardId = alarm.name.substring(cardShowPrefix.length); 185 var cardId = alarm.name.substring(cardShowPrefix.length);
148 instrumented.storage.local.get('notificationsData', function(items) { 186 instrumented.storage.local.get('notificationsData', function(items) {
149 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); 187 console.log('cardManager.onAlarm.get ' + JSON.stringify(items));
150 if (!items || !items.notificationsData) 188 if (!items || !items.notificationsData)
151 return; 189 return;
152 var notificationData = items.notificationsData[cardId]; 190 var notificationData = items.notificationsData[cardId];
153 if (!notificationData) 191 if (!notificationData)
154 return; 192 return;
155 193
156 showNotification(cardId, notificationData.cardCreateInfo); 194 showNotification(cardId, notificationData.cardCreateInfo);
157 }); 195 });
158 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { 196 } else if (alarm.name.indexOf(cardHidePrefix) == 0) {
159 // Alarm to hide the card. 197 // Alarm to hide the card.
160 var cardId = alarm.name.substring(cardHidePrefix.length); 198 var cardId = alarm.name.substring(cardHidePrefix.length);
161 chrome.notifications.clear(cardId, function() {}); 199 clear(cardId);
162 } 200 }
163 }); 201 });
164 202
165 return { 203 return {
166 update: update, 204 update: update,
167 clear: clear 205 clear: clear
168 }; 206 };
169 } 207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698