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

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

Issue 166033010: Convert Google Now's use of chrome.local.storage.get to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase to r252941 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/utility.js
diff --git a/chrome/browser/resources/google_now/utility.js b/chrome/browser/resources/google_now/utility.js
index 5a0042a9a7b3eda55253b6ad49d6a744b96e615a..1e72b3f73e61d4031480e36a1e95c857f9bb8748 100644
--- a/chrome/browser/resources/google_now/utility.js
+++ b/chrome/browser/resources/google_now/utility.js
@@ -368,7 +368,7 @@ var wrapper = (function() {
wrapperPluginInstance.prologue();
// Call the original callback.
- callback.apply(null, arguments);
+ var returnValue = callback.apply(null, arguments);
if (wrapperPluginInstance)
wrapperPluginInstance.epilogue();
@@ -376,6 +376,8 @@ var wrapper = (function() {
verify(isInWrappedCallback,
'Instrumented callback is not instrumented upon exit');
isInWrappedCallback = false;
+
+ return returnValue;
} catch (error) {
reportError(error);
}
@@ -479,6 +481,7 @@ wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0);
wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1);
wrapper.instrumentChromeApiFunction('identity.onSignInChanged.addListener', 0);
wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1);
+wrapper.instrumentChromeApiFunction('storage.local.get', 1);
wrapper.instrumentChromeApiFunction('webstorePrivate.getBrowserLogin', 0);
/**
@@ -534,6 +537,43 @@ Promise.prototype.catch = function() {
}();
/**
+ * Control promise rejection.
+ * @enum {boolean}
+ */
+var PromiseRejection = {
+ /** Disallow promise rejection */
+ DISALLOW: false,
+ /** Allow promise rejection */
+ ALLOW: true
+};
+
+/**
+ * Provides the promise equivalent of instrumented.storage.local.get.
+ * @param {Object} defaultStorageObject Default storage object to fill.
+ * @param {boolean} opt_allowPromiseRejection If PromiseRejection.ALLOW,
+ * allow promise rejection on errors, otherwise the default storage
+ * object is resolved.
+ * @return {Promise} A promise that fills the default storage object. On
+ * failure, if promise rejection is allowed, the promise is rejected,
+ * otherwise it is resolved to the default storage object.
+ */
+function fillFromChromeLocalStorage(
+ defaultStorageObject,
+ opt_allowPromiseRejection) {
+ return new Promise(function(resolve, reject) {
+ instrumented.storage.local.get(defaultStorageObject, function(items) {
+ if (items) {
+ resolve(items);
+ } else if (opt_allowPromiseRejection === PromiseRejection.ALLOW) {
+ reject();
+ } else {
+ resolve(defaultStorageObject);
+ }
+ });
+ });
+}
+
+/**
* Promise Pending Callback Data. Counts outstanding "then" and "catch"
* callbacks;
*
@@ -824,11 +864,9 @@ function buildAttemptManager(
* the planning is done.
*/
function planForNext(callback) {
- instrumented.storage.local.get(currentDelayStorageKey, function(items) {
- if (!items) {
- items = {};
- items[currentDelayStorageKey] = maximumDelaySeconds;
- }
+ var request = {};
+ request[currentDelayStorageKey] = maximumDelaySeconds;
+ fillFromChromeLocalStorage(request).then(function(items) {
console.log('planForNext-get-storage ' + JSON.stringify(items));
scheduleNextAttempt(items[currentDelayStorageKey]);
callback();
@@ -922,17 +960,17 @@ function buildAuthenticationManager() {
*/
function checkAndNotifyListeners() {
isSignedIn().then(function(signedIn) {
- instrumented.storage.local.get('lastSignedInState', function(items) {
- items = items || {};
- if (items.lastSignedInState != signedIn) {
- chrome.storage.local.set(
- {lastSignedInState: signedIn});
- listeners.forEach(function(callback) {
- callback();
- });
- }
+ fillFromChromeLocalStorage({lastSignedInState: undefined})
+ .then(function(items) {
+ if (items.lastSignedInState != signedIn) {
+ chrome.storage.local.set(
+ {lastSignedInState: signedIn});
+ listeners.forEach(function(callback) {
+ callback();
+ });
+ }
+ });
});
- });
}
instrumented.identity.onSignInChanged.addListener(function() {

Powered by Google App Engine
This is Rietveld 408576698