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 | |
185 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids)) { | |
nyquist
2014/04/24 22:27:05
Nit: Remove { } to be consistent?
| |
186 curr_ids.clear(); | |
187 } | |
188 | |
189 JNIEnv* env = AttachCurrentThread(); | |
190 ScopedJavaLocalRef<jobjectArray> java_accounts( | |
191 base::android::ToJavaArrayOfStrings(env, curr_ids)); | |
192 Java_OAuth2TokenService_saveStoredAccounts( | |
193 env, base::android::GetApplicationContext(), java_accounts.obj()); | |
194 } | |
195 | |
196 bool AndroidProfileOAuth2TokenService::ValidateAccounts( | |
173 const std::string& signed_in_account, | 197 const std::string& signed_in_account, |
174 const std::vector<std::string>& account_ids) { | 198 const std::vector<std::string>& prev_account_ids, |
175 if (signed_in_account.empty()) | 199 const std::vector<std::string>& curr_account_ids) { |
176 return; | 200 if (std::find(curr_account_ids.begin(), |
201 curr_account_ids.end(), | |
202 signed_in_account) != curr_account_ids.end()) { | |
203 // Test to see if an account is removed from the Android AccountManager. | |
204 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. | |
205 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | |
206 it != prev_account_ids.end(); it++) { | |
207 if (*it == signed_in_account) | |
208 continue; | |
177 | 209 |
178 if (std::find(account_ids.begin(), | 210 if (std::find(curr_account_ids.begin(), |
179 account_ids.end(), | 211 curr_account_ids.end(), |
180 signed_in_account) != account_ids.end()) { | 212 *it) == curr_account_ids.end()) { |
181 // Currently signed in account still exists among accounts on system. | 213 FireRefreshTokenRevoked(*it); |
182 std::vector<std::string> ids = GetAccounts(); | 214 } |
215 } | |
183 | 216 |
184 // Always fire the primary signed in account first. | 217 // Always fire the primary signed in account first. |
nyquist
2014/04/24 22:27:05
How would you feel about extracting the rest of th
| |
185 FireRefreshTokenAvailable(signed_in_account); | 218 FireRefreshTokenAvailable(signed_in_account); |
186 | 219 |
187 for (std::vector<std::string>::iterator it = ids.begin(); | 220 for (std::vector<std::string>::const_iterator it = curr_account_ids.begin(); |
188 it != ids.end(); it++) { | 221 it != curr_account_ids.end(); it++) { |
189 if (*it != signed_in_account) { | 222 if (*it != signed_in_account) { |
nyquist
2014/04/24 22:27:05
Nit: Remove { } to match the rest of this function
| |
190 FireRefreshTokenAvailable(*it); | 223 FireRefreshTokenAvailable(*it); |
191 } | 224 } |
192 } | 225 } |
226 return true; | |
193 } else { | 227 } else { |
194 // Currently signed in account does not any longer exist among accounts on | 228 // Currently signed in account does not any longer exist among accounts on |
nyquist
2014/04/24 22:27:05
How would you feel about extracting the rest of th
| |
195 // system. | 229 // system together with all other accounts. |
nyquist
2014/04/24 22:27:05
Could you add a comment for why we revoked all tok
| |
196 FireRefreshTokenRevoked(signed_in_account); | 230 if (!signed_in_account.empty()) { |
nyquist
2014/04/24 22:27:05
Nit: Could you add a comment as to why we need to
| |
231 FireRefreshTokenRevoked(signed_in_account); | |
232 } | |
233 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | |
234 it != prev_account_ids.end(); it++) { | |
235 if (*it == signed_in_account) | |
236 continue; | |
237 FireRefreshTokenRevoked(*it); | |
238 } | |
239 return false; | |
197 } | 240 } |
198 } | 241 } |
199 | 242 |
200 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( | 243 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( |
201 JNIEnv* env, | 244 JNIEnv* env, |
202 jobject obj, | 245 jobject obj, |
203 const jstring account_name) { | 246 const jstring account_name) { |
204 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 247 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
205 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); | 248 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); |
206 } | 249 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 | 288 |
246 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { | 289 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { |
247 // Notify native observers. | 290 // Notify native observers. |
248 OAuth2TokenService::FireRefreshTokensLoaded(); | 291 OAuth2TokenService::FireRefreshTokensLoaded(); |
249 // Notify Java observers. | 292 // Notify Java observers. |
250 JNIEnv* env = AttachCurrentThread(); | 293 JNIEnv* env = AttachCurrentThread(); |
251 Java_OAuth2TokenService_notifyRefreshTokensLoaded( | 294 Java_OAuth2TokenService_notifyRefreshTokensLoaded( |
252 env, java_ref_.obj()); | 295 env, java_ref_.obj()); |
253 } | 296 } |
254 | 297 |
298 void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { | |
299 std::vector<std::string> accounts = GetAccounts(); | |
300 for (std::vector<std::string>::iterator it = accounts.begin(); | |
301 it != accounts.end(); it++) { | |
302 FireRefreshTokenRevoked(*it); | |
303 } | |
304 } | |
305 | |
255 // Called from Java when fetching of an OAuth2 token is finished. The | 306 // Called from Java when fetching of an OAuth2 token is finished. The |
256 // |authToken| param is only valid when |result| is true. | 307 // |authToken| param is only valid when |result| is true. |
257 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 308 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, |
258 jstring authToken, | 309 jstring authToken, |
259 jboolean result, | 310 jboolean result, |
260 jlong nativeCallback) { | 311 jlong nativeCallback) { |
261 std::string token = ConvertJavaStringToUTF8(env, authToken); | 312 std::string token = ConvertJavaStringToUTF8(env, authToken); |
262 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 313 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
263 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 314 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
264 // Android does not provide enough information to know if the credentials are | 315 // Android does not provide enough information to know if the credentials are |
265 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 316 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
266 GoogleServiceAuthError err(result ? | 317 GoogleServiceAuthError err(result ? |
267 GoogleServiceAuthError::NONE : | 318 GoogleServiceAuthError::NONE : |
268 GoogleServiceAuthError::CONNECTION_FAILED); | 319 GoogleServiceAuthError::CONNECTION_FAILED); |
269 heap_callback->Run(err, token, base::Time()); | 320 heap_callback->Run(err, token, base::Time()); |
270 } | 321 } |
271 | 322 |
272 // static | 323 // static |
273 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 324 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
274 return RegisterNativesImpl(env); | 325 return RegisterNativesImpl(env); |
275 } | 326 } |
OLD | NEW |