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