| 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 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 */ | 481 */ |
| 482 function parseAndShowNotificationCards(response) { | 482 function parseAndShowNotificationCards(response) { |
| 483 console.log('parseAndShowNotificationCards ' + response); | 483 console.log('parseAndShowNotificationCards ' + response); |
| 484 var parsedResponse = JSON.parse(response); | 484 var parsedResponse = JSON.parse(response); |
| 485 | 485 |
| 486 if (parsedResponse.googleNowDisabled) { | 486 if (parsedResponse.googleNowDisabled) { |
| 487 chrome.storage.local.set({googleNowEnabled: false}); | 487 chrome.storage.local.set({googleNowEnabled: false}); |
| 488 // TODO(vadimt): Remove the line below once the server stops sending groups | 488 // TODO(vadimt): Remove the line below once the server stops sending groups |
| 489 // with 'googleNowDisabled' responses. | 489 // with 'googleNowDisabled' responses. |
| 490 parsedResponse.groups = {}; | 490 parsedResponse.groups = {}; |
| 491 // Google Now was enabled; now it's disabled. This is a state change. |
| 492 onStateChange(); |
| 491 } | 493 } |
| 492 | 494 |
| 493 var receivedGroups = parsedResponse.groups; | 495 var receivedGroups = parsedResponse.groups; |
| 494 | 496 |
| 495 // Populate groups with corresponding cards. | 497 // Populate groups with corresponding cards. |
| 496 if (parsedResponse.notifications) { | 498 if (parsedResponse.notifications) { |
| 497 for (var i = 0; i != parsedResponse.notifications.length; ++i) { | 499 for (var i = 0; i != parsedResponse.notifications.length; ++i) { |
| 498 var card = parsedResponse.notifications[i]; | 500 var card = parsedResponse.notifications[i]; |
| 499 var group = receivedGroups[card.groupName]; | 501 var group = receivedGroups[card.groupName]; |
| 500 group.cards = group.cards || []; | 502 group.cards = group.cards || []; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 var request = buildServerRequest('GET', 'settings/optin'); | 602 var request = buildServerRequest('GET', 'settings/optin'); |
| 601 | 603 |
| 602 request.onloadend = function(event) { | 604 request.onloadend = function(event) { |
| 603 console.log( | 605 console.log( |
| 604 'requestOptedIn-onloadend ' + request.status + ' ' + request.response); | 606 'requestOptedIn-onloadend ' + request.status + ' ' + request.response); |
| 605 if (request.status == HTTP_OK) { | 607 if (request.status == HTTP_OK) { |
| 606 var parsedResponse = JSON.parse(request.response); | 608 var parsedResponse = JSON.parse(request.response); |
| 607 if (parsedResponse.value) { | 609 if (parsedResponse.value) { |
| 608 chrome.storage.local.set({googleNowEnabled: true}); | 610 chrome.storage.local.set({googleNowEnabled: true}); |
| 609 optedInCallback(); | 611 optedInCallback(); |
| 612 // Google Now was disabled, now it's enabled. This is a state change. |
| 613 onStateChange(); |
| 610 } else { | 614 } else { |
| 611 scheduleNextPoll({}, false); | 615 scheduleNextPoll({}, false); |
| 612 } | 616 } |
| 613 } | 617 } |
| 614 }; | 618 }; |
| 615 | 619 |
| 616 setAuthorization(request, function(success) { | 620 setAuthorization(request, function(success) { |
| 617 if (success) | 621 if (success) |
| 618 request.send(); | 622 request.send(); |
| 619 }); | 623 }); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); | 925 updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); |
| 922 | 926 |
| 923 requestLocation(); | 927 requestLocation(); |
| 924 } | 928 } |
| 925 | 929 |
| 926 /** | 930 /** |
| 927 * Stops all machinery in the polling system. | 931 * Stops all machinery in the polling system. |
| 928 */ | 932 */ |
| 929 function stopPollingCards() { | 933 function stopPollingCards() { |
| 930 stopRequestLocation(); | 934 stopRequestLocation(); |
| 931 | |
| 932 updateCardsAttempts.stop(); | 935 updateCardsAttempts.stop(); |
| 933 | |
| 934 removeAllCards(); | 936 removeAllCards(); |
| 937 // Mark the Google Now as disabled to start with checking the opt-in state |
| 938 // next time startPollingCards() is called. |
| 939 chrome.storage.local.set({googleNowEnabled: false}); |
| 935 } | 940 } |
| 936 | 941 |
| 937 /** | 942 /** |
| 938 * Initializes the event page on install or on browser startup. | 943 * Initializes the event page on install or on browser startup. |
| 939 */ | 944 */ |
| 940 function initialize() { | 945 function initialize() { |
| 941 recordEvent(GoogleNowEvent.EXTENSION_START); | 946 recordEvent(GoogleNowEvent.EXTENSION_START); |
| 942 onStateChange(); | 947 onStateChange(); |
| 943 } | 948 } |
| 944 | 949 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 } | 986 } |
| 982 }); | 987 }); |
| 983 } | 988 } |
| 984 | 989 |
| 985 /** | 990 /** |
| 986 * Does the actual work of deciding what Google Now should do | 991 * Does the actual work of deciding what Google Now should do |
| 987 * based off of the current state of Chrome. | 992 * based off of the current state of Chrome. |
| 988 * @param {boolean} signedIn true if the user is signed in. | 993 * @param {boolean} signedIn true if the user is signed in. |
| 989 * @param {boolean} geolocationEnabled true if | 994 * @param {boolean} geolocationEnabled true if |
| 990 * the geolocation option is enabled. | 995 * the geolocation option is enabled. |
| 991 * @param {boolean} enableBackground true if | 996 * @param {boolean} canEnableBackground true if |
| 992 * the background permission should be requested. | 997 * the background permission can be requested. |
| 993 * @param {boolean} notificationEnabled true if | 998 * @param {boolean} notificationEnabled true if |
| 994 * Google Now for Chrome is allowed to show notifications. | 999 * Google Now for Chrome is allowed to show notifications. |
| 1000 * @param {boolean} googleNowEnabled true if |
| 1001 * the Google Now is enabled for the user. |
| 995 */ | 1002 */ |
| 996 function updateRunningState( | 1003 function updateRunningState( |
| 997 signedIn, | 1004 signedIn, |
| 998 geolocationEnabled, | 1005 geolocationEnabled, |
| 999 enableBackground, | 1006 canEnableBackground, |
| 1000 notificationEnabled) { | 1007 notificationEnabled, |
| 1008 googleNowEnabled) { |
| 1001 console.log( | 1009 console.log( |
| 1002 'State Update signedIn=' + signedIn + ' ' + | 1010 'State Update signedIn=' + signedIn + ' ' + |
| 1003 'geolocationEnabled=' + geolocationEnabled + ' ' + | 1011 'geolocationEnabled=' + geolocationEnabled + ' ' + |
| 1004 'enableBackground=' + enableBackground + ' ' + | 1012 'canEnableBackground=' + canEnableBackground + ' ' + |
| 1005 'notificationEnabled=' + notificationEnabled); | 1013 'notificationEnabled=' + notificationEnabled + ' ' + |
| 1014 'googleNowEnabled=' + googleNowEnabled); |
| 1006 | 1015 |
| 1007 // TODO(vadimt): Remove this line once state machine design is finalized. | 1016 // TODO(vadimt): Remove this line once state machine design is finalized. |
| 1008 geolocationEnabled = true; | 1017 geolocationEnabled = true; |
| 1009 | 1018 |
| 1010 var shouldPollCards = false; | 1019 var shouldPollCards = false; |
| 1011 var shouldSetBackground = false; | 1020 var shouldSetBackground = false; |
| 1012 | 1021 |
| 1013 if (signedIn && notificationEnabled) { | 1022 if (signedIn && notificationEnabled) { |
| 1014 if (geolocationEnabled) { | 1023 if (geolocationEnabled) { |
| 1015 if (enableBackground) | 1024 if (canEnableBackground && googleNowEnabled) |
| 1016 shouldSetBackground = true; | 1025 shouldSetBackground = true; |
| 1017 | 1026 |
| 1018 shouldPollCards = true; | 1027 shouldPollCards = true; |
| 1019 } | 1028 } |
| 1020 } else { | 1029 } else { |
| 1021 recordEvent(GoogleNowEvent.STOPPED); | 1030 recordEvent(GoogleNowEvent.STOPPED); |
| 1022 } | 1031 } |
| 1023 | 1032 |
| 1024 console.log( | 1033 console.log( |
| 1025 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + | 1034 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
| 1026 'setShouldPollCards=' + shouldPollCards); | 1035 'setShouldPollCards=' + shouldPollCards); |
| 1027 | 1036 |
| 1028 setBackgroundEnable(shouldSetBackground); | 1037 setBackgroundEnable(shouldSetBackground); |
| 1029 setShouldPollCards(shouldPollCards); | 1038 setShouldPollCards(shouldPollCards); |
| 1030 } | 1039 } |
| 1031 | 1040 |
| 1032 /** | 1041 /** |
| 1033 * Coordinates the behavior of Google Now for Chrome depending on | 1042 * Coordinates the behavior of Google Now for Chrome depending on |
| 1034 * Chrome and extension state. | 1043 * Chrome and extension state. |
| 1035 */ | 1044 */ |
| 1036 function onStateChange() { | 1045 function onStateChange() { |
| 1037 tasks.add(STATE_CHANGED_TASK_NAME, function() { | 1046 tasks.add(STATE_CHANGED_TASK_NAME, function() { |
| 1038 authenticationManager.isSignedIn(function(token) { | 1047 authenticationManager.isSignedIn(function(token) { |
| 1039 var signedIn = !!token; | 1048 var signedIn = !!token; |
| 1040 instrumented.metricsPrivate.getVariationParams( | 1049 instrumented.metricsPrivate.getVariationParams( |
| 1041 'GoogleNow', | 1050 'GoogleNow', |
| 1042 function(response) { | 1051 function(response) { |
| 1043 var enableBackground = | 1052 var canEnableBackground = |
| 1044 (!response || (response.enableBackground != 'false')); | 1053 (!response || (response.canEnableBackground != 'false')); |
| 1045 instrumented.notifications.getPermissionLevel(function(level) { | 1054 instrumented.notifications.getPermissionLevel(function(level) { |
| 1046 var notificationEnabled = (level == 'granted'); | 1055 var notificationEnabled = (level == 'granted'); |
| 1047 instrumented. | 1056 instrumented. |
| 1048 preferencesPrivate. | 1057 preferencesPrivate. |
| 1049 googleGeolocationAccessEnabled. | 1058 googleGeolocationAccessEnabled. |
| 1050 get({}, function(prefValue) { | 1059 get({}, function(prefValue) { |
| 1051 var geolocationEnabled = !!prefValue.value; | 1060 var geolocationEnabled = !!prefValue.value; |
| 1052 updateRunningState( | 1061 instrumented.storage.local.get( |
| 1053 signedIn, | 1062 'googleNowEnabled', |
| 1054 geolocationEnabled, | 1063 function(items) { |
| 1055 enableBackground, | 1064 var googleNowEnabled = |
| 1056 notificationEnabled); | 1065 items && !!items.googleNowEnabled; |
| 1066 updateRunningState( |
| 1067 signedIn, |
| 1068 geolocationEnabled, |
| 1069 canEnableBackground, |
| 1070 notificationEnabled, |
| 1071 googleNowEnabled); |
| 1072 }); |
| 1057 }); | 1073 }); |
| 1058 }); | 1074 }); |
| 1059 }); | 1075 }); |
| 1060 }); | 1076 }); |
| 1061 }); | 1077 }); |
| 1062 } | 1078 } |
| 1063 | 1079 |
| 1064 instrumented.runtime.onInstalled.addListener(function(details) { | 1080 instrumented.runtime.onInstalled.addListener(function(details) { |
| 1065 console.log('onInstalled ' + JSON.stringify(details)); | 1081 console.log('onInstalled ' + JSON.stringify(details)); |
| 1066 if (details.reason != 'chrome_update') { | 1082 if (details.reason != 'chrome_update') { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1166 lastPollNowPayloads: items.lastPollNowPayloads, | 1182 lastPollNowPayloads: items.lastPollNowPayloads, |
| 1167 notificationGroups: items.notificationGroups | 1183 notificationGroups: items.notificationGroups |
| 1168 }); | 1184 }); |
| 1169 | 1185 |
| 1170 updateNotificationsCards(); | 1186 updateNotificationsCards(); |
| 1171 } | 1187 } |
| 1172 }); | 1188 }); |
| 1173 }); | 1189 }); |
| 1174 } | 1190 } |
| 1175 }); | 1191 }); |
| OLD | NEW |