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

Side by Side Diff: sync/android/java/src/org/chromium/sync/signin/AccountManagerHelper.java

Issue 1440363002: Use GoogleAuthUtil's getToken instead of AccountManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 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 package org.chromium.sync.signin; 5 package org.chromium.sync.signin;
6 6
7 7
8 import android.Manifest; 8 import android.Manifest;
9 import android.accounts.Account; 9 import android.accounts.Account;
10 import android.accounts.AccountManager;
11 import android.accounts.AccountManagerFuture;
12 import android.accounts.AuthenticatorDescription; 10 import android.accounts.AuthenticatorDescription;
13 import android.accounts.AuthenticatorException;
14 import android.accounts.OperationCanceledException;
15 import android.content.Context; 11 import android.content.Context;
16 import android.content.pm.PackageManager; 12 import android.content.pm.PackageManager;
17 import android.os.AsyncTask; 13 import android.os.AsyncTask;
18 import android.os.Build; 14 import android.os.Build;
19 import android.os.Bundle;
20 import android.os.Process; 15 import android.os.Process;
21 import android.util.Log;
22 16
23 import org.chromium.base.Callback; 17 import org.chromium.base.Callback;
24 import org.chromium.base.ThreadUtils;
25 import org.chromium.base.VisibleForTesting; 18 import org.chromium.base.VisibleForTesting;
26 import org.chromium.net.NetworkChangeNotifier; 19 import org.chromium.net.NetworkChangeNotifier;
27 20
28 import java.io.IOException;
29 import java.util.ArrayList; 21 import java.util.ArrayList;
30 import java.util.List; 22 import java.util.List;
31 import java.util.Locale; 23 import java.util.Locale;
32 import java.util.concurrent.atomic.AtomicBoolean; 24 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.util.concurrent.atomic.AtomicInteger; 25 import java.util.concurrent.atomic.AtomicInteger;
34 import java.util.regex.Pattern; 26 import java.util.regex.Pattern;
35 27
36 /** 28 /**
37 * AccountManagerHelper wraps our access of AccountManager in Android. 29 * AccountManagerHelper wraps our access of AccountManager in Android.
38 * 30 *
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M 340 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
349 || mApplicationContext.checkPermission("android.permission.USE_C REDENTIALS", 341 || mApplicationContext.checkPermission("android.permission.USE_C REDENTIALS",
350 Process.myPid(), Process.myUid()) == PackageManager.PERMISSION_G RANTED; 342 Process.myPid(), Process.myUid()) == PackageManager.PERMISSION_G RANTED;
351 } 343 }
352 344
353 public boolean hasGetAccountsPermission() { 345 public boolean hasGetAccountsPermission() {
354 return mApplicationContext.checkPermission(Manifest.permission.GET_ACCOU NTS, 346 return mApplicationContext.checkPermission(Manifest.permission.GET_ACCOU NTS,
355 Process.myPid(), Process.myUid()) == PackageManager.PERMISSION_G RANTED; 347 Process.myPid(), Process.myUid()) == PackageManager.PERMISSION_G RANTED;
356 } 348 }
357 349
358 // Gets the auth token synchronously
359 private String getAuthTokenInner(AccountManagerFuture<Bundle> future,
360 AtomicBoolean isTransientError) {
361 try {
362 Bundle result = future.getResult();
363 if (result != null) {
364 return result.getString(AccountManager.KEY_AUTHTOKEN);
365 } else {
366 Log.w(TAG, "Auth token - getAuthToken returned null");
367 }
368 } catch (OperationCanceledException e) {
369 Log.w(TAG, "Auth token - operation cancelled", e);
370 } catch (AuthenticatorException e) {
371 Log.w(TAG, "Auth token - authenticator exception", e);
372 } catch (IOException e) {
373 Log.w(TAG, "Auth token - IO exception", e);
374 isTransientError.set(true);
375 }
376 return null;
377 }
378
379 private void getAuthTokenAsynchronously(final Account account, final String authTokenType, 350 private void getAuthTokenAsynchronously(final Account account, final String authTokenType,
380 final GetAuthTokenCallback callback, final AtomicInteger numTries, 351 final GetAuthTokenCallback callback, final AtomicInteger numTries,
381 final AtomicBoolean isTransientError, final ConnectionRetry retry) { 352 final AtomicBoolean isTransientError, final ConnectionRetry retry) {
382 // Return null token for no USE_CREDENTIALS permission.
383 if (!hasUseCredentialsPermission()) {
384 ThreadUtils.runOnUiThread(new Runnable() {
385 @Override
386 public void run() {
387 callback.tokenUnavailable(false);
388 }
389 });
390 return;
391 }
392 final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken (
393 account, authTokenType, true, null, null);
394 isTransientError.set(false); 353 isTransientError.set(false);
395
396 new AsyncTask<Void, Void, String>() { 354 new AsyncTask<Void, Void, String>() {
397 @Override 355 @Override
398 public String doInBackground(Void... params) { 356 public String doInBackground(Void... params) {
399 return getAuthTokenInner(future, isTransientError); 357 try {
358 return mAccountManager.getAuthToken(account, authTokenType);
359 } catch (AuthException ex) {
360 isTransientError.set(ex.isTransientError());
Bernhard Bauer 2015/11/19 15:23:33 Can you add a TODO here to handle the recovery int
knn 2015/11/19 16:09:25 Done.
361 }
362 return null;
400 } 363 }
401 @Override 364 @Override
402 public void onPostExecute(String authToken) { 365 public void onPostExecute(String authToken) {
403 onGotAuthTokenResult(account, authTokenType, authToken, callback , numTries, 366 onGotAuthTokenResult(account, authTokenType, authToken, callback , numTries,
404 isTransientError, retry); 367 isTransientError, retry);
405 } 368 }
406 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 369 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
407 } 370 }
408 371
409 private void onGotAuthTokenResult(Account account, String authTokenType, Str ing authToken, 372 private void onGotAuthTokenResult(Account account, String authTokenType, Str ing authToken,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 if (authToken != null && !authToken.isEmpty()) { 415 if (authToken != null && !authToken.isEmpty()) {
453 mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken); 416 mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
454 } 417 }
455 } 418 }
456 419
457 public void checkChildAccount(Account account, Callback<Boolean> callback) { 420 public void checkChildAccount(Account account, Callback<Boolean> callback) {
458 String[] features = {FEATURE_IS_CHILD_ACCOUNT_KEY}; 421 String[] features = {FEATURE_IS_CHILD_ACCOUNT_KEY};
459 mAccountManager.hasFeatures(account, features, callback); 422 mAccountManager.hasFeatures(account, features, callback);
460 } 423 }
461 } 424 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698