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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 * Time we keep dismissals after successful server dismiss requests. | 72 * Time we keep dismissals after successful server dismiss requests. |
73 */ | 73 */ |
74 var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes | 74 var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes |
75 | 75 |
76 /** | 76 /** |
77 * Default period for checking whether the user is opted in to Google Now. | 77 * Default period for checking whether the user is opted in to Google Now. |
78 */ | 78 */ |
79 var DEFAULT_OPTIN_CHECK_PERIOD_SECONDS = 60 * 60 * 24 * 7; // 1 week | 79 var DEFAULT_OPTIN_CHECK_PERIOD_SECONDS = 60 * 60 * 24 * 7; // 1 week |
80 | 80 |
81 /** | 81 /** |
82 * URL to open when the user clicked on a link for the our notification | |
83 * settings. | |
84 */ | |
85 var SETTINGS_URL = 'https://support.google.com/chrome/?p=ib_google_now_welcome'; | |
robliao
2013/11/01 22:29:35
Watch out for merge conflicts here.
| |
86 | |
87 /** | |
82 * Names for tasks that can be created by the extension. | 88 * Names for tasks that can be created by the extension. |
83 */ | 89 */ |
84 var UPDATE_CARDS_TASK_NAME = 'update-cards'; | 90 var UPDATE_CARDS_TASK_NAME = 'update-cards'; |
85 var DISMISS_CARD_TASK_NAME = 'dismiss-card'; | 91 var DISMISS_CARD_TASK_NAME = 'dismiss-card'; |
86 var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; | 92 var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; |
87 var STATE_CHANGED_TASK_NAME = 'state-changed'; | 93 var STATE_CHANGED_TASK_NAME = 'state-changed'; |
88 var SHOW_ON_START_TASK_NAME = 'show-cards-on-start'; | 94 var SHOW_ON_START_TASK_NAME = 'show-cards-on-start'; |
89 var ON_PUSH_MESSAGE_START_TASK_NAME = 'on-push-message'; | 95 var ON_PUSH_MESSAGE_START_TASK_NAME = 'on-push-message'; |
90 | 96 |
91 var LOCATION_WATCH_NAME = 'location-watch'; | 97 var LOCATION_WATCH_NAME = 'location-watch'; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 wrapper.instrumentChromeApiFunction('notifications.getPermissionLevel', 0); | 164 wrapper.instrumentChromeApiFunction('notifications.getPermissionLevel', 0); |
159 wrapper.instrumentChromeApiFunction('notifications.update', 2); | 165 wrapper.instrumentChromeApiFunction('notifications.update', 2); |
160 wrapper.instrumentChromeApiFunction('notifications.getAll', 0); | 166 wrapper.instrumentChromeApiFunction('notifications.getAll', 0); |
161 wrapper.instrumentChromeApiFunction( | 167 wrapper.instrumentChromeApiFunction( |
162 'notifications.onButtonClicked.addListener', 0); | 168 'notifications.onButtonClicked.addListener', 0); |
163 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); | 169 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); |
164 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); | 170 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); |
165 wrapper.instrumentChromeApiFunction( | 171 wrapper.instrumentChromeApiFunction( |
166 'notifications.onPermissionLevelChanged.addListener', 0); | 172 'notifications.onPermissionLevelChanged.addListener', 0); |
167 wrapper.instrumentChromeApiFunction( | 173 wrapper.instrumentChromeApiFunction( |
174 'notifications.onShowSettings.addListener', 0); | |
175 wrapper.instrumentChromeApiFunction( | |
168 'preferencesPrivate.googleGeolocationAccessEnabled.get', | 176 'preferencesPrivate.googleGeolocationAccessEnabled.get', |
169 1); | 177 1); |
170 wrapper.instrumentChromeApiFunction( | 178 wrapper.instrumentChromeApiFunction( |
171 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', | 179 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', |
172 0); | 180 0); |
173 wrapper.instrumentChromeApiFunction('permissions.contains', 1); | 181 wrapper.instrumentChromeApiFunction('permissions.contains', 1); |
174 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); | 182 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); |
175 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 183 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
176 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 184 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
177 wrapper.instrumentChromeApiFunction('tabs.create', 1); | 185 wrapper.instrumentChromeApiFunction('tabs.create', 1); |
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
835 */ | 843 */ |
836 function retryPendingDismissals() { | 844 function retryPendingDismissals() { |
837 tasks.add(RETRY_DISMISS_TASK_NAME, function() { | 845 tasks.add(RETRY_DISMISS_TASK_NAME, function() { |
838 dismissalAttempts.planForNext(function() { | 846 dismissalAttempts.planForNext(function() { |
839 processPendingDismissals(function(success) {}); | 847 processPendingDismissals(function(success) {}); |
840 }); | 848 }); |
841 }); | 849 }); |
842 } | 850 } |
843 | 851 |
844 /** | 852 /** |
853 * Opens a URL in a new tab. | |
854 * @param {string} url URL to open. | |
855 */ | |
856 function openUrl(url) { | |
857 instrumented.tabs.create({url: url}, function(tab) { | |
858 if (tab) | |
859 chrome.windows.update(tab.windowId, {focused: true}); | |
860 else | |
861 chrome.windows.create({url: url, focused: true}); | |
862 }); | |
863 } | |
864 | |
865 /** | |
845 * Opens URL corresponding to the clicked part of the notification. | 866 * Opens URL corresponding to the clicked part of the notification. |
846 * @param {string} chromeNotificationId chrome.notifications ID of the card. | 867 * @param {string} chromeNotificationId chrome.notifications ID of the card. |
847 * @param {function(Object): string} selector Function that extracts the url for | 868 * @param {function(Object): string} selector Function that extracts the url for |
848 * the clicked area from the button action URLs info. | 869 * the clicked area from the button action URLs info. |
849 */ | 870 */ |
850 function onNotificationClicked(chromeNotificationId, selector) { | 871 function onNotificationClicked(chromeNotificationId, selector) { |
851 instrumented.storage.local.get('notificationsData', function(items) { | 872 instrumented.storage.local.get('notificationsData', function(items) { |
852 var notificationData = items && | 873 var notificationData = items && |
853 items.notificationsData && | 874 items.notificationsData && |
854 items.notificationsData[chromeNotificationId]; | 875 items.notificationsData[chromeNotificationId]; |
855 | 876 |
856 if (!notificationData) | 877 if (!notificationData) |
857 return; | 878 return; |
858 | 879 |
859 var url = selector(notificationData.actionUrls); | 880 var url = selector(notificationData.actionUrls); |
860 if (!url) | 881 if (!url) |
861 return; | 882 return; |
862 | 883 |
863 instrumented.tabs.create({url: url}, function(tab) { | 884 openUrl(url); |
864 if (tab) | |
865 chrome.windows.update(tab.windowId, {focused: true}); | |
866 else | |
867 chrome.windows.create({url: url, focused: true}); | |
868 }); | |
869 }); | 885 }); |
870 } | 886 } |
871 | 887 |
872 /** | 888 /** |
873 * Callback for chrome.notifications.onClosed event. | 889 * Callback for chrome.notifications.onClosed event. |
874 * @param {string} chromeNotificationId chrome.notifications ID of the card. | 890 * @param {string} chromeNotificationId chrome.notifications ID of the card. |
875 * @param {boolean} byUser Whether the notification was closed by the user. | 891 * @param {boolean} byUser Whether the notification was closed by the user. |
876 */ | 892 */ |
877 function onNotificationClosed(chromeNotificationId, byUser) { | 893 function onNotificationClosed(chromeNotificationId, byUser) { |
878 if (!byUser) | 894 if (!byUser) |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1137 }); | 1153 }); |
1138 | 1154 |
1139 instrumented.notifications.onClosed.addListener(onNotificationClosed); | 1155 instrumented.notifications.onClosed.addListener(onNotificationClosed); |
1140 | 1156 |
1141 instrumented.notifications.onPermissionLevelChanged.addListener( | 1157 instrumented.notifications.onPermissionLevelChanged.addListener( |
1142 function(permissionLevel) { | 1158 function(permissionLevel) { |
1143 console.log('Notifications permissionLevel Change'); | 1159 console.log('Notifications permissionLevel Change'); |
1144 onStateChange(); | 1160 onStateChange(); |
1145 }); | 1161 }); |
1146 | 1162 |
1163 instrumented.notifications.onShowSettings.addListener(function() { | |
1164 openUrl(SETTINGS_URL); | |
1165 }); | |
1166 | |
1147 instrumented.location.onLocationUpdate.addListener(function(position) { | 1167 instrumented.location.onLocationUpdate.addListener(function(position) { |
1148 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 1168 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
1149 updateNotificationsCards(position); | 1169 updateNotificationsCards(position); |
1150 }); | 1170 }); |
1151 | 1171 |
1152 instrumented.pushMessaging.onMessage.addListener(function(message) { | 1172 instrumented.pushMessaging.onMessage.addListener(function(message) { |
1153 // message.payload will be '' when the extension first starts. | 1173 // message.payload will be '' when the extension first starts. |
1154 // Each time after signing in, we'll get latest payload for all channels. | 1174 // Each time after signing in, we'll get latest payload for all channels. |
1155 // So, we need to poll the server only when the payload is non-empty and has | 1175 // So, we need to poll the server only when the payload is non-empty and has |
1156 // changed. | 1176 // changed. |
(...skipping 24 matching lines...) Expand all Loading... | |
1181 lastPollNowPayloads: items.lastPollNowPayloads, | 1201 lastPollNowPayloads: items.lastPollNowPayloads, |
1182 notificationGroups: items.notificationGroups | 1202 notificationGroups: items.notificationGroups |
1183 }); | 1203 }); |
1184 | 1204 |
1185 updateNotificationsCards(); | 1205 updateNotificationsCards(); |
1186 } | 1206 } |
1187 }); | 1207 }); |
1188 }); | 1208 }); |
1189 } | 1209 } |
1190 }); | 1210 }); |
OLD | NEW |