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 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 Loading... |
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 Promise.then. | 440 * Add task tracking support to Promises. |
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 return originalThen.call(this, wrapper.wrapCallback(callback, false)); | 446 originalThen.call(this, wrapper.wrapCallback(callback, false)); |
447 } | |
448 }(); | |
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 } | 447 } |
459 }(); | 448 }(); |
460 | 449 |
461 /** | 450 /** |
462 * Builds the object to manage tasks (mutually exclusive chains of events). | 451 * Builds the object to manage tasks (mutually exclusive chains of events). |
463 * @param {function(string, string): boolean} areConflicting Function that | 452 * @param {function(string, string): boolean} areConflicting Function that |
464 * checks if a new task can't be added to a task queue that contains an | 453 * checks if a new task can't be added to a task queue that contains an |
465 * existing task. | 454 * existing task. |
466 * @return {Object} Task manager interface. | 455 * @return {Object} Task manager interface. |
467 */ | 456 */ |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
730 /** | 719 /** |
731 * Wraps chrome.identity to provide limited listening support for | 720 * Wraps chrome.identity to provide limited listening support for |
732 * the sign in state by polling periodically for the auth token. | 721 * the sign in state by polling periodically for the auth token. |
733 * @return {Object} The Authentication Manager interface. | 722 * @return {Object} The Authentication Manager interface. |
734 */ | 723 */ |
735 function buildAuthenticationManager() { | 724 function buildAuthenticationManager() { |
736 var alarmName = 'sign-in-alarm'; | 725 var alarmName = 'sign-in-alarm'; |
737 | 726 |
738 /** | 727 /** |
739 * Gets an OAuth2 access token. | 728 * Gets an OAuth2 access token. |
740 * @return {Promise} A promise to get the authentication token. If there is | 729 * @param {function(string=)} callback Called on completion. |
741 * no token, the request is rejected. | 730 * The string contains the token. It's undefined if there was an error. |
742 */ | 731 */ |
743 function getAuthToken() { | 732 function getAuthToken(callback) { |
744 return new Promise(function(resolve, reject) { | 733 instrumented.identity.getAuthToken({interactive: false}, function(token) { |
745 instrumented.identity.getAuthToken({interactive: false}, function(token) { | 734 token = chrome.runtime.lastError ? undefined : token; |
746 if (chrome.runtime.lastError || !token) { | 735 callback(token); |
747 reject(); | |
748 } else { | |
749 resolve(token); | |
750 } | |
751 }); | |
752 }); | 736 }); |
753 } | 737 } |
754 | 738 |
755 /** | 739 /** |
756 * Determines whether there is an account attached to the profile. | 740 * Determines whether there is an account attached to the profile. |
757 * @return {Promise} A promise to determine if there is an account attached | 741 * @param {function(boolean)} callback Called on completion. |
758 * to the profile. | |
759 */ | 742 */ |
760 function isSignedIn() { | 743 function isSignedIn(callback) { |
761 return new Promise(function(resolve) { | 744 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) { |
762 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) { | 745 callback(!!accountInfo.login); |
763 resolve(!!accountInfo.login); | |
764 }); | |
765 }); | 746 }); |
766 } | 747 } |
767 | 748 |
768 /** | 749 /** |
769 * Removes the specified cached token. | 750 * Removes the specified cached token. |
770 * @param {string} token Authentication Token to remove from the cache. | 751 * @param {string} token Authentication Token to remove from the cache. |
771 * @return {Promise} A promise that resolves on completion. | 752 * @param {function()} callback Called on completion. |
772 */ | 753 */ |
773 function removeToken(token) { | 754 function removeToken(token, callback) { |
774 return new Promise(function(resolve) { | 755 instrumented.identity.removeCachedAuthToken({token: token}, function() { |
775 instrumented.identity.removeCachedAuthToken({token: token}, function() { | 756 // Let Chrome now about a possible problem with the token. |
776 // Let Chrome know about a possible problem with the token. | 757 getAuthToken(function() {}); |
777 getAuthToken(); | 758 callback(); |
778 resolve(); | |
779 }); | |
780 }); | 759 }); |
781 } | 760 } |
782 | 761 |
783 var listeners = []; | 762 var listeners = []; |
784 | 763 |
785 /** | 764 /** |
786 * Registers a listener that gets called back when the signed in state | 765 * Registers a listener that gets called back when the signed in state |
787 * is found to be changed. | 766 * is found to be changed. |
788 * @param {function()} callback Called when the answer to isSignedIn changes. | 767 * @param {function()} callback Called when the answer to isSignedIn changes. |
789 */ | 768 */ |
790 function addListener(callback) { | 769 function addListener(callback) { |
791 listeners.push(callback); | 770 listeners.push(callback); |
792 } | 771 } |
793 | 772 |
794 /** | 773 /** |
795 * Checks if the last signed in state matches the current one. | 774 * Checks if the last signed in state matches the current one. |
796 * If it doesn't, it notifies the listeners of the change. | 775 * If it doesn't, it notifies the listeners of the change. |
797 */ | 776 */ |
798 function checkAndNotifyListeners() { | 777 function checkAndNotifyListeners() { |
799 isSignedIn().then(function(signedIn) { | 778 isSignedIn(function(signedIn) { |
800 instrumented.storage.local.get('lastSignedInState', function(items) { | 779 instrumented.storage.local.get('lastSignedInState', function(items) { |
801 items = items || {}; | 780 items = items || {}; |
802 if (items.lastSignedInState != signedIn) { | 781 if (items.lastSignedInState != signedIn) { |
803 chrome.storage.local.set( | 782 chrome.storage.local.set( |
804 {lastSignedInState: signedIn}); | 783 {lastSignedInState: signedIn}); |
805 listeners.forEach(function(callback) { | 784 listeners.forEach(function(callback) { |
806 callback(); | 785 callback(); |
807 }); | 786 }); |
808 } | 787 } |
809 }); | 788 }); |
(...skipping 13 matching lines...) Expand all Loading... |
823 // One hour is just an arbitrary amount of time chosen. | 802 // One hour is just an arbitrary amount of time chosen. |
824 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | 803 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
825 | 804 |
826 return { | 805 return { |
827 addListener: addListener, | 806 addListener: addListener, |
828 getAuthToken: getAuthToken, | 807 getAuthToken: getAuthToken, |
829 isSignedIn: isSignedIn, | 808 isSignedIn: isSignedIn, |
830 removeToken: removeToken | 809 removeToken: removeToken |
831 }; | 810 }; |
832 } | 811 } |
OLD | NEW |