OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 /** | 7 /** |
8 * @fileoverview The event page for Google Now for Chrome implementation. | 8 * @fileoverview The event page for Google Now for Chrome implementation. |
9 * The Google Now event page gets Google Now cards from the server and shows | 9 * The Google Now event page gets Google Now cards from the server and shows |
10 * them as Chrome notifications. | 10 * them as Chrome notifications. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 */ | 45 */ |
46 var MINIMUM_POLLING_PERIOD_SECONDS = 5 * 60; // 5 minutes | 46 var MINIMUM_POLLING_PERIOD_SECONDS = 5 * 60; // 5 minutes |
47 | 47 |
48 /** | 48 /** |
49 * Maximal period for polling for Google Now Notifications cards to use when the | 49 * Maximal period for polling for Google Now Notifications cards to use when the |
50 * period from the server is not available. | 50 * period from the server is not available. |
51 */ | 51 */ |
52 var MAXIMUM_POLLING_PERIOD_SECONDS = 60 * 60; // 1 hour | 52 var MAXIMUM_POLLING_PERIOD_SECONDS = 60 * 60; // 1 hour |
53 | 53 |
54 /** | 54 /** |
| 55 * Initial period for polling for Google Now optin notification after push |
| 56 * messaging indicates Google Now is enabled. |
| 57 */ |
| 58 var INITIAL_OPTIN_POLLING_PERIOD_SECONDS = 60; // 1 minute |
| 59 |
| 60 /** |
| 61 * Maximum period for polling for Google Now optin notification after push |
| 62 * messaging indicates Google Now is enabled. It is expected that the alarm |
| 63 * will be stopped after this. |
| 64 */ |
| 65 var MAXIMUM_OPTIN_POLLING_PERIOD_SECONDS = 16 * 60; // 16 minutes |
| 66 |
| 67 /** |
55 * Initial period for retrying the server request for dismissing cards. | 68 * Initial period for retrying the server request for dismissing cards. |
56 */ | 69 */ |
57 var INITIAL_RETRY_DISMISS_PERIOD_SECONDS = 60; // 1 minute | 70 var INITIAL_RETRY_DISMISS_PERIOD_SECONDS = 60; // 1 minute |
58 | 71 |
59 /** | 72 /** |
60 * Maximum period for retrying the server request for dismissing cards. | 73 * Maximum period for retrying the server request for dismissing cards. |
61 */ | 74 */ |
62 var MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS = 60 * 60; // 1 hour | 75 var MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS = 60 * 60; // 1 hour |
63 | 76 |
64 /** | 77 /** |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); | 208 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); |
196 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 209 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
197 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 210 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
198 wrapper.instrumentChromeApiFunction('tabs.create', 1); | 211 wrapper.instrumentChromeApiFunction('tabs.create', 1); |
199 | 212 |
200 var updateCardsAttempts = buildAttemptManager( | 213 var updateCardsAttempts = buildAttemptManager( |
201 'cards-update', | 214 'cards-update', |
202 requestCards, | 215 requestCards, |
203 INITIAL_POLLING_PERIOD_SECONDS, | 216 INITIAL_POLLING_PERIOD_SECONDS, |
204 MAXIMUM_POLLING_PERIOD_SECONDS); | 217 MAXIMUM_POLLING_PERIOD_SECONDS); |
| 218 var optInCheckAttempts = buildAttemptManager( |
| 219 'optin', |
| 220 pollOptedIn, |
| 221 INITIAL_OPTIN_POLLING_PERIOD_SECONDS, |
| 222 MAXIMUM_OPTIN_POLLING_PERIOD_SECONDS); |
205 var dismissalAttempts = buildAttemptManager( | 223 var dismissalAttempts = buildAttemptManager( |
206 'dismiss', | 224 'dismiss', |
207 retryPendingDismissals, | 225 retryPendingDismissals, |
208 INITIAL_RETRY_DISMISS_PERIOD_SECONDS, | 226 INITIAL_RETRY_DISMISS_PERIOD_SECONDS, |
209 MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS); | 227 MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS); |
210 var cardSet = buildCardSet(); | 228 var cardSet = buildCardSet(); |
211 | 229 |
212 var authenticationManager = buildAuthenticationManager(); | 230 var authenticationManager = buildAuthenticationManager(); |
213 | 231 |
214 /** | 232 /** |
(...skipping 874 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 * @return {Promise} A promise to determine the previous Google Now | 1107 * @return {Promise} A promise to determine the previous Google Now |
1090 * opt-in state. | 1108 * opt-in state. |
1091 */ | 1109 */ |
1092 function isGoogleNowEnabled() { | 1110 function isGoogleNowEnabled() { |
1093 return fillFromChromeLocalStorage({googleNowEnabled: false}) | 1111 return fillFromChromeLocalStorage({googleNowEnabled: false}) |
1094 .then(function(items) { | 1112 .then(function(items) { |
1095 return items.googleNowEnabled; | 1113 return items.googleNowEnabled; |
1096 }); | 1114 }); |
1097 } | 1115 } |
1098 | 1116 |
| 1117 /** |
| 1118 * Polls the optin state. |
| 1119 * Sometimes we get the response to the opted in result too soon during |
| 1120 * push messaging. We'll recheck the optin state a few times before giving up. |
| 1121 */ |
| 1122 function pollOptedIn() { |
| 1123 /** |
| 1124 * Cleans up any state used to recheck the opt-in poll. |
| 1125 */ |
| 1126 function clearPollingState() { |
| 1127 localStorage.removeItem('optedInCheckCount'); |
| 1128 optInCheckAttempts.stop(); |
| 1129 } |
| 1130 |
| 1131 /** |
| 1132 * Performs the actual work for checking the opt-in state and requesting cards |
| 1133 * on opted-in. |
| 1134 */ |
| 1135 function checkOptedIn() { |
| 1136 // Limit retries to 5. |
| 1137 if (localStorage.optedInCheckCount < 5) { |
| 1138 console.log(new Date() + |
| 1139 ' checkOptedIn Attempt ' + localStorage.optedInCheckCount); |
| 1140 localStorage.optedInCheckCount++; |
| 1141 requestOptedIn(function() { |
| 1142 clearPollingState(); |
| 1143 requestCards(); |
| 1144 }); |
| 1145 } else { |
| 1146 clearPollingState(); |
| 1147 } |
| 1148 } |
| 1149 |
| 1150 if (localStorage.optedInCheckCount === undefined) { |
| 1151 localStorage.optedInCheckCount = 0; |
| 1152 optInCheckAttempts.start(); |
| 1153 checkOptedIn(); |
| 1154 } else { |
| 1155 optInCheckAttempts.planForNext(checkOptedIn); |
| 1156 } |
| 1157 } |
| 1158 |
1099 instrumented.runtime.onInstalled.addListener(function(details) { | 1159 instrumented.runtime.onInstalled.addListener(function(details) { |
1100 console.log('onInstalled ' + JSON.stringify(details)); | 1160 console.log('onInstalled ' + JSON.stringify(details)); |
1101 if (details.reason != 'chrome_update') { | 1161 if (details.reason != 'chrome_update') { |
1102 initialize(); | 1162 initialize(); |
1103 } | 1163 } |
1104 }); | 1164 }); |
1105 | 1165 |
1106 instrumented.runtime.onStartup.addListener(function() { | 1166 instrumented.runtime.onStartup.addListener(function() { |
1107 console.log('onStartup'); | 1167 console.log('onStartup'); |
1108 | 1168 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1195 items.notificationGroups['PUSH' + message.subchannelId] = { | 1255 items.notificationGroups['PUSH' + message.subchannelId] = { |
1196 cards: [], | 1256 cards: [], |
1197 nextPollTime: Date.now() | 1257 nextPollTime: Date.now() |
1198 }; | 1258 }; |
1199 | 1259 |
1200 chrome.storage.local.set({ | 1260 chrome.storage.local.set({ |
1201 lastPollNowPayloads: items.lastPollNowPayloads, | 1261 lastPollNowPayloads: items.lastPollNowPayloads, |
1202 notificationGroups: items.notificationGroups | 1262 notificationGroups: items.notificationGroups |
1203 }); | 1263 }); |
1204 | 1264 |
1205 requestCards(); | 1265 pollOptedIn(); |
1206 } | 1266 } |
1207 }); | 1267 }); |
1208 }); | 1268 }); |
1209 } | 1269 } |
1210 }); | 1270 }); |
OLD | NEW |