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