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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 // send dismissals is scheduled. | 182 // send dismissals is scheduled. |
183 return true; | 183 return true; |
184 } | 184 } |
185 | 185 |
186 return false; | 186 return false; |
187 } | 187 } |
188 | 188 |
189 var tasks = buildTaskManager(areTasksConflicting); | 189 var tasks = buildTaskManager(areTasksConflicting); |
190 | 190 |
191 // Add error processing to API calls. | 191 // Add error processing to API calls. |
192 wrapper.instrumentChromeApiFunction('gcm.onMessage.addListener', 0); | |
192 wrapper.instrumentChromeApiFunction('metricsPrivate.getVariationParams', 1); | 193 wrapper.instrumentChromeApiFunction('metricsPrivate.getVariationParams', 1); |
193 wrapper.instrumentChromeApiFunction('notifications.clear', 1); | 194 wrapper.instrumentChromeApiFunction('notifications.clear', 1); |
194 wrapper.instrumentChromeApiFunction('notifications.create', 2); | 195 wrapper.instrumentChromeApiFunction('notifications.create', 2); |
195 wrapper.instrumentChromeApiFunction('notifications.getPermissionLevel', 0); | 196 wrapper.instrumentChromeApiFunction('notifications.getPermissionLevel', 0); |
196 wrapper.instrumentChromeApiFunction('notifications.update', 2); | 197 wrapper.instrumentChromeApiFunction('notifications.update', 2); |
197 wrapper.instrumentChromeApiFunction('notifications.getAll', 0); | 198 wrapper.instrumentChromeApiFunction('notifications.getAll', 0); |
198 wrapper.instrumentChromeApiFunction( | 199 wrapper.instrumentChromeApiFunction( |
199 'notifications.onButtonClicked.addListener', 0); | 200 'notifications.onButtonClicked.addListener', 0); |
200 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); | 201 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); |
201 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); | 202 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); |
202 wrapper.instrumentChromeApiFunction( | 203 wrapper.instrumentChromeApiFunction( |
203 'notifications.onPermissionLevelChanged.addListener', 0); | 204 'notifications.onPermissionLevelChanged.addListener', 0); |
204 wrapper.instrumentChromeApiFunction( | 205 wrapper.instrumentChromeApiFunction( |
205 'notifications.onShowSettings.addListener', 0); | 206 'notifications.onShowSettings.addListener', 0); |
206 wrapper.instrumentChromeApiFunction('permissions.contains', 1); | 207 wrapper.instrumentChromeApiFunction('permissions.contains', 1); |
207 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); | |
208 wrapper.instrumentChromeApiFunction('storage.onChanged.addListener', 0); | |
209 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 208 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
210 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 209 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
210 wrapper.instrumentChromeApiFunction('storage.onChanged.addListener', 0); | |
211 wrapper.instrumentChromeApiFunction('tabs.create', 1); | 211 wrapper.instrumentChromeApiFunction('tabs.create', 1); |
212 | 212 |
213 var updateCardsAttempts = buildAttemptManager( | 213 var updateCardsAttempts = buildAttemptManager( |
214 'cards-update', | 214 'cards-update', |
215 requestCards, | 215 requestCards, |
216 INITIAL_POLLING_PERIOD_SECONDS, | 216 INITIAL_POLLING_PERIOD_SECONDS, |
217 MAXIMUM_POLLING_PERIOD_SECONDS); | 217 MAXIMUM_POLLING_PERIOD_SECONDS); |
218 var optInPollAttempts = buildAttemptManager( | 218 var optInPollAttempts = buildAttemptManager( |
219 'optin', | 219 'optin', |
220 pollOptedInNoImmediateRecheck, | 220 pollOptedInNoImmediateRecheck, |
(...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1008 // We don't clear localStorage since those values are still relevant | 1008 // We don't clear localStorage since those values are still relevant |
1009 // across Google Now start-stop events. | 1009 // across Google Now start-stop events. |
1010 chrome.storage.local.clear(); | 1010 chrome.storage.local.clear(); |
1011 } | 1011 } |
1012 | 1012 |
1013 /** | 1013 /** |
1014 * Initializes the event page on install or on browser startup. | 1014 * Initializes the event page on install or on browser startup. |
1015 */ | 1015 */ |
1016 function initialize() { | 1016 function initialize() { |
1017 recordEvent(GoogleNowEvent.EXTENSION_START); | 1017 recordEvent(GoogleNowEvent.EXTENSION_START); |
1018 registerForGcm(); | |
1018 onStateChange(); | 1019 onStateChange(); |
1019 } | 1020 } |
1020 | 1021 |
1021 /** | 1022 /** |
1022 * Starts or stops the main pipeline for polling cards. | 1023 * Starts or stops the main pipeline for polling cards. |
1023 * @param {boolean} shouldPollCardsRequest true to start and | 1024 * @param {boolean} shouldPollCardsRequest true to start and |
1024 * false to stop polling cards. | 1025 * false to stop polling cards. |
1025 */ | 1026 */ |
1026 function setShouldPollCards(shouldPollCardsRequest) { | 1027 function setShouldPollCards(shouldPollCardsRequest) { |
1027 updateCardsAttempts.isRunning(function(currentValue) { | 1028 updateCardsAttempts.isRunning(function(currentValue) { |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1327 | 1328 |
1328 // Handles state change notifications for the Google Now enabled bit. | 1329 // Handles state change notifications for the Google Now enabled bit. |
1329 instrumented.storage.onChanged.addListener(function(changes, areaName) { | 1330 instrumented.storage.onChanged.addListener(function(changes, areaName) { |
1330 if (areaName === 'local') { | 1331 if (areaName === 'local') { |
1331 if ('googleNowEnabled' in changes) { | 1332 if ('googleNowEnabled' in changes) { |
1332 onStateChange(); | 1333 onStateChange(); |
1333 } | 1334 } |
1334 } | 1335 } |
1335 }); | 1336 }); |
1336 | 1337 |
1337 instrumented.pushMessaging.onMessage.addListener(function(message) { | 1338 instrumented.gcm.onMessage.addListener(function(message) { |
1338 // message.payload will be '' when the extension first starts. | 1339 console.log('gcm.onMessage ' + JSON.stringify(message)); |
1339 // Each time after signing in, we'll get latest payload for all channels. | 1340 if (!message || !message.data) { |
1340 // So, we need to poll the server only when the payload is non-empty and has | 1341 return; |
1341 // changed. | 1342 } |
1342 console.log('pushMessaging.onMessage ' + JSON.stringify(message)); | 1343 |
1343 if (message.payload.indexOf('REQUEST_CARDS') == 0) { | 1344 var payload = message.data.payload; |
1345 var subchannel = message.data.subchannelId; | |
robliao
2015/02/18 00:04:25
So we still need subchannel ID? I thought subchann
skare_
2015/02/19 16:48:10
made this 'tag' - I also don't think we'll need it
| |
1346 if (payload.indexOf('REQUEST_CARDS') == 0) { | |
1344 tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { | 1347 tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { |
1345 // Accept promise rejection on failure since it's safer to do nothing, | 1348 // Accept promise rejection on failure since it's safer to do nothing, |
1346 // preventing polling the server when the payload really didn't change. | 1349 // preventing polling the server when the payload really didn't change. |
1347 fillFromChromeLocalStorage({ | 1350 fillFromChromeLocalStorage({ |
1348 lastPollNowPayloads: {}, | 1351 lastPollNowPayloads: {}, |
1349 /** @type {Object<string, StoredNotificationGroup>} */ | 1352 /** @type {Object<string, StoredNotificationGroup>} */ |
1350 notificationGroups: {} | 1353 notificationGroups: {} |
1351 }, PromiseRejection.ALLOW).then(function(items) { | 1354 }, PromiseRejection.ALLOW).then(function(items) { |
1352 if (items.lastPollNowPayloads[message.subchannelId] != | 1355 if (items.lastPollNowPayloads[message.subchannelId] != payload) { |
robliao
2015/02/18 00:04:25
message.subchannelId -> subchannelId
Two more inst
skare_
2015/02/19 16:48:10
Done.
| |
1353 message.payload) { | 1356 items.lastPollNowPayloads[message.subchannelId] = payload; |
1354 items.lastPollNowPayloads[message.subchannelId] = message.payload; | |
1355 | 1357 |
1356 items.notificationGroups['PUSH' + message.subchannelId] = { | 1358 items.notificationGroups['PUSH' + message.subchannelId] = { |
1357 cards: [], | 1359 cards: [], |
1358 nextPollTime: Date.now() | 1360 nextPollTime: Date.now() |
1359 }; | 1361 }; |
1360 | 1362 |
1361 chrome.storage.local.set({ | 1363 chrome.storage.local.set({ |
1362 lastPollNowPayloads: items.lastPollNowPayloads, | 1364 lastPollNowPayloads: items.lastPollNowPayloads, |
1363 notificationGroups: items.notificationGroups | 1365 notificationGroups: items.notificationGroups |
1364 }); | 1366 }); |
1365 | 1367 |
1366 pollOptedInWithRecheck(); | 1368 pollOptedInWithRecheck(); |
1367 } | 1369 } |
1368 }); | 1370 }); |
1369 }); | 1371 }); |
1370 } | 1372 } |
1371 }); | 1373 }); |
OLD | NEW |