| 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.signin; | 5 package org.chromium.chrome.browser.signin; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.app.Activity; | 8 import android.app.Activity; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.preference.PreferenceManager; | 10 import android.preference.PreferenceManager; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 * implement this interface and register with {@link #addObserver}. | 46 * implement this interface and register with {@link #addObserver}. |
| 47 */ | 47 */ |
| 48 public interface OAuth2TokenServiceObserver { | 48 public interface OAuth2TokenServiceObserver { |
| 49 void onRefreshTokenAvailable(Account account); | 49 void onRefreshTokenAvailable(Account account); |
| 50 void onRefreshTokenRevoked(Account account); | 50 void onRefreshTokenRevoked(Account account); |
| 51 void onRefreshTokensLoaded(); | 51 void onRefreshTokensLoaded(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 private static final String OAUTH2_SCOPE_PREFIX = "oauth2:"; | 54 private static final String OAUTH2_SCOPE_PREFIX = "oauth2:"; |
| 55 | 55 |
| 56 private final long mNativeProfileOAuth2TokenService; | 56 private final long mNativeOAuth2TokenServiceDelegateAndroid; |
| 57 private final ObserverList<OAuth2TokenServiceObserver> mObservers; | 57 private final ObserverList<OAuth2TokenServiceObserver> mObservers; |
| 58 | 58 |
| 59 private OAuth2TokenService(long nativeOAuth2Service) { | 59 private OAuth2TokenService(long nativeOAuth2Service) { |
| 60 mNativeProfileOAuth2TokenService = nativeOAuth2Service; | 60 mNativeOAuth2TokenServiceDelegateAndroid = nativeOAuth2Service; |
| 61 mObservers = new ObserverList<OAuth2TokenServiceObserver>(); | 61 mObservers = new ObserverList<OAuth2TokenServiceObserver>(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 public static OAuth2TokenService getForProfile(Profile profile) { | 64 public static OAuth2TokenService getForProfile(Profile profile) { |
| 65 ThreadUtils.assertOnUiThread(); | 65 ThreadUtils.assertOnUiThread(); |
| 66 return (OAuth2TokenService) nativeGetForProfile(profile); | 66 return (OAuth2TokenService) nativeGetForProfile(profile); |
| 67 } | 67 } |
| 68 | 68 |
| 69 @CalledByNative | 69 @CalledByNative |
| 70 private static OAuth2TokenService create(long nativeOAuth2Service) { | 70 private static OAuth2TokenService create(long nativeOAuth2Service) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if (accessToken != null) { | 219 if (accessToken != null) { |
| 220 AccountManagerHelper.get(context).invalidateAuthToken(accessToken); | 220 AccountManagerHelper.get(context).invalidateAuthToken(accessToken); |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 @CalledByNative | 224 @CalledByNative |
| 225 public void validateAccounts(Context context, boolean forceNotifications) { | 225 public void validateAccounts(Context context, boolean forceNotifications) { |
| 226 ThreadUtils.assertOnUiThread(); | 226 ThreadUtils.assertOnUiThread(); |
| 227 String currentlySignedInAccount = | 227 String currentlySignedInAccount = |
| 228 ChromeSigninController.get(context).getSignedInAccountName(); | 228 ChromeSigninController.get(context).getSignedInAccountName(); |
| 229 nativeValidateAccounts(mNativeProfileOAuth2TokenService, currentlySigned
InAccount, | 229 nativeValidateAccounts(mNativeOAuth2TokenServiceDelegateAndroid, current
lySignedInAccount, |
| 230 forceNotifications); | 230 forceNotifications); |
| 231 } | 231 } |
| 232 | 232 |
| 233 /** | 233 /** |
| 234 * Triggers a notification to all observers of the native and Java instance
of the | 234 * Triggers a notification to all observers of the native and Java instance
of the |
| 235 * OAuth2TokenService that a refresh token is now available. This may cause
observers to retry | 235 * OAuth2TokenService that a refresh token is now available. This may cause
observers to retry |
| 236 * operations that require authentication. | 236 * operations that require authentication. |
| 237 */ | 237 */ |
| 238 public void fireRefreshTokenAvailable(Account account) { | 238 public void fireRefreshTokenAvailable(Account account) { |
| 239 ThreadUtils.assertOnUiThread(); | 239 ThreadUtils.assertOnUiThread(); |
| 240 assert account != null; | 240 assert account != null; |
| 241 nativeFireRefreshTokenAvailableFromJava(mNativeProfileOAuth2TokenService
, account.name); | 241 nativeFireRefreshTokenAvailableFromJava( |
| 242 mNativeOAuth2TokenServiceDelegateAndroid, account.name); |
| 242 } | 243 } |
| 243 | 244 |
| 244 @CalledByNative | 245 @CalledByNative |
| 245 public void notifyRefreshTokenAvailable(String accountName) { | 246 public void notifyRefreshTokenAvailable(String accountName) { |
| 246 assert accountName != null; | 247 assert accountName != null; |
| 247 Account account = AccountManagerHelper.createAccountFromName(accountName
); | 248 Account account = AccountManagerHelper.createAccountFromName(accountName
); |
| 248 for (OAuth2TokenServiceObserver observer : mObservers) { | 249 for (OAuth2TokenServiceObserver observer : mObservers) { |
| 249 observer.onRefreshTokenAvailable(account); | 250 observer.onRefreshTokenAvailable(account); |
| 250 } | 251 } |
| 251 } | 252 } |
| 252 | 253 |
| 253 /** | 254 /** |
| 254 * Triggers a notification to all observers of the native and Java instance
of the | 255 * Triggers a notification to all observers of the native and Java instance
of the |
| 255 * OAuth2TokenService that a refresh token is now revoked. | 256 * OAuth2TokenService that a refresh token is now revoked. |
| 256 */ | 257 */ |
| 257 public void fireRefreshTokenRevoked(Account account) { | 258 public void fireRefreshTokenRevoked(Account account) { |
| 258 ThreadUtils.assertOnUiThread(); | 259 ThreadUtils.assertOnUiThread(); |
| 259 assert account != null; | 260 assert account != null; |
| 260 nativeFireRefreshTokenRevokedFromJava(mNativeProfileOAuth2TokenService,
account.name); | 261 nativeFireRefreshTokenRevokedFromJava( |
| 262 mNativeOAuth2TokenServiceDelegateAndroid, account.name); |
| 261 } | 263 } |
| 262 | 264 |
| 263 @CalledByNative | 265 @CalledByNative |
| 264 public void notifyRefreshTokenRevoked(String accountName) { | 266 public void notifyRefreshTokenRevoked(String accountName) { |
| 265 assert accountName != null; | 267 assert accountName != null; |
| 266 Account account = AccountManagerHelper.createAccountFromName(accountName
); | 268 Account account = AccountManagerHelper.createAccountFromName(accountName
); |
| 267 for (OAuth2TokenServiceObserver observer : mObservers) { | 269 for (OAuth2TokenServiceObserver observer : mObservers) { |
| 268 observer.onRefreshTokenRevoked(account); | 270 observer.onRefreshTokenRevoked(account); |
| 269 } | 271 } |
| 270 } | 272 } |
| 271 | 273 |
| 272 /** | 274 /** |
| 273 * Triggers a notification to all observers of the native and Java instance
of the | 275 * Triggers a notification to all observers of the native and Java instance
of the |
| 274 * OAuth2TokenService that all refresh tokens now have been loaded. | 276 * OAuth2TokenService that all refresh tokens now have been loaded. |
| 275 */ | 277 */ |
| 276 public void fireRefreshTokensLoaded() { | 278 public void fireRefreshTokensLoaded() { |
| 277 ThreadUtils.assertOnUiThread(); | 279 ThreadUtils.assertOnUiThread(); |
| 278 nativeFireRefreshTokensLoadedFromJava(mNativeProfileOAuth2TokenService); | 280 nativeFireRefreshTokensLoadedFromJava(mNativeOAuth2TokenServiceDelegateA
ndroid); |
| 279 } | 281 } |
| 280 | 282 |
| 281 @CalledByNative | 283 @CalledByNative |
| 282 public void notifyRefreshTokensLoaded() { | 284 public void notifyRefreshTokensLoaded() { |
| 283 for (OAuth2TokenServiceObserver observer : mObservers) { | 285 for (OAuth2TokenServiceObserver observer : mObservers) { |
| 284 observer.onRefreshTokensLoaded(); | 286 observer.onRefreshTokensLoaded(); |
| 285 } | 287 } |
| 286 } | 288 } |
| 287 | 289 |
| 288 private static String[] getStoredAccounts(Context context) { | 290 private static String[] getStoredAccounts(Context context) { |
| 289 Set<String> accounts = | 291 Set<String> accounts = |
| 290 PreferenceManager.getDefaultSharedPreferences(context) | 292 PreferenceManager.getDefaultSharedPreferences(context) |
| 291 .getStringSet(STORED_ACCOUNTS_KEY, null); | 293 .getStringSet(STORED_ACCOUNTS_KEY, null); |
| 292 return accounts == null ? new String[]{} : accounts.toArray(new String[a
ccounts.size()]); | 294 return accounts == null ? new String[]{} : accounts.toArray(new String[a
ccounts.size()]); |
| 293 } | 295 } |
| 294 | 296 |
| 295 @CalledByNative | 297 @CalledByNative |
| 296 private static void saveStoredAccounts(Context context, String[] accounts) { | 298 private static void saveStoredAccounts(Context context, String[] accounts) { |
| 297 Set<String> set = new HashSet<String>(Arrays.asList(accounts)); | 299 Set<String> set = new HashSet<String>(Arrays.asList(accounts)); |
| 298 PreferenceManager.getDefaultSharedPreferences(context).edit() | 300 PreferenceManager.getDefaultSharedPreferences(context).edit() |
| 299 .putStringSet(STORED_ACCOUNTS_KEY, set).apply(); | 301 .putStringSet(STORED_ACCOUNTS_KEY, set).apply(); |
| 300 } | 302 } |
| 301 | 303 |
| 302 private static native Object nativeGetForProfile(Profile profile); | 304 private static native Object nativeGetForProfile(Profile profile); |
| 303 private static native void nativeOAuth2TokenFetched( | 305 private static native void nativeOAuth2TokenFetched( |
| 304 String authToken, boolean result, long nativeCallback); | 306 String authToken, boolean result, long nativeCallback); |
| 305 private native void nativeValidateAccounts( | 307 private native void nativeValidateAccounts(long nativeOAuth2TokenServiceDele
gateAndroid, |
| 306 long nativeAndroidProfileOAuth2TokenService, | 308 String currentlySignedInAccount, boolean forceNotifications); |
| 307 String currentlySignedInAccount, | |
| 308 boolean forceNotifications); | |
| 309 private native void nativeFireRefreshTokenAvailableFromJava( | 309 private native void nativeFireRefreshTokenAvailableFromJava( |
| 310 long nativeAndroidProfileOAuth2TokenService, String accountName); | 310 long nativeOAuth2TokenServiceDelegateAndroid, String accountName); |
| 311 private native void nativeFireRefreshTokenRevokedFromJava( | 311 private native void nativeFireRefreshTokenRevokedFromJava( |
| 312 long nativeAndroidProfileOAuth2TokenService, String accountName); | 312 long nativeOAuth2TokenServiceDelegateAndroid, String accountName); |
| 313 private native void nativeFireRefreshTokensLoadedFromJava( | 313 private native void nativeFireRefreshTokensLoadedFromJava( |
| 314 long nativeAndroidProfileOAuth2TokenService); | 314 long nativeOAuth2TokenServiceDelegateAndroid); |
| 315 } | 315 } |
| OLD | NEW |