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

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

Issue 35413005: Using instrumented.identity.onSignInChanged to track signed-in state changes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 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 | « no previous file | chrome/browser/resources/google_now/utility_test_util.js » ('j') | 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 instrumentChromeApiFunction: instrumentChromeApiFunction, 386 instrumentChromeApiFunction: instrumentChromeApiFunction,
387 registerWrapperPluginFactory: registerWrapperPluginFactory, 387 registerWrapperPluginFactory: registerWrapperPluginFactory,
388 checkInWrappedCallback: checkInWrappedCallback, 388 checkInWrappedCallback: checkInWrappedCallback,
389 debugGetStateString: debugGetStateString 389 debugGetStateString: debugGetStateString
390 }; 390 };
391 })(); 391 })();
392 392
393 wrapper.instrumentChromeApiFunction('alarms.get', 1); 393 wrapper.instrumentChromeApiFunction('alarms.get', 1);
394 wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0); 394 wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0);
395 wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1); 395 wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1);
396 wrapper.instrumentChromeApiFunction('identity.onSignInChanged.addListener', 0);
396 wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1); 397 wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1);
397 398
398 /** 399 /**
399 * Builds the object to manage tasks (mutually exclusive chains of events). 400 * Builds the object to manage tasks (mutually exclusive chains of events).
400 * @param {function(string, string): boolean} areConflicting Function that 401 * @param {function(string, string): boolean} areConflicting Function that
401 * checks if a new task can't be added to a task queue that contains an 402 * checks if a new task can't be added to a task queue that contains an
402 * existing task. 403 * existing task.
403 * @return {Object} Task manager interface. 404 * @return {Object} Task manager interface.
404 */ 405 */
405 function buildTaskManager(areConflicting) { 406 function buildTaskManager(areConflicting) {
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 }); 657 });
657 658
658 return { 659 return {
659 start: start, 660 start: start,
660 planForNext: planForNext, 661 planForNext: planForNext,
661 stop: stop, 662 stop: stop,
662 isRunning: isRunning 663 isRunning: isRunning
663 }; 664 };
664 } 665 }
665 666
666 // TODO(robliao): Ideally, the authentication watcher infrastructure
667 // below would be an API change to chrome.identity.
668 // When this happens, remove the code below.
669
670 /** 667 /**
671 * Wraps chrome.identity to provide limited listening support for 668 * Wraps chrome.identity to provide limited listening support for
672 * the sign in state by polling periodically for the auth token. 669 * the sign in state by polling periodically for the auth token.
673 * @return {Object} The Authentication Manager interface. 670 * @return {Object} The Authentication Manager interface.
674 */ 671 */
675 function buildAuthenticationManager() { 672 function buildAuthenticationManager() {
676 var alarmName = 'sign-in-alarm';
677
678 /** 673 /**
679 * Determines if the user is signed in and provides a token if signed in. 674 * Determines if the user is signed in and provides a token if signed in.
680 * @param {function(string=)} callback Called on completion. 675 * @param {function(string=)} callback Called on completion.
681 * If the user is signed in, the string contains the token. 676 * If the user is signed in, the string contains the token.
682 */ 677 */
683 function isSignedIn(callback) { 678 function isSignedIn(callback) {
684 instrumented.identity.getAuthToken({interactive: false}, function(token) { 679 instrumented.identity.getAuthToken({interactive: false}, function(token) {
685 token = chrome.runtime.lastError ? undefined : token; 680 token = chrome.runtime.lastError ? undefined : token;
686 callback(token); 681 callback(token);
687 checkAndNotifyListeners(!!token);
688 }); 682 });
689 } 683 }
690 684
691 /** 685 /**
692 * Removes the specified cached token. 686 * Removes the specified cached token.
693 * @param {string} token Authentication Token to remove from the cache. 687 * @param {string} token Authentication Token to remove from the cache.
694 * @param {function} callback Called on completion. 688 * @param {function} callback Called on completion.
695 */ 689 */
696 function removeToken(token, callback) { 690 function removeToken(token, callback) {
697 instrumented.identity.removeCachedAuthToken({token: token}, function() { 691 instrumented.identity.removeCachedAuthToken({token: token}, function() {
698 // Removing the token from the cache will change the sign in state. 692 // Attempting to get a fresh token will let Chrome know about a possible
699 // Repoll now to check the state and notify listeners. 693 // problem with the token.
robliao 2013/10/23 17:11:48 Once Chrome gets a fresh token, will the onSignInC
700 // This also lets Chrome now about a possible problem with the token.
701 isSignedIn(function() {}); 694 isSignedIn(function() {});
702 callback(); 695 callback();
703 }); 696 });
704 } 697 }
705 698
706 var listeners = [];
707
708 /** 699 /**
709 * Registers a listener that gets called back when the signed in state 700 * Registers a listener that gets called back when the signed in state
710 * is found to be changed. 701 * is found to be changed.
711 * @param {function} callback Called when the answer to isSignedIn changes. 702 * @param {function} callback Called when the answer to isSignedIn changes.
712 */ 703 */
713 function addListener(callback) { 704 function addListener(callback) {
714 listeners.push(callback); 705 instrumented.identity.onSignInChanged.addListener(callback);
715 } 706 }
716 707
717 /**
718 * Checks if the last signed in state matches the specified one.
719 * If it doesn't, it notifies the listeners of the change.
720 * @param {boolean} currentSignedInState The current signed in state.
721 */
722 function checkAndNotifyListeners(currentSignedInState) {
723 instrumented.storage.local.get('lastSignedInState', function(items) {
724 items = items || {};
725 if (items.lastSignedInState != currentSignedInState) {
726 chrome.storage.local.set(
727 {lastSignedInState: currentSignedInState});
728 if (items.lastSignedInState != undefined) {
729 listeners.forEach(function(callback) {
730 callback();
731 });
732 }
733 }
734 });
735 }
736
737 instrumented.alarms.onAlarm.addListener(function(alarm) {
738 if (alarm.name == alarmName)
739 isSignedIn(function() {});
740 });
741
742 // Poll for the sign in state every hour.
743 // One hour is just an arbitrary amount of time chosen.
744 chrome.alarms.create(alarmName, {periodInMinutes: 60});
745
746 return { 708 return {
747 addListener: addListener, 709 addListener: addListener,
748 isSignedIn: isSignedIn, 710 isSignedIn: isSignedIn,
749 removeToken: removeToken 711 removeToken: removeToken
750 }; 712 };
751 } 713 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/utility_test_util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698