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_string.h" | 9 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "chrome/browser/sync/profile_sync_service_android.h" | 14 #include "chrome/browser/sync/profile_sync_service_android.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "jni/AndroidProfileOAuth2TokenServiceHelper_jni.h" | 16 #include "jni/AndroidProfileOAuth2TokenServiceHelper_jni.h" |
| 16 | 17 |
| 17 using base::android::AttachCurrentThread; | 18 using base::android::AttachCurrentThread; |
| 18 using base::android::CheckException; | |
| 19 using base::android::ConvertJavaStringToUTF8; | 19 using base::android::ConvertJavaStringToUTF8; |
| 20 using base::android::ConvertUTF8ToJavaString; | 20 using base::android::ConvertUTF8ToJavaString; |
| 21 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
| 22 using content::BrowserThread; | 22 using content::BrowserThread; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { | 26 std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { |
| 27 // The Android AccountManager supports multiple scopes separated by a space: | 27 // The Android AccountManager supports multiple scopes separated by a space: |
| 28 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android | 28 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 47 FetchOAuth2TokenCallback; | 47 FetchOAuth2TokenCallback; |
| 48 | 48 |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { | 51 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { |
| 52 } | 52 } |
| 53 | 53 |
| 54 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() { | 54 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() { |
| 55 } | 55 } |
| 56 | 56 |
| 57 scoped_ptr<OAuth2TokenService::Request> | 57 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable( |
| 58 AndroidProfileOAuth2TokenService::StartRequestForUsername( | 58 const std::string& account_id) { |
| 59 const std::string& username, | 59 JNIEnv* env = AttachCurrentThread(); |
| 60 const OAuth2TokenService::ScopeSet& scopes, | 60 ScopedJavaLocalRef<jstring> j_account_id = |
| 61 OAuth2TokenService::Consumer* consumer) { | 61 ConvertUTF8ToJavaString(env, account_id); |
| 62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 62 jboolean refresh_token_is_available = |
| 63 | 63 Java_AndroidProfileOAuth2TokenServiceHelper_hasOAuth2RefreshToken( |
| 64 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 64 env, base::android::GetApplicationContext(), |
| 65 FetchOAuth2TokenWithUsername(request.get(), username, scopes); | 65 j_account_id.obj()); |
| 66 return request.PassAs<Request>(); | 66 return (bool)refresh_token_is_available; |
| 67 } | |
| 68 | |
| 69 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() { | |
| 70 SigninManagerBase* signin_manager = | |
| 71 SigninManagerFactory::GetForProfile(profile()); | |
| 72 return !signin_manager->GetAuthenticatedUsername().empty(); | |
| 73 } | 67 } |
| 74 | 68 |
| 75 void AndroidProfileOAuth2TokenService::InvalidateToken( | 69 void AndroidProfileOAuth2TokenService::InvalidateToken( |
| 76 const ScopeSet& scopes, | 70 const ScopeSet& scopes, |
| 77 const std::string& invalid_token) { | 71 const std::string& invalid_token) { |
| 78 OAuth2TokenService::InvalidateToken(scopes, invalid_token); | 72 OAuth2TokenService::InvalidateToken(scopes, invalid_token); |
|
Roger Tawa OOO till Jul 10th
2013/09/13 14:53:56
This is an access token not a refresh token. Would
fgorski
2013/09/13 22:06:46
Done. Renamed to access_token and propagated to OA
| |
| 79 | 73 |
| 80 JNIEnv* env = AttachCurrentThread(); | 74 JNIEnv* env = AttachCurrentThread(); |
| 81 ScopedJavaLocalRef<jstring> j_invalid_token = | 75 ScopedJavaLocalRef<jstring> j_invalid_token = |
| 82 ConvertUTF8ToJavaString(env, invalid_token); | 76 ConvertUTF8ToJavaString(env, invalid_token); |
| 83 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( | 77 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( |
| 84 env, base::android::GetApplicationContext(), | 78 env, base::android::GetApplicationContext(), |
| 85 j_invalid_token.obj()); | 79 j_invalid_token.obj()); |
| 86 } | 80 } |
| 87 | 81 |
| 82 std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { | |
| 83 std::vector<std::string> accounts; | |
| 84 JNIEnv* env = AttachCurrentThread(); | |
| 85 ScopedJavaLocalRef<jobjectArray> j_accounts = | |
| 86 Java_AndroidProfileOAuth2TokenServiceHelper_getAccounts( | |
| 87 env, base::android::GetApplicationContext()); | |
| 88 // TODO(fgorski): We may decide to filter out some of the accounts. | |
| 89 base::android::AppendJavaStringArrayToStringVector(env, | |
| 90 j_accounts.obj(), | |
| 91 &accounts); | |
| 92 return accounts; | |
| 93 } | |
| 94 | |
| 88 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( | 95 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( |
| 89 RequestImpl* request, | 96 RequestImpl* request, |
| 97 const std::string& account_id, | |
| 90 net::URLRequestContextGetter* getter, | 98 net::URLRequestContextGetter* getter, |
| 91 const std::string& client_id, | 99 const std::string& client_id, |
| 92 const std::string& client_secret, | 100 const std::string& client_secret, |
| 93 const OAuth2TokenService::ScopeSet& scopes) { | 101 const OAuth2TokenService::ScopeSet& scopes) { |
| 94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 95 std::string username = SigninManagerFactory::GetForProfile(profile())-> | 103 DCHECK(!account_id.empty()); |
| 96 GetAuthenticatedUsername(); | |
| 97 DCHECK(!username.empty()); | |
| 98 // Just ignore client_id, getter, etc since we don't use them on Android. | |
| 99 FetchOAuth2TokenWithUsername(request, username, scopes); | |
| 100 } | |
| 101 | 104 |
| 102 void AndroidProfileOAuth2TokenService::FetchOAuth2TokenWithUsername( | |
| 103 RequestImpl* request, | |
| 104 const std::string& username, | |
| 105 const OAuth2TokenService::ScopeSet& scopes) { | |
| 106 JNIEnv* env = AttachCurrentThread(); | 105 JNIEnv* env = AttachCurrentThread(); |
| 107 std::string scope = CombineScopes(scopes); | 106 std::string scope = CombineScopes(scopes); |
| 108 ScopedJavaLocalRef<jstring> j_username = | 107 ScopedJavaLocalRef<jstring> j_username = |
| 109 ConvertUTF8ToJavaString(env, username); | 108 ConvertUTF8ToJavaString(env, account_id); |
| 110 ScopedJavaLocalRef<jstring> j_scope = | 109 ScopedJavaLocalRef<jstring> j_scope = |
| 111 ConvertUTF8ToJavaString(env, scope); | 110 ConvertUTF8ToJavaString(env, scope); |
| 112 | 111 |
| 113 // Allocate a copy of the request WeakPtr on the heap, because the object | 112 // Allocate a copy of the request WeakPtr on the heap, because the object |
| 114 // needs to be passed through JNI as an int. | 113 // needs to be passed through JNI as an int. |
| 115 // It will be passed back to OAuth2TokenFetched(), where it will be freed. | 114 // It will be passed back to OAuth2TokenFetched(), where it will be freed. |
| 116 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 115 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 117 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, | 116 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, |
| 118 request->AsWeakPtr()))); | 117 request->AsWeakPtr()))); |
| 119 | 118 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 137 GoogleServiceAuthError err(result ? | 136 GoogleServiceAuthError err(result ? |
| 138 GoogleServiceAuthError::NONE : | 137 GoogleServiceAuthError::NONE : |
| 139 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 138 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 140 heap_callback->Run(err, token, base::Time()); | 139 heap_callback->Run(err, token, base::Time()); |
| 141 } | 140 } |
| 142 | 141 |
| 143 // static | 142 // static |
| 144 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 143 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
| 145 return RegisterNativesImpl(env); | 144 return RegisterNativesImpl(env); |
| 146 } | 145 } |
| OLD | NEW |