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 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1080 setBackgroundEnable(shouldSetBackground); | 1080 setBackgroundEnable(shouldSetBackground); |
1081 setShouldPollCards(shouldPollCards); | 1081 setShouldPollCards(shouldPollCards); |
1082 } | 1082 } |
1083 | 1083 |
1084 /** | 1084 /** |
1085 * Coordinates the behavior of Google Now for Chrome depending on | 1085 * Coordinates the behavior of Google Now for Chrome depending on |
1086 * Chrome and extension state. | 1086 * Chrome and extension state. |
1087 */ | 1087 */ |
1088 function onStateChange() { | 1088 function onStateChange() { |
1089 tasks.add(STATE_CHANGED_TASK_NAME, function() { | 1089 tasks.add(STATE_CHANGED_TASK_NAME, function() { |
| 1090 Promise.all([ |
| 1091 isSignedIn(), |
| 1092 isGeolocationEnabled(), |
| 1093 canEnableBackground(), |
| 1094 isNotificationsEnabled(), |
| 1095 isGoogleNowEnabled()]) |
| 1096 .then(function(results) { |
| 1097 updateRunningState.apply(null, results); |
| 1098 }); |
| 1099 }); |
| 1100 } |
| 1101 |
| 1102 /** |
| 1103 * Determines if the user is signed in. |
| 1104 * @return {Promise} A promise to evaluate the signed in state. |
| 1105 */ |
| 1106 function isSignedIn() { |
| 1107 return new Promise(function(resolve) { |
1090 authenticationManager.isSignedIn(function(signedIn) { | 1108 authenticationManager.isSignedIn(function(signedIn) { |
1091 instrumented.metricsPrivate.getVariationParams( | 1109 resolve(signedIn); |
1092 'GoogleNow', | |
1093 function(response) { | |
1094 var canEnableBackground = | |
1095 (!response || (response.canEnableBackground != 'false')); | |
1096 instrumented.notifications.getPermissionLevel(function(level) { | |
1097 var notificationEnabled = (level == 'granted'); | |
1098 instrumented. | |
1099 preferencesPrivate. | |
1100 googleGeolocationAccessEnabled. | |
1101 get({}, function(prefValue) { | |
1102 var geolocationEnabled = !!prefValue.value; | |
1103 instrumented.storage.local.get( | |
1104 'googleNowEnabled', | |
1105 function(items) { | |
1106 var googleNowEnabled = | |
1107 items && !!items.googleNowEnabled; | |
1108 updateRunningState( | |
1109 signedIn, | |
1110 geolocationEnabled, | |
1111 canEnableBackground, | |
1112 notificationEnabled, | |
1113 googleNowEnabled); | |
1114 }); | |
1115 }); | |
1116 }); | |
1117 }); | |
1118 }); | 1110 }); |
1119 }); | 1111 }); |
1120 } | 1112 } |
| 1113 |
| 1114 /** |
| 1115 * Gets the geolocation enabled preference. |
| 1116 * @return {Promise} A promise to get the geolocation enabled preference. |
| 1117 */ |
| 1118 function isGeolocationEnabled() { |
| 1119 return new Promise(function(resolve) { |
| 1120 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get( |
| 1121 {}, |
| 1122 function(prefValue) { |
| 1123 resolve(!!prefValue.value); |
| 1124 }); |
| 1125 }); |
| 1126 } |
| 1127 |
| 1128 /** |
| 1129 * Determines if background mode should be requested. |
| 1130 * @return {Promise} A promise to determine if background can be enabled. |
| 1131 */ |
| 1132 function canEnableBackground() { |
| 1133 return new Promise(function(resolve) { |
| 1134 instrumented.metricsPrivate.getVariationParams( |
| 1135 'GoogleNow', |
| 1136 function(response) { |
| 1137 resolve(!response || (response.canEnableBackground != 'false')); |
| 1138 }); |
| 1139 }); |
| 1140 } |
| 1141 |
| 1142 /** |
| 1143 * Checks if Google Now is enabled in the notifications center. |
| 1144 * @return {Promise} A promise to determine if Google Now is enabled |
| 1145 * in the notifications center. |
| 1146 */ |
| 1147 function isNotificationsEnabled() { |
| 1148 return new Promise(function(resolve) { |
| 1149 instrumented.notifications.getPermissionLevel(function(level) { |
| 1150 resolve(level == 'granted'); |
| 1151 }); |
| 1152 }); |
| 1153 } |
| 1154 |
| 1155 /** |
| 1156 * Gets the previous Google Now opt-in state. |
| 1157 * @return {Promise} A promise to determine the previous Google Now |
| 1158 * opt-in state. |
| 1159 */ |
| 1160 function isGoogleNowEnabled() { |
| 1161 return new Promise(function(resolve) { |
| 1162 instrumented.storage.local.get('googleNowEnabled', function(items) { |
| 1163 resolve(items && !!items.googleNowEnabled); |
| 1164 }); |
| 1165 }); |
| 1166 } |
1121 | 1167 |
1122 instrumented.runtime.onInstalled.addListener(function(details) { | 1168 instrumented.runtime.onInstalled.addListener(function(details) { |
1123 console.log('onInstalled ' + JSON.stringify(details)); | 1169 console.log('onInstalled ' + JSON.stringify(details)); |
1124 if (details.reason != 'chrome_update') { | 1170 if (details.reason != 'chrome_update') { |
1125 initialize(); | 1171 initialize(); |
1126 } | 1172 } |
1127 }); | 1173 }); |
1128 | 1174 |
1129 instrumented.runtime.onStartup.addListener(function() { | 1175 instrumented.runtime.onStartup.addListener(function() { |
1130 console.log('onStartup'); | 1176 console.log('onStartup'); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1232 lastPollNowPayloads: items.lastPollNowPayloads, | 1278 lastPollNowPayloads: items.lastPollNowPayloads, |
1233 notificationGroups: items.notificationGroups | 1279 notificationGroups: items.notificationGroups |
1234 }); | 1280 }); |
1235 | 1281 |
1236 updateNotificationsCards(); | 1282 updateNotificationsCards(); |
1237 } | 1283 } |
1238 }); | 1284 }); |
1239 }); | 1285 }); |
1240 } | 1286 } |
1241 }); | 1287 }); |
OLD | NEW |