| 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/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 "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "google_apis/gaia/gaia_auth_util.h" | 16 #include "google_apis/gaia/gaia_auth_util.h" |
| 17 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 17 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 18 #include "jni/OAuth2TokenService_jni.h" | 18 #include "jni/OAuth2TokenService_jni.h" |
| 19 | 19 |
| 20 using base::android::AttachCurrentThread; | 20 using base::android::AttachCurrentThread; |
| 21 using base::android::ConvertJavaStringToUTF8; | 21 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 23 using base::android::ScopedJavaLocalRef; | 23 using base::android::ScopedJavaLocalRef; |
| 24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 // Callback from FetchOAuth2TokenWithUsername(). | 28 // Callback from FetchOAuth2TokenWithUsername(). |
| 29 // Arguments: | 29 // Arguments: |
| 30 // - the error, or NONE if the token fetch was successful. | 30 // - the error, or NONE if the token fetch was successful. |
| 31 // - the OAuth2 access token. | 31 // - the OAuth2 access token. |
| 32 // - the expiry time of the token (may be null, indicating that the expiry | 32 // - the expiry time of the token (may be null, indicating that the expiry |
| 33 // time is unknown. | 33 // time is unknown. |
| 34 typedef base::Callback<void( | 34 typedef base::Callback<void(const GoogleServiceAuthError&, |
| 35 const GoogleServiceAuthError&, const std::string&, const base::Time&)> | 35 const std::string&, |
| 36 FetchOAuth2TokenCallback; | 36 const base::Time&)> FetchOAuth2TokenCallback; |
| 37 | 37 |
| 38 class AndroidAccessTokenFetcher : public OAuth2AccessTokenFetcher { | 38 class AndroidAccessTokenFetcher : public OAuth2AccessTokenFetcher { |
| 39 public: | 39 public: |
| 40 AndroidAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, | 40 AndroidAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, |
| 41 const std::string& account_id); | 41 const std::string& account_id); |
| 42 ~AndroidAccessTokenFetcher() override; | 42 ~AndroidAccessTokenFetcher() override; |
| 43 | 43 |
| 44 // Overrides from OAuth2AccessTokenFetcher: | 44 // Overrides from OAuth2AccessTokenFetcher: |
| 45 void Start(const std::string& client_id, | 45 void Start(const std::string& client_id, |
| 46 const std::string& client_secret, | 46 const std::string& client_secret, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 64 | 64 |
| 65 AndroidAccessTokenFetcher::AndroidAccessTokenFetcher( | 65 AndroidAccessTokenFetcher::AndroidAccessTokenFetcher( |
| 66 OAuth2AccessTokenConsumer* consumer, | 66 OAuth2AccessTokenConsumer* consumer, |
| 67 const std::string& account_id) | 67 const std::string& account_id) |
| 68 : OAuth2AccessTokenFetcher(consumer), | 68 : OAuth2AccessTokenFetcher(consumer), |
| 69 account_id_(account_id), | 69 account_id_(account_id), |
| 70 request_was_cancelled_(false), | 70 request_was_cancelled_(false), |
| 71 weak_factory_(this) { | 71 weak_factory_(this) { |
| 72 } | 72 } |
| 73 | 73 |
| 74 AndroidAccessTokenFetcher::~AndroidAccessTokenFetcher() {} | 74 AndroidAccessTokenFetcher::~AndroidAccessTokenFetcher() { |
| 75 } |
| 75 | 76 |
| 76 void AndroidAccessTokenFetcher::Start(const std::string& client_id, | 77 void AndroidAccessTokenFetcher::Start(const std::string& client_id, |
| 77 const std::string& client_secret, | 78 const std::string& client_secret, |
| 78 const std::vector<std::string>& scopes) { | 79 const std::vector<std::string>& scopes) { |
| 79 JNIEnv* env = AttachCurrentThread(); | 80 JNIEnv* env = AttachCurrentThread(); |
| 80 std::string scope = CombineScopes(scopes); | 81 std::string scope = CombineScopes(scopes); |
| 81 ScopedJavaLocalRef<jstring> j_username = | 82 ScopedJavaLocalRef<jstring> j_username = |
| 82 ConvertUTF8ToJavaString(env, account_id_); | 83 ConvertUTF8ToJavaString(env, account_id_); |
| 83 ScopedJavaLocalRef<jstring> j_scope = | 84 ScopedJavaLocalRef<jstring> j_scope = ConvertUTF8ToJavaString(env, scope); |
| 84 ConvertUTF8ToJavaString(env, scope); | |
| 85 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 85 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 86 new FetchOAuth2TokenCallback( | 86 new FetchOAuth2TokenCallback( |
| 87 base::Bind(&AndroidAccessTokenFetcher::OnAccessTokenResponse, | 87 base::Bind(&AndroidAccessTokenFetcher::OnAccessTokenResponse, |
| 88 weak_factory_.GetWeakPtr()))); | 88 weak_factory_.GetWeakPtr()))); |
| 89 | 89 |
| 90 // Call into Java to get a new token. | 90 // Call into Java to get a new token. |
| 91 Java_OAuth2TokenService_getOAuth2AuthToken( | 91 Java_OAuth2TokenService_getOAuth2AuthToken( |
| 92 env, base::android::GetApplicationContext(), | 92 env, base::android::GetApplicationContext(), j_username.obj(), |
| 93 j_username.obj(), | 93 j_scope.obj(), reinterpret_cast<intptr_t>(heap_callback.release())); |
| 94 j_scope.obj(), | |
| 95 reinterpret_cast<intptr_t>(heap_callback.release())); | |
| 96 } | 94 } |
| 97 | 95 |
| 98 void AndroidAccessTokenFetcher::CancelRequest() { | 96 void AndroidAccessTokenFetcher::CancelRequest() { |
| 99 request_was_cancelled_ = true; | 97 request_was_cancelled_ = true; |
| 100 } | 98 } |
| 101 | 99 |
| 102 void AndroidAccessTokenFetcher::OnAccessTokenResponse( | 100 void AndroidAccessTokenFetcher::OnAccessTokenResponse( |
| 103 const GoogleServiceAuthError& error, | 101 const GoogleServiceAuthError& error, |
| 104 const std::string& access_token, | 102 const std::string& access_token, |
| 105 const base::Time& expiration_time) { | 103 const base::Time& expiration_time) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 124 it != scopes.end(); ++it) { | 122 it != scopes.end(); ++it) { |
| 125 if (!scope.empty()) | 123 if (!scope.empty()) |
| 126 scope += " "; | 124 scope += " "; |
| 127 scope += *it; | 125 scope += *it; |
| 128 } | 126 } |
| 129 return scope; | 127 return scope; |
| 130 } | 128 } |
| 131 | 129 |
| 132 } // namespace | 130 } // namespace |
| 133 | 131 |
| 134 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; | 132 bool OAuth2TokenServiceDelegateAndroid::is_testing_profile_ = false; |
| 135 | 133 |
| 136 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { | 134 OAuth2TokenServiceDelegateAndroid::OAuth2TokenServiceDelegateAndroid() { |
| 137 DVLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; | 135 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ctor"; |
| 138 JNIEnv* env = AttachCurrentThread(); | 136 JNIEnv* env = AttachCurrentThread(); |
| 139 base::android::ScopedJavaLocalRef<jobject> local_java_ref = | 137 base::android::ScopedJavaLocalRef<jobject> local_java_ref = |
| 140 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); | 138 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); |
| 141 java_ref_.Reset(env, local_java_ref.obj()); | 139 java_ref_.Reset(env, local_java_ref.obj()); |
| 142 } | 140 } |
| 143 | 141 |
| 144 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() {} | 142 OAuth2TokenServiceDelegateAndroid::~OAuth2TokenServiceDelegateAndroid() { |
| 143 } |
| 145 | 144 |
| 146 // static | 145 // static |
| 147 jobject AndroidProfileOAuth2TokenService::GetForProfile( | 146 jobject OAuth2TokenServiceDelegateAndroid::GetForProfile( |
| 148 JNIEnv* env, jclass clazz, jobject j_profile_android) { | 147 JNIEnv* env, |
| 148 jclass clazz, |
| 149 jobject j_profile_android) { |
| 149 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android); | 150 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android); |
| 150 AndroidProfileOAuth2TokenService* service = | 151 ProfileOAuth2TokenService* service = |
| 151 ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(profile); | 152 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 152 return service->java_ref_.obj(); | 153 OAuth2TokenServiceDelegate* delegate = service->GetDelegate(); |
| 154 return static_cast<OAuth2TokenServiceDelegateAndroid*>(delegate) |
| 155 ->java_ref_.obj(); |
| 153 } | 156 } |
| 154 | 157 |
| 155 static jobject GetForProfile(JNIEnv* env, | 158 static jobject GetForProfile(JNIEnv* env, |
| 156 jclass clazz, | 159 jclass clazz, |
| 157 jobject j_profile_android) { | 160 jobject j_profile_android) { |
| 158 return AndroidProfileOAuth2TokenService::GetForProfile( | 161 return OAuth2TokenServiceDelegateAndroid::GetForProfile(env, clazz, |
| 159 env, clazz, j_profile_android); | 162 j_profile_android); |
| 160 } | 163 } |
| 161 | 164 |
| 162 void AndroidProfileOAuth2TokenService::Initialize( | 165 void OAuth2TokenServiceDelegateAndroid::Initialize() { |
| 163 SigninClient* client, | 166 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::Initialize"; |
| 164 SigninErrorController* signin_error_controller) { | |
| 165 DVLOG(1) << "AndroidProfileOAuth2TokenService::Initialize"; | |
| 166 ProfileOAuth2TokenService::Initialize(client, signin_error_controller); | |
| 167 | |
| 168 if (!is_testing_profile_) { | 167 if (!is_testing_profile_) { |
| 169 Java_OAuth2TokenService_validateAccounts( | 168 Java_OAuth2TokenService_validateAccounts( |
| 170 AttachCurrentThread(), java_ref_.obj(), | 169 AttachCurrentThread(), java_ref_.obj(), |
| 171 base::android::GetApplicationContext(), JNI_TRUE); | 170 base::android::GetApplicationContext(), JNI_TRUE); |
| 172 } | 171 } |
| 173 } | 172 } |
| 174 | 173 |
| 175 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable( | 174 bool OAuth2TokenServiceDelegateAndroid::RefreshTokenIsAvailable( |
| 176 const std::string& account_id) const { | 175 const std::string& account_id) const { |
| 177 JNIEnv* env = AttachCurrentThread(); | 176 JNIEnv* env = AttachCurrentThread(); |
| 178 ScopedJavaLocalRef<jstring> j_account_id = | 177 ScopedJavaLocalRef<jstring> j_account_id = |
| 179 ConvertUTF8ToJavaString(env, account_id); | 178 ConvertUTF8ToJavaString(env, account_id); |
| 180 jboolean refresh_token_is_available = | 179 jboolean refresh_token_is_available = |
| 181 Java_OAuth2TokenService_hasOAuth2RefreshToken( | 180 Java_OAuth2TokenService_hasOAuth2RefreshToken( |
| 182 env, base::android::GetApplicationContext(), | 181 env, base::android::GetApplicationContext(), j_account_id.obj()); |
| 183 j_account_id.obj()); | |
| 184 return refresh_token_is_available == JNI_TRUE; | 182 return refresh_token_is_available == JNI_TRUE; |
| 185 } | 183 } |
| 186 | 184 |
| 187 void AndroidProfileOAuth2TokenService::UpdateAuthError( | 185 void OAuth2TokenServiceDelegateAndroid::UpdateAuthError( |
| 188 const std::string& account_id, | 186 const std::string& account_id, |
| 189 const GoogleServiceAuthError& error) { | 187 const GoogleServiceAuthError& error) { |
| 190 // TODO(rogerta): do we need to update anything, or does the system handle it? | 188 // TODO(rogerta): do we need to update anything, or does the system handle it? |
| 191 } | 189 } |
| 192 | 190 |
| 193 std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { | 191 std::vector<std::string> OAuth2TokenServiceDelegateAndroid::GetAccounts() { |
| 194 std::vector<std::string> accounts; | 192 std::vector<std::string> accounts; |
| 195 JNIEnv* env = AttachCurrentThread(); | 193 JNIEnv* env = AttachCurrentThread(); |
| 196 ScopedJavaLocalRef<jobjectArray> j_accounts = | 194 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 197 Java_OAuth2TokenService_getAccounts( | 195 Java_OAuth2TokenService_getAccounts( |
| 198 env, base::android::GetApplicationContext()); | 196 env, base::android::GetApplicationContext()); |
| 199 // TODO(fgorski): We may decide to filter out some of the accounts. | 197 // TODO(fgorski): We may decide to filter out some of the accounts. |
| 200 base::android::AppendJavaStringArrayToStringVector(env, | 198 base::android::AppendJavaStringArrayToStringVector(env, j_accounts.obj(), |
| 201 j_accounts.obj(), | |
| 202 &accounts); | 199 &accounts); |
| 203 return accounts; | 200 return accounts; |
| 204 } | 201 } |
| 205 | 202 |
| 206 std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() { | 203 std::vector<std::string> |
| 204 OAuth2TokenServiceDelegateAndroid::GetSystemAccounts() { |
| 207 std::vector<std::string> accounts; | 205 std::vector<std::string> accounts; |
| 208 JNIEnv* env = AttachCurrentThread(); | 206 JNIEnv* env = AttachCurrentThread(); |
| 209 ScopedJavaLocalRef<jobjectArray> j_accounts = | 207 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 210 Java_OAuth2TokenService_getSystemAccounts( | 208 Java_OAuth2TokenService_getSystemAccounts( |
| 211 env, base::android::GetApplicationContext()); | 209 env, base::android::GetApplicationContext()); |
| 212 base::android::AppendJavaStringArrayToStringVector(env, | 210 base::android::AppendJavaStringArrayToStringVector(env, j_accounts.obj(), |
| 213 j_accounts.obj(), | |
| 214 &accounts); | 211 &accounts); |
| 215 return accounts; | 212 return accounts; |
| 216 } | 213 } |
| 217 | 214 |
| 218 OAuth2AccessTokenFetcher* | 215 OAuth2AccessTokenFetcher* |
| 219 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( | 216 OAuth2TokenServiceDelegateAndroid::CreateAccessTokenFetcher( |
| 220 const std::string& account_id, | 217 const std::string& account_id, |
| 221 net::URLRequestContextGetter* getter, | 218 net::URLRequestContextGetter* getter, |
| 222 OAuth2AccessTokenConsumer* consumer) { | 219 OAuth2AccessTokenConsumer* consumer) { |
| 223 ValidateAccountId(account_id); | 220 ValidateAccountId(account_id); |
| 224 return new AndroidAccessTokenFetcher(consumer, account_id); | 221 return new AndroidAccessTokenFetcher(consumer, account_id); |
| 225 } | 222 } |
| 226 | 223 |
| 227 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( | 224 void OAuth2TokenServiceDelegateAndroid::InvalidateOAuth2Token( |
| 228 const std::string& account_id, | 225 const std::string& account_id, |
| 229 const std::string& client_id, | 226 const std::string& client_id, |
| 230 const ScopeSet& scopes, | 227 const OAuth2TokenService::ScopeSet& scopes, |
| 231 const std::string& access_token) { | 228 const std::string& access_token) { |
| 232 ValidateAccountId(account_id); | 229 ValidateAccountId(account_id); |
| 233 OAuth2TokenService::InvalidateOAuth2Token(account_id, | |
| 234 client_id, | |
| 235 scopes, | |
| 236 access_token); | |
| 237 | |
| 238 JNIEnv* env = AttachCurrentThread(); | 230 JNIEnv* env = AttachCurrentThread(); |
| 239 ScopedJavaLocalRef<jstring> j_access_token = | 231 ScopedJavaLocalRef<jstring> j_access_token = |
| 240 ConvertUTF8ToJavaString(env, access_token); | 232 ConvertUTF8ToJavaString(env, access_token); |
| 241 Java_OAuth2TokenService_invalidateOAuth2AuthToken( | 233 Java_OAuth2TokenService_invalidateOAuth2AuthToken( |
| 242 env, base::android::GetApplicationContext(), | 234 env, base::android::GetApplicationContext(), j_access_token.obj()); |
| 243 j_access_token.obj()); | |
| 244 } | 235 } |
| 245 | 236 |
| 246 void AndroidProfileOAuth2TokenService::ValidateAccounts( | 237 void OAuth2TokenServiceDelegateAndroid::ValidateAccounts( |
| 247 JNIEnv* env, | 238 JNIEnv* env, |
| 248 jobject obj, | 239 jobject obj, |
| 249 jstring j_current_acc, | 240 jstring j_current_acc, |
| 250 jboolean j_force_notifications) { | 241 jboolean j_force_notifications) { |
| 251 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts from java"; | |
| 252 std::string signed_in_account; | 242 std::string signed_in_account; |
| 243 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts from java"; |
| 253 if (j_current_acc) | 244 if (j_current_acc) |
| 254 signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); | 245 signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc); |
| 255 if (!signed_in_account.empty()) | 246 if (!signed_in_account.empty()) |
| 256 signed_in_account = gaia::CanonicalizeEmail(signed_in_account); | 247 signed_in_account = gaia::CanonicalizeEmail(signed_in_account); |
| 257 ValidateAccounts(signed_in_account, j_force_notifications != JNI_FALSE); | 248 ValidateAccounts(signed_in_account, j_force_notifications != JNI_FALSE); |
| 258 } | 249 } |
| 259 | 250 |
| 260 void AndroidProfileOAuth2TokenService::ValidateAccounts( | 251 void OAuth2TokenServiceDelegateAndroid::ValidateAccounts( |
| 261 const std::string& signed_in_account, | 252 const std::string& signed_in_account, |
| 262 bool force_notifications) { | 253 bool force_notifications) { |
| 263 std::vector<std::string> prev_ids = GetAccounts(); | 254 std::vector<std::string> prev_ids = GetAccounts(); |
| 264 std::vector<std::string> curr_ids = GetSystemAccounts(); | 255 std::vector<std::string> curr_ids = GetSystemAccounts(); |
| 265 std::vector<std::string> refreshed_ids; | 256 std::vector<std::string> refreshed_ids; |
| 266 std::vector<std::string> revoked_ids; | 257 std::vector<std::string> revoked_ids; |
| 267 | 258 |
| 268 // Canonicalize system accounts. |prev_ids| is already done. | 259 // Canonicalize system accounts. |prev_ids| is already done. |
| 269 for (size_t i = 0; i < curr_ids.size(); ++i) | 260 for (size_t i = 0; i < curr_ids.size(); ++i) |
| 270 curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]); | 261 curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]); |
| 271 for (size_t i = 0; i < prev_ids.size(); ++i) | 262 for (size_t i = 0; i < prev_ids.size(); ++i) |
| 272 ValidateAccountId(prev_ids[i]); | 263 ValidateAccountId(prev_ids[i]); |
| 273 | 264 |
| 274 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 265 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 275 << " sigined_in_account=" << signed_in_account | 266 << " sigined_in_account=" << signed_in_account |
| 276 << " prev_ids=" << prev_ids.size() | 267 << " prev_ids=" << prev_ids.size() << " curr_ids=" << curr_ids.size() |
| 277 << " curr_ids=" << curr_ids.size() | |
| 278 << " force=" << (force_notifications ? "true" : "false"); | 268 << " force=" << (force_notifications ? "true" : "false"); |
| 279 | 269 |
| 280 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, | 270 if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids, |
| 281 revoked_ids, force_notifications)) { | 271 revoked_ids, force_notifications)) { |
| 282 curr_ids.clear(); | 272 curr_ids.clear(); |
| 283 } | 273 } |
| 284 | 274 |
| 285 ScopedBatchChange batch(this); | 275 ScopedBatchChange batch(this); |
| 286 | 276 |
| 287 JNIEnv* env = AttachCurrentThread(); | 277 JNIEnv* env = AttachCurrentThread(); |
| 288 ScopedJavaLocalRef<jobjectArray> java_accounts( | 278 ScopedJavaLocalRef<jobjectArray> java_accounts( |
| 289 base::android::ToJavaArrayOfStrings(env, curr_ids)); | 279 base::android::ToJavaArrayOfStrings(env, curr_ids)); |
| 290 Java_OAuth2TokenService_saveStoredAccounts( | 280 Java_OAuth2TokenService_saveStoredAccounts( |
| 291 env, base::android::GetApplicationContext(), java_accounts.obj()); | 281 env, base::android::GetApplicationContext(), java_accounts.obj()); |
| 292 | 282 |
| 293 for (std::vector<std::string>::iterator it = refreshed_ids.begin(); | 283 for (std::vector<std::string>::iterator it = refreshed_ids.begin(); |
| 294 it != refreshed_ids.end(); it++) { | 284 it != refreshed_ids.end(); it++) { |
| 295 FireRefreshTokenAvailable(*it); | 285 FireRefreshTokenAvailable(*it); |
| 296 } | 286 } |
| 297 | 287 |
| 298 for (std::vector<std::string>::iterator it = revoked_ids.begin(); | 288 for (std::vector<std::string>::iterator it = revoked_ids.begin(); |
| 299 it != revoked_ids.end(); it++) { | 289 it != revoked_ids.end(); it++) { |
| 300 FireRefreshTokenRevoked(*it); | 290 FireRefreshTokenRevoked(*it); |
| 301 } | 291 } |
| 302 } | 292 } |
| 303 | 293 |
| 304 bool AndroidProfileOAuth2TokenService::ValidateAccounts( | 294 bool OAuth2TokenServiceDelegateAndroid::ValidateAccounts( |
| 305 const std::string& signed_in_account, | 295 const std::string& signed_in_account, |
| 306 const std::vector<std::string>& prev_account_ids, | 296 const std::vector<std::string>& prev_account_ids, |
| 307 const std::vector<std::string>& curr_account_ids, | 297 const std::vector<std::string>& curr_account_ids, |
| 308 std::vector<std::string>& refreshed_ids, | 298 std::vector<std::string>& refreshed_ids, |
| 309 std::vector<std::string>& revoked_ids, | 299 std::vector<std::string>& revoked_ids, |
| 310 bool force_notifications) { | 300 bool force_notifications) { |
| 311 if (std::find(curr_account_ids.begin(), | 301 if (std::find(curr_account_ids.begin(), curr_account_ids.end(), |
| 312 curr_account_ids.end(), | |
| 313 signed_in_account) != curr_account_ids.end()) { | 302 signed_in_account) != curr_account_ids.end()) { |
| 314 // Test to see if an account is removed from the Android AccountManager. | 303 // Test to see if an account is removed from the Android AccountManager. |
| 315 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. | 304 // If so, invoke FireRefreshTokenRevoked to notify the reconcilor. |
| 316 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | 305 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); |
| 317 it != prev_account_ids.end(); it++) { | 306 it != prev_account_ids.end(); it++) { |
| 318 if (*it == signed_in_account) | 307 if (*it == signed_in_account) |
| 319 continue; | 308 continue; |
| 320 | 309 |
| 321 if (std::find(curr_account_ids.begin(), | 310 if (std::find(curr_account_ids.begin(), curr_account_ids.end(), *it) == |
| 322 curr_account_ids.end(), | 311 curr_account_ids.end()) { |
| 323 *it) == curr_account_ids.end()) { | 312 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 324 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | |
| 325 << "revoked=" << *it; | 313 << "revoked=" << *it; |
| 326 revoked_ids.push_back(*it); | 314 revoked_ids.push_back(*it); |
| 327 } | 315 } |
| 328 } | 316 } |
| 329 | 317 |
| 330 if (force_notifications || | 318 if (force_notifications || |
| 331 std::find(prev_account_ids.begin(), prev_account_ids.end(), | 319 std::find(prev_account_ids.begin(), prev_account_ids.end(), |
| 332 signed_in_account) == prev_account_ids.end()) { | 320 signed_in_account) == prev_account_ids.end()) { |
| 333 // Always fire the primary signed in account first. | 321 // Always fire the primary signed in account first. |
| 334 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 322 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 335 << "refreshed=" << signed_in_account; | 323 << "refreshed=" << signed_in_account; |
| 336 refreshed_ids.push_back(signed_in_account); | 324 refreshed_ids.push_back(signed_in_account); |
| 337 } | 325 } |
| 338 | 326 |
| 339 for (std::vector<std::string>::const_iterator it = curr_account_ids.begin(); | 327 for (std::vector<std::string>::const_iterator it = curr_account_ids.begin(); |
| 340 it != curr_account_ids.end(); it++) { | 328 it != curr_account_ids.end(); it++) { |
| 341 if (*it != signed_in_account) { | 329 if (*it != signed_in_account) { |
| 342 if (force_notifications || | 330 if (force_notifications || |
| 343 std::find(prev_account_ids.begin(), | 331 std::find(prev_account_ids.begin(), prev_account_ids.end(), *it) == |
| 344 prev_account_ids.end(), | 332 prev_account_ids.end()) { |
| 345 *it) == prev_account_ids.end()) { | 333 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 346 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | |
| 347 << "refreshed=" << *it; | 334 << "refreshed=" << *it; |
| 348 refreshed_ids.push_back(*it); | 335 refreshed_ids.push_back(*it); |
| 349 } | 336 } |
| 350 } | 337 } |
| 351 } | 338 } |
| 352 return true; | 339 return true; |
| 353 } else { | 340 } else { |
| 354 // Currently signed in account does not any longer exist among accounts on | 341 // Currently signed in account does not any longer exist among accounts on |
| 355 // system together with all other accounts. | 342 // system together with all other accounts. |
| 356 if (std::find(prev_account_ids.begin(), prev_account_ids.end(), | 343 if (std::find(prev_account_ids.begin(), prev_account_ids.end(), |
| 357 signed_in_account) != prev_account_ids.end()) { | 344 signed_in_account) != prev_account_ids.end()) { |
| 358 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 345 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 359 << "revoked=" << signed_in_account; | 346 << "revoked=" << signed_in_account; |
| 360 revoked_ids.push_back(signed_in_account); | 347 revoked_ids.push_back(signed_in_account); |
| 361 } | 348 } |
| 362 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); | 349 for (std::vector<std::string>::const_iterator it = prev_account_ids.begin(); |
| 363 it != prev_account_ids.end(); it++) { | 350 it != prev_account_ids.end(); it++) { |
| 364 if (*it == signed_in_account) | 351 if (*it == signed_in_account) |
| 365 continue; | 352 continue; |
| 366 DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:" | 353 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::ValidateAccounts:" |
| 367 << "revoked=" << *it; | 354 << "revoked=" << *it; |
| 368 revoked_ids.push_back(*it); | 355 revoked_ids.push_back(*it); |
| 369 } | 356 } |
| 370 return false; | 357 return false; |
| 371 } | 358 } |
| 372 } | 359 } |
| 373 | 360 |
| 374 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava( | 361 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailableFromJava( |
| 375 JNIEnv* env, | 362 JNIEnv* env, |
| 376 jobject obj, | 363 jobject obj, |
| 377 const jstring account_name) { | 364 const jstring account_name) { |
| 378 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 365 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
| 379 AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id); | 366 // Notify native observers. |
| 367 FireRefreshTokenAvailable(account_id); |
| 380 } | 368 } |
| 381 | 369 |
| 382 void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable( | 370 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable( |
| 383 const std::string& account_id) { | 371 const std::string& account_id) { |
| 384 DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable id=" | 372 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenAvailable id=" |
| 385 << account_id; | 373 << account_id; |
| 386 | |
| 387 // Notify native observers. | |
| 388 OAuth2TokenService::FireRefreshTokenAvailable(account_id); | |
| 389 // Notify Java observers. | |
| 390 JNIEnv* env = AttachCurrentThread(); | 374 JNIEnv* env = AttachCurrentThread(); |
| 391 ScopedJavaLocalRef<jstring> account_name = | 375 ScopedJavaLocalRef<jstring> account_name = |
| 392 ConvertUTF8ToJavaString(env, account_id); | 376 ConvertUTF8ToJavaString(env, account_id); |
| 393 Java_OAuth2TokenService_notifyRefreshTokenAvailable( | 377 Java_OAuth2TokenService_notifyRefreshTokenAvailable(env, java_ref_.obj(), |
| 394 env, java_ref_.obj(), account_name.obj()); | 378 account_name.obj()); |
| 379 OAuth2TokenServiceDelegate::FireRefreshTokenAvailable(account_id); |
| 395 } | 380 } |
| 396 | 381 |
| 397 void AndroidProfileOAuth2TokenService::FireRefreshTokenRevokedFromJava( | 382 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevokedFromJava( |
| 398 JNIEnv* env, | 383 JNIEnv* env, |
| 399 jobject obj, | 384 jobject obj, |
| 400 const jstring account_name) { | 385 const jstring account_name) { |
| 401 std::string account_id = ConvertJavaStringToUTF8(env, account_name); | 386 std::string account_id = ConvertJavaStringToUTF8(env, account_name); |
| 402 AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked(account_id); | 387 // Notify native observers. |
| 388 FireRefreshTokenRevoked(account_id); |
| 403 } | 389 } |
| 404 | 390 |
| 405 void AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked( | 391 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked( |
| 406 const std::string& account_id) { | 392 const std::string& account_id) { |
| 407 DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked id=" | 393 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokenRevoked id=" |
| 408 << account_id; | 394 << account_id; |
| 409 | |
| 410 // Notify native observers. | |
| 411 OAuth2TokenService::FireRefreshTokenRevoked(account_id); | |
| 412 // Notify Java observers. | |
| 413 JNIEnv* env = AttachCurrentThread(); | 395 JNIEnv* env = AttachCurrentThread(); |
| 414 ScopedJavaLocalRef<jstring> account_name = | 396 ScopedJavaLocalRef<jstring> account_name = |
| 415 ConvertUTF8ToJavaString(env, account_id); | 397 ConvertUTF8ToJavaString(env, account_id); |
| 416 Java_OAuth2TokenService_notifyRefreshTokenRevoked( | 398 Java_OAuth2TokenService_notifyRefreshTokenRevoked(env, java_ref_.obj(), |
| 417 env, java_ref_.obj(), account_name.obj()); | 399 account_name.obj()); |
| 400 OAuth2TokenServiceDelegate::FireRefreshTokenRevoked(account_id); |
| 418 } | 401 } |
| 419 | 402 |
| 420 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoadedFromJava( | 403 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoadedFromJava( |
| 421 JNIEnv* env, | 404 JNIEnv* env, |
| 422 jobject obj) { | 405 jobject obj) { |
| 423 AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded(); | 406 // Notify native observers. |
| 407 FireRefreshTokensLoaded(); |
| 424 } | 408 } |
| 425 | 409 |
| 426 void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() { | 410 void OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoaded() { |
| 427 DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded"; | 411 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::FireRefreshTokensLoaded"; |
| 428 // Notify native observers. | |
| 429 OAuth2TokenService::FireRefreshTokensLoaded(); | |
| 430 // Notify Java observers. | |
| 431 JNIEnv* env = AttachCurrentThread(); | 412 JNIEnv* env = AttachCurrentThread(); |
| 432 Java_OAuth2TokenService_notifyRefreshTokensLoaded( | 413 Java_OAuth2TokenService_notifyRefreshTokensLoaded(env, java_ref_.obj()); |
| 433 env, java_ref_.obj()); | 414 OAuth2TokenServiceDelegate::FireRefreshTokensLoaded(); |
| 434 } | 415 } |
| 435 | 416 |
| 436 void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { | 417 void OAuth2TokenServiceDelegateAndroid::RevokeAllCredentials() { |
| 437 DVLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; | 418 DVLOG(1) << "OAuth2TokenServiceDelegateAndroid::RevokeAllCredentials"; |
| 438 ScopedBatchChange batch(this); | 419 ScopedBatchChange batch(this); |
| 439 std::vector<std::string> accounts = GetAccounts(); | 420 std::vector<std::string> accounts = GetAccounts(); |
| 440 for (std::vector<std::string>::iterator it = accounts.begin(); | 421 for (std::vector<std::string>::iterator it = accounts.begin(); |
| 441 it != accounts.end(); it++) { | 422 it != accounts.end(); it++) { |
| 442 FireRefreshTokenRevoked(*it); | 423 FireRefreshTokenRevoked(*it); |
| 443 } | 424 } |
| 444 | 425 |
| 445 // Clear everything on the Java side as well. | 426 // Clear everything on the Java side as well. |
| 446 std::vector<std::string> empty; | 427 std::vector<std::string> empty; |
| 447 JNIEnv* env = AttachCurrentThread(); | 428 JNIEnv* env = AttachCurrentThread(); |
| 448 ScopedJavaLocalRef<jobjectArray> java_accounts( | 429 ScopedJavaLocalRef<jobjectArray> java_accounts( |
| 449 base::android::ToJavaArrayOfStrings(env, empty)); | 430 base::android::ToJavaArrayOfStrings(env, empty)); |
| 450 Java_OAuth2TokenService_saveStoredAccounts( | 431 Java_OAuth2TokenService_saveStoredAccounts( |
| 451 env, base::android::GetApplicationContext(), java_accounts.obj()); | 432 env, base::android::GetApplicationContext(), java_accounts.obj()); |
| 452 } | 433 } |
| 453 | 434 |
| 454 // Called from Java when fetching of an OAuth2 token is finished. The | 435 // Called from Java when fetching of an OAuth2 token is finished. The |
| 455 // |authToken| param is only valid when |result| is true. | 436 // |authToken| param is only valid when |result| is true. |
| 456 void OAuth2TokenFetched( | 437 void OAuth2TokenFetched(JNIEnv* env, |
| 457 JNIEnv* env, | 438 jclass clazz, |
| 458 jclass clazz, | 439 jstring authToken, |
| 459 jstring authToken, | 440 jboolean result, |
| 460 jboolean result, | 441 jlong nativeCallback) { |
| 461 jlong nativeCallback) { | |
| 462 std::string token; | 442 std::string token; |
| 463 if (authToken) | 443 if (authToken) |
| 464 token = ConvertJavaStringToUTF8(env, authToken); | 444 token = ConvertJavaStringToUTF8(env, authToken); |
| 465 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 445 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 466 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 446 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 467 // Android does not provide enough information to know if the credentials are | 447 // Android does not provide enough information to know if the credentials are |
| 468 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 448 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
| 469 GoogleServiceAuthError err(result ? | 449 GoogleServiceAuthError err(result |
| 470 GoogleServiceAuthError::NONE : | 450 ? GoogleServiceAuthError::NONE |
| 471 GoogleServiceAuthError::CONNECTION_FAILED); | 451 : GoogleServiceAuthError::CONNECTION_FAILED); |
| 472 heap_callback->Run(err, token, base::Time()); | 452 heap_callback->Run(err, token, base::Time()); |
| 473 } | 453 } |
| 474 | 454 |
| 475 // static | 455 // static |
| 476 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 456 bool OAuth2TokenServiceDelegateAndroid::Register(JNIEnv* env) { |
| 477 return RegisterNativesImpl(env); | 457 return RegisterNativesImpl(env); |
| 478 } | 458 } |
| OLD | NEW |