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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 return false; | 146 return false; |
147 } | 147 } |
148 | 148 |
149 var tasks = buildTaskManager(areTasksConflicting); | 149 var tasks = buildTaskManager(areTasksConflicting); |
150 | 150 |
151 // Add error processing to API calls. | 151 // Add error processing to API calls. |
152 wrapper.instrumentChromeApiFunction('location.onLocationUpdate.addListener', 0); | 152 wrapper.instrumentChromeApiFunction('location.onLocationUpdate.addListener', 0); |
153 wrapper.instrumentChromeApiFunction('metricsPrivate.getVariationParams', 1); | 153 wrapper.instrumentChromeApiFunction('metricsPrivate.getVariationParams', 1); |
154 wrapper.instrumentChromeApiFunction('notifications.clear', 1); | 154 wrapper.instrumentChromeApiFunction('notifications.clear', 1); |
155 wrapper.instrumentChromeApiFunction('notifications.create', 2); | 155 wrapper.instrumentChromeApiFunction('notifications.create', 2); |
156 wrapper.instrumentChromeApiFunction('notifications.getPermissionLevel', 0); | |
156 wrapper.instrumentChromeApiFunction('notifications.update', 2); | 157 wrapper.instrumentChromeApiFunction('notifications.update', 2); |
157 wrapper.instrumentChromeApiFunction('notifications.getAll', 0); | 158 wrapper.instrumentChromeApiFunction('notifications.getAll', 0); |
158 wrapper.instrumentChromeApiFunction( | 159 wrapper.instrumentChromeApiFunction( |
159 'notifications.onButtonClicked.addListener', 0); | 160 'notifications.onButtonClicked.addListener', 0); |
160 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); | 161 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); |
161 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); | 162 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); |
162 wrapper.instrumentChromeApiFunction( | 163 wrapper.instrumentChromeApiFunction( |
164 'notifications.onPermissionLevelChanged.addListener', 0); | |
165 wrapper.instrumentChromeApiFunction( | |
163 'preferencesPrivate.googleGeolocationAccessEnabled.get', | 166 'preferencesPrivate.googleGeolocationAccessEnabled.get', |
164 1); | 167 1); |
165 wrapper.instrumentChromeApiFunction( | 168 wrapper.instrumentChromeApiFunction( |
166 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', | 169 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', |
167 0); | 170 0); |
168 wrapper.instrumentChromeApiFunction('permissions.contains', 1); | 171 wrapper.instrumentChromeApiFunction('permissions.contains', 1); |
169 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); | 172 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); |
170 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 173 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
171 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 174 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
172 wrapper.instrumentChromeApiFunction('tabs.create', 1); | 175 wrapper.instrumentChromeApiFunction('tabs.create', 1); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 function(items) { | 269 function(items) { |
267 console.log('showNotificationCards-get ' + | 270 console.log('showNotificationCards-get ' + |
268 JSON.stringify(items)); | 271 JSON.stringify(items)); |
269 items = items || {}; | 272 items = items || {}; |
270 items.notificationsData = items.notificationsData || {}; | 273 items.notificationsData = items.notificationsData || {}; |
271 items.recentDismissals = items.recentDismissals || {}; | 274 items.recentDismissals = items.recentDismissals || {}; |
272 | 275 |
273 instrumented.notifications.getAll(function(notifications) { | 276 instrumented.notifications.getAll(function(notifications) { |
274 console.log('showNotificationCards-getAll ' + | 277 console.log('showNotificationCards-getAll ' + |
275 JSON.stringify(notifications)); | 278 JSON.stringify(notifications)); |
276 // TODO(vadimt): Figure out what to do when notifications are | 279 // TODO(vadimt): Figure out what to do when notifications are |
vadimt
2013/10/23 22:29:14
Please remove this comment.
robliao
2013/10/23 22:54:44
Done.
| |
277 // disabled for our extension. | 280 // disabled for our extension. |
278 notifications = notifications || {}; | 281 notifications = notifications || {}; |
279 | 282 |
280 // Build a set of non-expired recent dismissals. It will be used for | 283 // Build a set of non-expired recent dismissals. It will be used for |
281 // client-side filtering of cards. | 284 // client-side filtering of cards. |
282 var updatedRecentDismissals = {}; | 285 var updatedRecentDismissals = {}; |
283 var currentTimeMs = Date.now(); | 286 var currentTimeMs = Date.now(); |
284 for (var chromeNotificationId in items.recentDismissals) { | 287 for (var chromeNotificationId in items.recentDismissals) { |
285 if (currentTimeMs - items.recentDismissals[chromeNotificationId] < | 288 if (currentTimeMs - items.recentDismissals[chromeNotificationId] < |
286 DISMISS_RETENTION_TIME_MS) { | 289 DISMISS_RETENTION_TIME_MS) { |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
913 } | 916 } |
914 | 917 |
915 /** | 918 /** |
916 * Does the actual work of deciding what Google Now should do | 919 * Does the actual work of deciding what Google Now should do |
917 * based off of the current state of Chrome. | 920 * based off of the current state of Chrome. |
918 * @param {boolean} signedIn true if the user is signed in. | 921 * @param {boolean} signedIn true if the user is signed in. |
919 * @param {boolean} geolocationEnabled true if | 922 * @param {boolean} geolocationEnabled true if |
920 * the geolocation option is enabled. | 923 * the geolocation option is enabled. |
921 * @param {boolean} enableBackground true if | 924 * @param {boolean} enableBackground true if |
922 * the background permission should be requested. | 925 * the background permission should be requested. |
926 * @param {boolean} notificationEnabled true if | |
927 * Google Now for Chrome is allowed to show notifications. | |
923 */ | 928 */ |
924 function updateRunningState( | 929 function updateRunningState( |
925 signedIn, | 930 signedIn, |
926 geolocationEnabled, | 931 geolocationEnabled, |
927 enableBackground) { | 932 enableBackground, |
933 notificationEnabled) { | |
928 console.log( | 934 console.log( |
929 'State Update signedIn=' + signedIn + ' ' + | 935 'State Update signedIn=' + signedIn + ' ' + |
930 'geolocationEnabled=' + geolocationEnabled); | 936 'geolocationEnabled=' + geolocationEnabled + ' ' + |
937 'enableBackground=' + enableBackground + ' ' + | |
938 'notificationEnabled=' + notificationEnabled); | |
931 | 939 |
932 // TODO(vadimt): Remove this line once state machine design is finalized. | 940 // TODO(vadimt): Remove this line once state machine design is finalized. |
933 geolocationEnabled = true; | 941 geolocationEnabled = true; |
934 | 942 |
935 var shouldPollCards = false; | 943 var shouldPollCards = false; |
936 var shouldSetBackground = false; | 944 var shouldSetBackground = false; |
937 | 945 |
938 if (signedIn) { | 946 if (signedIn && notificationEnabled) { |
939 if (geolocationEnabled) { | 947 if (geolocationEnabled) { |
940 if (enableBackground) | 948 if (enableBackground) |
941 shouldSetBackground = true; | 949 shouldSetBackground = true; |
942 | 950 |
943 shouldPollCards = true; | 951 shouldPollCards = true; |
944 } | 952 } |
945 } else { | 953 } else { |
946 recordEvent(GoogleNowEvent.STOPPED); | 954 recordEvent(GoogleNowEvent.STOPPED); |
947 } | 955 } |
948 | 956 |
(...skipping 11 matching lines...) Expand all Loading... | |
960 */ | 968 */ |
961 function onStateChange() { | 969 function onStateChange() { |
962 tasks.add(STATE_CHANGED_TASK_NAME, function() { | 970 tasks.add(STATE_CHANGED_TASK_NAME, function() { |
963 authenticationManager.isSignedIn(function(token) { | 971 authenticationManager.isSignedIn(function(token) { |
964 var signedIn = !!token; | 972 var signedIn = !!token; |
965 instrumented.metricsPrivate.getVariationParams( | 973 instrumented.metricsPrivate.getVariationParams( |
966 'GoogleNow', | 974 'GoogleNow', |
967 function(response) { | 975 function(response) { |
968 var enableBackground = | 976 var enableBackground = |
969 (!response || (response.enableBackground != 'false')); | 977 (!response || (response.enableBackground != 'false')); |
970 instrumented. | 978 instrumented.notifications.getPermissionLevel(function(level) { |
979 var notificationEnabled = (level == 'granted'); | |
980 instrumented. | |
971 preferencesPrivate. | 981 preferencesPrivate. |
972 googleGeolocationAccessEnabled. | 982 googleGeolocationAccessEnabled. |
973 get({}, function(prefValue) { | 983 get({}, function(prefValue) { |
974 var geolocationEnabled = !!prefValue.value; | 984 var geolocationEnabled = !!prefValue.value; |
975 updateRunningState( | 985 updateRunningState( |
976 signedIn, | 986 signedIn, |
977 geolocationEnabled, | 987 geolocationEnabled, |
978 enableBackground); | 988 enableBackground, |
989 notificationEnabled); | |
979 }); | 990 }); |
991 }); | |
980 }); | 992 }); |
981 }); | 993 }); |
982 }); | 994 }); |
983 } | 995 } |
984 | 996 |
985 instrumented.runtime.onInstalled.addListener(function(details) { | 997 instrumented.runtime.onInstalled.addListener(function(details) { |
986 console.log('onInstalled ' + JSON.stringify(details)); | 998 console.log('onInstalled ' + JSON.stringify(details)); |
987 if (details.reason != 'chrome_update') { | 999 if (details.reason != 'chrome_update') { |
988 initialize(); | 1000 initialize(); |
989 } | 1001 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1037 'GoogleNow.ButtonClicked' + buttonIndex); | 1049 'GoogleNow.ButtonClicked' + buttonIndex); |
1038 onNotificationClicked(chromeNotificationId, function(actionUrls) { | 1050 onNotificationClicked(chromeNotificationId, function(actionUrls) { |
1039 var url = actionUrls.buttonUrls[buttonIndex]; | 1051 var url = actionUrls.buttonUrls[buttonIndex]; |
1040 verify(url, 'onButtonClicked: no url for a button'); | 1052 verify(url, 'onButtonClicked: no url for a button'); |
1041 return url; | 1053 return url; |
1042 }); | 1054 }); |
1043 }); | 1055 }); |
1044 | 1056 |
1045 instrumented.notifications.onClosed.addListener(onNotificationClosed); | 1057 instrumented.notifications.onClosed.addListener(onNotificationClosed); |
1046 | 1058 |
1059 instrumented.notifications.onPermissionLevelChanged.addListener( | |
1060 function(permissionLevel) { | |
1061 console.log('Notifications permissionLevel Change'); | |
1062 onStateChange(); | |
1063 }); | |
1064 | |
1047 instrumented.location.onLocationUpdate.addListener(function(position) { | 1065 instrumented.location.onLocationUpdate.addListener(function(position) { |
1048 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 1066 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
1049 updateNotificationsCards(position); | 1067 updateNotificationsCards(position); |
1050 }); | 1068 }); |
1051 | 1069 |
1052 instrumented.pushMessaging.onMessage.addListener(function(message) { | 1070 instrumented.pushMessaging.onMessage.addListener(function(message) { |
1053 // message.payload will be '' when the extension first starts. | 1071 // message.payload will be '' when the extension first starts. |
1054 // Each time after signing in, we'll get latest payload for all channels. | 1072 // Each time after signing in, we'll get latest payload for all channels. |
1055 // So, we need to poll the server only when the payload is non-empty and has | 1073 // So, we need to poll the server only when the payload is non-empty and has |
1056 // changed. | 1074 // changed. |
1057 console.log('pushMessaging.onMessage ' + JSON.stringify(message)); | 1075 console.log('pushMessaging.onMessage ' + JSON.stringify(message)); |
1058 if (message.subchannelId == SUBCHANNEL_ID_POLL_NOW && message.payload) { | 1076 if (message.subchannelId == SUBCHANNEL_ID_POLL_NOW && message.payload) { |
1059 tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { | 1077 tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { |
1060 instrumented.storage.local.get('lastPollNowPayload', function(items) { | 1078 instrumented.storage.local.get('lastPollNowPayload', function(items) { |
1061 if (items && items.lastPollNowPayload != message.payload) { | 1079 if (items && items.lastPollNowPayload != message.payload) { |
1062 chrome.storage.local.set({lastPollNowPayload: message.payload}); | 1080 chrome.storage.local.set({lastPollNowPayload: message.payload}); |
1063 | 1081 |
1064 updateCardsAttempts.isRunning(function(running) { | 1082 updateCardsAttempts.isRunning(function(running) { |
1065 if (running) | 1083 if (running) |
1066 requestNotificationGroups([]); | 1084 requestNotificationGroups([]); |
1067 }); | 1085 }); |
1068 } | 1086 } |
1069 }); | 1087 }); |
1070 }); | 1088 }); |
1071 } | 1089 } |
1072 }); | 1090 }); |
OLD | NEW |