| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 type: 'histogram-linear', | 244 type: 'histogram-linear', |
| 245 min: 1, | 245 min: 1, |
| 246 max: GoogleNowEvent.EVENTS_TOTAL, | 246 max: GoogleNowEvent.EVENTS_TOTAL, |
| 247 buckets: GoogleNowEvent.EVENTS_TOTAL + 1 | 247 buckets: GoogleNowEvent.EVENTS_TOTAL + 1 |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 chrome.metricsPrivate.recordValue(metricDescription, event); | 250 chrome.metricsPrivate.recordValue(metricDescription, event); |
| 251 } | 251 } |
| 252 | 252 |
| 253 /** | 253 /** |
| 254 * Records a notification clicked event. |
| 255 * @param {number|undefined} cardTypeId Card type ID. |
| 256 */ |
| 257 function recordNotificationClick(cardTypeId) { |
| 258 if (cardTypeId !== undefined) { |
| 259 chrome.metricsPrivate.recordSparseValue( |
| 260 'GoogleNow.Card.Clicked', cardTypeId); |
| 261 } |
| 262 } |
| 263 |
| 264 /** |
| 265 * Records a button clicked event. |
| 266 * @param {number|undefined} cardTypeId Card type ID. |
| 267 * @param {number} buttonIndex Button Index |
| 268 */ |
| 269 function recordButtonClick(cardTypeId, buttonIndex) { |
| 270 if (cardTypeId !== undefined) { |
| 271 chrome.metricsPrivate.recordSparseValue( |
| 272 'GoogleNow.Card.Button.Clicked' + buttonIndex, cardTypeId); |
| 273 } |
| 274 } |
| 275 |
| 276 /** |
| 254 * Checks the result of the HTTP Request and updates the authentication | 277 * Checks the result of the HTTP Request and updates the authentication |
| 255 * manager on any failure. | 278 * manager on any failure. |
| 256 * @param {string} token Authentication token to validate against an | 279 * @param {string} token Authentication token to validate against an |
| 257 * XMLHttpRequest. | 280 * XMLHttpRequest. |
| 258 * @return {function(XMLHttpRequest)} Function that validates the token with the | 281 * @return {function(XMLHttpRequest)} Function that validates the token with the |
| 259 * supplied XMLHttpRequest. | 282 * supplied XMLHttpRequest. |
| 260 */ | 283 */ |
| 261 function checkAuthenticationStatus(token) { | 284 function checkAuthenticationStatus(token) { |
| 262 return function(request) { | 285 return function(request) { |
| 263 if (request.status == HTTP_FORBIDDEN || | 286 if (request.status == HTTP_FORBIDDEN || |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 chrome.windows.update(tab.windowId, {focused: true}); | 819 chrome.windows.update(tab.windowId, {focused: true}); |
| 797 else | 820 else |
| 798 chrome.windows.create({url: url, focused: true}); | 821 chrome.windows.create({url: url, focused: true}); |
| 799 }); | 822 }); |
| 800 } | 823 } |
| 801 | 824 |
| 802 /** | 825 /** |
| 803 * Opens URL corresponding to the clicked part of the notification. | 826 * Opens URL corresponding to the clicked part of the notification. |
| 804 * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID of | 827 * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID of |
| 805 * the card. | 828 * the card. |
| 806 * @param {function((ActionUrls|undefined)): (string|undefined)} selector | 829 * @param {function(NotificationDataEntry): (string|undefined)} selector |
| 807 * Function that extracts the url for the clicked area from the button | 830 * Function that extracts the url for the clicked area from the |
| 808 * action URLs info. | 831 * notification data entry. |
| 809 */ | 832 */ |
| 810 function onNotificationClicked(chromeNotificationId, selector) { | 833 function onNotificationClicked(chromeNotificationId, selector) { |
| 811 fillFromChromeLocalStorage({ | 834 fillFromChromeLocalStorage({ |
| 812 /** @type {Object.<string, NotificationDataEntry>} */ | 835 /** @type {Object.<string, NotificationDataEntry>} */ |
| 813 notificationsData: {} | 836 notificationsData: {} |
| 814 }).then(function(items) { | 837 }).then(function(items) { |
| 815 /** @type {(NotificationDataEntry|undefined)} */ | 838 /** @type {(NotificationDataEntry|undefined)} */ |
| 816 var notificationData = items.notificationsData[chromeNotificationId]; | 839 var notificationDataEntry = items.notificationsData[chromeNotificationId]; |
| 817 if (!notificationData) | 840 if (!notificationDataEntry) |
| 818 return; | 841 return; |
| 819 | 842 |
| 820 var url = selector(notificationData.actionUrls); | 843 var url = selector(notificationDataEntry); |
| 821 if (!url) | 844 if (!url) |
| 822 return; | 845 return; |
| 823 | 846 |
| 824 openUrl(url); | 847 openUrl(url); |
| 825 }); | 848 }); |
| 826 } | 849 } |
| 827 | 850 |
| 828 /** | 851 /** |
| 829 * Callback for chrome.notifications.onClosed event. | 852 * Callback for chrome.notifications.onClosed event. |
| 830 * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID of | 853 * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID of |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1103 }); | 1126 }); |
| 1104 | 1127 |
| 1105 authenticationManager.addListener(function() { | 1128 authenticationManager.addListener(function() { |
| 1106 console.log('signIn State Change'); | 1129 console.log('signIn State Change'); |
| 1107 onStateChange(); | 1130 onStateChange(); |
| 1108 }); | 1131 }); |
| 1109 | 1132 |
| 1110 instrumented.notifications.onClicked.addListener( | 1133 instrumented.notifications.onClicked.addListener( |
| 1111 function(chromeNotificationId) { | 1134 function(chromeNotificationId) { |
| 1112 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); | 1135 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); |
| 1113 onNotificationClicked(chromeNotificationId, function(actionUrls) { | 1136 onNotificationClicked(chromeNotificationId, |
| 1114 return actionUrls && actionUrls.messageUrl; | 1137 function(notificationDataEntry) { |
| 1115 }); | 1138 var actionUrls = notificationDataEntry.actionUrls; |
| 1116 }); | 1139 var url = actionUrls && actionUrls.messageUrl; |
| 1140 if (url) { |
| 1141 recordNotificationClick(notificationDataEntry.cardTypeId); |
| 1142 } |
| 1143 return url; |
| 1144 }); |
| 1145 }); |
| 1117 | 1146 |
| 1118 instrumented.notifications.onButtonClicked.addListener( | 1147 instrumented.notifications.onButtonClicked.addListener( |
| 1119 function(chromeNotificationId, buttonIndex) { | 1148 function(chromeNotificationId, buttonIndex) { |
| 1120 chrome.metricsPrivate.recordUserAction( | 1149 chrome.metricsPrivate.recordUserAction( |
| 1121 'GoogleNow.ButtonClicked' + buttonIndex); | 1150 'GoogleNow.ButtonClicked' + buttonIndex); |
| 1122 onNotificationClicked(chromeNotificationId, function(actionUrls) { | 1151 onNotificationClicked(chromeNotificationId, |
| 1123 var url = actionUrls.buttonUrls[buttonIndex]; | 1152 function(notificationDataEntry) { |
| 1124 verify(url !== undefined, 'onButtonClicked: no url for a button'); | 1153 var actionUrls = notificationDataEntry.actionUrls; |
| 1125 return url; | 1154 var url = actionUrls.buttonUrls[buttonIndex]; |
| 1126 }); | 1155 if (url) { |
| 1127 }); | 1156 recordButtonClick(notificationDataEntry.cardTypeId, buttonIndex); |
| 1157 } else { |
| 1158 verify(false, 'onButtonClicked: no url for a button'); |
| 1159 } |
| 1160 return url; |
| 1161 }); |
| 1162 }); |
| 1128 | 1163 |
| 1129 instrumented.notifications.onClosed.addListener(onNotificationClosed); | 1164 instrumented.notifications.onClosed.addListener(onNotificationClosed); |
| 1130 | 1165 |
| 1131 instrumented.notifications.onPermissionLevelChanged.addListener( | 1166 instrumented.notifications.onPermissionLevelChanged.addListener( |
| 1132 function(permissionLevel) { | 1167 function(permissionLevel) { |
| 1133 console.log('Notifications permissionLevel Change'); | 1168 console.log('Notifications permissionLevel Change'); |
| 1134 onStateChange(); | 1169 onStateChange(); |
| 1135 }); | 1170 }); |
| 1136 | 1171 |
| 1137 instrumented.notifications.onShowSettings.addListener(function() { | 1172 instrumented.notifications.onShowSettings.addListener(function() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1166 lastPollNowPayloads: items.lastPollNowPayloads, | 1201 lastPollNowPayloads: items.lastPollNowPayloads, |
| 1167 notificationGroups: items.notificationGroups | 1202 notificationGroups: items.notificationGroups |
| 1168 }); | 1203 }); |
| 1169 | 1204 |
| 1170 requestCards(); | 1205 requestCards(); |
| 1171 } | 1206 } |
| 1172 }); | 1207 }); |
| 1173 }); | 1208 }); |
| 1174 } | 1209 } |
| 1175 }); | 1210 }); |
| OLD | NEW |