Chromium Code Reviews| 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 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 */ | 478 */ |
| 479 function parseAndShowNotificationCards(response) { | 479 function parseAndShowNotificationCards(response) { |
| 480 console.log('parseAndShowNotificationCards ' + response); | 480 console.log('parseAndShowNotificationCards ' + response); |
| 481 var parsedResponse = JSON.parse(response); | 481 var parsedResponse = JSON.parse(response); |
| 482 | 482 |
| 483 if (parsedResponse.googleNowDisabled) { | 483 if (parsedResponse.googleNowDisabled) { |
| 484 chrome.storage.local.set({googleNowEnabled: false}); | 484 chrome.storage.local.set({googleNowEnabled: false}); |
| 485 // TODO(vadimt): Remove the line below once the server stops sending groups | 485 // TODO(vadimt): Remove the line below once the server stops sending groups |
| 486 // with 'googleNowDisabled' responses. | 486 // with 'googleNowDisabled' responses. |
| 487 parsedResponse.groups = {}; | 487 parsedResponse.groups = {}; |
| 488 onStateChange(); | |
|
robliao
2013/10/31 18:29:41
Comment that if we make it here, Google Now was pr
vadimt
2013/10/31 20:14:08
Done.
| |
| 488 } | 489 } |
| 489 | 490 |
| 490 var receivedGroups = parsedResponse.groups; | 491 var receivedGroups = parsedResponse.groups; |
| 491 | 492 |
| 492 // Populate groups with corresponding cards. | 493 // Populate groups with corresponding cards. |
| 493 if (parsedResponse.notifications) { | 494 if (parsedResponse.notifications) { |
| 494 for (var i = 0; i != parsedResponse.notifications.length; ++i) { | 495 for (var i = 0; i != parsedResponse.notifications.length; ++i) { |
| 495 var card = parsedResponse.notifications[i]; | 496 var card = parsedResponse.notifications[i]; |
| 496 var group = receivedGroups[card.groupName]; | 497 var group = receivedGroups[card.groupName]; |
| 497 group.cards = group.cards || []; | 498 group.cards = group.cards || []; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 597 var request = buildServerRequest('GET', 'settings/optin'); | 598 var request = buildServerRequest('GET', 'settings/optin'); |
| 598 | 599 |
| 599 request.onloadend = function(event) { | 600 request.onloadend = function(event) { |
| 600 console.log( | 601 console.log( |
| 601 'requestOptedIn-onloadend ' + request.status + ' ' + request.response); | 602 'requestOptedIn-onloadend ' + request.status + ' ' + request.response); |
| 602 if (request.status == HTTP_OK) { | 603 if (request.status == HTTP_OK) { |
| 603 var parsedResponse = JSON.parse(request.response); | 604 var parsedResponse = JSON.parse(request.response); |
| 604 if (parsedResponse.value) { | 605 if (parsedResponse.value) { |
| 605 chrome.storage.local.set({googleNowEnabled: true}); | 606 chrome.storage.local.set({googleNowEnabled: true}); |
| 606 optedInCallback(); | 607 optedInCallback(); |
| 608 onStateChange(); | |
|
robliao
2013/10/31 18:29:41
Same here. If we're here, Google Now was previousl
vadimt
2013/10/31 20:14:08
Done.
| |
| 607 } else { | 609 } else { |
| 608 scheduleNextPoll({}); | 610 scheduleNextPoll({}); |
| 609 } | 611 } |
| 610 } | 612 } |
| 611 }; | 613 }; |
| 612 | 614 |
| 613 setAuthorization(request, function(success) { | 615 setAuthorization(request, function(success) { |
| 614 if (success) | 616 if (success) |
| 615 request.send(); | 617 request.send(); |
| 616 }); | 618 }); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 918 updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); | 920 updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); |
| 919 | 921 |
| 920 requestLocation(); | 922 requestLocation(); |
| 921 } | 923 } |
| 922 | 924 |
| 923 /** | 925 /** |
| 924 * Stops all machinery in the polling system. | 926 * Stops all machinery in the polling system. |
| 925 */ | 927 */ |
| 926 function stopPollingCards() { | 928 function stopPollingCards() { |
| 927 stopRequestLocation(); | 929 stopRequestLocation(); |
| 928 | |
| 929 updateCardsAttempts.stop(); | 930 updateCardsAttempts.stop(); |
| 930 | |
| 931 removeAllCards(); | 931 removeAllCards(); |
| 932 chrome.storage.local.set({googleNowEnabled: false}); | |
|
robliao
2013/10/31 18:29:41
Semantically, this isn't necessarily correct if th
vadimt
2013/10/31 20:14:08
When the user checks us again in NC, we should sta
robliao
2013/10/31 23:05:01
This would be an excellent place for a comment.
On
vadimt
2013/11/01 00:49:47
Done.
| |
| 932 } | 933 } |
| 933 | 934 |
| 934 /** | 935 /** |
| 935 * Initializes the event page on install or on browser startup. | 936 * Initializes the event page on install or on browser startup. |
| 936 */ | 937 */ |
| 937 function initialize() { | 938 function initialize() { |
| 938 recordEvent(GoogleNowEvent.EXTENSION_START); | 939 recordEvent(GoogleNowEvent.EXTENSION_START); |
| 939 onStateChange(); | 940 onStateChange(); |
| 940 } | 941 } |
| 941 | 942 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 978 } | 979 } |
| 979 }); | 980 }); |
| 980 } | 981 } |
| 981 | 982 |
| 982 /** | 983 /** |
| 983 * Does the actual work of deciding what Google Now should do | 984 * Does the actual work of deciding what Google Now should do |
| 984 * based off of the current state of Chrome. | 985 * based off of the current state of Chrome. |
| 985 * @param {boolean} signedIn true if the user is signed in. | 986 * @param {boolean} signedIn true if the user is signed in. |
| 986 * @param {boolean} geolocationEnabled true if | 987 * @param {boolean} geolocationEnabled true if |
| 987 * the geolocation option is enabled. | 988 * the geolocation option is enabled. |
| 988 * @param {boolean} enableBackground true if | 989 * @param {boolean} canEnableBackground true if |
| 989 * the background permission should be requested. | 990 * the background permission can be requested. |
| 990 * @param {boolean} notificationEnabled true if | 991 * @param {boolean} notificationEnabled true if |
| 991 * Google Now for Chrome is allowed to show notifications. | 992 * Google Now for Chrome is allowed to show notifications. |
| 993 * @param {boolean} googleNowEnabled true if | |
| 994 * the Google Now is enabled for the user. | |
| 992 */ | 995 */ |
| 993 function updateRunningState( | 996 function updateRunningState( |
| 994 signedIn, | 997 signedIn, |
| 995 geolocationEnabled, | 998 geolocationEnabled, |
| 996 enableBackground, | 999 canEnableBackground, |
| 997 notificationEnabled) { | 1000 notificationEnabled, |
| 1001 googleNowEnabled) { | |
| 998 console.log( | 1002 console.log( |
| 999 'State Update signedIn=' + signedIn + ' ' + | 1003 'State Update signedIn=' + signedIn + ' ' + |
| 1000 'geolocationEnabled=' + geolocationEnabled + ' ' + | 1004 'geolocationEnabled=' + geolocationEnabled + ' ' + |
| 1001 'enableBackground=' + enableBackground + ' ' + | 1005 'canEnableBackground=' + canEnableBackground + ' ' + |
| 1002 'notificationEnabled=' + notificationEnabled); | 1006 'notificationEnabled=' + notificationEnabled + ' ' + |
| 1007 'googleNowEnabled=' + googleNowEnabled); | |
| 1003 | 1008 |
| 1004 // TODO(vadimt): Remove this line once state machine design is finalized. | 1009 // TODO(vadimt): Remove this line once state machine design is finalized. |
| 1005 geolocationEnabled = true; | 1010 geolocationEnabled = true; |
| 1006 | 1011 |
| 1007 var shouldPollCards = false; | 1012 var shouldPollCards = false; |
| 1008 var shouldSetBackground = false; | 1013 var shouldSetBackground = false; |
| 1009 | 1014 |
| 1010 if (signedIn && notificationEnabled) { | 1015 if (signedIn && notificationEnabled) { |
| 1011 if (geolocationEnabled) { | 1016 if (geolocationEnabled) { |
| 1012 if (enableBackground) | 1017 if (canEnableBackground && googleNowEnabled) |
| 1013 shouldSetBackground = true; | 1018 shouldSetBackground = true; |
| 1014 | 1019 |
| 1015 shouldPollCards = true; | 1020 shouldPollCards = true; |
| 1016 } | 1021 } |
| 1017 } else { | 1022 } else { |
| 1018 recordEvent(GoogleNowEvent.STOPPED); | 1023 recordEvent(GoogleNowEvent.STOPPED); |
| 1019 } | 1024 } |
| 1020 | 1025 |
| 1021 console.log( | 1026 console.log( |
| 1022 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + | 1027 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
| 1023 'setShouldPollCards=' + shouldPollCards); | 1028 'setShouldPollCards=' + shouldPollCards); |
| 1024 | 1029 |
| 1025 setBackgroundEnable(shouldSetBackground); | 1030 setBackgroundEnable(shouldSetBackground); |
| 1026 setShouldPollCards(shouldPollCards); | 1031 setShouldPollCards(shouldPollCards); |
| 1027 } | 1032 } |
| 1028 | 1033 |
| 1029 /** | 1034 /** |
| 1030 * Coordinates the behavior of Google Now for Chrome depending on | 1035 * Coordinates the behavior of Google Now for Chrome depending on |
| 1031 * Chrome and extension state. | 1036 * Chrome and extension state. |
| 1032 */ | 1037 */ |
| 1033 function onStateChange() { | 1038 function onStateChange() { |
| 1034 tasks.add(STATE_CHANGED_TASK_NAME, function() { | 1039 tasks.add(STATE_CHANGED_TASK_NAME, function() { |
| 1035 authenticationManager.isSignedIn(function(token) { | 1040 authenticationManager.isSignedIn(function(token) { |
| 1036 var signedIn = !!token; | 1041 var signedIn = !!token; |
| 1037 instrumented.metricsPrivate.getVariationParams( | 1042 instrumented.metricsPrivate.getVariationParams( |
| 1038 'GoogleNow', | 1043 'GoogleNow', |
| 1039 function(response) { | 1044 function(response) { |
| 1040 var enableBackground = | 1045 var canEnableBackground = |
| 1041 (!response || (response.enableBackground != 'false')); | 1046 (!response || (response.canEnableBackground != 'false')); |
| 1042 instrumented.notifications.getPermissionLevel(function(level) { | 1047 instrumented.notifications.getPermissionLevel(function(level) { |
| 1043 var notificationEnabled = (level == 'granted'); | 1048 var notificationEnabled = (level == 'granted'); |
| 1044 instrumented. | 1049 instrumented. |
| 1045 preferencesPrivate. | 1050 preferencesPrivate. |
| 1046 googleGeolocationAccessEnabled. | 1051 googleGeolocationAccessEnabled. |
| 1047 get({}, function(prefValue) { | 1052 get({}, function(prefValue) { |
| 1048 var geolocationEnabled = !!prefValue.value; | 1053 var geolocationEnabled = !!prefValue.value; |
| 1049 updateRunningState( | 1054 instrumented.storage.local.get( |
| 1050 signedIn, | 1055 'googleNowEnabled', |
| 1051 geolocationEnabled, | 1056 function(items) { |
| 1052 enableBackground, | 1057 var googleNowEnabled = |
| 1053 notificationEnabled); | 1058 items && !!items.googleNowEnabled; |
| 1059 updateRunningState( | |
| 1060 signedIn, | |
| 1061 geolocationEnabled, | |
| 1062 canEnableBackground, | |
| 1063 notificationEnabled, | |
| 1064 googleNowEnabled); | |
| 1065 }); | |
| 1054 }); | 1066 }); |
| 1055 }); | 1067 }); |
| 1056 }); | 1068 }); |
| 1057 }); | 1069 }); |
| 1058 }); | 1070 }); |
| 1059 } | 1071 } |
| 1060 | 1072 |
| 1061 instrumented.runtime.onInstalled.addListener(function(details) { | 1073 instrumented.runtime.onInstalled.addListener(function(details) { |
| 1062 console.log('onInstalled ' + JSON.stringify(details)); | 1074 console.log('onInstalled ' + JSON.stringify(details)); |
| 1063 if (details.reason != 'chrome_update') { | 1075 if (details.reason != 'chrome_update') { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1163 lastPollNowPayloads: items.lastPollNowPayloads, | 1175 lastPollNowPayloads: items.lastPollNowPayloads, |
| 1164 notificationGroups: items.notificationGroups | 1176 notificationGroups: items.notificationGroups |
| 1165 }); | 1177 }); |
| 1166 | 1178 |
| 1167 updateNotificationsCards(); | 1179 updateNotificationsCards(); |
| 1168 } | 1180 } |
| 1169 }); | 1181 }); |
| 1170 }); | 1182 }); |
| 1171 } | 1183 } |
| 1172 }); | 1184 }); |
| OLD | NEW |