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

Unified Diff: chrome/browser/resources/google_now/background.js

Issue 158003003: Convert Google Now's State Change Gathering Mechanism to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Presubmit Created 6 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/google_now/background.js
diff --git a/chrome/browser/resources/google_now/background.js b/chrome/browser/resources/google_now/background.js
index 400409e630bec2dc23485853748e5ab47a3f700a..bd8042299efed252bff972ff4b32512bca1efd19 100644
--- a/chrome/browser/resources/google_now/background.js
+++ b/chrome/browser/resources/google_now/background.js
@@ -1087,34 +1087,80 @@ function updateRunningState(
*/
function onStateChange() {
tasks.add(STATE_CHANGED_TASK_NAME, function() {
+ Promise.all([
+ isSignedIn(),
skare_ 2014/02/11 20:07:44 consider .all([ new Promise(isSignedIn), new P
robliao 2014/02/11 21:53:21 An async operation hands out promises to encapsula
skare_ 2014/02/12 19:55:58 ok if it makes things easier later; the suggestion
robliao 2014/02/12 20:57:25 That's one of them. The other is here: http://wiki
+ isGeolocationEnabled(),
+ canEnableBackground(),
+ isNotificationsEnabled(),
+ isGoogleNowEnabled()])
+ .then(function(results) {
+ updateRunningState.apply(null, results);
+ });
+ });
+}
+
+/**
+ * Determines if the user is signed in.
+ * @return {Promise} A promise to evaluate the signed in state.
+ */
+function isSignedIn() {
+ return new Promise(function(onSuccess) {
authenticationManager.isSignedIn(function(signedIn) {
- instrumented.metricsPrivate.getVariationParams(
- 'GoogleNow',
- function(response) {
- var canEnableBackground =
- (!response || (response.canEnableBackground != 'false'));
- instrumented.notifications.getPermissionLevel(function(level) {
- var notificationEnabled = (level == 'granted');
- instrumented.
- preferencesPrivate.
- googleGeolocationAccessEnabled.
- get({}, function(prefValue) {
- var geolocationEnabled = !!prefValue.value;
- instrumented.storage.local.get(
- 'googleNowEnabled',
- function(items) {
- var googleNowEnabled =
- items && !!items.googleNowEnabled;
- updateRunningState(
- signedIn,
- geolocationEnabled,
- canEnableBackground,
- notificationEnabled,
- googleNowEnabled);
- });
- });
- });
- });
+ onSuccess(signedIn);
+ });
+ });
+}
+
+/**
+ * Gets the geolocation enabled preference.
+ * @return {Promise} A promise to get the geolocation enabled preference.
+ */
+function isGeolocationEnabled() {
skare_ 2014/02/11 20:07:44 I don't know what the best chrome/google promises
robliao 2014/02/11 21:53:21 A promise only provides one evaluation. Each new e
+ return new Promise(function(onSuccess) {
+ instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get(
+ {},
+ function(prefValue) {
+ onSuccess(!!prefValue.value);
+ });
+ });
+}
+
+/**
+ * Determines if background mode should be requested.
+ * @return {Promise} A promise to determine if background can be enabled.
+ */
+function canEnableBackground() {
+ return new Promise(function(onSuccess) {
+ instrumented.metricsPrivate.getVariationParams(
+ 'GoogleNow',
+ function(response) {
+ onSuccess(!response || (response.canEnableBackground != 'false'));
+ });
+ });
+}
+
+/**
+ * Checks if Google Now is enabled in the notifications center.
+ * @return {Promise} A promise to determine if Google Now is enabled
+ * in the notifications center.
+ */
+function isNotificationsEnabled() {
+ return new Promise(function(onSuccess) {
+ instrumented.notifications.getPermissionLevel(function(level) {
+ onSuccess(level == 'granted');
+ });
+ });
+}
+
+/**
+ * Gets the previous Google Now opt-in state.
+ * @return {Promise} A promise to determine the previous Google Now
+ * opt-in state.
+ */
+function isGoogleNowEnabled() {
+ return new Promise(function(onSuccess) {
+ instrumented.storage.local.get('googleNowEnabled', function(items) {
+ onSuccess(items && !!items.googleNowEnabled);
});
});
}
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/common_test_util.js » ('j') | chrome/browser/resources/google_now/utility.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698