| 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; |
| 312 bool account_validation_result = true; |
| 285 | 313 |
| 286 // Canonicalize system accounts. |prev_ids| is already done. | |
| 287 for (size_t i = 0; i < curr_ids.size(); ++i) | 314 for (size_t i = 0; i < curr_ids.size(); ++i) |
| 288 curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]); | 315 curr_ids[i] = MapAccountNameToAccountId(curr_ids[i]); |
| 316 |
| 289 for (size_t i = 0; i < prev_ids.size(); ++i) | 317 for (size_t i = 0; i < prev_ids.size(); ++i) |
| 290 ValidateAccountId(prev_ids[i]); | 318 ValidateAccountId(prev_ids[i]); |
| 291 | 319 |
| 292 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" | 320 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 293 << " sigined_in_account=" << signed_in_account | 321 << " sigined_in_account=" << signed_in_account |
| 294 << " prev_ids=" << prev_ids.size() << " curr_ids=" << curr_ids.size() | 322 << " prev_ids=" << prev_ids.size() << " curr_ids=" << curr_ids.size() |
| 295 << " force=" << (force_notifications ? "true" : "false"); | 323 << " force=" << (force_notifications ? "true" : "false"); |
| 296 | 324 |
| 297 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, | 325 account_validation_result = |
| 298 revoked_ids, force_notifications)) { | 326 ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, |
| 299 curr_ids.clear(); | 327 revoked_ids, force_notifications); |
| 300 } | |
| 301 | 328 |
| 302 ScopedBatchChange batch(this); | 329 ScopedBatchChange batch(this); |
| 303 | 330 |
| 304 JNIEnv* env = AttachCurrentThread(); | 331 JNIEnv* env = AttachCurrentThread(); |
| 305 ScopedJavaLocalRef<jobjectArray> java_accounts( | 332 ScopedJavaLocalRef<jobjectArray> java_accounts; |
| 306 base::android::ToJavaArrayOfStrings(env, curr_ids)); | 333 if (account_validation_result) { |
| 334 java_accounts = base::android::ToJavaArrayOfStrings(env, curr_ids); |
| 335 } else { |
| 336 java_accounts = |
| 337 base::android::ToJavaArrayOfStrings(env, std::vector<std::string>()); |
| 338 } |
| 307 Java_OAuth2TokenService_saveStoredAccounts( | 339 Java_OAuth2TokenService_saveStoredAccounts( |
| 308 env, base::android::GetApplicationContext(), java_accounts.obj()); | 340 env, base::android::GetApplicationContext(), java_accounts.obj()); |
| 309 | 341 |
| 310 for (std::vector<std::string>::iterator it = refreshed_ids.begin(); | 342 for (std::vector<std::string>::iterator it = refreshed_ids.begin(); |
| 311 it != refreshed_ids.end(); it++) { | 343 it != refreshed_ids.end(); it++) { |
| 312 FireRefreshTokenAvailable(*it); | 344 FireRefreshTokenAvailable(*it); |
| 313 } | 345 } |
| 314 | 346 |
| 315 for (std::vector<std::string>::iterator it = revoked_ids.begin(); | 347 for (std::vector<std::string>::iterator it = revoked_ids.begin(); |
| 316 it != revoked_ids.end(); it++) { | 348 it != revoked_ids.end(); it++) { |
| 317 FireRefreshTokenRevoked(*it); | 349 FireRefreshTokenRevoked(*it); |
| 318 } | 350 } |
| 351 |
| 352 if (fire_refresh_token_loaded_ == RT_WAIT_FOR_VALIDATION) { |
| 353 fire_refresh_token_loaded_ = RT_LOADED; |
| 354 FireRefreshTokensLoaded(); |
| 355 } else if (fire_refresh_token_loaded_ == RT_LOAD_NOT_START) { |
| 356 fire_refresh_token_loaded_ = RT_HAS_BEEN_VALIDATED; |
| 357 } |
| 358 |
| 359 // Clear accounts no longer exist on device from AccountTrackerService. |
| 360 std::vector<AccountInfo> accounts_info = |
| 361 account_tracker_service_->GetAccounts(); |
| 362 for (auto info : accounts_info) { |
| 363 auto it = curr_ids.begin(); |
| 364 for (; it != curr_ids.end(); ++it) { |
| 365 if (*it == info.account_id) |
| 366 break; |
| 367 } |
| 368 if (it == curr_ids.end()) |
| 369 account_tracker_service_->RemoveAccount(info.account_id); |
| 370 } |
| 319 } | 371 } |
| 320 | 372 |
| 321 bool OAuth2TokenServiceDelegateAndroid::ValidateAccounts( | 373 bool OAuth2TokenServiceDelegateAndroid::ValidateAccounts( |
| 322 const std::string& signed_in_account, | 374 const std::string& signed_in_account, |
| 323 const std::vector<std::string>& prev_account_ids, | 375 const std::vector<std::string>& prev_account_ids, |
| 324 const std::vector<std::string>& curr_account_ids, | 376 const std::vector<std::string>& curr_account_ids, |
| 325 std::vector<std::string>& refreshed_ids, | 377 std::vector<std::string>& refreshed_ids, |
| 326 std::vector<std::string>& revoked_ids, | 378 std::vector<std::string>& revoked_ids, |
| 327 bool force_notifications) { | 379 bool force_notifications) { |
| 328 if (std::find(curr_account_ids.begin(), curr_account_ids.end(), | 380 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); | 434 revoked_ids.push_back(*it); |
| 383 } | 435 } |
| 384 return false; | 436 return false; |
| 385 } | 437 } |
| 386 } | 438 } |
| 387 | 439 |
| 388 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailableFromJava( | 440 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailableFromJava( |
| 389 JNIEnv* env, | 441 JNIEnv* env, |
| 390 jobject obj, | 442 jobject obj, |
| 391 const jstring account_name) { | 443 const jstring account_name) { |
| 392 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 444 std::string account_id = |
| 445 MapAccountNameToAccountId(ConvertJavaStringToUTF8(env, account_name)); |
| 393 // Notify native observers. | 446 // Notify native observers. |
| 394 FireRefreshTokenAvailable(account_id); | 447 FireRefreshTokenAvailable(account_id); |
| 395 } | 448 } |
| 396 | 449 |
| 397 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable( | 450 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable( |
| 398 const std::string& account_id) { | 451 const std::string& account_id) { |
| 399 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable id=" | 452 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable id=" |
| 400 << account_id; | 453 << account_id; |
| 401 JNIEnv* env = AttachCurrentThread(); | 454 JNIEnv* env = AttachCurrentThread(); |
| 402 ScopedJavaLocalRef<jstring> account_name = | 455 ScopedJavaLocalRef<jstring> account_name = |
| 403 ConvertUTF8ToJavaString(env, account_id); | 456 ConvertUTF8ToJavaString(env, MapAccountIdToAccountName(account_id)); |
| 404 Java_OAuth2TokenService_notifyRefreshTokenAvailable(env, java_ref_.obj(), | 457 Java_OAuth2TokenService_notifyRefreshTokenAvailable(env, java_ref_.obj(), |
| 405 account_name.obj()); | 458 account_name.obj()); |
| 406 OAuth2TokenServiceDelegate::FireRefreshTokenAvailable(account_id); | 459 OAuth2TokenServiceDelegate::FireRefreshTokenAvailable(account_id); |
| 407 } | 460 } |
| 408 | 461 |
| 409 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevokedFromJava( | 462 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevokedFromJava( |
| 410 JNIEnv* env, | 463 JNIEnv* env, |
| 411 jobject obj, | 464 jobject obj, |
| 412 const jstring account_name) { | 465 const jstring account_name) { |
| 413 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 466 std::string account_id = |
| 467 MapAccountNameToAccountId(ConvertJavaStringToUTF8(env, account_name)); |
| 414 // Notify native observers. | 468 // Notify native observers. |
| 415 FireRefreshTokenRevoked(account_id); | 469 FireRefreshTokenRevoked(account_id); |
| 416 } | 470 } |
| 417 | 471 |
| 418 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked( | 472 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked( |
| 419 const std::string& account_id) { | 473 const std::string& account_id) { |
| 420 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked id=" | 474 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked id=" |
| 421 << account_id; | 475 << account_id; |
| 422 JNIEnv* env = AttachCurrentThread(); | 476 JNIEnv* env = AttachCurrentThread(); |
| 423 ScopedJavaLocalRef<jstring> account_name = | 477 ScopedJavaLocalRef<jstring> account_name = |
| 424 ConvertUTF8ToJavaString(env, account_id); | 478 ConvertUTF8ToJavaString(env, MapAccountIdToAccountName(account_id)); |
| 425 Java_OAuth2TokenService_notifyRefreshTokenRevoked(env, java_ref_.obj(), | 479 Java_OAuth2TokenService_notifyRefreshTokenRevoked(env, java_ref_.obj(), |
| 426 account_name.obj()); | 480 account_name.obj()); |
| 427 OAuth2TokenServiceDelegate::FireRefreshTokenRevoked(account_id); | 481 OAuth2TokenServiceDelegate::FireRefreshTokenRevoked(account_id); |
| 428 } | 482 } |
| 429 | 483 |
| 430 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoadedFromJava( | 484 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoadedFromJava( |
| 431 JNIEnv* env, | 485 JNIEnv* env, |
| 432 jobject obj) { | 486 jobject obj) { |
| 433 // Notify native observers. | 487 // Notify native observers. |
| 434 FireRefreshTokensLoaded(); | 488 FireRefreshTokensLoaded(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 452 | 506 |
| 453 // Clear everything on the Java side as well. | 507 // Clear everything on the Java side as well. |
| 454 std::vector<std::string> empty; | 508 std::vector<std::string> empty; |
| 455 JNIEnv* env = AttachCurrentThread(); | 509 JNIEnv* env = AttachCurrentThread(); |
| 456 ScopedJavaLocalRef<jobjectArray> java_accounts( | 510 ScopedJavaLocalRef<jobjectArray> java_accounts( |
| 457 base::android::ToJavaArrayOfStrings(env, empty)); | 511 base::android::ToJavaArrayOfStrings(env, empty)); |
| 458 Java_OAuth2TokenService_saveStoredAccounts( | 512 Java_OAuth2TokenService_saveStoredAccounts( |
| 459 env, base::android::GetApplicationContext(), java_accounts.obj()); | 513 env, base::android::GetApplicationContext(), java_accounts.obj()); |
| 460 } | 514 } |
| 461 | 515 |
| 516 void OAuth2TokenServiceDelegateAndroid::LoadCredentials( |
| 517 const std::string& primary_account_id) { |
| 518 if (fire_refresh_token_loaded_ == RT_HAS_BEEN_VALIDATED) { |
| 519 fire_refresh_token_loaded_ = RT_LOADED; |
| 520 FireRefreshTokensLoaded(); |
| 521 } else if (fire_refresh_token_loaded_ == RT_LOAD_NOT_START) { |
| 522 fire_refresh_token_loaded_ = RT_WAIT_FOR_VALIDATION; |
| 523 } |
| 524 } |
| 525 |
| 526 std::string OAuth2TokenServiceDelegateAndroid::MapAccountIdToAccountName( |
| 527 const std::string& account_id) const { |
| 528 std::string account_name = |
| 529 account_tracker_service_->GetAccountInfo(account_id).email; |
| 530 DCHECK(!account_name.empty() || account_id.empty()); |
| 531 return account_name; |
| 532 } |
| 533 |
| 534 std::string OAuth2TokenServiceDelegateAndroid::MapAccountNameToAccountId( |
| 535 const std::string& account_name) const { |
| 536 std::string account_id = |
| 537 account_tracker_service_->FindAccountInfoByEmail(account_name).account_id; |
| 538 DCHECK(!account_id.empty() || account_name.empty()); |
| 539 return account_id; |
| 540 } |
| 541 |
| 462 // Called from Java when fetching of an OAuth2 token is finished. The | 542 // Called from Java when fetching of an OAuth2 token is finished. The |
| 463 // |authToken| param is only valid when |result| is true. | 543 // |authToken| param is only valid when |result| is true. |
| 464 void OAuth2TokenFetched(JNIEnv* env, | 544 void OAuth2TokenFetched(JNIEnv* env, |
| 465 const JavaParamRef<jclass>& clazz, | 545 const JavaParamRef<jclass>& clazz, |
| 466 const JavaParamRef<jstring>& authToken, | 546 const JavaParamRef<jstring>& authToken, |
| 467 jboolean isTransientError, | 547 jboolean isTransientError, |
| 468 jlong nativeCallback) { | 548 jlong nativeCallback) { |
| 469 std::string token; | 549 std::string token; |
| 470 if (authToken) | 550 if (authToken) |
| 471 token = ConvertJavaStringToUTF8(env, authToken); | 551 token = ConvertJavaStringToUTF8(env, authToken); |
| 472 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 552 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 473 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 553 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 474 GoogleServiceAuthError | 554 GoogleServiceAuthError |
| 475 err(authToken | 555 err(authToken |
| 476 ? GoogleServiceAuthError::NONE | 556 ? GoogleServiceAuthError::NONE |
| 477 : isTransientError | 557 : isTransientError |
| 478 ? GoogleServiceAuthError::CONNECTION_FAILED | 558 ? GoogleServiceAuthError::CONNECTION_FAILED |
| 479 : GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 559 : GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 480 heap_callback->Run(err, token, base::Time()); | 560 heap_callback->Run(err, token, base::Time()); |
| 481 } | 561 } |
| 482 | 562 |
| 483 // static | 563 // static |
| 484 bool OAuth2TokenServiceDelegateAndroid::Register(JNIEnv* env) { | 564 bool OAuth2TokenServiceDelegateAndroid::Register(JNIEnv* env) { |
| 485 return RegisterNativesImpl(env); | 565 return RegisterNativesImpl(env); |
| 486 } | 566 } |
| OLD | NEW |