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

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

Issue 25114002: Finishing API error processing, and cleanup (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase 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
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/background_test_util.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); 161 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0);
162 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); 162 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0);
163 wrapper.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); 163 wrapper.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0);
164 wrapper.instrumentChromeApiFunction( 164 wrapper.instrumentChromeApiFunction(
165 'preferencesPrivate.googleGeolocationAccessEnabled.get', 165 'preferencesPrivate.googleGeolocationAccessEnabled.get',
166 1); 166 1);
167 wrapper.instrumentChromeApiFunction( 167 wrapper.instrumentChromeApiFunction(
168 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', 168 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener',
169 0); 169 0);
170 wrapper.instrumentChromeApiFunction('permissions.contains', 1); 170 wrapper.instrumentChromeApiFunction('permissions.contains', 1);
171 wrapper.instrumentChromeApiFunction('permissions.remove', 1);
172 wrapper.instrumentChromeApiFunction('permissions.request', 1);
173 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); 171 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0);
174 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); 172 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0);
175 wrapper.instrumentChromeApiFunction('tabs.create', 1); 173 wrapper.instrumentChromeApiFunction('tabs.create', 1);
176 wrapper.instrumentChromeApiFunction('storage.local.get', 1); 174 wrapper.instrumentChromeApiFunction('storage.local.get', 1);
177 175
178 var updateCardsAttempts = buildAttemptManager( 176 var updateCardsAttempts = buildAttemptManager(
179 'cards-update', 177 'cards-update',
180 requestLocation, 178 requestLocation,
181 INITIAL_POLLING_PERIOD_SECONDS, 179 INITIAL_POLLING_PERIOD_SECONDS,
182 MAXIMUM_POLLING_PERIOD_SECONDS); 180 MAXIMUM_POLLING_PERIOD_SECONDS);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 * @param {Object.<string, MergedCard>} cards Map from chromeNotificationId to 260 * @param {Object.<string, MergedCard>} cards Map from chromeNotificationId to
263 * the merged card, containing cards to show. 261 * the merged card, containing cards to show.
264 */ 262 */
265 function showNotificationCards(cards) { 263 function showNotificationCards(cards) {
266 console.log('showNotificationCards ' + JSON.stringify(cards)); 264 console.log('showNotificationCards ' + JSON.stringify(cards));
267 265
268 instrumented.storage.local.get(['notificationsData', 'recentDismissals'], 266 instrumented.storage.local.get(['notificationsData', 'recentDismissals'],
269 function(items) { 267 function(items) {
270 console.log('showNotificationCards-get ' + 268 console.log('showNotificationCards-get ' +
271 JSON.stringify(items)); 269 JSON.stringify(items));
270 items = items || {};
272 items.notificationsData = items.notificationsData || {}; 271 items.notificationsData = items.notificationsData || {};
273 items.recentDismissals = items.recentDismissals || {}; 272 items.recentDismissals = items.recentDismissals || {};
274 273
275 instrumented.notifications.getAll(function(notifications) { 274 instrumented.notifications.getAll(function(notifications) {
276 console.log('showNotificationCards-getAll ' + 275 console.log('showNotificationCards-getAll ' +
277 JSON.stringify(notifications)); 276 JSON.stringify(notifications));
278 // TODO(vadimt): Figure out what to do when notifications are 277 // TODO(vadimt): Figure out what to do when notifications are
279 // disabled for our extension. 278 // disabled for our extension.
280 notifications = notifications || {}; 279 notifications = notifications || {};
281 280
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 for (var i = 0; i != parsedResponse.notifications.length; ++i) { 462 for (var i = 0; i != parsedResponse.notifications.length; ++i) {
464 var card = parsedResponse.notifications[i]; 463 var card = parsedResponse.notifications[i];
465 var group = groups[card.groupName]; 464 var group = groups[card.groupName];
466 group.cards = group.cards || []; 465 group.cards = group.cards || [];
467 group.cards.push(card); 466 group.cards.push(card);
468 } 467 }
469 } 468 }
470 469
471 instrumented.storage.local.get('notificationGroups', function(items) { 470 instrumented.storage.local.get('notificationGroups', function(items) {
472 console.log('parseAndShowNotificationCards-get ' + JSON.stringify(items)); 471 console.log('parseAndShowNotificationCards-get ' + JSON.stringify(items));
472 items = items || {};
473 items.notificationGroups = items.notificationGroups || {}; 473 items.notificationGroups = items.notificationGroups || {};
474 474
475 var now = Date.now(); 475 var now = Date.now();
476 476
477 // Build updated set of groups and merge cards from all groups into one set. 477 // Build updated set of groups and merge cards from all groups into one set.
478 var updatedGroups = {}; 478 var updatedGroups = {};
479 var mergedCards = {}; 479 var mergedCards = {};
480 480
481 for (var groupName in groups) { 481 for (var groupName in groups) {
482 var receivedGroup = groups[groupName]; 482 var receivedGroup = groups[groupName];
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 ' from ' + NOTIFICATION_CARDS_URL); 523 ' from ' + NOTIFICATION_CARDS_URL);
524 524
525 if (!NOTIFICATION_CARDS_URL) 525 if (!NOTIFICATION_CARDS_URL)
526 return; 526 return;
527 527
528 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL); 528 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL);
529 529
530 instrumented.storage.local.get('notificationGroups', function(items) { 530 instrumented.storage.local.get('notificationGroups', function(items) {
531 console.log('requestNotificationCards-storage-get ' + 531 console.log('requestNotificationCards-storage-get ' +
532 JSON.stringify(items)); 532 JSON.stringify(items));
533 items = items || {};
533 534
534 var requestParameters = '?timeZoneOffsetMs=' + 535 var requestParameters = '?timeZoneOffsetMs=' +
535 (-new Date().getTimezoneOffset() * MS_IN_MINUTE); 536 (-new Date().getTimezoneOffset() * MS_IN_MINUTE);
536 537
537 if (items.notificationGroups) { 538 if (items.notificationGroups) {
538 var now = Date.now(); 539 var now = Date.now();
539 540
540 for (var groupName in items.notificationGroups) { 541 for (var groupName in items.notificationGroups) {
541 var group = items.notificationGroups[groupName]; 542 var group = items.notificationGroups[groupName];
542 if (group.nextPollTime <= now) 543 if (group.nextPollTime <= now)
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 /** 683 /**
683 * Tries to send dismiss requests for all pending dismissals. 684 * Tries to send dismiss requests for all pending dismissals.
684 * @param {function(boolean)} callbackBoolean Completion callback with 'success' 685 * @param {function(boolean)} callbackBoolean Completion callback with 'success'
685 * parameter. Success means that no pending dismissals are left. 686 * parameter. Success means that no pending dismissals are left.
686 */ 687 */
687 function processPendingDismissals(callbackBoolean) { 688 function processPendingDismissals(callbackBoolean) {
688 instrumented.storage.local.get(['pendingDismissals', 'recentDismissals'], 689 instrumented.storage.local.get(['pendingDismissals', 'recentDismissals'],
689 function(items) { 690 function(items) {
690 console.log('processPendingDismissals-storage-get ' + 691 console.log('processPendingDismissals-storage-get ' +
691 JSON.stringify(items)); 692 JSON.stringify(items));
693 items = items || {};
692 items.pendingDismissals = items.pendingDismissals || []; 694 items.pendingDismissals = items.pendingDismissals || [];
693 items.recentDismissals = items.recentDismissals || {}; 695 items.recentDismissals = items.recentDismissals || {};
694 696
695 var dismissalsChanged = false; 697 var dismissalsChanged = false;
696 698
697 function onFinish(success) { 699 function onFinish(success) {
698 if (dismissalsChanged) { 700 if (dismissalsChanged) {
699 chrome.storage.local.set({ 701 chrome.storage.local.set({
700 pendingDismissals: items.pendingDismissals, 702 pendingDismissals: items.pendingDismissals,
701 recentDismissals: items.recentDismissals 703 recentDismissals: items.recentDismissals
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 } 812 }
811 813
812 // At this point we are guaranteed that the notification is a now card. 814 // At this point we are guaranteed that the notification is a now card.
813 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); 815 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed');
814 816
815 tasks.add(DISMISS_CARD_TASK_NAME, function() { 817 tasks.add(DISMISS_CARD_TASK_NAME, function() {
816 dismissalAttempts.start(); 818 dismissalAttempts.start();
817 819
818 instrumented.storage.local.get( 820 instrumented.storage.local.get(
819 ['pendingDismissals', 'notificationsData'], function(items) { 821 ['pendingDismissals', 'notificationsData'], function(items) {
822 items = items || {};
820 items.pendingDismissals = items.pendingDismissals || []; 823 items.pendingDismissals = items.pendingDismissals || [];
821 items.notificationsData = items.notificationsData || {}; 824 items.notificationsData = items.notificationsData || {};
822 825
823 // Deleting the notification in case it was re-added while this task was 826 // Deleting the notification in case it was re-added while this task was
824 // scheduled, waiting for execution; also cleaning notification's data 827 // scheduled, waiting for execution; also cleaning notification's data
825 // from storage. 828 // from storage.
826 cardSet.clear(chromeNotificationId, true); 829 cardSet.clear(chromeNotificationId, true);
827 830
828 var notificationData = items.notificationsData[chromeNotificationId]; 831 var notificationData = items.notificationsData[chromeNotificationId];
829 832
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 // determine if we are already running). To mitigate this, we will 882 // determine if we are already running). To mitigate this, we will
880 // shut everything down on initialize before starting everything up. 883 // shut everything down on initialize before starting everything up.
881 stopPollingCards(); 884 stopPollingCards();
882 onStateChange(); 885 onStateChange();
883 } 886 }
884 887
885 /** 888 /**
886 * Starts or stops the polling of cards. 889 * Starts or stops the polling of cards.
887 * @param {boolean} shouldPollCardsRequest true to start and 890 * @param {boolean} shouldPollCardsRequest true to start and
888 * false to stop polling cards. 891 * false to stop polling cards.
889 * @param {function} callback Called on completion.
890 */ 892 */
891 function setShouldPollCards(shouldPollCardsRequest, callback) { 893 function setShouldPollCards(shouldPollCardsRequest) {
892 tasks.debugSetStepName(
893 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning');
894 updateCardsAttempts.isRunning(function(currentValue) { 894 updateCardsAttempts.isRunning(function(currentValue) {
895 if (shouldPollCardsRequest != currentValue) { 895 if (shouldPollCardsRequest != currentValue) {
896 console.log('Action Taken setShouldPollCards=' + shouldPollCardsRequest); 896 console.log('Action Taken setShouldPollCards=' + shouldPollCardsRequest);
897 if (shouldPollCardsRequest) 897 if (shouldPollCardsRequest)
898 startPollingCards(); 898 startPollingCards();
899 else 899 else
900 stopPollingCards(); 900 stopPollingCards();
901 } else { 901 } else {
902 console.log( 902 console.log(
903 'Action Ignored setShouldPollCards=' + shouldPollCardsRequest); 903 'Action Ignored setShouldPollCards=' + shouldPollCardsRequest);
904 } 904 }
905 callback();
906 }); 905 });
907 } 906 }
908 907
909 /** 908 /**
910 * Shows or hides the toast. 909 * Shows or hides the toast.
911 * @param {boolean} visibleRequest true to show the toast and 910 * @param {boolean} visibleRequest true to show the toast and
912 * false to hide the toast. 911 * false to hide the toast.
913 * @param {function} callback Called on completion.
914 */ 912 */
915 function setToastVisible(visibleRequest, callback) { 913 function setToastVisible(visibleRequest) {
916 tasks.debugSetStepName(
917 'setToastVisible-shouldSetToastVisible-getAllNotifications');
918 instrumented.notifications.getAll(function(notifications) { 914 instrumented.notifications.getAll(function(notifications) {
919 // TODO(vadimt): Figure out what to do when notifications are disabled for 915 // TODO(vadimt): Figure out what to do when notifications are disabled for
920 // our extension. 916 // our extension.
921 notifications = notifications || {}; 917 notifications = notifications || {};
922 918
923 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) { 919 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) {
924 console.log('Action Taken setToastVisible=' + visibleRequest); 920 console.log('Action Taken setToastVisible=' + visibleRequest);
925 if (visibleRequest) 921 if (visibleRequest)
926 showWelcomeToast(); 922 showWelcomeToast();
927 else 923 else
928 hideWelcomeToast(); 924 hideWelcomeToast();
929 } else { 925 } else {
930 console.log('Action Ignored setToastVisible=' + visibleRequest); 926 console.log('Action Ignored setToastVisible=' + visibleRequest);
931 } 927 }
932
933 callback();
934 }); 928 });
935 } 929 }
936 930
937 /** 931 /**
938 * Enables or disables the Google Now background permission. 932 * Enables or disables the Google Now background permission.
939 * @param {boolean} backgroundEnable true to run in the background. 933 * @param {boolean} backgroundEnable true to run in the background.
940 * false to not run in the background. 934 * false to not run in the background.
941 * @param {function} callback Called on completion.
942 */ 935 */
943 function setBackgroundEnable(backgroundEnable, callback) { 936 function setBackgroundEnable(backgroundEnable) {
944 instrumented.permissions.contains({permissions: ['background']}, 937 instrumented.permissions.contains({permissions: ['background']},
945 function(hasPermission) { 938 function(hasPermission) {
946 if (backgroundEnable != hasPermission) { 939 if (backgroundEnable != hasPermission) {
947 console.log('Action Taken setBackgroundEnable=' + backgroundEnable); 940 console.log('Action Taken setBackgroundEnable=' + backgroundEnable);
948 if (backgroundEnable) 941 if (backgroundEnable)
949 instrumented.permissions.request( 942 chrome.permissions.request({permissions: ['background']});
950 {permissions: ['background']},
951 function() {
952 callback();
953 });
954 else 943 else
955 instrumented.permissions.remove( 944 chrome.permissions.remove({permissions: ['background']});
956 {permissions: ['background']},
957 function() {
958 callback();
959 });
960 } else { 945 } else {
961 console.log('Action Ignored setBackgroundEnable=' + backgroundEnable); 946 console.log('Action Ignored setBackgroundEnable=' + backgroundEnable);
962 callback();
963 } 947 }
964 }); 948 });
965 } 949 }
966 950
967 /** 951 /**
968 * Does the actual work of deciding what Google Now should do 952 * Does the actual work of deciding what Google Now should do
969 * based off of the current state of Chrome. 953 * based off of the current state of Chrome.
970 * @param {boolean} signedIn true if the user is signed in. 954 * @param {boolean} signedIn true if the user is signed in.
971 * @param {boolean} geolocationEnabled true if 955 * @param {boolean} geolocationEnabled true if
972 * the geolocation option is enabled. 956 * the geolocation option is enabled.
973 * @param {boolean} userRespondedToToast true if 957 * @param {boolean} userRespondedToToast true if
974 * the user has responded to the toast. 958 * the user has responded to the toast.
975 * @param {boolean} enableBackground true if 959 * @param {boolean} enableBackground true if
976 * the background permission should be requested. 960 * the background permission should be requested.
977 * @param {function()} callback Call this function on completion.
978 */ 961 */
979 function updateRunningState( 962 function updateRunningState(
980 signedIn, 963 signedIn,
981 geolocationEnabled, 964 geolocationEnabled,
982 userRespondedToToast, 965 userRespondedToToast,
983 enableBackground, 966 enableBackground) {
984 callback) {
985 console.log( 967 console.log(
986 'State Update signedIn=' + signedIn + ' ' + 968 'State Update signedIn=' + signedIn + ' ' +
987 'geolocationEnabled=' + geolocationEnabled + ' ' + 969 'geolocationEnabled=' + geolocationEnabled + ' ' +
988 'userRespondedToToast=' + userRespondedToToast); 970 'userRespondedToToast=' + userRespondedToToast);
989 971
990 // TODO(vadimt): Remove this line once state machine design is finalized. 972 // TODO(vadimt): Remove this line once state machine design is finalized.
991 geolocationEnabled = userRespondedToToast = true; 973 geolocationEnabled = userRespondedToToast = true;
992 974
993 var shouldSetToastVisible = false; 975 var shouldSetToastVisible = false;
994 var shouldPollCards = false; 976 var shouldPollCards = false;
(...skipping 21 matching lines...) Expand all
1016 } 998 }
1017 } else { 999 } else {
1018 recordEvent(GoogleNowEvent.STOPPED); 1000 recordEvent(GoogleNowEvent.STOPPED);
1019 } 1001 }
1020 1002
1021 console.log( 1003 console.log(
1022 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + 1004 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' +
1023 'setToastVisible=' + shouldSetToastVisible + ' ' + 1005 'setToastVisible=' + shouldSetToastVisible + ' ' +
1024 'setShouldPollCards=' + shouldPollCards); 1006 'setShouldPollCards=' + shouldPollCards);
1025 1007
1026 setBackgroundEnable(shouldSetBackground, function() { 1008 setBackgroundEnable(shouldSetBackground);
1027 setToastVisible(shouldSetToastVisible, function() { 1009 setToastVisible(shouldSetToastVisible);
1028 setShouldPollCards(shouldPollCards, callback); 1010 setShouldPollCards(shouldPollCards);
1029 });
1030 });
1031 } 1011 }
1032 1012
1033 /** 1013 /**
1034 * Coordinates the behavior of Google Now for Chrome depending on 1014 * Coordinates the behavior of Google Now for Chrome depending on
1035 * Chrome and extension state. 1015 * Chrome and extension state.
1036 */ 1016 */
1037 function onStateChange() { 1017 function onStateChange() {
1038 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { 1018 tasks.add(STATE_CHANGED_TASK_NAME, function() {
1039 tasks.debugSetStepName('onStateChange-isSignedIn');
1040 authenticationManager.isSignedIn(function(token) { 1019 authenticationManager.isSignedIn(function(token) {
1041 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; 1020 var signedIn = !!token && !!NOTIFICATION_CARDS_URL;
1042 instrumented.metricsPrivate.getVariationParams( 1021 instrumented.metricsPrivate.getVariationParams(
1043 'GoogleNow', 1022 'GoogleNow',
1044 function(response) { 1023 function(response) {
1045 var enableBackground = 1024 var enableBackground =
1046 (!response || (response.enableBackground != 'false')); 1025 (!response || (response.enableBackground != 'false'));
1047 tasks.debugSetStepName(
1048 'onStateChange-get-googleGeolocationAccessEnabledPref');
1049 instrumented. 1026 instrumented.
1050 preferencesPrivate. 1027 preferencesPrivate.
1051 googleGeolocationAccessEnabled. 1028 googleGeolocationAccessEnabled.
1052 get({}, function(prefValue) { 1029 get({}, function(prefValue) {
1053 var geolocationEnabled = !!prefValue.value; 1030 var geolocationEnabled = !!prefValue.value;
1054 tasks.debugSetStepName(
1055 'onStateChange-get-userRespondedToToast');
1056 instrumented.storage.local.get( 1031 instrumented.storage.local.get(
1057 'userRespondedToToast', 1032 'userRespondedToToast',
1058 function(items) { 1033 function(items) {
1059 var userRespondedToToast = 1034 var userRespondedToToast =
1060 !items || !!items.userRespondedToToast; 1035 !items || !!items.userRespondedToToast;
1061 updateRunningState( 1036 updateRunningState(
1062 signedIn, 1037 signedIn,
1063 geolocationEnabled, 1038 geolocationEnabled,
1064 userRespondedToToast, 1039 userRespondedToToast,
1065 enableBackground, 1040 enableBackground);
1066 callback);
1067 }); 1041 });
1068 }); 1042 });
1069 }); 1043 });
1070 }); 1044 });
1071 }); 1045 });
1072 } 1046 }
1073 1047
1074 /** 1048 /**
1075 * Displays a toast to the user asking if they want to opt in to receiving 1049 * Displays a toast to the user asking if they want to opt in to receiving
1076 * Google Now cards. 1050 * Google Now cards.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 1130
1157 instrumented.location.onLocationUpdate.addListener(function(position) { 1131 instrumented.location.onLocationUpdate.addListener(function(position) {
1158 recordEvent(GoogleNowEvent.LOCATION_UPDATE); 1132 recordEvent(GoogleNowEvent.LOCATION_UPDATE);
1159 updateNotificationsCards(position); 1133 updateNotificationsCards(position);
1160 }); 1134 });
1161 1135
1162 instrumented.omnibox.onInputEntered.addListener(function(text) { 1136 instrumented.omnibox.onInputEntered.addListener(function(text) {
1163 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; 1137 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text;
1164 initialize(); 1138 initialize();
1165 }); 1139 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/background_test_util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698