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

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

Issue 51333004: Using a private API to determine if there is an account for the profile (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 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 bfcb6ed076fdc3892b2b13433265944ef5ce702a..4090139423cedbe3ec4412248df47e2cf823c75f 100644
--- a/chrome/browser/resources/google_now/utility.js
+++ b/chrome/browser/resources/google_now/utility.js
@@ -395,6 +395,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('webstorePrivate.getBrowserLogin', 0);
/**
* Builds the object to manage tasks (mutually exclusive chains of events).
@@ -674,15 +675,24 @@ function buildAuthenticationManager() {
var alarmName = 'sign-in-alarm';
/**
- * Determines if the user is signed in and provides a token if signed in.
+ * Gets an OAuth2 access token.
* @param {function(string=)} callback Called on completion.
- * If the user is signed in, the string contains the token.
+ * The string contains the token. It's undefined if there was an error.
*/
- function isSignedIn(callback) {
+ function getAuthToken(callback) {
instrumented.identity.getAuthToken({interactive: false}, function(token) {
token = chrome.runtime.lastError ? undefined : token;
callback(token);
- checkAndNotifyListeners(!!token);
+ });
+ }
+
+ /**
+ * Determines whether there is an account attached to the profile.
+ * @param {function(boolean)} callback Called on completion.
+ */
+ function isSignedIn(callback) {
+ instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) {
+ callback(!!accountInfo.login);
});
}
@@ -693,10 +703,8 @@ function buildAuthenticationManager() {
*/
function removeToken(token, callback) {
instrumented.identity.removeCachedAuthToken({token: token}, function() {
- // Removing the token from the cache will change the sign in state.
- // Repoll now to check the state and notify listeners.
- // This also lets Chrome now about a possible problem with the token.
- isSignedIn(function() {});
+ // Let Chrome now about a possible problem with the token.
+ getAuthToken(function() {});
callback();
});
}
@@ -713,32 +721,33 @@ function buildAuthenticationManager() {
}
/**
- * Checks if the last signed in state matches the specified one.
+ * Checks if the last signed in state matches the current one.
* If it doesn't, it notifies the listeners of the change.
- * @param {boolean} currentSignedInState The current signed in state.
*/
- function checkAndNotifyListeners(currentSignedInState) {
- instrumented.storage.local.get('lastSignedInState', function(items) {
- items = items || {};
- if (items.lastSignedInState != currentSignedInState) {
- chrome.storage.local.set(
- {lastSignedInState: currentSignedInState});
- if (items.lastSignedInState != undefined) {
- listeners.forEach(function(callback) {
- callback();
- });
+ function checkAndNotifyListeners() {
+ isSignedIn(function(signedIn) {
+ instrumented.storage.local.get('lastSignedInState', function(items) {
+ items = items || {};
+ if (items.lastSignedInState != signedIn) {
+ chrome.storage.local.set(
+ {lastSignedInState: signedIn});
+ if (items.lastSignedInState != undefined) {
+ listeners.forEach(function(callback) {
+ callback();
+ });
+ }
}
- }
+ });
});
}
instrumented.identity.onSignInChanged.addListener(function() {
- isSignedIn(function() {});
+ checkAndNotifyListeners();
});
instrumented.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name == alarmName)
- isSignedIn(function() {});
+ checkAndNotifyListeners();
});
// Poll for the sign in state every hour.
@@ -747,6 +756,7 @@ function buildAuthenticationManager() {
return {
addListener: addListener,
+ getAuthToken: getAuthToken,
isSignedIn: isSignedIn,
removeToken: removeToken
};
« no previous file with comments | « chrome/browser/resources/google_now/manifest.json ('k') | chrome/browser/resources/google_now/utility_test_util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698