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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 ScopedJavaLocalRef<jobjectArray> j_accounts = | 91 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 92 Java_OAuth2TokenService_getAccounts( | 92 Java_OAuth2TokenService_getAccounts( |
| 93 env, base::android::GetApplicationContext()); | 93 env, base::android::GetApplicationContext()); |
| 94 // TODO(fgorski): We may decide to filter out some of the accounts. | 94 // TODO(fgorski): We may decide to filter out some of the accounts. |
| 95 base::android::AppendJavaStringArrayToStringVector(env, | 95 base::android::AppendJavaStringArrayToStringVector(env, |
| 96 j_accounts.obj(), | 96 j_accounts.obj(), |
| 97 &accounts); | 97 &accounts); |
| 98 return accounts; | 98 return accounts; |
| 99 } | 99 } |
| 100 | 100 |
| 101 std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() { | |
| 102 std::vector<std::string> accounts; | |
| 103 JNIEnv* env = AttachCurrentThread(); | |
| 104 ScopedJavaLocalRef<jobjectArray> j_accounts = | |
| 105 Java_OAuth2TokenService_getSystemAccounts( | |
| 106 env, base::android::GetApplicationContext()); | |
| 107 base::android::AppendJavaStringArrayToStringVector(env, | |
| 108 j_accounts.obj(), | |
| 109 &accounts); | |
| 110 return accounts; | |
| 111 } | |
| 112 | |
| 101 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( | 113 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( |
| 102 RequestImpl* request, | 114 RequestImpl* request, |
| 103 const std::string& account_id, | 115 const std::string& account_id, |
| 104 net::URLRequestContextGetter* getter, | 116 net::URLRequestContextGetter* getter, |
| 105 const std::string& client_id, | 117 const std::string& client_id, |
| 106 const std::string& client_secret, | 118 const std::string& client_secret, |
| 107 const OAuth2TokenService::ScopeSet& scopes) { | 119 const OAuth2TokenService::ScopeSet& scopes) { |
| 108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 109 DCHECK(!account_id.empty()); | 121 DCHECK(!account_id.empty()); |
| 110 | 122 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 access_token); | 162 access_token); |
| 151 | 163 |
| 152 JNIEnv* env = AttachCurrentThread(); | 164 JNIEnv* env = AttachCurrentThread(); |
| 153 ScopedJavaLocalRef<jstring> j_access_token = | 165 ScopedJavaLocalRef<jstring> j_access_token = |
| 154 ConvertUTF8ToJavaString(env, access_token); | 166 ConvertUTF8ToJavaString(env, access_token); |
| 155 Java_OAuth2TokenService_invalidateOAuth2AuthToken( | 167 Java_OAuth2TokenService_invalidateOAuth2AuthToken( |
| 156 env, base::android::GetApplicationContext(), | 168 env, base::android::GetApplicationContext(), |
| 157 j_access_token.obj()); | 169 j_access_token.obj()); |
| 158 } | 170 } |
| 159 | 171 |
| 160 void AndroidProfileOAuth2TokenService::ValidateAccounts(JNIEnv* env, | 172 void AndroidProfileOAuth2TokenService::ValidateAccounts( |
| 161 jobject obj, | 173 JNIEnv* env, |
| 162 jobjectArray accounts, | 174 jobject obj, |
| 163 jstring j_current_acc) { | 175 jstring j_current_acc) { |
| 164 std::vector<std::string> account_ids; | |
| 165 base::android::AppendJavaStringArrayToStringVector(env, | |
| 166 accounts, | |
| 167 &account_ids); | |
| 168 std::string signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); | 176 std::string signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); |
| 169 ValidateAccounts(signed_in_account, account_ids); | 177 ValidateAccounts(signed_in_account); |
| 170 } | 178 } |
| 171 | 179 |
| 172 void AndroidProfileOAuth2TokenService::ValidateAccounts( | 180 void AndroidProfileOAuth2TokenService::ValidateAccounts( |
| 181 const std::string& signed_in_account) { | |
| 182 std::vector<std::string> prev_ids = GetAccounts(); | |
| 183 std::vector<std::string> curr_ids = GetSystemAccounts(); | |
| 184 if (ValidateAccounts(signed_in_account, prev_ids, curr_ids)) { | |
| 185 JNIEnv* env = AttachCurrentThread(); | |
| 186 ScopedJavaLocalRef<jobjectArray> java_accounts( | |
| 187 base::android::ToJavaArrayOfStrings(env, curr_ids)); | |
| 188 Java_OAuth2TokenService_saveStoredAccounts( | |
| 189 env, base::android::GetApplicationContext(), java_accounts.obj()); | |
| 190 } else { | |
| 191 JNIEnv* env = AttachCurrentThread(); | |
| 192 std::vector<std::string> empty; | |
| 193 ScopedJavaLocalRef<jobjectArray> java_accounts( | |
| 194 base::android::ToJavaArrayOfStrings(env, empty)); | |
| 195 Java_OAuth2TokenService_saveStoredAccounts( | |
| 196 env, base::android::GetApplicationContext(), java_accounts.obj()); | |
| 197 } | |
| 198 } | |
|
Roger Tawa OOO till Jul 10th
2014/04/23 15:03:59
Nit: maybe remove dupe code:
std::vector<std::str
acleung1
2014/04/24 06:59:15
Done.
| |
| 199 | |
| 200 bool AndroidProfileOAuth2TokenService::ValidateAccounts( | |
| 173 const std::string& signed_in_account, | 201 const std::string& signed_in_account, |
| 174 const std::vector<std::string>& account_ids) { | 202 const std::vector<std::string>& prev_account_ids, |
| 203 const std::vector<std::string>& curr_account_ids) { | |
| 175 if (signed_in_account.empty()) | 204 if (signed_in_account.empty()) |
| 176 return; | 205 return false; |
|
Roger Tawa OOO till Jul 10th
2014/04/23 15:03:59
I think lines 204-205 should be removed. That way
acleung1
2014/04/24 06:59:15
Done.
| |
| 177 | 206 |
| 178 if (std::find(account_ids.begin(), | 207 if (std::find(curr_account_ids.begin(), |
| 179 account_ids.end(), | 208 curr_account_ids.end(), |
| 180 signed_in_account) != account_ids.end()) { | 209 signed_in_account) != curr_account_ids.end()) { |
| 181 // Currently signed in account still exists among accounts on system. | 210 // Test to see if an account is removed from the Android AccountManager. |
| 182 std::vector<std::string> ids = GetAccounts(); | 211 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. |
| 212 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | |
| 213 it != prev_account_ids.end(); it++) { | |
| 214 if (*it == signed_in_account) | |
| 215 continue; | |
| 216 | |
| 217 if (std::find(curr_account_ids.begin(), | |
| 218 curr_account_ids.end(), | |
| 219 *it) == curr_account_ids.end()) { | |
| 220 FireRefreshTokenRevoked(*it); | |
| 221 } | |
| 222 } | |
| 183 | 223 |
| 184 // Always fire the primary signed in account first. | 224 // Always fire the primary signed in account first. |
| 185 FireRefreshTokenAvailable(signed_in_account); | 225 FireRefreshTokenAvailable(signed_in_account); |
| 186 | 226 |
| 187 for (std::vector<std::string>::iterator it = ids.begin(); | 227 for (std::vector<std::string>::const_iterator it = curr_account_ids.begin(); |
| 188 it != ids.end(); it++) { | 228 it != curr_account_ids.end(); it++) { |
| 189 if (*it != signed_in_account) { | 229 if (*it != signed_in_account) { |
| 190 FireRefreshTokenAvailable(*it); | 230 FireRefreshTokenAvailable(*it); |
| 191 } | 231 } |
| 192 } | 232 } |
| 233 return true; | |
| 193 } else { | 234 } else { |
| 194 // Currently signed in account does not any longer exist among accounts on | 235 // Currently signed in account does not any longer exist among accounts on |
| 195 // system. | 236 // system together with all other accounts. |
| 196 FireRefreshTokenRevoked(signed_in_account); | 237 FireRefreshTokenRevoked(signed_in_account); |
| 238 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | |
| 239 it != prev_account_ids.end(); it++) { | |
| 240 if (*it == signed_in_account) | |
| 241 continue; | |
| 242 FireRefreshTokenRevoked(*it); | |
| 243 } | |
| 244 return false; | |
| 197 } | 245 } |
| 198 } | 246 } |
| 199 | 247 |
| 200 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( | 248 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( |
| 201 JNIEnv* env, | 249 JNIEnv* env, |
| 202 jobject obj, | 250 jobject obj, |
| 203 const jstring account_name) { | 251 const jstring account_name) { |
| 204 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 252 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
| 205 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); | 253 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); |
| 206 } | 254 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 | 293 |
| 246 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { | 294 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { |
| 247 // Notify native observers. | 295 // Notify native observers. |
| 248 OAuth2TokenService::FireRefreshTokensLoaded(); | 296 OAuth2TokenService::FireRefreshTokensLoaded(); |
| 249 // Notify Java observers. | 297 // Notify Java observers. |
| 250 JNIEnv* env = AttachCurrentThread(); | 298 JNIEnv* env = AttachCurrentThread(); |
| 251 Java_OAuth2TokenService_notifyRefreshTokensLoaded( | 299 Java_OAuth2TokenService_notifyRefreshTokensLoaded( |
| 252 env, java_ref_.obj()); | 300 env, java_ref_.obj()); |
| 253 } | 301 } |
| 254 | 302 |
| 303 void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { | |
| 304 std::vector<std::string> accounts = GetAccounts(); | |
| 305 for (std::vector<std::string>::iterator it = accounts.begin(); | |
| 306 it != accounts.end(); it++) { | |
| 307 FireRefreshTokenRevoked(*it); | |
| 308 } | |
| 309 } | |
| 310 | |
| 255 // Called from Java when fetching of an OAuth2 token is finished. The | 311 // Called from Java when fetching of an OAuth2 token is finished. The |
| 256 // |authToken| param is only valid when |result| is true. | 312 // |authToken| param is only valid when |result| is true. |
| 257 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 313 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, |
| 258 jstring authToken, | 314 jstring authToken, |
| 259 jboolean result, | 315 jboolean result, |
| 260 jlong nativeCallback) { | 316 jlong nativeCallback) { |
| 261 std::string token = ConvertJavaStringToUTF8(env, authToken); | 317 std::string token = ConvertJavaStringToUTF8(env, authToken); |
| 262 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 318 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 263 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 319 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 264 // Android does not provide enough information to know if the credentials are | 320 // Android does not provide enough information to know if the credentials are |
| 265 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 321 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
| 266 GoogleServiceAuthError err(result ? | 322 GoogleServiceAuthError err(result ? |
| 267 GoogleServiceAuthError::NONE : | 323 GoogleServiceAuthError::NONE : |
| 268 GoogleServiceAuthError::CONNECTION_FAILED); | 324 GoogleServiceAuthError::CONNECTION_FAILED); |
| 269 heap_callback->Run(err, token, base::Time()); | 325 heap_callback->Run(err, token, base::Time()); |
| 270 } | 326 } |
| 271 | 327 |
| 272 // static | 328 // static |
| 273 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 329 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
| 274 return RegisterNativesImpl(env); | 330 return RegisterNativesImpl(env); |
| 275 } | 331 } |
| OLD | NEW |