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

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: CR Feedback 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
« no previous file with comments | « chrome/browser/resources/google_now/common_test_util.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 })(); 430 })();
431 431
432 wrapper.instrumentChromeApiFunction('alarms.get', 1); 432 wrapper.instrumentChromeApiFunction('alarms.get', 1);
433 wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0); 433 wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0);
434 wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1); 434 wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1);
435 wrapper.instrumentChromeApiFunction('identity.onSignInChanged.addListener', 0); 435 wrapper.instrumentChromeApiFunction('identity.onSignInChanged.addListener', 0);
436 wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1); 436 wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1);
437 wrapper.instrumentChromeApiFunction('webstorePrivate.getBrowserLogin', 0); 437 wrapper.instrumentChromeApiFunction('webstorePrivate.getBrowserLogin', 0);
438 438
439 /** 439 /**
440 * Add task tracking support to Promises. 440 * Add task tracking support to Promise.then.
441 * @override 441 * @override
442 */ 442 */
443 Promise.prototype.then = function() { 443 Promise.prototype.then = function() {
444 var originalThen = Promise.prototype.then; 444 var originalThen = Promise.prototype.then;
445 return function(callback) { 445 return function(callback) {
446 originalThen.call(this, wrapper.wrapCallback(callback, false)); 446 return originalThen.call(this, wrapper.wrapCallback(callback, false));
447 } 447 }
448 }(); 448 }();
449 449
450 /**
451 * Add task tracking support to Promise.catch.
452 * @override
453 */
454 Promise.prototype.catch = function() {
455 var originalCatch = Promise.prototype.catch;
456 return function(callback) {
457 return originalCatch.call(this, wrapper.wrapCallback(callback, false));
458 }
459 }();
460
450 /** 461 /**
451 * Builds the object to manage tasks (mutually exclusive chains of events). 462 * Builds the object to manage tasks (mutually exclusive chains of events).
452 * @param {function(string, string): boolean} areConflicting Function that 463 * @param {function(string, string): boolean} areConflicting Function that
453 * checks if a new task can't be added to a task queue that contains an 464 * checks if a new task can't be added to a task queue that contains an
454 * existing task. 465 * existing task.
455 * @return {Object} Task manager interface. 466 * @return {Object} Task manager interface.
456 */ 467 */
457 function buildTaskManager(areConflicting) { 468 function buildTaskManager(areConflicting) {
458 /** 469 /**
459 * Queue of scheduled tasks. The first element, if present, corresponds to the 470 * Queue of scheduled tasks. The first element, if present, corresponds to the
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 /** 730 /**
720 * Wraps chrome.identity to provide limited listening support for 731 * Wraps chrome.identity to provide limited listening support for
721 * the sign in state by polling periodically for the auth token. 732 * the sign in state by polling periodically for the auth token.
722 * @return {Object} The Authentication Manager interface. 733 * @return {Object} The Authentication Manager interface.
723 */ 734 */
724 function buildAuthenticationManager() { 735 function buildAuthenticationManager() {
725 var alarmName = 'sign-in-alarm'; 736 var alarmName = 'sign-in-alarm';
726 737
727 /** 738 /**
728 * Gets an OAuth2 access token. 739 * Gets an OAuth2 access token.
729 * @param {function(string=)} callback Called on completion. 740 * @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. 741 * no token, the request is rejected.
731 */ 742 */
732 function getAuthToken(callback) { 743 function getAuthToken() {
733 instrumented.identity.getAuthToken({interactive: false}, function(token) { 744 return new Promise(function(resolve, reject) {
734 token = chrome.runtime.lastError ? undefined : token; 745 instrumented.identity.getAuthToken({interactive: false}, function(token) {
735 callback(token); 746 if (chrome.runtime.lastError || !token) {
747 reject();
748 } else {
749 resolve(token);
750 }
751 });
736 }); 752 });
737 } 753 }
738 754
739 /** 755 /**
740 * Determines whether there is an account attached to the profile. 756 * Determines whether there is an account attached to the profile.
741 * @param {function(boolean)} callback Called on completion. 757 * @return {Promise} A promise to determine if there is an account attached
758 * to the profile.
742 */ 759 */
743 function isSignedIn(callback) { 760 function isSignedIn() {
744 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) { 761 return new Promise(function(resolve) {
745 callback(!!accountInfo.login); 762 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) {
763 resolve(!!accountInfo.login);
764 });
746 }); 765 });
747 } 766 }
748 767
749 /** 768 /**
750 * Removes the specified cached token. 769 * Removes the specified cached token.
751 * @param {string} token Authentication Token to remove from the cache. 770 * @param {string} token Authentication Token to remove from the cache.
752 * @param {function()} callback Called on completion. 771 * @return {Promise} A promise that resolves on completion.
753 */ 772 */
754 function removeToken(token, callback) { 773 function removeToken(token) {
755 instrumented.identity.removeCachedAuthToken({token: token}, function() { 774 return new Promise(function(resolve) {
756 // Let Chrome now about a possible problem with the token. 775 instrumented.identity.removeCachedAuthToken({token: token}, function() {
757 getAuthToken(function() {}); 776 // Let Chrome now about a possible problem with the token.
rgustafson 2014/02/14 02:12:07 know
robliao 2014/02/14 02:28:25 Done.
758 callback(); 777 getAuthToken();
778 resolve();
779 });
759 }); 780 });
760 } 781 }
761 782
762 var listeners = []; 783 var listeners = [];
763 784
764 /** 785 /**
765 * Registers a listener that gets called back when the signed in state 786 * Registers a listener that gets called back when the signed in state
766 * is found to be changed. 787 * is found to be changed.
767 * @param {function()} callback Called when the answer to isSignedIn changes. 788 * @param {function()} callback Called when the answer to isSignedIn changes.
768 */ 789 */
769 function addListener(callback) { 790 function addListener(callback) {
770 listeners.push(callback); 791 listeners.push(callback);
771 } 792 }
772 793
773 /** 794 /**
774 * Checks if the last signed in state matches the current one. 795 * Checks if the last signed in state matches the current one.
775 * If it doesn't, it notifies the listeners of the change. 796 * If it doesn't, it notifies the listeners of the change.
776 */ 797 */
777 function checkAndNotifyListeners() { 798 function checkAndNotifyListeners() {
778 isSignedIn(function(signedIn) { 799 isSignedIn().then(function(signedIn) {
779 instrumented.storage.local.get('lastSignedInState', function(items) { 800 instrumented.storage.local.get('lastSignedInState', function(items) {
780 items = items || {}; 801 items = items || {};
781 if (items.lastSignedInState != signedIn) { 802 if (items.lastSignedInState != signedIn) {
782 chrome.storage.local.set( 803 chrome.storage.local.set(
783 {lastSignedInState: signedIn}); 804 {lastSignedInState: signedIn});
784 listeners.forEach(function(callback) { 805 listeners.forEach(function(callback) {
785 callback(); 806 callback();
786 }); 807 });
787 } 808 }
788 }); 809 });
(...skipping 13 matching lines...) Expand all
802 // One hour is just an arbitrary amount of time chosen. 823 // One hour is just an arbitrary amount of time chosen.
803 chrome.alarms.create(alarmName, {periodInMinutes: 60}); 824 chrome.alarms.create(alarmName, {periodInMinutes: 60});
804 825
805 return { 826 return {
806 addListener: addListener, 827 addListener: addListener,
807 getAuthToken: getAuthToken, 828 getAuthToken: getAuthToken,
808 isSignedIn: isSignedIn, 829 isSignedIn: isSignedIn,
809 removeToken: removeToken 830 removeToken: removeToken
810 }; 831 };
811 } 832 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/google_now/common_test_util.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698