| 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 #include "chrome/browser/signin/oauth2_token_service_delegate_android.h" | 5 #include "chrome/browser/signin/oauth2_token_service_delegate_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "chrome/browser/profiles/profile_android.h" | 12 #include "chrome/browser/profiles/profile_android.h" |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 14 #include "chrome/browser/sync/profile_sync_service_android.h" | 14 #include "chrome/browser/sync/profile_sync_service_android.h" |
| 15 #include "components/signin/core/browser/account_info.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "google_apis/gaia/gaia_auth_util.h" | 17 #include "google_apis/gaia/gaia_auth_util.h" |
| 17 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 18 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 18 #include "jni/OAuth2TokenService_jni.h" | 19 #include "jni/OAuth2TokenService_jni.h" |
| 19 | 20 |
| 20 using base::android::AttachCurrentThread; | 21 using base::android::AttachCurrentThread; |
| 21 using base::android::ConvertJavaStringToUTF8; | 22 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ConvertUTF8ToJavaString; | 23 using base::android::ConvertUTF8ToJavaString; |
| 23 using base::android::ScopedJavaLocalRef; | 24 using base::android::ScopedJavaLocalRef; |
| 24 using content::BrowserThread; | 25 using content::BrowserThread; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 132 |
| 132 bool OAuth2TokenServiceDelegateAndroid::is_testing_profile_ = false; | 133 bool OAuth2TokenServiceDelegateAndroid::is_testing_profile_ = false; |
| 133 | 134 |
| 134 OAuth2TokenServiceDelegateAndroid::ErrorInfo::ErrorInfo() | 135 OAuth2TokenServiceDelegateAndroid::ErrorInfo::ErrorInfo() |
| 135 : error(GoogleServiceAuthError::NONE) {} | 136 : error(GoogleServiceAuthError::NONE) {} |
| 136 | 137 |
| 137 OAuth2TokenServiceDelegateAndroid::ErrorInfo::ErrorInfo( | 138 OAuth2TokenServiceDelegateAndroid::ErrorInfo::ErrorInfo( |
| 138 const GoogleServiceAuthError& error) | 139 const GoogleServiceAuthError& error) |
| 139 : error(error) {} | 140 : error(error) {} |
| 140 | 141 |
| 141 OAuth2TokenServiceDelegateAndroid::OAuth2TokenServiceDelegateAndroid() { | 142 OAuth2TokenServiceDelegateAndroid::OAuth2TokenServiceDelegateAndroid( |
| 143 AccountTrackerService* account_tracker_service) |
| 144 : account_tracker_service_(account_tracker_service), |
| 145 fire_refresh_token_loaded_(RT_LOAD_NOT_START) { |
| 142 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ctor"; | 146 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ctor"; |
| 147 DCHECK(account_tracker_service_); |
| 143 JNIEnv* env = AttachCurrentThread(); | 148 JNIEnv* env = AttachCurrentThread(); |
| 144 base::android::ScopedJavaLocalRef<jobject> local_java_ref = | 149 base::android::ScopedJavaLocalRef<jobject> local_java_ref = |
| 145 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); | 150 Java_OAuth2TokenService_create(env, |
| 151 base::android::GetApplicationContext(), |
| 152 reinterpret_cast<intptr_t>(this)); |
| 146 java_ref_.Reset(env, local_java_ref.obj()); | 153 java_ref_.Reset(env, local_java_ref.obj()); |
| 154 |
| 155 if (account_tracker_service_->GetMigrationState() == |
| 156 AccountTrackerService::MIGRATION_IN_PROGRESS) { |
| 157 std::vector<std::string> accounts = GetAccounts(); |
| 158 std::vector<std::string> accounts_id; |
| 159 for (auto account_name : accounts) { |
| 160 AccountInfo account_info = |
| 161 account_tracker_service_->FindAccountInfoByEmail(account_name); |
| 162 DCHECK(!account_info.gaia.empty()); |
| 163 accounts_id.push_back(account_info.gaia); |
| 164 } |
| 165 ScopedJavaLocalRef<jobjectArray> java_accounts( |
| 166 base::android::ToJavaArrayOfStrings(env, accounts_id)); |
| 167 Java_OAuth2TokenService_saveStoredAccounts( |
| 168 env, base::android::GetApplicationContext(), java_accounts.obj()); |
| 169 } |
| 170 |
| 171 if (!is_testing_profile_) { |
| 172 Java_OAuth2TokenService_validateAccounts( |
| 173 AttachCurrentThread(), java_ref_.obj(), |
| 174 base::android::GetApplicationContext(), JNI_TRUE); |
| 175 } |
| 147 } | 176 } |
| 148 | 177 |
| 149 OAuth2TokenServiceDelegateAndroid::~OAuth2TokenServiceDelegateAndroid() { | 178 OAuth2TokenServiceDelegateAndroid::~OAuth2TokenServiceDelegateAndroid() { |
| 150 } | 179 } |
| 151 | 180 |
| 152 // static | 181 // static |
| 153 ScopedJavaLocalRef<jobject> OAuth2TokenServiceDelegateAndroid::GetForProfile( | 182 ScopedJavaLocalRef<jobject> OAuth2TokenServiceDelegateAndroid::GetForProfile( |
| 154 JNIEnv* env, | 183 JNIEnv* env, |
| 155 jclass clazz, | 184 jclass clazz, |
| 156 jobject j_profile_android) { | 185 jobject j_profile_android) { |
| 157 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android); | 186 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android); |
| 158 ProfileOAuth2TokenService* service = | 187 ProfileOAuth2TokenService* service = |
| 159 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 188 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 160 OAuth2TokenServiceDelegate* delegate = service->GetDelegate(); | 189 OAuth2TokenServiceDelegate* delegate = service->GetDelegate(); |
| 161 return ScopedJavaLocalRef<jobject>( | 190 return ScopedJavaLocalRef<jobject>( |
| 162 static_cast<OAuth2TokenServiceDelegateAndroid*>(delegate)->java_ref_); | 191 static_cast<OAuth2TokenServiceDelegateAndroid*>(delegate)->java_ref_); |
| 163 } | 192 } |
| 164 | 193 |
| 165 static ScopedJavaLocalRef<jobject> GetForProfile( | 194 static ScopedJavaLocalRef<jobject> GetForProfile( |
| 166 JNIEnv* env, | 195 JNIEnv* env, |
| 167 const JavaParamRef<jclass>& clazz, | 196 const JavaParamRef<jclass>& clazz, |
| 168 const JavaParamRef<jobject>& j_profile_android) { | 197 const JavaParamRef<jobject>& j_profile_android) { |
| 169 return OAuth2TokenServiceDelegateAndroid::GetForProfile(env, clazz, | 198 return OAuth2TokenServiceDelegateAndroid::GetForProfile(env, clazz, |
| 170 j_profile_android); | 199 j_profile_android); |
| 171 } | 200 } |
| 172 | 201 |
| 173 void OAuth2TokenServiceDelegateAndroid::Initialize() { | |
| 174 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::Initialize"; | |
| 175 if (!is_testing_profile_) { | |
| 176 Java_OAuth2TokenService_validateAccounts( | |
| 177 AttachCurrentThread(), java_ref_.obj(), | |
| 178 base::android::GetApplicationContext(), JNI_TRUE); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 bool OAuth2TokenServiceDelegateAndroid::RefreshTokenIsAvailable( | 202 bool OAuth2TokenServiceDelegateAndroid::RefreshTokenIsAvailable( |
| 183 const std::string& account_id) const { | 203 const std::string& account_id) const { |
| 204 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::RefreshTokenIsAvailable" |
| 205 << " account= " << account_id; |
| 206 std::string account_name = MapAccountIdToAccountName(account_id); |
| 184 JNIEnv* env = AttachCurrentThread(); | 207 JNIEnv* env = AttachCurrentThread(); |
| 185 ScopedJavaLocalRef<jstring> j_account_id = | 208 ScopedJavaLocalRef<jstring> j_account_id = |
| 186 ConvertUTF8ToJavaString(env, account_id); | 209 ConvertUTF8ToJavaString(env, account_name); |
| 187 jboolean refresh_token_is_available = | 210 jboolean refresh_token_is_available = |
| 188 Java_OAuth2TokenService_hasOAuth2RefreshToken( | 211 Java_OAuth2TokenService_hasOAuth2RefreshToken( |
| 189 env, base::android::GetApplicationContext(), j_account_id.obj()); | 212 env, base::android::GetApplicationContext(), j_account_id.obj()); |
| 190 return refresh_token_is_available == JNI_TRUE; | 213 return refresh_token_is_available == JNI_TRUE; |
| 191 } | 214 } |
| 192 | 215 |
| 193 bool OAuth2TokenServiceDelegateAndroid::RefreshTokenHasError( | 216 bool OAuth2TokenServiceDelegateAndroid::RefreshTokenHasError( |
| 194 const std::string& account_id) const { | 217 const std::string& account_id) const { |
| 195 auto it = errors_.find(account_id); | 218 auto it = errors_.find(account_id); |
| 196 // TODO(rogerta): should we distinguish between transient and persistent? | 219 // TODO(rogerta): should we distinguish between transient and persistent? |
| (...skipping 20 matching lines...) Expand all Loading... |
| 217 ScopedJavaLocalRef<jobjectArray> j_accounts = | 240 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 218 Java_OAuth2TokenService_getAccounts( | 241 Java_OAuth2TokenService_getAccounts( |
| 219 env, base::android::GetApplicationContext()); | 242 env, base::android::GetApplicationContext()); |
| 220 // TODO(fgorski): We may decide to filter out some of the accounts. | 243 // TODO(fgorski): We may decide to filter out some of the accounts. |
| 221 base::android::AppendJavaStringArrayToStringVector(env, j_accounts.obj(), | 244 base::android::AppendJavaStringArrayToStringVector(env, j_accounts.obj(), |
| 222 &accounts); | 245 &accounts); |
| 223 return accounts; | 246 return accounts; |
| 224 } | 247 } |
| 225 | 248 |
| 226 std::vector<std::string> | 249 std::vector<std::string> |
| 227 OAuth2TokenServiceDelegateAndroid::GetSystemAccounts() { | 250 OAuth2TokenServiceDelegateAndroid::GetSystemAccountNames() { |
| 228 std::vector<std::string> accounts; | 251 std::vector<std::string> account_names; |
| 229 JNIEnv* env = AttachCurrentThread(); | 252 JNIEnv* env = AttachCurrentThread(); |
| 230 ScopedJavaLocalRef<jobjectArray> j_accounts = | 253 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 231 Java_OAuth2TokenService_getSystemAccounts( | 254 Java_OAuth2TokenService_getSystemAccountNames( |
| 232 env, base::android::GetApplicationContext()); | 255 env, base::android::GetApplicationContext()); |
| 233 base::android::AppendJavaStringArrayToStringVector(env, j_accounts.obj(), | 256 base::android::AppendJavaStringArrayToStringVector(env, j_accounts.obj(), |
| 234 &accounts); | 257 &account_names); |
| 235 return accounts; | 258 return account_names; |
| 236 } | 259 } |
| 237 | 260 |
| 238 OAuth2AccessTokenFetcher* | 261 OAuth2AccessTokenFetcher* |
| 239 OAuth2TokenServiceDelegateAndroid::CreateAccessTokenFetcher( | 262 OAuth2TokenServiceDelegateAndroid::CreateAccessTokenFetcher( |
| 240 const std::string& account_id, | 263 const std::string& account_id, |
| 241 net::URLRequestContextGetter* getter, | 264 net::URLRequestContextGetter* getter, |
| 242 OAuth2AccessTokenConsumer* consumer) { | 265 OAuth2AccessTokenConsumer* consumer) { |
| 266 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::CreateAccessTokenFetcher" |
| 267 << " account= " << account_id; |
| 243 ValidateAccountId(account_id); | 268 ValidateAccountId(account_id); |
| 244 return new AndroidAccessTokenFetcher(consumer, account_id); | 269 return new AndroidAccessTokenFetcher(consumer, |
| 270 MapAccountIdToAccountName(account_id)); |
| 245 } | 271 } |
| 246 | 272 |
| 247 void OAuth2TokenServiceDelegateAndroid::InvalidateAccessToken( | 273 void OAuth2TokenServiceDelegateAndroid::InvalidateAccessToken( |
| 248 const std::string& account_id, | 274 const std::string& account_id, |
| 249 const std::string& client_id, | 275 const std::string& client_id, |
| 250 const OAuth2TokenService::ScopeSet& scopes, | 276 const OAuth2TokenService::ScopeSet& scopes, |
| 251 const std::string& access_token) { | 277 const std::string& access_token) { |
| 252 ValidateAccountId(account_id); | 278 ValidateAccountId(account_id); |
| 253 JNIEnv* env = AttachCurrentThread(); | 279 JNIEnv* env = AttachCurrentThread(); |
| 254 ScopedJavaLocalRef<jstring> j_access_token = | 280 ScopedJavaLocalRef<jstring> j_access_token = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 265 std::string signed_in_account; | 291 std::string signed_in_account; |
| 266 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts from java"; | 292 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts from java"; |
| 267 if (j_current_acc) | 293 if (j_current_acc) |
| 268 signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); | 294 signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); |
| 269 if (!signed_in_account.empty()) | 295 if (!signed_in_account.empty()) |
| 270 signed_in_account = gaia::CanonicalizeEmail(signed_in_account); | 296 signed_in_account = gaia::CanonicalizeEmail(signed_in_account); |
| 271 | 297 |
| 272 // Clear any auth errors so that client can retry to get access tokens. | 298 // Clear any auth errors so that client can retry to get access tokens. |
| 273 errors_.clear(); | 299 errors_.clear(); |
| 274 | 300 |
| 275 ValidateAccounts(signed_in_account, j_force_notifications != JNI_FALSE); | 301 ValidateAccounts(MapAccountNameToAccountId(signed_in_account), |
| 302 j_force_notifications != JNI_FALSE); |
| 276 } | 303 } |
| 277 | 304 |
| 278 void OAuth2TokenServiceDelegateAndroid::ValidateAccounts( | 305 void OAuth2TokenServiceDelegateAndroid::ValidateAccounts( |
| 279 const std::string& signed_in_account, | 306 const std::string& signed_in_account, |
| 280 bool force_notifications) { | 307 bool force_notifications) { |
| 281 std::vector<std::string> prev_ids = GetAccounts(); | 308 std::vector<std::string> prev_ids = GetAccounts(); |
| 282 std::vector<std::string> curr_ids = GetSystemAccounts(); | 309 std::vector<std::string> curr_ids = GetSystemAccountNames(); |
| 283 std::vector<std::string> refreshed_ids; | 310 std::vector<std::string> refreshed_ids; |
| 284 std::vector<std::string> revoked_ids; | 311 std::vector<std::string> revoked_ids; |
| 285 | 312 |
| 286 // Canonicalize system accounts. |prev_ids| is already done. | |
| 287 for (size_t i = 0; i < curr_ids.size(); ++i) | 313 for (size_t i = 0; i < curr_ids.size(); ++i) |
| 288 curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]); | 314 curr_ids[i] = MapAccountNameToAccountId(curr_ids[i]); |
| 315 |
| 289 for (size_t i = 0; i < prev_ids.size(); ++i) | 316 for (size_t i = 0; i < prev_ids.size(); ++i) |
| 290 ValidateAccountId(prev_ids[i]); | 317 ValidateAccountId(prev_ids[i]); |
| 291 | 318 |
| 292 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" | 319 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 293 << " sigined_in_account=" << signed_in_account | 320 << " sigined_in_account=" << signed_in_account |
| 294 << " prev_ids=" << prev_ids.size() << " curr_ids=" << curr_ids.size() | 321 << " prev_ids=" << prev_ids.size() << " curr_ids=" << curr_ids.size() |
| 295 << " force=" << (force_notifications ? "true" : "false"); | 322 << " force=" << (force_notifications ? "true" : "false"); |
| 296 | 323 |
| 297 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, | 324 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, |
| 298 revoked_ids, force_notifications)) { | 325 revoked_ids, force_notifications)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 309 | 336 |
| 310 for (std::vector<std::string>::iterator it = refreshed_ids.begin(); | 337 for (std::vector<std::string>::iterator it = refreshed_ids.begin(); |
| 311 it != refreshed_ids.end(); it++) { | 338 it != refreshed_ids.end(); it++) { |
| 312 FireRefreshTokenAvailable(*it); | 339 FireRefreshTokenAvailable(*it); |
| 313 } | 340 } |
| 314 | 341 |
| 315 for (std::vector<std::string>::iterator it = revoked_ids.begin(); | 342 for (std::vector<std::string>::iterator it = revoked_ids.begin(); |
| 316 it != revoked_ids.end(); it++) { | 343 it != revoked_ids.end(); it++) { |
| 317 FireRefreshTokenRevoked(*it); | 344 FireRefreshTokenRevoked(*it); |
| 318 } | 345 } |
| 346 |
| 347 if (fire_refresh_token_loaded_ == RT_WAIT_FOR_VALIDATION) { |
| 348 fire_refresh_token_loaded_ = RT_LOADED; |
| 349 FireRefreshTokensLoaded(); |
| 350 } else if (fire_refresh_token_loaded_ == RT_LOAD_NOT_START) { |
| 351 fire_refresh_token_loaded_ = RT_HAS_BEEN_VALIDATED; |
| 352 } |
| 319 } | 353 } |
| 320 | 354 |
| 321 bool OAuth2TokenServiceDelegateAndroid::ValidateAccounts( | 355 bool OAuth2TokenServiceDelegateAndroid::ValidateAccounts( |
| 322 const std::string& signed_in_account, | 356 const std::string& signed_in_account, |
| 323 const std::vector<std::string>& prev_account_ids, | 357 const std::vector<std::string>& prev_account_ids, |
| 324 const std::vector<std::string>& curr_account_ids, | 358 const std::vector<std::string>& curr_account_ids, |
| 325 std::vector<std::string>& refreshed_ids, | 359 std::vector<std::string>& refreshed_ids, |
| 326 std::vector<std::string>& revoked_ids, | 360 std::vector<std::string>& revoked_ids, |
| 327 bool force_notifications) { | 361 bool force_notifications) { |
| 328 if (std::find(curr_account_ids.begin(), curr_account_ids.end(), | 362 if (std::find(curr_account_ids.begin(), curr_account_ids.end(), |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 revoked_ids.push_back(*it); | 416 revoked_ids.push_back(*it); |
| 383 } | 417 } |
| 384 return false; | 418 return false; |
| 385 } | 419 } |
| 386 } | 420 } |
| 387 | 421 |
| 388 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailableFromJava( | 422 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailableFromJava( |
| 389 JNIEnv* env, | 423 JNIEnv* env, |
| 390 jobject obj, | 424 jobject obj, |
| 391 const jstring account_name) { | 425 const jstring account_name) { |
| 392 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 426 std::string account_id = |
| 427 MapAccountNameToAccountId(ConvertJavaStringToUTF8(env, account_name)); |
| 393 // Notify native observers. | 428 // Notify native observers. |
| 394 FireRefreshTokenAvailable(account_id); | 429 FireRefreshTokenAvailable(account_id); |
| 395 } | 430 } |
| 396 | 431 |
| 397 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable( | 432 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable( |
| 398 const std::string& account_id) { | 433 const std::string& account_id) { |
| 399 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable id=" | 434 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable id=" |
| 400 << account_id; | 435 << account_id; |
| 401 JNIEnv* env = AttachCurrentThread(); | 436 JNIEnv* env = AttachCurrentThread(); |
| 402 ScopedJavaLocalRef<jstring> account_name = | 437 ScopedJavaLocalRef<jstring> account_name = |
| 403 ConvertUTF8ToJavaString(env, account_id); | 438 ConvertUTF8ToJavaString(env, MapAccountIdToAccountName(account_id)); |
| 404 Java_OAuth2TokenService_notifyRefreshTokenAvailable(env, java_ref_.obj(), | 439 Java_OAuth2TokenService_notifyRefreshTokenAvailable(env, java_ref_.obj(), |
| 405 account_name.obj()); | 440 account_name.obj()); |
| 406 OAuth2TokenServiceDelegate::FireRefreshTokenAvailable(account_id); | 441 OAuth2TokenServiceDelegate::FireRefreshTokenAvailable(account_id); |
| 407 } | 442 } |
| 408 | 443 |
| 409 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevokedFromJava( | 444 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevokedFromJava( |
| 410 JNIEnv* env, | 445 JNIEnv* env, |
| 411 jobject obj, | 446 jobject obj, |
| 412 const jstring account_name) { | 447 const jstring account_name) { |
| 413 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 448 std::string account_id = |
| 449 MapAccountNameToAccountId(ConvertJavaStringToUTF8(env, account_name)); |
| 414 // Notify native observers. | 450 // Notify native observers. |
| 415 FireRefreshTokenRevoked(account_id); | 451 FireRefreshTokenRevoked(account_id); |
| 416 } | 452 } |
| 417 | 453 |
| 418 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked( | 454 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked( |
| 419 const std::string& account_id) { | 455 const std::string& account_id) { |
| 420 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked id=" | 456 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked id=" |
| 421 << account_id; | 457 << account_id; |
| 422 JNIEnv* env = AttachCurrentThread(); | 458 JNIEnv* env = AttachCurrentThread(); |
| 423 ScopedJavaLocalRef<jstring> account_name = | 459 ScopedJavaLocalRef<jstring> account_name = |
| 424 ConvertUTF8ToJavaString(env, account_id); | 460 ConvertUTF8ToJavaString(env, MapAccountIdToAccountName(account_id)); |
| 425 Java_OAuth2TokenService_notifyRefreshTokenRevoked(env, java_ref_.obj(), | 461 Java_OAuth2TokenService_notifyRefreshTokenRevoked(env, java_ref_.obj(), |
| 426 account_name.obj()); | 462 account_name.obj()); |
| 427 OAuth2TokenServiceDelegate::FireRefreshTokenRevoked(account_id); | 463 OAuth2TokenServiceDelegate::FireRefreshTokenRevoked(account_id); |
| 428 } | 464 } |
| 429 | 465 |
| 430 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoadedFromJava( | 466 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoadedFromJava( |
| 431 JNIEnv* env, | 467 JNIEnv* env, |
| 432 jobject obj) { | 468 jobject obj) { |
| 433 // Notify native observers. | 469 // Notify native observers. |
| 434 FireRefreshTokensLoaded(); | 470 FireRefreshTokensLoaded(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 452 | 488 |
| 453 // Clear everything on the Java side as well. | 489 // Clear everything on the Java side as well. |
| 454 std::vector<std::string> empty; | 490 std::vector<std::string> empty; |
| 455 JNIEnv* env = AttachCurrentThread(); | 491 JNIEnv* env = AttachCurrentThread(); |
| 456 ScopedJavaLocalRef<jobjectArray> java_accounts( | 492 ScopedJavaLocalRef<jobjectArray> java_accounts( |
| 457 base::android::ToJavaArrayOfStrings(env, empty)); | 493 base::android::ToJavaArrayOfStrings(env, empty)); |
| 458 Java_OAuth2TokenService_saveStoredAccounts( | 494 Java_OAuth2TokenService_saveStoredAccounts( |
| 459 env, base::android::GetApplicationContext(), java_accounts.obj()); | 495 env, base::android::GetApplicationContext(), java_accounts.obj()); |
| 460 } | 496 } |
| 461 | 497 |
| 498 void OAuth2TokenServiceDelegateAndroid::LoadCredentials( |
| 499 const std::string& primary_account_id) { |
| 500 if (fire_refresh_token_loaded_ == RT_HAS_BEEN_VALIDATED) { |
| 501 fire_refresh_token_loaded_ = RT_LOADED; |
| 502 FireRefreshTokensLoaded(); |
| 503 } else if (fire_refresh_token_loaded_ == RT_LOAD_NOT_START) { |
| 504 fire_refresh_token_loaded_ = RT_WAIT_FOR_VALIDATION; |
| 505 } |
| 506 } |
| 507 |
| 508 std::string OAuth2TokenServiceDelegateAndroid::MapAccountIdToAccountName( |
| 509 const std::string& account_id) const { |
| 510 std::string account_name = |
| 511 account_tracker_service_->GetAccountInfo(account_id).email; |
| 512 DCHECK(!account_name.empty() || account_id.empty()); |
| 513 return account_name; |
| 514 } |
| 515 |
| 516 std::string OAuth2TokenServiceDelegateAndroid::MapAccountNameToAccountId( |
| 517 const std::string& account_name) const { |
| 518 std::string account_id = |
| 519 account_tracker_service_->FindAccountInfoByEmail(account_name).account_id; |
| 520 DCHECK(!account_id.empty() || account_name.empty()); |
| 521 return account_id; |
| 522 } |
| 523 |
| 462 // Called from Java when fetching of an OAuth2 token is finished. The | 524 // Called from Java when fetching of an OAuth2 token is finished. The |
| 463 // |authToken| param is only valid when |result| is true. | 525 // |authToken| param is only valid when |result| is true. |
| 464 void OAuth2TokenFetched(JNIEnv* env, | 526 void OAuth2TokenFetched(JNIEnv* env, |
| 465 const JavaParamRef<jclass>& clazz, | 527 const JavaParamRef<jclass>& clazz, |
| 466 const JavaParamRef<jstring>& authToken, | 528 const JavaParamRef<jstring>& authToken, |
| 467 jboolean isTransientError, | 529 jboolean isTransientError, |
| 468 jlong nativeCallback) { | 530 jlong nativeCallback) { |
| 469 std::string token; | 531 std::string token; |
| 470 if (authToken) | 532 if (authToken) |
| 471 token = ConvertJavaStringToUTF8(env, authToken); | 533 token = ConvertJavaStringToUTF8(env, authToken); |
| 472 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 534 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 473 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 535 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 474 GoogleServiceAuthError | 536 GoogleServiceAuthError |
| 475 err(authToken | 537 err(authToken |
| 476 ? GoogleServiceAuthError::NONE | 538 ? GoogleServiceAuthError::NONE |
| 477 : isTransientError | 539 : isTransientError |
| 478 ? GoogleServiceAuthError::CONNECTION_FAILED | 540 ? GoogleServiceAuthError::CONNECTION_FAILED |
| 479 : GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 541 : GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 480 heap_callback->Run(err, token, base::Time()); | 542 heap_callback->Run(err, token, base::Time()); |
| 481 } | 543 } |
| 482 | 544 |
| 483 // static | 545 // static |
| 484 bool OAuth2TokenServiceDelegateAndroid::Register(JNIEnv* env) { | 546 bool OAuth2TokenServiceDelegateAndroid::Register(JNIEnv* env) { |
| 485 return RegisterNativesImpl(env); | 547 return RegisterNativesImpl(env); |
| 486 } | 548 } |
| OLD | NEW |