Chromium Code Reviews| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 access_token); | 150 access_token); |
| 151 | 151 |
| 152 JNIEnv* env = AttachCurrentThread(); | 152 JNIEnv* env = AttachCurrentThread(); |
| 153 ScopedJavaLocalRef<jstring> j_access_token = | 153 ScopedJavaLocalRef<jstring> j_access_token = |
| 154 ConvertUTF8ToJavaString(env, access_token); | 154 ConvertUTF8ToJavaString(env, access_token); |
| 155 Java_OAuth2TokenService_invalidateOAuth2AuthToken( | 155 Java_OAuth2TokenService_invalidateOAuth2AuthToken( |
| 156 env, base::android::GetApplicationContext(), | 156 env, base::android::GetApplicationContext(), |
| 157 j_access_token.obj()); | 157 j_access_token.obj()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void AndroidProfileOAuth2TokenService::ValidateAccounts(JNIEnv* env, | 160 bool AndroidProfileOAuth2TokenService::ValidateAccounts( |
| 161 jobject obj, | 161 JNIEnv* env, |
| 162 jobjectArray accounts, | 162 jobject obj, |
| 163 jstring j_current_acc) { | 163 jobjectArray prev_account_ids, |
| 164 jstring j_current_acc) { | |
| 164 std::vector<std::string> account_ids; | 165 std::vector<std::string> account_ids; |
| 165 base::android::AppendJavaStringArrayToStringVector(env, | 166 base::android::AppendJavaStringArrayToStringVector(env, |
| 166 accounts, | 167 prev_account_ids, |
| 167 &account_ids); | 168 &account_ids); |
| 168 std::string signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); | 169 std::string signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); |
| 169 ValidateAccounts(signed_in_account, account_ids); | 170 return ValidateAccounts(signed_in_account, account_ids); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void AndroidProfileOAuth2TokenService::ValidateAccounts( | 173 bool AndroidProfileOAuth2TokenService::ValidateAccounts( |
| 173 const std::string& signed_in_account, | 174 const std::string& signed_in_account, |
| 174 const std::vector<std::string>& account_ids) { | 175 const std::vector<std::string>& prev_account_ids) { |
| 175 if (signed_in_account.empty()) | 176 if (signed_in_account.empty()) |
| 176 return; | 177 return false; |
|
Roger Tawa OOO till Jul 10th
2014/04/10 15:30:54
Should this return here, or should we still fire t
| |
| 177 | 178 |
| 179 std::vector<std::string> account_ids = GetAccounts(); | |
| 178 if (std::find(account_ids.begin(), | 180 if (std::find(account_ids.begin(), |
| 179 account_ids.end(), | 181 account_ids.end(), |
| 180 signed_in_account) != account_ids.end()) { | 182 signed_in_account) != account_ids.end()) { |
| 181 // Currently signed in account still exists among accounts on system. | 183 // Test to see if an account is removed from the Android AccountManager. |
| 182 std::vector<std::string> ids = GetAccounts(); | 184 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. |
| 185 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | |
| 186 it != prev_account_ids.end(); it++) { | |
| 187 if (*it == signed_in_account) | |
| 188 continue; | |
| 189 | |
| 190 if (std::find(account_ids.begin(), | |
| 191 account_ids.end(), | |
| 192 *it) == account_ids.end()) { | |
| 193 FireRefreshTokenRevoked(*it); | |
| 194 } | |
| 195 } | |
| 183 | 196 |
| 184 // Always fire the primary signed in account first. | 197 // Always fire the primary signed in account first. |
| 185 FireRefreshTokenAvailable(signed_in_account); | 198 FireRefreshTokenAvailable(signed_in_account); |
| 186 | 199 |
| 187 for (std::vector<std::string>::iterator it = ids.begin(); | 200 for (std::vector<std::string>::iterator it = account_ids.begin(); |
| 188 it != ids.end(); it++) { | 201 it != account_ids.end(); it++) { |
| 189 if (*it != signed_in_account) { | 202 if (*it != signed_in_account) { |
| 190 FireRefreshTokenAvailable(*it); | 203 FireRefreshTokenAvailable(*it); |
| 191 } | 204 } |
| 192 } | 205 } |
| 206 return true; | |
| 193 } else { | 207 } else { |
| 194 // Currently signed in account does not any longer exist among accounts on | 208 // Currently signed in account does not any longer exist among accounts on |
| 195 // system. | 209 // system together with all other accounts. |
| 196 FireRefreshTokenRevoked(signed_in_account); | 210 FireRefreshTokenRevoked(signed_in_account); |
| 211 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | |
| 212 it != prev_account_ids.end(); it++) { | |
| 213 if (*it == signed_in_account) | |
| 214 continue; | |
| 215 FireRefreshTokenRevoked(*it); | |
| 216 } | |
| 217 return false; | |
| 197 } | 218 } |
| 198 } | 219 } |
|
Roger Tawa OOO till Jul 10th
2014/04/10 15:30:54
I think it would be good to write tests for this f
| |
| 199 | 220 |
| 200 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( | 221 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( |
| 201 JNIEnv* env, | 222 JNIEnv* env, |
| 202 jobject obj, | 223 jobject obj, |
| 203 const jstring account_name) { | 224 const jstring account_name) { |
| 204 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 225 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
| 205 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); | 226 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); |
| 206 } | 227 } |
| 207 | 228 |
| 208 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable( | 229 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 | 266 |
| 246 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { | 267 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { |
| 247 // Notify native observers. | 268 // Notify native observers. |
| 248 OAuth2TokenService::FireRefreshTokensLoaded(); | 269 OAuth2TokenService::FireRefreshTokensLoaded(); |
| 249 // Notify Java observers. | 270 // Notify Java observers. |
| 250 JNIEnv* env = AttachCurrentThread(); | 271 JNIEnv* env = AttachCurrentThread(); |
| 251 Java_OAuth2TokenService_notifyRefreshTokensLoaded( | 272 Java_OAuth2TokenService_notifyRefreshTokensLoaded( |
| 252 env, java_ref_.obj()); | 273 env, java_ref_.obj()); |
| 253 } | 274 } |
| 254 | 275 |
| 276 void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { | |
| 277 std::vector<std::string> accounts = GetAccounts(); | |
| 278 for (std::vector<std::string>::iterator it = accounts.begin(); | |
| 279 it != accounts.end(); it++) { | |
| 280 FireRefreshTokenRevoked(*it); | |
| 281 } | |
| 282 } | |
| 283 | |
| 255 // Called from Java when fetching of an OAuth2 token is finished. The | 284 // Called from Java when fetching of an OAuth2 token is finished. The |
| 256 // |authToken| param is only valid when |result| is true. | 285 // |authToken| param is only valid when |result| is true. |
| 257 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 286 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, |
| 258 jstring authToken, | 287 jstring authToken, |
| 259 jboolean result, | 288 jboolean result, |
| 260 jlong nativeCallback) { | 289 jlong nativeCallback) { |
| 261 std::string token = ConvertJavaStringToUTF8(env, authToken); | 290 std::string token = ConvertJavaStringToUTF8(env, authToken); |
| 262 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 291 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 263 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 292 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 264 // Android does not provide enough information to know if the credentials are | 293 // Android does not provide enough information to know if the credentials are |
| 265 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 294 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
| 266 GoogleServiceAuthError err(result ? | 295 GoogleServiceAuthError err(result ? |
| 267 GoogleServiceAuthError::NONE : | 296 GoogleServiceAuthError::NONE : |
| 268 GoogleServiceAuthError::CONNECTION_FAILED); | 297 GoogleServiceAuthError::CONNECTION_FAILED); |
| 269 heap_callback->Run(err, token, base::Time()); | 298 heap_callback->Run(err, token, base::Time()); |
| 270 } | 299 } |
| 271 | 300 |
| 272 // static | 301 // static |
| 273 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 302 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
| 274 return RegisterNativesImpl(env); | 303 return RegisterNativesImpl(env); |
| 275 } | 304 } |
| OLD | NEW |