OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 package org.chromium.chrome.browser.sync; | 5 package org.chromium.chrome.browser.sync; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.accounts.AccountManager; | |
nyquist
2013/03/25 21:01:21
Should not import this.
Patrick Dubroy
2013/03/25 21:15:49
Done.
| |
8 import android.content.Context; | 9 import android.content.Context; |
9 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
10 import android.util.Log; | 11 import android.util.Log; |
11 | 12 |
12 import com.google.common.annotations.VisibleForTesting; | 13 import com.google.common.annotations.VisibleForTesting; |
13 | 14 |
14 import org.chromium.base.CalledByNative; | 15 import org.chromium.base.CalledByNative; |
15 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
16 import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator; | 17 import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator; |
17 import org.chromium.sync.internal_api.pub.SyncDecryptionPassphraseType; | 18 import org.chromium.sync.internal_api.pub.SyncDecryptionPassphraseType; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 ThreadUtils.assertOnUiThread(); | 83 ThreadUtils.assertOnUiThread(); |
83 // We should store the application context, as we outlive any activity w hich may create us. | 84 // We should store the application context, as we outlive any activity w hich may create us. |
84 mContext = context.getApplicationContext(); | 85 mContext = context.getApplicationContext(); |
85 | 86 |
86 // This may cause us to create ProfileSyncService even if sync has not | 87 // This may cause us to create ProfileSyncService even if sync has not |
87 // been set up, but ProfileSyncService::Startup() won't be called until | 88 // been set up, but ProfileSyncService::Startup() won't be called until |
88 // credentials are available. | 89 // credentials are available. |
89 mNativeProfileSyncServiceAndroid = nativeInit(); | 90 mNativeProfileSyncServiceAndroid = nativeInit(); |
90 } | 91 } |
91 | 92 |
93 @CalledByNative | |
94 private static int getProfileSyncServiceAndroid(Context context) { | |
95 return get(context).mNativeProfileSyncServiceAndroid; | |
96 } | |
97 | |
92 /** | 98 /** |
93 * If we are currently in the process of setting up sync, this method clears the | 99 * If we are currently in the process of setting up sync, this method clears the |
94 * sync setup in progress flag. | 100 * sync setup in progress flag. |
95 */ | 101 */ |
96 @VisibleForTesting | 102 @VisibleForTesting |
97 public void finishSyncFirstSetupIfNeeded() { | 103 public void finishSyncFirstSetupIfNeeded() { |
98 if (isFirstSetupInProgress()) { | 104 if (isFirstSetupInProgress()) { |
99 setSyncSetupCompleted(); | 105 setSyncSetupCompleted(); |
100 setSetupInProgress(false); | 106 setSetupInProgress(false); |
101 } | 107 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 "This may lead to unexpected tab sync behavior."); | 162 "This may lead to unexpected tab sync behavior."); |
157 return; | 163 return; |
158 } | 164 } |
159 String sessionTag = SESSION_TAG_PREFIX + uniqueTag; | 165 String sessionTag = SESSION_TAG_PREFIX + uniqueTag; |
160 if (!nativeSetSyncSessionsId(mNativeProfileSyncServiceAndroid, sessionTa g)) { | 166 if (!nativeSetSyncSessionsId(mNativeProfileSyncServiceAndroid, sessionTa g)) { |
161 Log.e(TAG, "Unable to write session sync tag. " + | 167 Log.e(TAG, "Unable to write session sync tag. " + |
162 "This may lead to unexpected tab sync behavior."); | 168 "This may lead to unexpected tab sync behavior."); |
163 } | 169 } |
164 } | 170 } |
165 | 171 |
172 private Account getAccountOrNullFromUsername(String username) { | |
173 if (username == null) { | |
174 Log.e(TAG, "username is null"); | |
175 return null; | |
176 } | |
177 | |
178 final AccountManagerHelper accountManagerHelper = AccountManagerHelper.g et(mContext); | |
179 final Account account = accountManagerHelper.getAccountFromName(username ); | |
180 if (account == null) { | |
181 Log.e(TAG, "Account not found for provided username."); | |
182 return null; | |
183 } | |
184 return account; | |
185 } | |
186 | |
166 /** | 187 /** |
167 * Requests a new auth token from the AccountManager. Invalidates the old to ken | 188 * Requests a new auth token from the AccountManager. Invalidates the old to ken |
168 * if |invalidAuthToken| is not empty. | 189 * if |invalidAuthToken| is not empty. |
169 */ | 190 */ |
170 @CalledByNative | 191 @CalledByNative |
171 public void getNewAuthToken(final String username, final String invalidAuthT oken) { | 192 public void getNewAuthToken(final String username, final String invalidAuthT oken) { |
172 if (username == null) { | 193 final Account account = getAccountOrNullFromUsername(username); |
173 Log.e(TAG, "username is null"); | 194 if (account == null) return; |
174 return; | |
175 } | |
176 | |
177 final AccountManagerHelper accountManagerHelper = AccountManagerHelper.g et(mContext); | |
178 final Account account = accountManagerHelper.getAccountFromName(username ); | |
179 if (account == null) { | |
180 Log.e(TAG, "Account not found for provided username."); | |
181 return; | |
182 } | |
183 | 195 |
184 // Since this is blocking, do it in the background. | 196 // Since this is blocking, do it in the background. |
185 new AsyncTask<Void, Void, String>() { | 197 new AsyncTask<Void, Void, String>() { |
186 | 198 |
187 @Override | 199 @Override |
188 public String doInBackground(Void... params) { | 200 public String doInBackground(Void... params) { |
189 // Invalidate our old auth token and fetch a new one. | 201 // Invalidate our old auth token and fetch a new one. |
202 AccountManagerHelper accountManagerHelper = AccountManagerHelper .get(mContext); | |
190 return accountManagerHelper.getNewAuthToken( | 203 return accountManagerHelper.getNewAuthToken( |
191 account, invalidAuthToken, SyncStatusHelper.AUTH_TOKEN_T YPE_SYNC); | 204 account, invalidAuthToken, SyncStatusHelper.AUTH_TOKEN_TYPE_ SYNC); |
192 } | 205 } |
193 | 206 |
194 @Override | 207 @Override |
195 public void onPostExecute(String authToken) { | 208 public void onPostExecute(String authToken) { |
196 if (authToken == null) { | 209 if (authToken == null) { |
197 // DO NOT COMMIT do we really need this TODO? We trigger a c all to | |
198 // requestSyncFromNativeChrome() when an account changes and sync is setup. | |
199 // TODO(sync): Need to hook LOGIN_ACCOUNTS_CHANGED_ACTION (h ttp://b/5354713). | 210 // TODO(sync): Need to hook LOGIN_ACCOUNTS_CHANGED_ACTION (h ttp://b/5354713). |
200 Log.d(TAG, "Auth token for sync was null."); | 211 Log.d(TAG, "Auth token for sync was null."); |
201 } else { | 212 } else { |
202 Log.d(TAG, "Successfully retrieved sync auth token."); | 213 Log.d(TAG, "Successfully retrieved sync auth token."); |
203 nativeTokenAvailable(mNativeProfileSyncServiceAndroid, usern ame, authToken); | 214 nativeTokenAvailable(mNativeProfileSyncServiceAndroid, usern ame, authToken); |
204 } | 215 } |
205 } | 216 } |
206 }.execute(); | 217 }.execute(); |
207 } | 218 } |
208 | 219 |
220 /** | |
221 * Called by native to invalidate an OAuth2 token. | |
222 */ | |
223 @CalledByNative | |
224 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.
| |
225 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.
| |
226 } | |
227 | |
228 /** | |
229 * Called by native to retrieve OAuth2 tokens. | |
230 * | |
231 * @param username the native username (full address) | |
232 * @param scope the scope to get an auth token for (without Android-style 'o auth2:' prefix). | |
233 * @param oldAuthToken if provided, the token will be invalidated before get ting a new token. | |
234 * @param nativeCallback the pointer to the native callback that should be r un upon completion. | |
235 */ | |
236 @CalledByNative | |
237 public void getOAuth2AuthToken( | |
238 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.
| |
239 final Account account = getAccountOrNullFromUsername(username); | |
240 if (account == null) { | |
241 nativeOAuth2TokenFetched( | |
242 mNativeProfileSyncServiceAndroid, nativeCallback, null, false); | |
243 return; | |
244 } | |
245 final String oauth2Scope = "oauth2:" + scope; | |
246 | |
247 new AsyncTask<Void, Void, String>() { | |
248 @Override | |
249 public String doInBackground(Void... params) { | |
250 AccountManagerHelper accountManagerHelper = AccountManagerHelper .get(mContext); | |
251 return accountManagerHelper.getAuthTokenFromBackground(account, oauth2Scope); | |
252 } | |
253 | |
254 @Override | |
255 public void onPostExecute(String authToken) { | |
256 nativeOAuth2TokenFetched( | |
257 mNativeProfileSyncServiceAndroid, nativeCallback, authToken, authToken != null); | |
258 } | |
259 }.execute(); | |
260 } | |
261 | |
209 /** | 262 /** |
210 * Checks if a password or a passphrase is required for decryption of sync d ata. | 263 * Checks if a password or a passphrase is required for decryption of sync d ata. |
211 * <p/> | 264 * <p/> |
212 * Returns NONE if the state is unavailable, or decryption passphrase/passwo rd is not required. | 265 * Returns NONE if the state is unavailable, or decryption passphrase/passwo rd is not required. |
213 * | 266 * |
214 * @return the enum describing the decryption passphrase type required | 267 * @return the enum describing the decryption passphrase type required |
215 */ | 268 */ |
216 public SyncDecryptionPassphraseType getSyncDecryptionPassphraseTypeIfRequire d() { | 269 public SyncDecryptionPassphraseType getSyncDecryptionPassphraseTypeIfRequire d() { |
217 // ProfileSyncService::IsUsingSecondaryPassphrase() requires the sync ba ckend to be | 270 // ProfileSyncService::IsUsingSecondaryPassphrase() requires the sync ba ckend to be |
218 // initialized, and that happens just after OnPassphraseRequired(). Ther efore, we need to | 271 // initialized, and that happens just after OnPassphraseRequired(). Ther efore, we need to |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 private native void nativeSetSyncSetupCompleted(int nativeProfileSyncService Android); | 580 private native void nativeSetSyncSetupCompleted(int nativeProfileSyncService Android); |
528 private native boolean nativeHasSyncSetupCompleted(int nativeProfileSyncServ iceAndroid); | 581 private native boolean nativeHasSyncSetupCompleted(int nativeProfileSyncServ iceAndroid); |
529 private native boolean nativeHasKeepEverythingSynced(int nativeProfileSyncSe rviceAndroid); | 582 private native boolean nativeHasKeepEverythingSynced(int nativeProfileSyncSe rviceAndroid); |
530 private native boolean nativeIsAutofillSyncEnabled(int nativeProfileSyncServ iceAndroid); | 583 private native boolean nativeIsAutofillSyncEnabled(int nativeProfileSyncServ iceAndroid); |
531 private native boolean nativeIsBookmarkSyncEnabled(int nativeProfileSyncServ iceAndroid); | 584 private native boolean nativeIsBookmarkSyncEnabled(int nativeProfileSyncServ iceAndroid); |
532 private native boolean nativeIsPasswordSyncEnabled(int nativeProfileSyncServ iceAndroid); | 585 private native boolean nativeIsPasswordSyncEnabled(int nativeProfileSyncServ iceAndroid); |
533 private native boolean nativeIsTypedUrlSyncEnabled(int nativeProfileSyncServ iceAndroid); | 586 private native boolean nativeIsTypedUrlSyncEnabled(int nativeProfileSyncServ iceAndroid); |
534 private native boolean nativeIsSessionSyncEnabled(int nativeProfileSyncServi ceAndroid); | 587 private native boolean nativeIsSessionSyncEnabled(int nativeProfileSyncServi ceAndroid); |
535 private native boolean nativeHasUnrecoverableError(int nativeProfileSyncServ iceAndroid); | 588 private native boolean nativeHasUnrecoverableError(int nativeProfileSyncServ iceAndroid); |
536 private native String nativeGetAboutInfoForTest(int nativeProfileSyncService Android); | 589 private native String nativeGetAboutInfoForTest(int nativeProfileSyncService Android); |
590 private native void nativeOAuth2TokenFetched( | |
591 int nativeProfileSyncServiceAndroid, int nativeCallback, String auth Token, | |
592 boolean result); | |
537 } | 593 } |
OLD | NEW |