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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java

Issue 12880014: Get OAuth2TokenService working on Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change args to const refs. Created 7 years, 9 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/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java b/chrome/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java
index 7874dff69f02e25f669be4151ab65869aa158510..7502f100299b0fd7f02d95163716bac95674f611 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java
@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.sync;
import android.accounts.Account;
+import android.accounts.AccountManager;
nyquist 2013/03/25 21:01:21 Should not import this.
Patrick Dubroy 2013/03/25 21:15:49 Done.
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
@@ -89,6 +90,11 @@ public class ProfileSyncService {
mNativeProfileSyncServiceAndroid = nativeInit();
}
+ @CalledByNative
+ private static int getProfileSyncServiceAndroid(Context context) {
+ return get(context).mNativeProfileSyncServiceAndroid;
+ }
+
/**
* If we are currently in the process of setting up sync, this method clears the
* sync setup in progress flag.
@@ -163,23 +169,29 @@ public class ProfileSyncService {
}
}
- /**
- * Requests a new auth token from the AccountManager. Invalidates the old token
- * if |invalidAuthToken| is not empty.
- */
- @CalledByNative
- public void getNewAuthToken(final String username, final String invalidAuthToken) {
+ private Account getAccountOrNullFromUsername(String username) {
if (username == null) {
Log.e(TAG, "username is null");
- return;
+ return null;
}
final AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(mContext);
final Account account = accountManagerHelper.getAccountFromName(username);
if (account == null) {
Log.e(TAG, "Account not found for provided username.");
- return;
+ return null;
}
+ return account;
+ }
+
+ /**
+ * Requests a new auth token from the AccountManager. Invalidates the old token
+ * if |invalidAuthToken| is not empty.
+ */
+ @CalledByNative
+ public void getNewAuthToken(final String username, final String invalidAuthToken) {
+ final Account account = getAccountOrNullFromUsername(username);
+ if (account == null) return;
// Since this is blocking, do it in the background.
new AsyncTask<Void, Void, String>() {
@@ -187,15 +199,14 @@ public class ProfileSyncService {
@Override
public String doInBackground(Void... params) {
// Invalidate our old auth token and fetch a new one.
+ AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(mContext);
return accountManagerHelper.getNewAuthToken(
- account, invalidAuthToken, SyncStatusHelper.AUTH_TOKEN_TYPE_SYNC);
+ account, invalidAuthToken, SyncStatusHelper.AUTH_TOKEN_TYPE_SYNC);
}
@Override
public void onPostExecute(String authToken) {
if (authToken == null) {
- // DO NOT COMMIT do we really need this TODO? We trigger a call to
- // requestSyncFromNativeChrome() when an account changes and sync is setup.
// TODO(sync): Need to hook LOGIN_ACCOUNTS_CHANGED_ACTION (http://b/5354713).
Log.d(TAG, "Auth token for sync was null.");
} else {
@@ -206,6 +217,48 @@ public class ProfileSyncService {
}.execute();
}
+ /**
+ * Called by native to invalidate an OAuth2 token.
+ */
+ @CalledByNative
+ public void invalidateOAuth2AuthToken(String scope, String access_token) {
nyquist 2013/03/25 21:01:21 camelCase
Patrick Dubroy 2013/03/25 21:15:49 Done.
+ AccountManager.get(mContext).invalidateAuthToken(scope, access_token);
nyquist 2013/03/25 21:01:21 Do not use the AccountManager directly. Always go
Patrick Dubroy 2013/03/25 21:15:49 Done.
+ }
+
+ /**
+ * Called by native to retrieve OAuth2 tokens.
+ *
+ * @param username the native username (full address)
+ * @param scope the scope to get an auth token for (without Android-style 'oauth2:' prefix).
+ * @param oldAuthToken if provided, the token will be invalidated before getting a new token.
+ * @param nativeCallback the pointer to the native callback that should be run upon completion.
+ */
+ @CalledByNative
+ public void getOAuth2AuthToken(
+ String username, String scope, final int nativeCallback) {
nyquist 2013/03/25 21:01:21 Nit: Does this fit on the line above?
Patrick Dubroy 2013/03/25 21:15:49 Done.
+ final Account account = getAccountOrNullFromUsername(username);
+ if (account == null) {
+ nativeOAuth2TokenFetched(
+ mNativeProfileSyncServiceAndroid, nativeCallback, null, false);
+ return;
+ }
+ final String oauth2Scope = "oauth2:" + scope;
+
+ new AsyncTask<Void, Void, String>() {
+ @Override
+ public String doInBackground(Void... params) {
+ AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(mContext);
+ return accountManagerHelper.getAuthTokenFromBackground(account, oauth2Scope);
+ }
+
+ @Override
+ public void onPostExecute(String authToken) {
+ nativeOAuth2TokenFetched(
+ mNativeProfileSyncServiceAndroid, nativeCallback, authToken, authToken != null);
+ }
+ }.execute();
+ }
+
/**
* Checks if a password or a passphrase is required for decryption of sync data.
* <p/>
@@ -534,4 +587,7 @@ public class ProfileSyncService {
private native boolean nativeIsSessionSyncEnabled(int nativeProfileSyncServiceAndroid);
private native boolean nativeHasUnrecoverableError(int nativeProfileSyncServiceAndroid);
private native String nativeGetAboutInfoForTest(int nativeProfileSyncServiceAndroid);
+ private native void nativeOAuth2TokenFetched(
+ int nativeProfileSyncServiceAndroid, int nativeCallback, String authToken,
+ boolean result);
}
« no previous file with comments | « no previous file | chrome/browser/history/web_history_service.cc » ('j') | chrome/browser/sync/profile_sync_service_android.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698