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/android_profile_oauth2_token_service.h" | 5 #include "chrome/browser/signin/android_profile_oauth2_token_service.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" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 scope += *it; | 127 scope += *it; |
128 } | 128 } |
129 return scope; | 129 return scope; |
130 } | 130 } |
131 | 131 |
132 } // namespace | 132 } // namespace |
133 | 133 |
134 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; | 134 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; |
135 | 135 |
136 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { | 136 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { |
137 VLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; | 137 DVLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; |
138 JNIEnv* env = AttachCurrentThread(); | 138 JNIEnv* env = AttachCurrentThread(); |
139 base::android::ScopedJavaLocalRef<jobject> local_java_ref = | 139 base::android::ScopedJavaLocalRef<jobject> local_java_ref = |
140 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); | 140 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); |
141 java_ref_.Reset(env, local_java_ref.obj()); | 141 java_ref_.Reset(env, local_java_ref.obj()); |
142 } | 142 } |
143 | 143 |
144 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() {} | 144 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() {} |
145 | 145 |
146 // static | 146 // static |
147 jobject AndroidProfileOAuth2TokenService::GetForProfile( | 147 jobject AndroidProfileOAuth2TokenService::GetForProfile( |
148 JNIEnv* env, jclass clazz, jobject j_profile_android) { | 148 JNIEnv* env, jclass clazz, jobject j_profile_android) { |
149 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android); | 149 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android); |
150 AndroidProfileOAuth2TokenService* service = | 150 AndroidProfileOAuth2TokenService* service = |
151 ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(profile); | 151 ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(profile); |
152 return service->java_ref_.obj(); | 152 return service->java_ref_.obj(); |
153 } | 153 } |
154 | 154 |
155 static jobject GetForProfile(JNIEnv* env, | 155 static jobject GetForProfile(JNIEnv* env, |
156 jclass clazz, | 156 jclass clazz, |
157 jobject j_profile_android) { | 157 jobject j_profile_android) { |
158 return AndroidProfileOAuth2TokenService::GetForProfile( | 158 return AndroidProfileOAuth2TokenService::GetForProfile( |
159 env, clazz, j_profile_android); | 159 env, clazz, j_profile_android); |
160 } | 160 } |
161 | 161 |
162 void AndroidProfileOAuth2TokenService::Initialize(SigninClient* client) { | 162 void AndroidProfileOAuth2TokenService::Initialize(SigninClient* client) { |
163 VLOG(1) << "AndroidProfileOAuth2TokenService::Initialize"; | 163 DVLOG(1) << "AndroidProfileOAuth2TokenService::Initialize"; |
164 ProfileOAuth2TokenService::Initialize(client); | 164 ProfileOAuth2TokenService::Initialize(client); |
165 | 165 |
166 if (!is_testing_profile_) { | 166 if (!is_testing_profile_) { |
167 Java_OAuth2TokenService_validateAccounts( | 167 Java_OAuth2TokenService_validateAccounts( |
168 AttachCurrentThread(), java_ref_.obj(), | 168 AttachCurrentThread(), java_ref_.obj(), |
169 base::android::GetApplicationContext(), JNI_TRUE); | 169 base::android::GetApplicationContext(), JNI_TRUE); |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable( | 173 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 Java_OAuth2TokenService_invalidateOAuth2AuthToken( | 239 Java_OAuth2TokenService_invalidateOAuth2AuthToken( |
240 env, base::android::GetApplicationContext(), | 240 env, base::android::GetApplicationContext(), |
241 j_access_token.obj()); | 241 j_access_token.obj()); |
242 } | 242 } |
243 | 243 |
244 void AndroidProfileOAuth2TokenService::ValidateAccounts( | 244 void AndroidProfileOAuth2TokenService::ValidateAccounts( |
245 JNIEnv* env, | 245 JNIEnv* env, |
246 jobject obj, | 246 jobject obj, |
247 jstring j_current_acc, | 247 jstring j_current_acc, |
248 jboolean j_force_notifications) { | 248 jboolean j_force_notifications) { |
249 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts from java"; | 249 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts from java"; |
250 std::string signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); | 250 std::string signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); |
251 if (!signed_in_account.empty()) | 251 if (!signed_in_account.empty()) |
252 signed_in_account = gaia::CanonicalizeEmail(signed_in_account); | 252 signed_in_account = gaia::CanonicalizeEmail(signed_in_account); |
253 ValidateAccounts(signed_in_account, j_force_notifications != JNI_FALSE); | 253 ValidateAccounts(signed_in_account, j_force_notifications != JNI_FALSE); |
254 } | 254 } |
255 | 255 |
256 void AndroidProfileOAuth2TokenService::ValidateAccounts( | 256 void AndroidProfileOAuth2TokenService::ValidateAccounts( |
257 const std::string& signed_in_account, | 257 const std::string& signed_in_account, |
258 bool force_notifications) { | 258 bool force_notifications) { |
259 std::vector<std::string> prev_ids = GetAccounts(); | 259 std::vector<std::string> prev_ids = GetAccounts(); |
260 std::vector<std::string> curr_ids = GetSystemAccounts(); | 260 std::vector<std::string> curr_ids = GetSystemAccounts(); |
261 std::vector<std::string> refreshed_ids; | 261 std::vector<std::string> refreshed_ids; |
262 std::vector<std::string> revoked_ids; | 262 std::vector<std::string> revoked_ids; |
263 | 263 |
264 // Canonicalize system accounts. |prev_ids| is already done. | 264 // Canonicalize system accounts. |prev_ids| is already done. |
265 for (size_t i = 0; i < curr_ids.size(); ++i) | 265 for (size_t i = 0; i < curr_ids.size(); ++i) |
266 curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]); | 266 curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]); |
267 for (size_t i = 0; i < prev_ids.size(); ++i) | 267 for (size_t i = 0; i < prev_ids.size(); ++i) |
268 ValidateAccountId(prev_ids[i]); | 268 ValidateAccountId(prev_ids[i]); |
269 | 269 |
270 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 270 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" |
271 << " sigined_in_account=" << signed_in_account | 271 << " sigined_in_account=" << signed_in_account |
272 << " prev_ids=" << prev_ids.size() | 272 << " prev_ids=" << prev_ids.size() |
273 << " curr_ids=" << curr_ids.size() | 273 << " curr_ids=" << curr_ids.size() |
274 << " force=" << (force_notifications ? "true" : "false"); | 274 << " force=" << (force_notifications ? "true" : "false"); |
275 | 275 |
276 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, | 276 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, |
277 revoked_ids, force_notifications)) { | 277 revoked_ids, force_notifications)) { |
278 curr_ids.clear(); | 278 curr_ids.clear(); |
279 } | 279 } |
280 | 280 |
281 ScopedBatchChange batch(this); | 281 ScopedBatchChange batch(this); |
282 | 282 |
283 JNIEnv* env = AttachCurrentThread(); | 283 JNIEnv* env = AttachCurrentThread(); |
284 ScopedJavaLocalRef<jobjectArray> java_accounts( | 284 ScopedJavaLocalRef<jobjectArray> java_accounts( |
(...skipping 25 matching lines...) Expand all Loading... |
310 // Test to see if an account is removed from the Android AccountManager. | 310 // Test to see if an account is removed from the Android AccountManager. |
311 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. | 311 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. |
312 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | 312 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); |
313 it != prev_account_ids.end(); it++) { | 313 it != prev_account_ids.end(); it++) { |
314 if (*it == signed_in_account) | 314 if (*it == signed_in_account) |
315 continue; | 315 continue; |
316 | 316 |
317 if (std::find(curr_account_ids.begin(), | 317 if (std::find(curr_account_ids.begin(), |
318 curr_account_ids.end(), | 318 curr_account_ids.end(), |
319 *it) == curr_account_ids.end()) { | 319 *it) == curr_account_ids.end()) { |
320 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 320 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" |
321 << "revoked=" << *it; | 321 << "revoked=" << *it; |
322 revoked_ids.push_back(*it); | 322 revoked_ids.push_back(*it); |
323 } | 323 } |
324 } | 324 } |
325 | 325 |
326 if (force_notifications || | 326 if (force_notifications || |
327 std::find(prev_account_ids.begin(), prev_account_ids.end(), | 327 std::find(prev_account_ids.begin(), prev_account_ids.end(), |
328 signed_in_account) == prev_account_ids.end()) { | 328 signed_in_account) == prev_account_ids.end()) { |
329 // Always fire the primary signed in account first. | 329 // Always fire the primary signed in account first. |
330 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 330 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" |
331 << "refreshed=" << signed_in_account; | 331 << "refreshed=" << signed_in_account; |
332 refreshed_ids.push_back(signed_in_account); | 332 refreshed_ids.push_back(signed_in_account); |
333 } | 333 } |
334 | 334 |
335 for (std::vector<std::string>::const_iterator it = curr_account_ids.begin(); | 335 for (std::vector<std::string>::const_iterator it = curr_account_ids.begin(); |
336 it != curr_account_ids.end(); it++) { | 336 it != curr_account_ids.end(); it++) { |
337 if (*it != signed_in_account) { | 337 if (*it != signed_in_account) { |
338 if (force_notifications || | 338 if (force_notifications || |
339 std::find(prev_account_ids.begin(), | 339 std::find(prev_account_ids.begin(), |
340 prev_account_ids.end(), | 340 prev_account_ids.end(), |
341 *it) == prev_account_ids.end()) { | 341 *it) == prev_account_ids.end()) { |
342 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 342 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" |
343 << "refreshed=" << *it; | 343 << "refreshed=" << *it; |
344 refreshed_ids.push_back(*it); | 344 refreshed_ids.push_back(*it); |
345 } | 345 } |
346 } | 346 } |
347 } | 347 } |
348 return true; | 348 return true; |
349 } else { | 349 } else { |
350 // Currently signed in account does not any longer exist among accounts on | 350 // Currently signed in account does not any longer exist among accounts on |
351 // system together with all other accounts. | 351 // system together with all other accounts. |
352 if (std::find(prev_account_ids.begin(), prev_account_ids.end(), | 352 if (std::find(prev_account_ids.begin(), prev_account_ids.end(), |
353 signed_in_account) != prev_account_ids.end()) { | 353 signed_in_account) != prev_account_ids.end()) { |
354 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 354 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" |
355 << "revoked=" << signed_in_account; | 355 << "revoked=" << signed_in_account; |
356 revoked_ids.push_back(signed_in_account); | 356 revoked_ids.push_back(signed_in_account); |
357 } | 357 } |
358 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | 358 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); |
359 it != prev_account_ids.end(); it++) { | 359 it != prev_account_ids.end(); it++) { |
360 if (*it == signed_in_account) | 360 if (*it == signed_in_account) |
361 continue; | 361 continue; |
362 VLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 362 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" |
363 << "revoked=" << *it; | 363 << "revoked=" << *it; |
364 revoked_ids.push_back(*it); | 364 revoked_ids.push_back(*it); |
365 } | 365 } |
366 return false; | 366 return false; |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
370 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( | 370 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( |
371 JNIEnv* env, | 371 JNIEnv* env, |
372 jobject obj, | 372 jobject obj, |
373 const jstring account_name) { | 373 const jstring account_name) { |
374 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 374 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
375 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); | 375 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); |
376 } | 376 } |
377 | 377 |
378 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable( | 378 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable( |
379 const std::string& account_id) { | 379 const std::string& account_id) { |
380 VLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable id=" | 380 DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable id=" |
381 << account_id; | 381 << account_id; |
382 | 382 |
383 // Notify native observers. | 383 // Notify native observers. |
384 OAuth2TokenService::FireRefreshTokenAvailable(account_id); | 384 OAuth2TokenService::FireRefreshTokenAvailable(account_id); |
385 // Notify Java observers. | 385 // Notify Java observers. |
386 JNIEnv* env = AttachCurrentThread(); | 386 JNIEnv* env = AttachCurrentThread(); |
387 ScopedJavaLocalRef<jstring> account_name = | 387 ScopedJavaLocalRef<jstring> account_name = |
388 ConvertUTF8ToJavaString(env, account_id); | 388 ConvertUTF8ToJavaString(env, account_id); |
389 Java_OAuth2TokenService_notifyRefreshTokenAvailable( | 389 Java_OAuth2TokenService_notifyRefreshTokenAvailable( |
390 env, java_ref_.obj(), account_name.obj()); | 390 env, java_ref_.obj(), account_name.obj()); |
391 } | 391 } |
392 | 392 |
393 void AndroidProfileOAuth2TokenService::FireRefreshTokenRevokedFromJava( | 393 void AndroidProfileOAuth2TokenService::FireRefreshTokenRevokedFromJava( |
394 JNIEnv* env, | 394 JNIEnv* env, |
395 jobject obj, | 395 jobject obj, |
396 const jstring account_name) { | 396 const jstring account_name) { |
397 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 397 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
398 AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked(account_id); | 398 AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked(account_id); |
399 } | 399 } |
400 | 400 |
401 void AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked( | 401 void AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked( |
402 const std::string& account_id) { | 402 const std::string& account_id) { |
403 VLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked id=" | 403 DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked id=" |
404 << account_id; | 404 << account_id; |
405 | 405 |
406 // Notify native observers. | 406 // Notify native observers. |
407 OAuth2TokenService::FireRefreshTokenRevoked(account_id); | 407 OAuth2TokenService::FireRefreshTokenRevoked(account_id); |
408 // Notify Java observers. | 408 // Notify Java observers. |
409 JNIEnv* env = AttachCurrentThread(); | 409 JNIEnv* env = AttachCurrentThread(); |
410 ScopedJavaLocalRef<jstring> account_name = | 410 ScopedJavaLocalRef<jstring> account_name = |
411 ConvertUTF8ToJavaString(env, account_id); | 411 ConvertUTF8ToJavaString(env, account_id); |
412 Java_OAuth2TokenService_notifyRefreshTokenRevoked( | 412 Java_OAuth2TokenService_notifyRefreshTokenRevoked( |
413 env, java_ref_.obj(), account_name.obj()); | 413 env, java_ref_.obj(), account_name.obj()); |
414 } | 414 } |
415 | 415 |
416 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoadedFromJava( | 416 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoadedFromJava( |
417 JNIEnv* env, | 417 JNIEnv* env, |
418 jobject obj) { | 418 jobject obj) { |
419 AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded(); | 419 AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded(); |
420 } | 420 } |
421 | 421 |
422 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { | 422 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { |
423 VLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded"; | 423 DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded"; |
424 // Notify native observers. | 424 // Notify native observers. |
425 OAuth2TokenService::FireRefreshTokensLoaded(); | 425 OAuth2TokenService::FireRefreshTokensLoaded(); |
426 // Notify Java observers. | 426 // Notify Java observers. |
427 JNIEnv* env = AttachCurrentThread(); | 427 JNIEnv* env = AttachCurrentThread(); |
428 Java_OAuth2TokenService_notifyRefreshTokensLoaded( | 428 Java_OAuth2TokenService_notifyRefreshTokensLoaded( |
429 env, java_ref_.obj()); | 429 env, java_ref_.obj()); |
430 } | 430 } |
431 | 431 |
432 void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { | 432 void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { |
433 VLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; | 433 DVLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; |
434 ScopedBatchChange batch(this); | 434 ScopedBatchChange batch(this); |
435 std::vector<std::string> accounts = GetAccounts(); | 435 std::vector<std::string> accounts = GetAccounts(); |
436 for (std::vector<std::string>::iterator it = accounts.begin(); | 436 for (std::vector<std::string>::iterator it = accounts.begin(); |
437 it != accounts.end(); it++) { | 437 it != accounts.end(); it++) { |
438 FireRefreshTokenRevoked(*it); | 438 FireRefreshTokenRevoked(*it); |
439 } | 439 } |
440 | 440 |
441 // Clear everything on the Java side as well. | 441 // Clear everything on the Java side as well. |
442 std::vector<std::string> empty; | 442 std::vector<std::string> empty; |
443 JNIEnv* env = AttachCurrentThread(); | 443 JNIEnv* env = AttachCurrentThread(); |
(...skipping 19 matching lines...) Expand all Loading... |
463 GoogleServiceAuthError err(result ? | 463 GoogleServiceAuthError err(result ? |
464 GoogleServiceAuthError::NONE : | 464 GoogleServiceAuthError::NONE : |
465 GoogleServiceAuthError::CONNECTION_FAILED); | 465 GoogleServiceAuthError::CONNECTION_FAILED); |
466 heap_callback->Run(err, token, base::Time()); | 466 heap_callback->Run(err, token, base::Time()); |
467 } | 467 } |
468 | 468 |
469 // static | 469 // static |
470 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 470 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
471 return RegisterNativesImpl(env); | 471 return RegisterNativesImpl(env); |
472 } | 472 } |
OLD | NEW |