OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.sync.signin; | |
6 | |
7 import android.accounts.Account; | |
8 import android.accounts.AccountManager; | |
9 import android.accounts.AccountManagerCallback; | |
10 import android.accounts.AccountManagerFuture; | |
11 import android.accounts.AuthenticatorDescription; | |
12 import android.accounts.AuthenticatorException; | |
13 import android.accounts.OperationCanceledException; | |
14 import android.content.Context; | |
15 import android.os.AsyncTask; | |
16 import android.os.SystemClock; | |
17 | |
18 import com.google.android.gms.auth.GoogleAuthException; | |
19 import com.google.android.gms.auth.GoogleAuthUtil; | |
20 import com.google.android.gms.auth.GooglePlayServicesAvailabilityException; | |
21 | |
22 import org.chromium.base.Callback; | |
23 import org.chromium.base.Log; | |
24 import org.chromium.base.ThreadUtils; | |
25 import org.chromium.base.annotations.MainDex; | |
26 import org.chromium.base.library_loader.LibraryLoader; | |
27 import org.chromium.base.metrics.RecordHistogram; | |
28 | |
29 import java.io.IOException; | |
30 import java.util.concurrent.TimeUnit; | |
31 | |
32 /** | |
33 * Default implementation of {@link AccountManagerDelegate} which delegates all
calls to the | |
34 * Android account manager. | |
35 */ | |
36 @MainDex | |
37 public class SystemAccountManagerDelegate implements AccountManagerDelegate { | |
38 | |
39 private final AccountManager mAccountManager; | |
40 private final Context mApplicationContext; | |
41 private static final String TAG = "Auth"; | |
42 | |
43 public SystemAccountManagerDelegate(Context context) { | |
44 mApplicationContext = context.getApplicationContext(); | |
45 mAccountManager = AccountManager.get(context.getApplicationContext()); | |
46 } | |
47 | |
48 @Override | |
49 public Account[] getAccountsByType(String type) { | |
50 if (!AccountManagerHelper.get(mApplicationContext).hasGetAccountsPermiss
ion()) { | |
51 return new Account[]{}; | |
52 } | |
53 long now = SystemClock.elapsedRealtime(); | |
54 Account[] accounts = mAccountManager.getAccountsByType(type); | |
55 long elapsed = SystemClock.elapsedRealtime() - now; | |
56 recordElapsedTimeHistogram("Signin.AndroidGetAccountsTime_AccountManager
", elapsed); | |
57 return accounts; | |
58 } | |
59 | |
60 @Override | |
61 public void getAccountsByType(final String type, final Callback<Account[]> c
allback) { | |
62 new AsyncTask<Void, Void, Account[]>() { | |
63 @Override | |
64 protected Account[] doInBackground(Void... params) { | |
65 return getAccountsByType(type); | |
66 } | |
67 | |
68 @Override | |
69 protected void onPostExecute(Account[] accounts) { | |
70 callback.onResult(accounts); | |
71 } | |
72 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | |
73 } | |
74 | |
75 @Override | |
76 public String getAuthToken(Account account, String authTokenScope) throws Au
thException { | |
77 assert !ThreadUtils.runningOnUiThread(); | |
78 assert AccountManagerHelper.GOOGLE_ACCOUNT_TYPE.equals(account.type); | |
79 try { | |
80 return GoogleAuthUtil.getTokenWithNotification(mApplicationContext,
account, | |
81 authTokenScope, null); | |
82 } catch (GoogleAuthException ex) { | |
83 // This case includes a UserRecoverableNotifiedException, but most c
lients will have | |
84 // their own retry mechanism anyway. | |
85 // TODO(bauerb): Investigate integrating the callback with Connectio
nRetry. | |
86 throw new AuthException(false /* isTransientError */, ex); | |
87 } catch (IOException ex) { | |
88 throw new AuthException(true /* isTransientError */, ex); | |
89 } | |
90 } | |
91 | |
92 @Override | |
93 public void invalidateAuthToken(String authToken) throws AuthException { | |
94 try { | |
95 GoogleAuthUtil.clearToken(mApplicationContext, authToken); | |
96 } catch (GooglePlayServicesAvailabilityException ex) { | |
97 throw new AuthException(false /* isTransientError */, ex); | |
98 } catch (GoogleAuthException ex) { | |
99 throw new AuthException(false /* isTransientError */, ex); | |
100 } catch (IOException ex) { | |
101 throw new AuthException(true /* isTransientError */, ex); | |
102 } | |
103 } | |
104 | |
105 @Override | |
106 public AuthenticatorDescription[] getAuthenticatorTypes() { | |
107 return mAccountManager.getAuthenticatorTypes(); | |
108 } | |
109 | |
110 @Override | |
111 public void hasFeatures(Account account, String[] features, final Callback<B
oolean> callback) { | |
112 if (!AccountManagerHelper.get(mApplicationContext).hasGetAccountsPermiss
ion()) { | |
113 ThreadUtils.postOnUiThread(new Runnable() { | |
114 @Override | |
115 public void run() { | |
116 callback.onResult(false); | |
117 } | |
118 }); | |
119 return; | |
120 } | |
121 mAccountManager.hasFeatures(account, features, new AccountManagerCallbac
k<Boolean>() { | |
122 @Override | |
123 public void run(AccountManagerFuture<Boolean> future) { | |
124 assert future.isDone(); | |
125 boolean hasFeatures = false; | |
126 try { | |
127 hasFeatures = future.getResult(); | |
128 } catch (AuthenticatorException | IOException e) { | |
129 Log.e(TAG, "Error while checking features: ", e); | |
130 } catch (OperationCanceledException e) { | |
131 Log.e(TAG, "Checking features was cancelled. This should not
happen."); | |
132 } | |
133 callback.onResult(hasFeatures); | |
134 } | |
135 }, null /* handler */); | |
136 } | |
137 | |
138 /** | |
139 * Records a histogram value for how long time an action has taken using | |
140 * {@link RecordHistogram#recordTimesHistogram(String, long, TimeUnit))} iff
the browser | |
141 * process has been initialized. | |
142 * | |
143 * @param histogramName the name of the histogram. | |
144 * @param elapsedMs the elapsed time in milliseconds. | |
145 */ | |
146 protected static void recordElapsedTimeHistogram(String histogramName, long
elapsedMs) { | |
147 if (!LibraryLoader.isInitialized()) return; | |
148 RecordHistogram.recordTimesHistogram(histogramName, elapsedMs, TimeUnit.
MILLISECONDS); | |
149 } | |
150 } | |
OLD | NEW |