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

Side by Side Diff: chrome/browser/resources/google_now/utility.js

Issue 162273002: Convert Google Now's Authentication Manager to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 Utility objects and functions for Google Now extension. 8 * @fileoverview Utility objects and functions for Google Now extension.
9 * Most important entities here: 9 * Most important entities here:
10 * (1) 'wrapper' is a module used to add error handling and other services to 10 * (1) 'wrapper' is a module used to add error handling and other services to
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 /** 719 /**
720 * Wraps chrome.identity to provide limited listening support for 720 * Wraps chrome.identity to provide limited listening support for
721 * the sign in state by polling periodically for the auth token. 721 * the sign in state by polling periodically for the auth token.
722 * @return {Object} The Authentication Manager interface. 722 * @return {Object} The Authentication Manager interface.
723 */ 723 */
724 function buildAuthenticationManager() { 724 function buildAuthenticationManager() {
725 var alarmName = 'sign-in-alarm'; 725 var alarmName = 'sign-in-alarm';
726 726
727 /** 727 /**
728 * Gets an OAuth2 access token. 728 * Gets an OAuth2 access token.
729 * @param {function(string=)} callback Called on completion. 729 * @return {Promise} A promise to get the authentication token. If there is
730 * The string contains the token. It's undefined if there was an error. 730 * no token, the request is rejected.
731 */ 731 */
732 function getAuthToken(callback) { 732 function getAuthToken() {
733 instrumented.identity.getAuthToken({interactive: false}, function(token) { 733 return new Promise(function(resolve, reject) {
734 token = chrome.runtime.lastError ? undefined : token; 734 instrumented.identity.getAuthToken({interactive: false}, function(token) {
735 callback(token); 735 if (chrome.runtime.lastError || !token) {
736 reject();
737 } else {
738 resolve(token);
739 }
740 });
736 }); 741 });
737 } 742 }
738 743
739 /** 744 /**
740 * Determines whether there is an account attached to the profile. 745 * Determines whether there is an account attached to the profile.
741 * @param {function(boolean)} callback Called on completion. 746 * @return {Promise} A promise to determine if there is an account attached
747 * to the profile.
742 */ 748 */
743 function isSignedIn(callback) { 749 function isSignedIn() {
744 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) { 750 return new Promise(function(resolve) {
745 callback(!!accountInfo.login); 751 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) {
752 resolve(!!accountInfo.login);
753 });
746 }); 754 });
747 } 755 }
748 756
749 /** 757 /**
750 * Removes the specified cached token. 758 * Removes the specified cached token.
751 * @param {string} token Authentication Token to remove from the cache. 759 * @param {string} token Authentication Token to remove from the cache.
752 * @param {function()} callback Called on completion. 760 * @return {Promise} A promise that resolves on completion.
753 */ 761 */
754 function removeToken(token, callback) { 762 function removeToken(token) {
755 instrumented.identity.removeCachedAuthToken({token: token}, function() { 763 return new Promise(function(resolve) {
756 // Let Chrome now about a possible problem with the token. 764 instrumented.identity.removeCachedAuthToken({token: token}, function() {
rgustafson 2014/02/13 21:31:40 Keep the comment in, but now -> know. It's not imm
robliao 2014/02/13 21:41:33 Done.
757 getAuthToken(function() {}); 765 getAuthToken();
758 callback(); 766 resolve();
767 });
759 }); 768 });
760 } 769 }
761 770
762 var listeners = []; 771 var listeners = [];
763 772
764 /** 773 /**
765 * Registers a listener that gets called back when the signed in state 774 * Registers a listener that gets called back when the signed in state
766 * is found to be changed. 775 * is found to be changed.
767 * @param {function()} callback Called when the answer to isSignedIn changes. 776 * @param {function()} callback Called when the answer to isSignedIn changes.
768 */ 777 */
769 function addListener(callback) { 778 function addListener(callback) {
770 listeners.push(callback); 779 listeners.push(callback);
771 } 780 }
772 781
773 /** 782 /**
774 * Checks if the last signed in state matches the current one. 783 * Checks if the last signed in state matches the current one.
775 * If it doesn't, it notifies the listeners of the change. 784 * If it doesn't, it notifies the listeners of the change.
776 */ 785 */
777 function checkAndNotifyListeners() { 786 function checkAndNotifyListeners() {
778 isSignedIn(function(signedIn) { 787 isSignedIn(function(signedIn) {
rgustafson 2014/02/13 21:31:40 This doesn't have a parameter anymore. Needs more
robliao 2014/02/13 21:41:33 Indeed! Done. On 2014/02/13 21:31:40, rgustafson w
779 instrumented.storage.local.get('lastSignedInState', function(items) { 788 instrumented.storage.local.get('lastSignedInState', function(items) {
780 items = items || {}; 789 items = items || {};
781 if (items.lastSignedInState != signedIn) { 790 if (items.lastSignedInState != signedIn) {
782 chrome.storage.local.set( 791 chrome.storage.local.set(
783 {lastSignedInState: signedIn}); 792 {lastSignedInState: signedIn});
784 listeners.forEach(function(callback) { 793 listeners.forEach(function(callback) {
785 callback(); 794 callback();
786 }); 795 });
787 } 796 }
788 }); 797 });
(...skipping 13 matching lines...) Expand all
802 // One hour is just an arbitrary amount of time chosen. 811 // One hour is just an arbitrary amount of time chosen.
803 chrome.alarms.create(alarmName, {periodInMinutes: 60}); 812 chrome.alarms.create(alarmName, {periodInMinutes: 60});
804 813
805 return { 814 return {
806 addListener: addListener, 815 addListener: addListener,
807 getAuthToken: getAuthToken, 816 getAuthToken: getAuthToken,
808 isSignedIn: isSignedIn, 817 isSignedIn: isSignedIn,
809 removeToken: removeToken 818 removeToken: removeToken
810 }; 819 };
811 } 820 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698